बिटमेक्स के लंबित आदेश रणनीति का विस्तृत विवरण

लेखक:अच्छाई, बनाया गयाः 2019-01-16 15:28:13, अद्यतन किया गयाः 2019-01-22 14:16:48

BitMEX क्रिप्टोक्यूरेंसी लीवरेज ट्रेडिंग के लिए पसंद का मंच बन गया है, लेकिन इसके एपीआई ट्रेडिंग प्रतिबंध सख्त हैं और स्वचालित व्यापारियों को बहुत भ्रमित महसूस करते हैं। यह लेख मुख्य रूप से एफएमजेड मात्रात्मक ट्रेडिंग प्लेटफॉर्म में एपीआई के उपयोग पर कुछ सुझाव साझा करता है, मुख्य रूप से बाजार बनाने की रणनीति के लिए।

1. बिटमेक्स की विशेषताएं

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

2.बिटमेक्स एपीआई आवृत्ति सीमा

REST एपीआई की अनुरोध आवृत्ति हर 5 मिनट में 300 बार तक सीमित है, लगभग हर सेकंड में 1 बार के बराबर है, यह सीमा अन्य ट्रेडिंग प्लेटफार्मों की तुलना में बहुत सख्त है। सीमा पार करने के बाद, Rate limit exceeded संकेत दिया जाएगा। यदि आप सीमा को पार करते रहते हैं, तो आईपी एक घंटे के लिए अक्षम हो सकता है। कम समय में कई अक्षम होने से एक सप्ताह अक्षम हो जाएगा। प्रत्येक एपीआई अनुरोध के लिए, बिटमेक्स हेडर डेटा लौटाएगा, हेडर डेटा का उपयोग शेष अनुरोधों की वर्तमान संख्या देखने के लिए किया जाता है। वास्तव में, यदि एपीआई का ठीक से उपयोग किया जाता है, तो यह आवृत्ति सीमा से अधिक नहीं होगा और आम तौर पर जांच की आवश्यकता नहीं है।

3.बाजार की बोली प्राप्त करने के लिए वेबसॉकेट का उपयोग करें

BitMEX REST API अधिक प्रतिबंधात्मक है। आधिकारिक सिफारिश वेबसॉकेट प्रोटोकॉल का अधिक उपयोग करना है, और औसत विनिमय की तुलना में अधिक डेटा प्रकारों को धक्का देना है। विशिष्ट उपयोग के लिए निम्नलिखित बिंदुओं पर ध्यान देंः

यदि गहराई डेटा पुश समय बहुत लंबा है, तो एक त्रुटि होगी, जो वास्तविक गहराई से मेल नहीं खाती है। यह अनुमान लगाया जाता है कि पुश में बहुत अधिक गहराई परिवर्तन और चूक है, लेकिन सामान्य तौर पर, उत्कृष्ट तरलता के कारण, आप ticker या trades की सदस्यता ले सकते हैं। आदेश विवरण पुश बहुत गायब है और लगभग अनुपलब्ध है। खाते की जानकारी के पुश में महत्वपूर्ण देरी होती है, अधिमानतः REST API का उपयोग करके। जब बाजार बहुत अस्थिर होता है, तो धक्का देरी कुछ सेकंड तक पहुंच जाएगी। निम्नलिखित कोड वास्तविक समय में बाजार और खाता जानकारी प्राप्त करने के लिए वेबसॉकेट प्रोटोकॉल का उपयोग करता है, मुख्य रूप से बाजार बनाने की रणनीतियों के लिए। विशिष्ट उपयोग मुख्य))) फ़ंक्शन में किया जाना चाहिए।

var ticker  = {price:0, buy:0, sell:0, time:0} //Ticker information, the latest price, "buy one" price, "sell one" price, update time
//Account information, respectively, position, buying and selling price, buying and selling quantity, position status, order Id
var info = {position:0, buyPrice:0, sellPrice:0, buyAmount:0, sellAmount:0, buyState:0, sellState:0, buyId:0, sellId:0}
var buyListId = []//Global variables, pre-emptive buying id list, will described below
var sellListId = []
var APIKEY = 'your api id' //Need to fill in the BitMEX API ID here. Note that it is not a key, which is required for websocket protocol authentication.
var expires = parseInt(Date.now() / 1000) + 10
var signature = exchange.HMAC("sha256", "hex", "GET/realtime" + expires, "{{secretkey}}")//The secretkey will be automatically replaced at the bottom level and does not need to be filled in.
var bitmexClient = Dial("wss://www.bitmex.com/realtime", 60)
var auth = JSON.stringify({args: [APIKEY, expires, signature], op: "authKeyExpires"})//Authentication information, otherwise you cannot subscribe to the account
bitmexClient.write(auth)
bitmexClient.write('{"op": "subscribe", "args": ["position","execution","trade:XBTUSD"]}')//Subscribed to positions, order execution and perpetual contract real-time transaction
while(true){
    var data = bitmexClient.read()
    if(data){
        bitmexData = JSON.parse(data)
        if('table' in bitmexData && bitmexData.table == 'trade'){
            data = bitmexData.data
            ticker.price = parseFloat(data[data.length-1].price)//The latest transaction price, will push multiple transactions at a time, take one will be ok
            //You can get the "buy one" and "sell one" price according to the direction of the latest transaction, without subscribing to the depth.
            if(data[data.length-1].side == 'Buy'){
                ticker.sell = parseFloat(data[data.length-1].price)
                ticker.buy = parseFloat(data[data.length-1].price)-0.5
            }else{
                ticker.buy = parseFloat(data[data.length-1].price)
                ticker.sell = parseFloat(data[data.length-1].price)+0.5
            }
            ticker.time =  new Date(data[data.length-1].timestamp);//Update time, can be used to determine the delay
        }
    }else if(bitmexData.table == 'position'){
        var position = parseInt(bitmexData.data[0].currentQty)  
        if(position != info.position){
            Log('Position change: ', position, info.position, '#FF0000@')//Position change Log, and pushed to WeChat, remove @ means Do not push
            info.position = position  
        }
        info.position  = parseInt(bitmexData.data[0].currentQty)  
    }
}

4. आदेश देने का कौशल

BitMEX आधिकारिक तौर पर आदेश देने के लिए bulk ordering और order modification का उपयोग करने की सलाह देता है। bulk ordering को BitMEX वास्तविक समय ऑडिटिंग, जोखिम जांच, मार्जिन गणना और कमीशन के कारण तेजी से निष्पादित किया जा सकता है। इसलिए, bulk ordering की आवृत्ति की गणना सामान्य आवृत्ति के दसवें हिस्से के रूप में की जाती है। इसके अलावा, हमारे ऑर्डर ऑपरेशन को API के उपयोग को कम करने के लिए bulk ordering और order modification की विधि का उपयोग करना चाहिए। क्वेरी ऑर्डर स्थिति को भी आवृत्ति का उपयोग करके API का उपभोग करने की आवश्यकता होती है। यह परिवर्तन या संशोधन आदेश विफलता स्थिति के अनुसार ऑर्डर स्थिति का न्याय कर सकता है।

bulk ordering ऑर्डर की मात्रा को सीमित नहीं करता है (बहुत अधिक नहीं हो सकता है), वास्तव में, एक एकल ऑर्डर भी bulk ordering इंटरफ़ेस का उपयोग कर सकता है। ऑर्डर को संशोधित करने के संचालन के कारण, हम pre-order कुछ ऑर्डर कर सकते हैं जहां कीमत बहुत विचलित होती है, ये ऑर्डर निष्पादित नहीं किए जाएंगे, लेकिन जब हमें ऑर्डर देने की आवश्यकता होती है, तो हमें केवल रखे गए ऑर्डर की कीमत और मात्रा को संशोधित करने की आवश्यकता होती है। जब ऑर्डर को संशोधित करने में विफलता होती है, तो इसका उपयोग ऑर्डर निष्पादित करने के लिए एक संकेत के रूप में भी किया जा सकता है।

निम्नलिखित विशिष्ट कार्यान्वयन कोड हैः

// Cancel all orders and reset global variables
function cancelAll(){
    exchange.IO("api","DELETE","/api/v1/order/all","symbol=XBTUSD")//Call IO extension revocation
    info = {position:0, buyPrice:0, sellPrice:0, buyAmount:0, sellAmount:0, buyState:0, sellState:0, buyId:0, sellId:0}
    buyListId = []
    sellListId = []
}
//placing alternate order
function waitOrders(){
    var orders = []
    if(buyListId.length<4){
        //When the number of inspections is insufficient, place another "bulk"
        for(var i=0;i<7;i++){
            //Due to BitMEX restrictions, the price can not be excessively excessive, the order quantity can not be too small, and the "execInst" parameter guarantees that only the market making transaction can be executed.
            orders.push({symbol:'XBTUSD', side:'Buy', orderQty:100, price:ticker.buy-400+i, execInst:'ParticipateDoNotInitiate'})
        }
    }
    if(sellListId.length<4){
        for(var i=0;i<7;i++){
            orders.push({symbol:'XBTUSD', side:'Sell', orderQty:100, price:ticker.buy+400+i, execInst:'ParticipateDoNotInitiate'})
        }
    }
    if(orders.length>0){
        var param = "orders=" + JSON.stringify(orders);
        var ids = exchange.IO("api", "POST", "/api/v1/order/bulk", param);//Bulk orders submitted here
        for(var i=0;i<ids.length;i++){
            if(ids.side == 'Buy'){
                buyListId.push(ids.orderID)
            }else{
                sellListId.push(ids.orderID)
            }
        }
    }
}
//Modify order function
function amendOrders(order, direction, price, amount, id){
    var param = "orders=" + JSON.stringify(order);
    var ret = exchange.IO("api", "PUT", "/api/v1/order/bulk", param);//Modify one order at a time
    //Modification occurs error
    if(!ret){
        var err = GetLastError()
        //overloaded unmodified strategy, need to recycle the order id
        if(err.includes('The system is currently overloaded')){
            if(id){
                if(direction == 'buy'){
                    buyListId.push(id)
                }else{
                    sellListId.push(id)
                }
            }
            Sleep(1000)
            return
        }
        //Illegal order status, indicating that the order to be modified has been completely executed
        else if(err.includes('Invalid ordStatus')){
            Log(order, direction)
            if(direction == 'buy'){
                info.buyId = 0
                info.buyState = 0
                info.buyAmount = 0
                info.buyPrice = 0
            }else{
                info.sellId = 0
                info.sellState = 0
                info.sellAmount = 0
                info.sellPrice = 0
            }
            //Since the push is not timely, update the position with the "rest" protocol here.
            pos = _C(exchange.GetPosition)
            if(pos.length>0){
                info.position = pos[0].Type == 0 ? pos[0].Amount : -pos[0].Amount
            }else{
                info.position = 0
            }
        }
        //Unknown error cannot be modified, all orders are cancelled, reset once
        else if(err.includes('Invalid orderID')){
            cancelAll()
            Log('Invalid orderID,reset once')
        }
        //Exceed the frequency limit, you can continue to try after hibernation
        else if(err.includes('Rate limit exceeded')){
            Sleep(2000)
            return
        }
        //The account is banned, all orders are revoked, and sleep is awaiting recovery for a long time.
        else if(err.includes('403 Forbidden')){
            cancelAll()
            Log('403,reset once')
            Sleep(5*60*1000)
        }
    }else{
        //Modify order successfully
        if(direction == 'buy'){
            info.buyState = 1
            info.buyPrice = price
            info.buyAmount = amount
        }else{
            info.sellState = 1
            info.sellPrice = price
            info.sellAmount = amount
        }
    }
}
//0.5 price change
function fixSize(num){
    if(num>=_N(num,0)+0.75){
        num = _N(num,0)+1
    }else if(num>=_N(num,0)+0.5){
        num=_N(num,0)+0.5
    }else{
        num=_N(num,0)
    }
    return num
}
//Trading function
function trade(){
    waitOrders()//Check if you need a replacement order
    var buyPrice = fixSize(ticker.buy-5) //For demonstration purposes only, specific transactions should be written by yourself.
    var sellPrice = fixSize(ticker.sell+5)
    var buyAmount =  500
    var sellAmount = 500
    //Modify from an alternate order when there is no order
    if(info.buyState == 0  && buyListId.length > 0){
        info.buyId = buyListId.shift()
        amendOrders([{orderID:info.buyId, price:buyPrice, orderQty:buyAmount}],'buy', group, buyPrice, buyAmount, info.buyId)
    }
    if(info.sellState == 0 && sellListId.length > 0){
        info.sellId = sellListId.shift()
        amendOrders([{orderID: info.sellId, price:sellPrice, orderQty:sellAmount}],'sell', group, sellPrice, sellAmount, info.sellId )
    }
    //Existing orders need to change price
    if(buyPrice !=  info.buyPrice && info.buyState == 1){
        amendOrders([{orderID:info.buyId, price:buyPrice, orderQty:buyAmount}],'buy', group, buyPrice, buyAmount)
    }
    if(sellPrice != info.sellPrice && info.sellState == 1){
        amendOrders([{orderID:info.sellId, price:sellPrice, orderQty:sellAmount}],'sell', group, sellPrice, sellAmount)
    }
}

5. अन्य

BitMEX का सर्वर डबलिन, आयरलैंड में Amazon के सर्वर में है। जब आप डबलिन में AWS क्लाउड सर्वर चुनते हैं तो सर्वर चलाने की रणनीति पिंग 1ms से कम होती है, लेकिन जब धक्का देने में देरी होती है, तो अधिभार समस्या हल नहीं हो सकती है। इसके अलावा, जब खाता लॉग इन होता है, तो सर्वर एजेंट संयुक्त राज्य अमेरिका और अन्य स्थानों में नहीं हो सकता है जहां क्रिप्टोक्यूरेंसी ट्रेडिंग की अनुमति नहीं है। विनियमन के कारण, खाते पर प्रतिबंध लगा दिया जाएगा।

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


अधिक

जीएमजीफिलWS कनेक्टर त्रुटि उत्पन्न करता हैः ReferenceError: पहचानकर्ता 'डेटा' undefined at...

घासएक कोड त्रुटि, समस्या ठीक कर दी गई है