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

Realize an idea with 60 Lines of Code -- Contract Bottom Fishing Strategy

Author: FMZ~Lydia, Created: 2022-11-08 15:30:01, Updated: 2024-12-02 21:41:40

img

The grid strategy, Martingale strategy, which prefer market fluctuations, have their own drawbacks, and they have been tested for some time in the ETH contract market. I often chat with our new and old users on FMZ.COM to share the experiences. One thing about this sort of strategy is very much in agreement with a friend, that is, as for the contracts, going long has less risk than going short in the digital currency market. Or to be simple, the worst is decreasing to zero, but the increasing is infinite.

So, will Martingale, Grid and other strategies just go long, not go short, and spread the risk of bottom fishing in a long range be better than bilateral position? This idea sounds very good, but no one knows whether it can be put into practice. But, at least, we can backtest it simply. So we have today’s article topic – design a contract bottom fishing strategy.

Swift development based on FMZ.COM

The code to implement this idea is really simple, thanks to the flexibility of the platform, interface encapsulation, powerful backtesting system and so on. The complete code has only 60 lines (for code writing specification, many of them are not abbreviated).

The design idea of the strategy is very simple. Place the buying order down at intervals according to the initial price at the beginning of the logic and if the price continues to decline, we continue to place the buying order, and continue to go bottom fishing. Then we place the closing position order based on the position price increasing a certain profit margin, and wait for the position closing. If the position is closed, the above logic will be repeated with the current price as the initial price. The strategy will not go short to hold the position, but go long only.

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 parameter design is also very simple:

img

With these 6 parameters only.

Look at the backtesting result

Set the backtesting time range at random:.

img

Backtest:

img

img

img

It looks like the grid strategy, Martingale strategy very much~. Students who are beginners is usually afraid of the strategy with long code, which is easy to be dissuaded. A short and concise strategy to get started is easier and more appropriate to understand the strategy ideas and learn the logical design.

The strategy code is for study and research purposes only.


Related

More