OKEx V5 Take Profit and Stop Loss Interface
JavaScript strategy by 夏天不打你.
JavaScriptOther
The original spec has no written description. Read the source below.
Source JavaScript
// Place long orders and stop loss
function longStopLossInOkex(num, symbol, price, size) {
let real_symbol = symbol.replace("_USDT", "") + "-USDT-SWAP";
var param = "instId=" + real_symbol + "&tdMode=cross" + "&side=sell" + "&posSide=long" + "&ordType=conditional" + "&sz=" + size.toString() + "&slTriggerPx=" + price.toString() + "&slOrdPx=-1";
var ret = exchanges[num].IO("api", "POST", "/api/v5/trade/order-algo", param);
Log(exchanges[num].GetLabel(), ": Place long orders and stop loss:", ret);
return true;
}
// Place long order to take profit
function longTakeProfitInOkex(num, symbol, price, size) {
let real_symbol = symbol.replace("_USDT", "") + "-USDT-SWAP";
var param = "instId=" + real_symbol + "&tdMode=cross" + "&side=sell" + "&posSide=long" + "&ordType=conditional" + "&sz=" + size.toString() + "&tpTriggerPx=" + price.toString() + "&tpOrdPx=-1";
var ret = exchanges[num].IO("api", "POST", "/api/v5/trade/order-algo", param);
Log(exchanges[num].GetLabel(), ": Place long order to take profit:", ret);
return true;
}
// Place short order and stop loss
function shortStopLossInOkex(num, symbol, price, size) {
let real_symbol = symbol.replace("_USDT", "") + "-USDT-SWAP";
var param = "instId=" + real_symbol + "&tdMode=cross" + "&side=buy" + "&posSide=short" + "&ordType=conditional" + "&sz=" + size.toString() + "&slTriggerPx=" + price.toString() + "&slOrdPx=-1";
var ret = exchanges[num].IO("api", "POST", "/api/v5/trade/order-algo", param);
Log(exchanges[num].GetLabel(), ": Place short order and stop loss:", ret);
return true;
}
// Place short order to take profit
function shortTakeProfitInOkex(num, symbol, price, size) {
let real_symbol = symbol.replace("_USDT", "") + "-USDT-SWAP";
var param = "instId=" + real_symbol + "&tdMode=cross" + "&side=buy" + "&posSide=short" + "&ordType=conditional" + "&sz=" + size.toString() + "&tpTriggerPx=" + price.toString() + "&tpOrdPx=-1";
var ret = exchanges[num].IO("api", "POST", "/api/v5/trade/order-algo", param);
Log(exchanges[num].GetLabel(), ": Place short order to take profit:", ret);
return true;
}
// Take profit and stop loss for long orders
function longTpAndSlInOkex(num, symbol, tp_price, sl_price, size) {
let real_symbol = symbol.replace("_USDT", "") + "-USDT-SWAP";
var param = "instId=" + real_symbol + "&tdMode=cross" + "&side=sell" + "&posSide=long" + "&ordType=oco" + "&sz=" + size.toString() + "&tpTriggerPx=" + tp_price.toString() + "&tpOrdPx=-1"
+ "&slTriggerPx=" + sl_price.toString() + "&slOrdPx=-1";
var ret = exchanges[num].IO("api", "POST", "/api/v5/trade/order-algo", param);
Log(exchanges[num].GetLabel(), ": Take profit and stop loss for long orders:", ret);
return true;
}
// Take profit and stop loss for short orders
function shortTpAndSlInOkex(num, symbol, tp_price, sl_price, size) {
let real_symbol = symbol.replace("_USDT", "") + "-USDT-SWAP";
var param = "instId=" + real_symbol + "&tdMode=cross" + "&side=buy" + "&posSide=short" + "&ordType=oco" + "&sz=" + size.toString() + "&tpTriggerPx=" + tp_price.toString() + "&tpOrdPx=-1"
+ "&slTriggerPx=" + sl_price.toString() + "&slOrdPx=-1";
var ret = exchanges[num].IO("api", "POST", "/api/v5/trade/order-algo", param);
Log(exchanges[num].GetLabel(), ": Take profit and stop loss for short orders:", ret);
return true;
}
function main() {
shortTakeProfitInOkex(0, "ETH_USDT", 2800, 1);
shortTpAndSlInOkex(0, "ETH_USDT", 2800, 3000, 1);
}Published by 夏天不打你 on the FMZ strategy square · last modified 2022-04-07. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/OKEx-V5-Take-Profit-and-Stop-Loss-Interface.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.