क्रिप्टोकरेंसी अनुबंध सरल आदेश-पर्यवेक्षण बॉट

लेखक:निनाबादास, बनाया गयाः 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 V5 इंटरफ़ेस को अपडेट करने के बाद, और OKEX सिम्युलेटेड बॉट का उपयोग किया जा सकता है, मैंने परीक्षण करने के लिए दो OKEX सिम्युलेटेड बॉट्स के एपीआई केए का उपयोग किया, बहुत सुविधाजनक रूप से।

जोड़ा जाने वाला पहला विनिमय वस्तु संदर्भ मंच है, और आदेश पर्यवेक्षण मंच संचालन के लिए संदर्भ मंच के खाते का अनुसरण करता है। ओकेएक्स सिमुलेटेड बॉट पेज पर, संदर्भ प्लेटफॉर्म खाता मैन्युअल रूप से 3 ईटीएच त्रैमासिक क्रिप्टो-मार्जिन किए गए अनुबंध रखता है।

img

यह देखा जा सकता है कि बॉट ने स्थिति परिवर्तन का पता लगाया, और निम्नलिखित संचालन।

img

चलो 2 अनुबंध पदों है कि हम अभी खोला बंद करने की कोशिश करते हैं. पदों को बंद करने के बाद पदों चित्र में दिखाए जाते हैंः

img

रोबोट ने ऑपरेशन किया और 2 अनुबंध बंद किए।

img

रणनीति को अनुकूलन के बिना सरल और समझने में आसान तरीके से डिज़ाइन किया गया है। बेहतर भाग को ऑर्डर की देखरेख करते समय परिसंपत्ति का पता लगाने जैसे विवरणों से भी निपटने की आवश्यकता है। डिजाइन को सरल बनाने के लिए, ऑर्डर पर्यवेक्षण ऑर्डर के लिए बाजार ऑर्डर का उपयोग किया जाता है। रणनीति केवल सीखने के विचार प्रदान करती है, और बॉट को आपकी आवश्यकताओं के अनुसार अनुकूलित किया जा सकता है।

रणनीतिक पता:https://www.fmz.com/strategy/270012

अपनी टिप्पणी छोड़ने के लिए आपका स्वागत है।


अधिक