TradingView Strategy Signal Order Bot for OKX

ยท

## Strategy Source Code  

/*

// Global variables
const BUY = "buy", SELL = "sell", LONG = "long", SHORT = "short";
const COVER_LONG = "cover_long", COVER_SHORT = "cover_short", SPK = "spk", BPK = "bpk";

// Account Information Table
const accountInformation = {
type: 'table',
title: 'Account Overview',
cols: ['Initial Balance', 'Wallet Balance', 'Margin Balance', 'Free Margin', 'Used Margin', 'Total Profit', 'ROI'],
rows: null
};

// Position Table
const binanceFundingRate = {
type: 'table',
title: 'OKX Perpetual Positions',
cols: ['Pair', 'Direction', 'Amount', 'Entry Price', 'Position Value', 'Leverage', 'Margin', 'PnL'],
rows: null
};

let initialPrincipalUsdt = null;
let revenueUsdt = 0;

// Functions
function accountInformationfunction() {
exchange.SetContractType("swap");
let account = _C(exchange.GetAccount);
if (!account) { Log("No assets detected."); return; }

if (initialPrincipalUsdt === null) {

initialPrincipalUsdt = account.Info.data[0].totalEq;  
_G("initialPrincipalUsdt", initialPrincipalUsdt);  

}

let InitialBalance = Number(initialPrincipalUsdt);
let WalletBalance = account.Info.data[0].totalEq;
let TotalRevenue = Number(WalletBalance) - InitialBalance;
let Yield = ((TotalRevenue / InitialBalance) * 100).toFixed(2) + "%";

accountInformation.rows = [[InitialBalance, WalletBalance, account.Info.data[0].isoEq, account.Info.data[0].isoEq, account.Info.data[0].isoEq, TotalRevenue, Yield]];
}

function binanceFundingRatefunction() {
binanceFundingRate.rows = [];
exchange.SetContractType("swap");
let y = 0;
for (let i = 0; i < exchanges.length; i++) {

let position = _C(exchange.GetPosition);  
if (position) {  
  for (let iii = 0; iii < position.length; iii++) {  
    binanceFundingRate.rows[y++] = [  
      position[iii].Info.instId,  
      position[iii].Type === 0 ? "BUY" : "SELL",  
      position[iii].Amount,  
      position[iii].Price,  
      position[iii].Amount * position[iii].Price,  
      position[iii].MarginLevel,  
      position[iii].Margin,  
      position[iii].Profit  
    ];  
  }  
}  

}
}

// Main Function
function main() {
exchange.SetPrecision(QuotePrecision, BasePrecision);
let eType = 0;
let eName = exchange.GetName();
if (/Futures_/.test(eName)) {

eType = 1;  
exchange.SetContractType(Ct);  

}

let lastMsg = "", count = 0;
initialPrincipalUsdt = _G("initialPrincipalUsdt");

while (true) {

let cmd = GetCommand();  
if (cmd) {  
  let [action, amount] = cmd.split(":");  
  if (eType === 0) { // Spot  
    if (action === BUY) exchange.Buy(-1, parseFloat(amount));  
    else if (action === SELL) exchange.Sell(-1, parseFloat(amount));  
  } else if (eType === 1) { // Futures  
    let ticker = _C(exchange.GetTicker);  
    if (action === LONG) {  
      exchange.SetDirection("buy");  
      exchange.Buy(-1, parseFloat(amount));  
    } else if (action === SHORT) {  
      exchange.SetDirection("sell");  
      exchange.Sell(-1, parseFloat(amount));  
    }  
  }  
}  

if (count % 100 === 0) {  
  accountInformationfunction();  
  binanceFundingRatefunction();  
  if (count % 600 === 0) LogProfit(revenueUsdt, '&');  
}  

LogStatus(`Last command: ${lastMsg}\n`,  
  `\`${JSON.stringify("Last update: " + _D())}\`\n`,  
  `\`${JSON.stringify([accountInformation])}\`\n`,  
  `\`${JSON.stringify([binanceFundingRate])}\`\n`  
);  
count++;  
Sleep(1000);  

}
}


---

## Key Features  
1. **Multi-Exchange Support**: Works with both spot and futures trading.  
2. **Real-Time Execution**: Processes TradingView signals instantly.  
3. **Detailed Reporting**: Trades, margins, and PnL are logged in structured tables.  

๐Ÿ‘‰ [Explore Advanced Trading Bots](https://www.okx.com/join/BLOCKSTAR)  

---

## FAQ  

### How do I set up the bot?  
1. Configure API keys in the script.  
2. Deploy the bot on a supported platform (e.g., FMZ).  

### What exchanges are supported?  
Currently optimized for OKX, but adaptable to Binance and others.  

### Can I customize the strategy?  
Yes! Modify the `action` handlers in the source code.  

---

๐Ÿ‘‰ [Start Trading Smarter Today](https://www.okx.com/join/BLOCKSTAR)  

*Note: All hyperlinks except the specified OKX link have been removed as per guidelines.*  

Highlights