The resource loading... loading...

The Python spreadsheet platform balancing strategy

Author: Inventors quantify - small dreams, Created: 2020-02-05 10:02:03, Updated: 2023-10-12 21:20:47

img

The Python spreadsheet platform balancing strategy

The JavaScript version

The policy address:https://www.fmz.com/strategy/345

In this article, we will practice porting a simple JavaScript policy. Through the porting policy, we will become more familiar with the inventions of quantitative trading platform interface calls, and understand the slight differences between different languages when developing platform policies.

The strategy

The following is a description from the JavaScript version:

This requires building a warehouse, say you have $5,000 in the account, and a coin, and if the value of the coin is greater than the balance in the account is $5,000 and the spread is greater than the depreciation, say the coin is now worth $6,000, you sell it, say $6,000-$5,000, or $6,000-$2,000, or $6,000/$2,000, or $6,000/$2,000, or $6,000/$2,000, or $6,000/$2,000, or $5,000/$4,000, or $4,000/$2,000, or $4,000/$2,000, or $5,000/$4,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $5,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000/$2,000, or $4,000

The strategy is very simple, and the JavaScript version of the code is not long, only more than 70 lines. Ported to a simpler syntax Python language strategy, the code is more shortened, very suitable for beginners to learn, on the inventor quantitative trading platform there is a large number of developers share code, language supportJavaScript/C++/PythonSo, having more knowledge of a development language is not only helpful for learning, research and development strategies, but also a better understanding of the various API interfaces of the platform.

The Strategy Code

'''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)

The code starts with

'''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}]
'''

This is the retest configuration, which means that the retest configuration (setting) is saved in the form of code, which is automatically configured according to this setting when retested. This part can be deleted, deleted, and when retested, it is necessary to manually set the retest configuration information on the retest page. See also:https://www.fmz.com/bbs-topic/859

The policy's parameters are perfectly consistent with the JavaScript version, the policy code is also sentence-by-sentence ported, the program structure is unchanged, and can be compared sentence-by-sentence, looking at the differences between the policies written in different languages.

Reassessment

Parameters are configuredimg

Statistical dataimg

img

The policy address:https://www.fmz.com/strategy/183374

The strategy is for reference learning only, retesting tests, and interest in optimizing upgrades.


Related

More

Treasures from HeavenGood cow.