websocket version of OKEX cross-term hedging strategy (learning)

Author: The Little Dream, Date: 2019-04-17 10:10:55
Tags: HedgeWebscoket

OKEX cross-currency hedging strategy (teachable)

  • This is a screenshot of the video:img

  • The only thing that can be done is to make the original set, the reverse set can be modified, the contract can be changed, that is, the reverse set.

  • Add two exchange objects, the first one for the quarter, the second one for the week.

  • There is a lot of room to optimize, the teaching strategy is careful, and there is a certain risk in the transition period.

  • Orders can be placed at the same price.

  • Welcome to feedback BUG.

Teaching strategies, practical use and caution.

Teaching strategies, practical use and caution.

Teaching strategies, practical use and caution.


function Hedge (isOpen, retSetA, retSetB) {
    exchanges[0].SetDirection(isOpen ? "sell" : "closesell")
    exchanges[1].SetDirection(isOpen ? "buy" : "closebuy");
    (function (routineA, routineB) {
        Log(routineA.wait(), routineB.wait(), retSetA, retSetB)
    })(exchanges[0].Go(isOpen ? "Sell" : "Buy", -1, _ContractNum), exchanges[1].Go(isOpen ? "Buy" : "Sell", -1, _ContractNum))
}

function main () {
    var param = {"op": "subscribe", "args": ["futures/ticker:" + _Instrument_id_A, "futures/ticker:" + _Instrument_id_B]}
    var client = Dial("wss://real.okex.com:8443/ws/v3|compress=gzip_raw&mode=recv&reconnect=true&payload=" + JSON.stringify(param))
    client.write(JSON.stringify(param))
    var tickerA, tickerB 
    var arr = []
    for (var i = 0 ; i < _Count ; i++) {
        arr.push({open: _Begin + i * _Add, cover: _Begin + i * _Add - _Profit, isHold: false})
    }
    while (1) {
        var tab = {type: "table", title: "状态", cols: ["节点信息"], rows: []}
        Sleep(10) 
        var ret = client.read(-2)
        if (!ret || ret == "") {
            continue
        }

        var obj = null
        try {
            obj = JSON.parse(ret)
        } catch (e) {
            Log(e)
            continue
        }

        if (obj.table == "futures/ticker" && obj.data[0].instrument_id == _Instrument_id_A) {   
            tickerA = obj.data[0]
        } else if (obj.table == "futures/ticker" && obj.data[0].instrument_id == _Instrument_id_B) {
            tickerB = obj.data[0]
        }

        if (tickerA && tickerB) {
            $.PlotLine(tickerA.instrument_id + "-" + tickerB.instrument_id, tickerA.last - tickerB.last)
            for (var j = 0 ; j < arr.length; j++) {
                if (tickerA.best_bid - tickerB.best_ask > arr[j].open && !arr[j].isHold) {   
                    Hedge(true, exchanges[0].SetContractType("quarter"), exchanges[1].SetContractType("this_week"))
                    arr[j].isHold = true
                }
                if (tickerA.best_ask - tickerB.best_bid < arr[j].cover && arr[j].isHold) {
                    Hedge(false, exchanges[0].SetContractType("quarter"), exchanges[1].SetContractType("this_week"))
                    arr[j].isHold = false 
                }
                tab.rows.push([JSON.stringify(arr[j])])
            }
        }
        LogStatus(_D(), "\n `" + JSON.stringify(tab) + "`")
    }
}

Related

More

chaoOK, V5 is now a private channel.

I love Jimmy.I'm going to try to find a way to do that, but I'm not sure how to do it. Time Platform type price quantity information 2021-02-13 00:00:00 Error main:12:12 - TypeError: Cannot read property 'write' of undefined 2021-02-13 00:00:00 Error sandbox not support Dial

fmzeroWhere's the teaching video?

The Little DreamThis policy cannot be used if the dial function is not supported.