资源加载中... loading...

60-Line Code Realizing One Thought - Contract Bottom Fishing

Author: Ninabadass, Created: 2022-04-02 18:27:54, Updated: 2022-04-02 18:29:47

60-Line Code Realizing One Thought - Contract Bottom Fishing

Grid strategy and Martingale strategy, which likes to oscillate the market quotes, have inherent disadvantages, and similar strategies have been tested in the ETH contract market for a period of time. Also I often chat and share experiences with new and old players on FMZ.COM. There is one thing I really agree with a friend about this type of strategy. That is to do contracts in the currency circle, and the risk of doing long is less than that of doing shorting. Or simply speaking, the worst fall is to return to zero, but the rise is infinite.

So, do strategies such as Martingale and Grid only make long, but not short? Is it better to spread the risk of bottom fishing in a long range than to do bilateral? This idea sounds very good, but no one knows whether it can withstand the actual combat. But at least we can simply backtest this idea. Therefore, we have the topic of today’s article - Design a Contract Bottom Fishing Strategy.

Fast Development Based on FMZ.COM

The code to implement this idea is very simple, thanks to the flexibility, interface encapsulation and powerful backtest system of FMZ platform, and so on. The entire code is only 60 lines (for the code writing specifications, many lines that can be abbreviated, are not abbreviated).

The design of the strategy idea is very simple. According to the initial price at the beginning of the logic, place a buy order, if the interval distance is reducing. If the price continues to decline, continue to place a buy order, and keep bottom fishing. Then pend the close position order after the position price adding a certain profit spread, and wait to close. If the position is closed, the above logic is repeated with the current price as the initial price. The strategy does not do short, only does long.

Strategy source code:

function cancelAll() {
    while (true) {
        var orders = _C(exchange.GetOrders)
        if (orders.length == 0) { 
            break 
        }
        for (var i = 0 ; i < orders.length ; i++) {
            exchange.CancelOrder(orders[i].Id, orders[i])
            Sleep(interval)
        }
    }
}

function getLong(arr, kind) {
    var ret = null 
    for (var i = 0 ; i < arr.length ; i++) {
        if (arr[i].Type == (kind == "pos" ? PD_LONG : ORDER_TYPE_BUY)) {
            ret = arr[i]
        }
    }
    return ret
}

function pendingBidOrders(firstPrice) {
    var index = 0
    var amount = baseAmount
    while (true) {
        var pos = _C(exchange.GetPosition)
        var price = firstPrice - index * baseSpacing
        amount *= ratio
        index++
        exchange.SetDirection("buy")
        exchange.Buy(price, amount)        
        if (pos.length != 0) {
            var longPos = getLong(pos, "pos")
            if (longPos) {
                exchange.SetDirection("closebuy")
                exchange.Sell(longPos.Price + profitTarget, longPos.Amount)
            }
        }
        while (true) {
            Sleep(interval)
            if (!getLong(_C(exchange.GetOrders), "orders")) {
                cancelAll()
                break
            }
            if (!getLong(_C(exchange.GetPosition), "pos")) {
                cancelAll()
                return 
            }
        }
    }
}

function main() {
    exchange.SetContractType(symbol)
    while (true) {
        pendingBidOrders(_C(exchange.GetTicker).Last)
    }
}

The design of the parameters is also very simple:

img

There are only several parameters.

Let’s see the backtest result of dozens of lines of codes

Set the backtest time range randomly:

img

Backtest:

img

img

It looks like a grid or Martingale type strategy. Are new learners who just get started very afraid of this kind of long strategies and are easily persuaded to quit? A brief and concise strategy introduction is more suitable, and it is easier to digest strategy ideas and learn logical design.

The strategy codes above are only used for study and research.


More