0bps

Strategy vault / Other

Binance OKX Perpetual Automatic Hedging

JavaScript strategy by 小草.

JavaScriptOther
Author
小草
Complexity
Intermediate · 58 lines
Parameters
8

The original spec has no written description. Read the source below.

Parameters

NameDefaultWhat it controls
Direction0Pending order direction: Buy | Sell
Amount100Transaction quantity
Ice_amounttrueIceberg commission size
Open_diff10Opening price difference
Close_diff2Close the difference
Amount_NfalseQuantity Precision
Price_N2Price Precision
Multiplier10OKXContract multiplier

Source JavaScript

//Add the first exchangeOKXFutures, the second Binance futures
var pos = {okx:0, bn:0}
exchanges[0].SetContractType('swap')
exchanges[1].SetContractType('swap')
function GetPosition(){
    let position = _C(exchanges[0].GetPosition)
    if(position.length == 0){
        pos.okx = 0
    }else if(position[0].Type == 0){
        pos.okx = position[0].Amount
    } else if(position[0].Type == 1){
        pos.okx =  -position[0].Amount
    }
    
    position = _C(exchanges[1].GetPosition)
    if(position.length == 0){
        pos.bn = 0
    }else  if(position[0].Type == 0){
        pos.bn =  position[0].Amount
    }else if(position[0].Type == 1){
        pos.bn =  -position[0].Amount
    }
    
}

function main(){
    GetPosition()
    let init_position = pos.bn
    while(true){
        GetPosition()
        let ice_amount = Ice_amount
        let deal_amount = Direction == 0 ? pos.bn - init_position : init_position - pos.bn
        let ticker_okx = exchanges[0].Go('GetTicker')
        let ticker_bn = exchanges[1].Go('GetTicker')
        ticker_okx = ticker_okx.wait(2000)
        ticker_bn = ticker_bn.wait(2000)
        LogStatus('position:'+JSON.stringify(pos)+', okxPrice:'+ticker_okx.Buy+', Binance Price:'+ticker_bn.Sell+', Spread:'+ _N(ticker_okx.Buy - ticker_bn.Sell,2)+', executed:'+deal_amount)
        if((Amount - deal_amount ) > 1){
            if(Direction == 0 && ticker_okx.Buy - ticker_bn.Sell > Open_diff){ //Long bn shortok
                Log('position:'+JSON.stringify(pos)+', okxPrice:'+ticker_okx.Buy+', Binance Price:'+ticker_bn.Sell+', Spread:'+ _N(ticker_okx.Buy - ticker_bn.Sell,2)+', executed:'+deal_amount)
                exchanges[0].SetDirection('sell')
                exchanges[0].Sell(_N(ticker_okx.Buy*0.98, Price_N), _N(ice_amount*Multiplier, 0))
                exchanges[1].SetDirection('buy')
                exchanges[1].Buy(_N(ticker_bn.Sell*1.02, Price_N), _N(ice_amount, Amount_N))
            }else if(ticker_okx.Sell - ticker_bn.Buy < Close_diff){
                Log('position:'+JSON.stringify(pos)+', okxPrice:'+ticker_okx.Buy+', Binance Price:'+ticker_bn.Sell+', Spread:'+ _N(ticker_okx.Buy - ticker_bn.Sell,2)+', executed:'+deal_amount)
                exchanges[1].SetDirection('sell')
                exchanges[1].Sell(_N(ticker_bn.Buy*0.98, Price_N), _N(ice_amount, Amount_N))
                exchanges[0].SetDirection('buy')
                exchanges[0].Buy(_N(ticker_okx.Sell*1.02, Price_N), _N(ice_amount*Multiplier, 1))
            }
        }
        Sleep(700)
        if((Amount - deal_amount ) < 1){
            return 'Transaction completed ' + deal_amount + '\n'
        }         
    }
}

Published by 小草 on the FMZ strategy square · last modified 2024-01-01. 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.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Binance-OKX-Perpetual-Automatic-Hedging.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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.

Related specs

esc

Try , , or .

↑↓ move↵ open