Thoughts on asset movement through contract hedging strategy
Tags: contract, strategy, fmzquant, asset
Published: 2024-05-11 03:21:06

Recently, there has been a lot of news about the digital currency market and the exchange. For a time, all currency friends were in a state of panic, worrying about the security of their blockchain assets. There are also many small advertisements of 10% and 20% discount for idle second-hand currencies in various currency market groups. There are many kinds of strategies of "seeking to lose money steadily while making money steadily". Many users also teased "If there is a stable moneymaker, why do you want a stable moneyloser?"
It is true that both making stable profits and losing stable money are money printer, which is not easy to find.
Forgive me for my poor English.

However, there are still some unstable ones. For example, through contract hedging, we can make profits while making losses as much as possible.

DEMO strategy
```
/*backtest
start: 2020-09-30 00:00:00
end: 2020-10-19 00:00:00
period: 1d
basePeriod: 1m
exchanges: [{"eid":"Futures_OKCoin","currency":"BTC_USD"},{"eid":"Futures_HuobiDM","currency":"BTC_USD"}]
*/

var step = 20    // Step length of adding position price

function main() {
    var pos1 = []
    var pos2 = []
    var ct = "quarter"                         // For example, quarterly contract
    exchanges[0].SetContractType(ct)
    exchanges[1].SetContractType(ct)
    var diff = 0

    while (true) {
        var r1 = exchanges[0].Go("GetDepth")   // Exchange A
        var r2 = exchanges[1].Go("GetDepth")   // Exchange B
        var depth1 = r1.wait()
        var depth2 = r2.wait()

        if(depth1.Bids[0].Price - depth2.Asks[0].Price > diff) {
            if(pos1.length == 0 && pos2.length == 0) {
                var info1 = $.OpenShort(exchanges[0], ct, 10)
                var info2 = $.OpenLong(exchanges[1], ct, 10)
                pos1 = _C(exchanges[0].GetPosition)
                pos2 = _C(exchanges[1].GetPosition)
                diff = depth1.Bids[0].Price - depth2.Asks[0].Price
            } else if(depth1.Bids[0].Price - depth2.Asks[0].Price > diff + step) {
                var info1 = $.OpenShort(exchanges[0], ct, 10)
                var info2 = $.OpenLong(exchanges[1], ct, 10)
                pos1 = _C(exchanges[0].GetPosition)
                pos2 = _C(exchanges[1].GetPosition)
                diff = depth1.Bids[0].Price - depth2.Asks[0].Price
            }
        }
        
        if(pos1.length != 0 && pos1[0].Profit < -0.001) {
            var info1 = $.CoverShort(exchanges[0], ct, pos1[0].Amount)
            var info2 = $.CoverLong(exchanges[1], ct, pos2[0].Amount)
            pos1 = _C(exchanges[0].GetPosition)
            pos2 = _C(exchanges[1].GetPosition)
            diff = 0
        }
        LogStatus(_D(), diff)
        Sleep(500)
    }
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrkc1quxmg9heuk0iq8g.png)

Strategy logic:
The strategy starts to initialize the position variables pos1 and pos2 as empty arrays. The strategy enters the main loop. At the beginning of each loop, the depth data (order book data) of the contracts of the two exchanges are obtained to calculate the price difference. If the price difference continues to expand and beyond the "last price difference plus a step length", continue hedging and adding positions. When the position is held, it is detected that the position loss of the first exchange exceeds a certain value (such as -0.001), then close the position. Repeat in this way.

The principle is very simple, that is, when the price difference is large, then de-hedge. When waiting for the loss of the expected loss of the exchange position, close the position. If the price difference continues to expand, continue to add positions to hedge until the expected loss of the exchange position loss. The important parameters are: the amount of loss to close the position, the step length of adding position price difference, and the hedging amount.

The strategy is rather rudimentary,  just to verify the idea, the real bot is not available. There are still many issues to be considered for a real bot, for example, whether the contract to be traded is currency standard or U standard, and whether the multipliers of different contracts in exchanges A and B are the same.

In this way, one exchange will lose money, and the loss part will become the profit part of another exchange (price difference, there may be hedging loss, that is, the loss is more than the profit). The strategy adopts a futures trading class library, $.OverShort, $.OpenShort, these are the interface functions of the template. To run the above DEMO, you need to reference this class library.

The above strategy prototype is just the simplest exploration, and there may be more details to consider in actual operation, for example, the amount of positions can be designed to incremental. This is just an example here. Similar strategies should be able to optimize more, and experts are welcome to give suggestions.

From: https://blog.mathquant.com/2022/12/19/thoughts-on-asset-movement-through-contract-hedging-strategy.html