Preliminary Study on Backtesting of Digital Currency Options Strategy
Tags: backtest, strategy, cryptocurrency, fmzquant
Published: 2024-06-19 07:34:59

Recently, FMZ Platform has upgraded the backtesting system to support the backtesting of digital currency options. This time, it supports some option data of the Deribit exchange. So we have better tools for option trading learning and strategy verification.

## Deribit options backtest
The Deribit option defined in the backtest system is European style, and the value of one contract is 1BTC. The option contract code is: BTC-7AUG20-12750-C.

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

Operations such as setting contracts and obtaining positions are the same as digital currency futures.
Set contract: exchange.SetContractType("BTC-7AUG20-12750-C")
Get position: var pos = exchange.GetPosition()

The price of an option contract is the premium of an option contract, and the option buyer needs to pay the option premium to the option seller. The buyer has the right to exercise, and the seller has the obligation to exercise. Before the option contract is exercised, it can be traded (such as liquidation, settlement of obligations).

## Examples of common option trading combinations
Sell a call option and buy a spot.
```
/*backtest
start: 2020-07-27 00:00:00
end: 2020-08-05 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Deribit","currency":"BTC_USD"},{"eid":"OKEX","currency":"BTC_USDT","balance":100000}]
*/

function main() {
    exchanges[0].SetContractType('BTC-7AUG20-12750-C');
    var initSpotAcc = _C(exchanges[1].GetAccount)
    var isFirst = true
    while(true) {
        var optionTicker = exchanges[0].GetTicker()
        var spotTicker = exchanges[1].GetTicker()
        if(isFirst) {
            exchanges[0].SetDirection("sell")
            exchanges[0].Sell(optionTicker.Buy, 1)
            exchanges[1].Buy(spotTicker.Sell, 1)
            
            isFirst = false 
        }
        
        var optionPos = _C(exchanges[0].GetPosition)
        var nowSpotAcc = _C(exchanges[1].GetAccount)
        var diffStocks = (nowSpotAcc.Stocks - initSpotAcc.Stocks)
        var diffBalance = (nowSpotAcc.Balance - initSpotAcc.Balance)
        var spotProfit = diffBalance + diffStocks * spotTicker.Last
        var optionProfit = optionPos[0].Profit * spotTicker.Last
        LogProfit(spotProfit + optionProfit)
        $.PlotLine("Spots", spotProfit)
        $.PlotLine("Options", optionProfit)
        Sleep(500)
    }
}
```

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

Options can provide a certain degree of hedging protection to the assets purchased in spot. Generally used when optimistic about the spot and willing to hold the spot. The risk lies in the drop in spot prices. Although to a certain extent, options can make up for a certain spot loss, but after the loss exceeds the option premium, there will be a net loss.

In addition, the liquidity of the digital currency options market is average, and sometimes no counterparty is found. This is also an issue that needs to be considered.

Similarly, we can replace spot with futures, the code is as follows:
```
/*backtest
start: 2020-07-27 00:00:00
end: 2020-08-05 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Deribit","currency":"BTC_USD"},{"eid":"Futures_OKCoin","currency":"BTC_USD"}]
*/

function main() {
    exchanges[0].SetContractType('BTC-7AUG20-12750-C');
    exchanges[1].SetContractType("quarter")
    var isFirst = true
    while(true) {
        var optionTicker = exchanges[0].GetTicker()
        var futuresTicker = exchanges[1].GetTicker()
        if(isFirst) {
            exchanges[0].SetDirection("sell")
            exchanges[0].Sell(optionTicker.Buy, 1)
            
            exchanges[1].SetDirection("buy")
            exchanges[1].Buy(futuresTicker.Sell, _N(1 * futuresTicker.Sell / 100, 0))
            
            isFirst = false 
        }
        
        var optionPos = _C(exchanges[0].GetPosition)
        var futuresPos = _C(exchanges[1].GetPosition)
        
        
        var futuresProfit = futuresPos[0].Profit 
        var optionProfit = optionPos[0].Profit
        LogProfit(futuresProfit + optionProfit)
        $.PlotLine("Futures", futuresProfit)
        $.PlotLine("Options", optionProfit)
        Sleep(500)
    }
}
```

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

Futures can reduce the capital occupied than spot, but the risk is higher than that of spot.

In addition, there are many other options trading combinations:

- Bull call option spread bull call spread
- Bear Put option Spread Bear Put Spread
Those who are interested can study it in the backtest system.

From: https://www.fmz.com/digest-topic/6056