暗号通貨契約 単純なオーダー監督ボット

作者: リン・ハーンニナバダス作成日: 2022-04-11 10:47:42,更新日: 2022-04-11 10:53:37

暗号通貨契約 単純なオーダー監督ボット

本日は簡単な注文監督ボットの契約版を実装します. このボットは,

デザインのアイデア

契約バージョンのオーダー監督ボットとスポットバージョンの違いは大きく,スポットオーダー監督は主に口座資産の変化を監視することによって実現できる.先物バージョンは口座のポジション変化を監視する必要がある. したがって,先物バージョンの状況はより複雑で,先物に関する長期と短期のポジションには異なる契約があり,一連の詳細に対処する必要があります.コアアイデアは,ポジションの変化を監視し,ポジション変化に基づいてオーダー監督アクションを誘発することです.当初は長期と短期を一緒に扱うように設計されましたが,それを扱うのは複雑であることがわかりました.問題を分析した後,長期と短期を別々に扱うことを決定しました.

戦略の実施

戦略パラメータ:

img

バックテストをサポートし,観察のためにバックテストにデフォルト設定を直接使用できます.

ソースコード:

/*backtest
start: 2021-03-18 00:00:00
end: 2021-04-07 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_OKCoin","currency":"BTC_USD"},{"eid":"Futures_OKCoin","currency":"BTC_USD"},{"eid":"Futures_OKCoin","currency":"BTC_USD"}]
*/

function test() {
    // test function 
    var ts = new Date().getTime()    
    if (ts % (1000 * 60 * 60 * 6) > 1000 * 60 * 60 * 5.5) {
        Sleep(1000 * 60 * 10)
        var nowPosAmount = getPosAmount(_C(exchange.GetPosition), refCt)
        var longPosAmount = nowPosAmount.long
        var shortPosAmount = nowPosAmount.short
        var x = Math.random()
        if (x > 0.7) {
            exchange.SetDirection("buy")
            exchange.Buy(-1, _N(Math.max(1, x * 10), 0), "the reference account tests ordering#FF0000")
        } else if(x < 0.2) {
            exchange.SetDirection("sell")
            exchange.Sell(-1, _N(Math.max(1, x * 10), 0), "the reference account tests ordering#FF0000")
        } else if(x >= 0.2 && x <= 0.5 && longPosAmount > 4) {
            exchange.SetDirection("closebuy")
            exchange.Sell(-1, longPosAmount, "the reference account tests closing positions#FF0000")
        } else if(shortPosAmount > 4) {
            exchange.SetDirection("closesell")
            exchange.Buy(-1, _N(shortPosAmount / 2, 0), "he reference account tests closing position#FF0000")
        }
    }
}

function getPosAmount(pos, ct) {
    var longPosAmount = 0
    var shortPosAmount = 0
    _.each(pos, function(ele) {
        if (ele.ContractType == ct && ele.Type == PD_LONG) {
            longPosAmount = ele.Amount
        } else if (ele.ContractType == ct && ele.Type == PD_SHORT) {
            shortPosAmount = ele.Amount
        }
    })
    return {long: longPosAmount, short: shortPosAmount}
}

function trade(e, ct, type, delta) {
    var nowPosAmount = getPosAmount(_C(e.GetPosition), ct)
    var nowAmount = type == PD_LONG ? nowPosAmount.long : nowPosAmount.short
    if (delta > 0) {
        // open position
        var tradeFunc = type == PD_LONG ? e.Buy : e.Sell
        e.SetDirection(type == PD_LONG ? "buy" : "sell")
        tradeFunc(-1, delta)
    } else if (delta < 0) {
        // close position 
        var tradeFunc = type == PD_LONG ? e.Sell : e.Buy
        e.SetDirection(type == PD_LONG ? "closebuy" : "closesell")
        if (nowAmount <= 0) {
            Log("no position detected")
            return 
        }
        tradeFunc(-1, Math.min(nowAmount, Math.abs(delta)))
    } else {
        throw "error"
    }
}

function main() {
    LogReset(1)
    if (exchanges.length < 2) {
        throw "no platform with order supervision"
    }
    var exName = exchange.GetName()
    // detect the platform for reference 
    if (!exName.includes("Futures_")) {
        throw "only support futures order supervising"
    }
    Log("start monitoring", exName, "platform", "#FF0000")
    
    // detect the order supervising platform 
    for (var i = 1 ; i < exchanges.length ; i++) {
        if (exchanges[i].GetName() != exName) {
            throw "The order supervising platform is different from the reference platform!"
        }
    }
    
    // set trading pair and contract 
    _.each(exchanges, function(e) {
        if (!IsVirtual()) {
            e.SetCurrency(refCurrency)
            if (isSimulate) {
                if (e.GetName() == "Futures_OKCoin") {
                    e.IO("simulate", true)
                }
            }
        }
        e.SetContractType(refCt)
    })

    var initRefPosAmount = getPosAmount(_C(exchange.GetPosition), refCt)
    while(true) {
        if (IsVirtual()) {    // only simulate during backtest 
            test()            // test function, which simulates a reference account to trade automatically, to trigger the order supervising of the account        
        }
        Sleep(5000)
        var nowRefPosAmount = getPosAmount(_C(exchange.GetPosition), refCt)
        var tbl = {
            type : "table", 
            title : "position",
            cols : ["name", "label", "long", "short", "account asset (Stocks)", "account assest (Balance)"],
            rows : []
        }
        _.each(exchanges, function(e) {
            var pos = getPosAmount(_C(e.GetPosition), refCt)
            var acc = _C(e.GetAccount)
            tbl.rows.push([e.GetName(), e.GetLabel(), pos.long, pos.short, acc.Stocks, acc.Balance])
        })
        LogStatus(_D(), "\n`" + JSON.stringify(tbl) + "`")
        
        // calculate the position amount of change 
        var longPosDelta = nowRefPosAmount.long - initRefPosAmount.long
        var shortPosDelta = nowRefPosAmount.short - initRefPosAmount.short

        // detect the change 
        if (longPosDelta == 0 && shortPosDelta == 0) {
            continue
        } else {
            // detect the position change 
            for (var i = 1 ; i < exchanges.length ; i++) {
                // execute the action of long
                if (longPosDelta != 0) {
                    Log(exchanges[i].GetName(), exchanges[i].GetLabel(), "Execute long order supervising, amount of change:", longPosDelta)
                    trade(exchanges[i], refCt, PD_LONG, longPosDelta)
                }
                // execute the action of short
                if (shortPosDelta != 0) {
                    Log(exchanges[i].GetName(), exchanges[i].GetLabel(), "Execute short order supervising, amount of change:", shortPosDelta)
                    trade(exchanges[i], refCt, PD_SHORT, shortPosDelta)
                }
            }
        }

        // after the operation of order supervising, update
        initRefPosAmount = nowRefPosAmount
    }
}

テスト

OKEXシミュレーションボットが使えるようになったので 2つのOKEXシミュレーションボットの APIKAYをテストしました とても便利です

追加される最初の交換オブジェクトは参照プラットフォームであり,注文監督プラットフォームは,参照プラットフォームのアカウントに従って動作します. OKEXのシミュレーションボットページでは,リファレンスプラットフォームアカウントが ETHで3つの四半期暗号化保証契約を手動に配置します.

img

ボットが位置変化を検知し,次の操作を行ったことがわかります.

img

2つの契約のポジションを閉じるようにしましょう. ポジションを閉じた後のポジションは,図に示されています.

img

ロボットが2つの契約を締結しました

img

戦略は,最適化なしでシンプルで理解しやすい方法で設計されています. 改善された部分は,注文の監督時に資産検出などの詳細に対処する必要があります. デザインを簡素化するために,市場注文は注文の監督命令に使用されます. 戦略は学習アイデアのみを提供し,ボットはお客様のニーズに応じて最適化できます.

戦略アドレス:https://www.fmz.com/strategy/270012

ご意見をお聞かせください


もっと