Thinking about moving assets through a contract hedging strategy

Author: The Cond, Created: 2020-10-20 16:48:32, Updated: 2023-09-26 20:59:29

img

Thinking about moving assets through a contract hedging strategy

Recently, the currency circle can be said to be constant news, the news of the exchange is also sky-high. At one time, all the currency friends were nervous, they were worried about the security of their blockchain assets. All the currency circle groups also had 9 discounts, 8 discounts and small ads for putting away second-hand coins. It's true that stable earnings, stable losses, are the same thing.money printerI'm not sure I can find it. Sorry for my clumsy English.

However, there are still uncertainties, such as the possibility of maximizing losses and gains through contract hedging.

DEMO policy

/*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    // 加仓价格步长

function main() {
    var pos1 = []
    var pos2 = []
    var ct = "quarter"                         // 例如用季度合约
    exchanges[0].SetContractType(ct)
    exchanges[1].SetContractType(ct)
    var diff = 0

    while (true) {
        var r1 = exchanges[0].Go("GetDepth")   // A交易所
        var r2 = exchanges[1].Go("GetDepth")   // 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)
    }
}

img

img

The strategic logic: The strategy starts by initializing the holding variable pos1, pos2 as an empty array. The strategy enters the main cycle, each cycle starts by obtaining the depth data of the contracts of the two exchanges (the order thin data), calculating the spread. If the spread continues to expand beyond the last spread plus a step length, then continue to hedge the position. When holding the position, detect the holding loss of the first exchange above a certain value (e.g.: -0.001), the placement.

The principle is actually very simple, that is, the spread is large, against the pull. The exchange that expects a loss holds a loss when it holds a loss, if the spread continues to expand, it continues to hedge until the exchange that expects a loss holds a loss. A few more important parameters are: how much of the loss holds a loss, the pace of the spread, the amount of hedging.

The strategy is relatively simple, just to verify the idea, the real disk is not available. The real disk also has many questions to consider, such as whether the contract to be traded is the price of the currency, or the price of U, whether the different contract multiples of A and B exchanges are the same, etc.

This results in one exchange losing money and the other exchange losing money (a difference problem, there may be a hedged loss, i.e. more losses than gains).$.CoverShort,$.OpenShortThese are the interface functions of the template, which DEMO above needs to reference in order to run.

The above strategy prototype is only the simplest little exploration, and there may be more details to consider in the specific practical operation, such as the increase in capacity that can be designed as incremental. Here is just a throw of the dice, similar strategies should have more optimization, welcome experts to give suggestions.


Related

More

The bone knifePlease install it as soon as possible, if you want to use it, I want to rent it and take out my game coin.

The Little DreamThe author begins by saying that instability and bad things can be reversed.