पायथन प्लेटफ़ॉर्म संतुलन रणनीति (शिक्षण)

लेखक:छोटे सपने, दिनांक: 2020-02-03 22:43:38
टैगः

जावास्क्रिप्ट प्लेटफ़ॉर्म के लिए संतुलन नीति

यह एक भंडारण की आवश्यकता है, उदाहरण के लिए, एक खाते में 5000 डॉलर हैं, और एक सिक्का, अगर सिक्का का मूल्य खाते में शेष राशि से अधिक है 5000 और मूल्य का अंतर है, तो यह $ 6000 है, तो इसे बेच दें, उदाहरण के लिए, 6000-5000 / 6000 / 2 सिक्का, यह बताता है कि सिक्का बढ़ गया है, इसे वापस ले लो, अगर सिक्का गिर गया है, उदाहरण के लिए, $ 4000 / 4000 / 4 / 2 सिक्का, इसे खरीदें, अगर सिक्का गिर गया है, तो कुछ और खरीदें, अगर यह गिर गया है, तो इसे फिर से बेचें, जैसे कि एक समतल, दोनों तरफ अलग-अलग प्रतिभूति है, इसलिए मैं इसे संतुलन रणनीति कहता हूं।

लेख का पताःhttps://www.fmz.com/bbs-topic/4986


'''backtest
start: 2019-12-01 00:00:00
end: 2020-02-01 11:00:00
period: 1m
exchanges: [{"eid":"OKEX","currency":"BTC_USDT","stocks":1}]
'''

InitAccount = None

def CancelPendingOrders():
    ret = False
    while True:
        orders = _C(exchange.GetOrders)
        if len(orders) == 0 :
            return ret

        for j in range(len(orders)):
            exchange.CancelOrder(orders[j].Id)
            ret = True
            if j < len(orders) - 1:
                Sleep(Interval)
    return ret 

def onTick():
    acc = _C(exchange.GetAccount)
    ticker = _C(exchange.GetTicker)
    spread = ticker.Sell - ticker.Buy
    diffAsset = (acc.Balance - (acc.Stocks * ticker.Sell)) / 2
    ratio = diffAsset / acc.Balance
    LogStatus("ratio:", ratio, _D())
    if abs(ratio) < threshold:
        return False
    if ratio > 0 :
        buyPrice = _N(ticker.Sell + spread, ZPrecision)
        buyAmount = _N(diffAsset / buyPrice, XPrecision)
        if buyAmount < MinStock:
            return False
        exchange.Buy(buyPrice, buyAmount, diffAsset, ratio)
    else :
        sellPrice = _N(ticker.Buy - spread, ZPrecision)
        sellAmount = _N(-diffAsset / sellPrice, XPrecision)
        if sellAmount < MinStock:
            return False 
        exchange.Sell(sellPrice, sellAmount, diffAsset, ratio)
    return True

def main():
    global InitAccount, LoopInterval
    InitAccount = _C(exchange.GetAccount)
    LoopInterval = max(LoopInterval, 1)
    while True:
        if onTick():
            Sleep(1000)
            CancelPendingOrders()
            Log(_C(exchange.GetAccount))
        Sleep(LoopInterval * 1000)

अधिक