JavaScript Version

Strategy address: https://www.fmz.com/strategy/345

In this article, let's practice porting a simple JavaScript strategy. Through the strategy porting, we can become more familiar with the call of the FMZ Quant Trading Platform interface, and understand the slight differences between different languages in the platform development strategy. In fact, the difference between the JavaScript version and the Python version is very little, because the interface calls are basically the same.

Strategy Description

Description quoted from JavaScript version:

This requires to open a position. For example, if the account has 5000 yuan and one currency, if the value of the currency is greater than the account balance of 5000 yuan and the price difference exceeds the threshold value, for example, if the currency is now worth 6000 yuan, then sell (6000-5000)/6000/2 currencies, indicating that the currency has appreciated and we can convert the money back. If the currency has depreciated, for example, 4000 yuan, then we buy (5000-4000)/4000/2 currencies. If the currency decreases, buy some. If it rises again, sell again, Like the balance, the two sides have different hedges, so I call it the equilibrium strategy

The principle of the strategy is very simple. The code of the JavaScript version is not long, only more than 70 lines. The Python language strategy with more concise grammar is transplanted, and the code is much shorter, which is very suitable for beginners to learn. There are a lot of code shared by developers on the FMZ Quant Trading Platform, and the language supports JavaScript/C++/Python, etc. Therefore, mastering more development languages is not only helpful for learning, research, and development strategies, but also familiar with the various API interfaces of the platform.

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

It refers to the backtesting configuration, which means that the backtesting configuration (settings) is saved in the form of code and configured automatically according to the setting during the backtesting. This part can be deleted. If it is deleted, we need to set the backtesting configuration information on the backtesting page manually.
Reference: https://www.fmz.com/bbs-topic/859

The parameters of this strategy are completely consistent with the JavaScript version. The strategy code is also transplanted sentence by sentence. The program structure has not changed. You can compare the strategies written in different languages sentence by sentence.

Backtesting

Parameter configuration

/upload/asset/28e91c45a78537fdaa5f9.png

Statistics

/upload/asset/28d5340ae25e16cdfbc3f.png
/upload/asset/28e2a9b0b7ed41d5229bb.png

Strategy address: https://www.fmz.com/strategy/183374

The strategy is for reference, learning and back testing only. If you are interested, you can optimize and upgrade it.

Leave a Reply

Your email address will not be published. Required fields are marked *