Modified Deribit futures API to accommodate options quantitative trading

Author: The Little Dream, Created: 2019-10-29 14:57:54, Updated: 2023-10-17 21:20:50

img

There are many digital currency futures exchanges, but as a futures derivative, digital currency options trading, there are not many exchanges on the market, there are Deribit, BitMEX. In the field of quantitative trading, options trading also has several strategies, such as the options strategy mentioned in some of the information:

Types
This is the first time I've seen this video. Buying a put option Selling a put option The bull market sees the price of aluminum falling The bull market is falling
Buy a bear option Selling the options The bear market sees the price of uranium falling The bear market is falling
This is the first time I have seen this kind of thing. Selling the crossover Selling wide Buying a crossover Buying a broadband
How to hedge: Stand by to watch Standby and drop Protective eyewear Protectionist downturn
Multiple boundaries Blank head

Cited byConnection

Writing options trading strategies still requires a solid foundation, basic ordering, market acquisition, withdrawal, holding, etc. operations should be familiar. Writing strategies still use inventors' quantitative trading platforms, although inventors' quantitative trading platforms currently support the field of digital currency quantitative trading mainly in the field of coin trading, contract trading, leverage trading.

Deribit and related information

The API documentation:https://docs.deribit.com/v2/?javascript#public-get_last_settlements_by_instrumentThe simulation disc:https://docs.deribit.com/v2/?javascript#public-get_last_settlements_by_instrument

You can register an account on the platform, open the API KEY, and access the API KEY. Configure the inventor's quantitative trading platform as if it were a physical disk.img

There are four basic concepts to understand about options trading:img

  • Exercise of power: The parties to the multi-space option complete the option contract on that date.
  • Put option price: On the put day, the option's over-the-counter counterparty completes the option contract at the put price.
  • Right price: The price at which the option is offered, which is the same as the spot futures, with a buy and a sell price. It is worth noting that since the liquidity of options is generally worse than that of futures and spot options, the bid-ask spread is likely to be very large, which should be especially noted here! After the transaction, the transaction price is the cost of the option multiple, at which time the multiple gains rights (exercising the right to the option); while the option blank as the right fee, adds an obligation, once the multiple demands the exercise of the right, the blank must cooperate.
  • See also Call Put Option: The so-called bearish option is the option multiple holds on a certain trading day, at a certain trading price, to the option head demand the right to buy a set of bitcoins, the head has a cooperative obligation; while the so-called bearish option is the option multiple holds on a certain trading day, at a certain trading price, to the head demand the right to sell a set of bitcoins, the head has a cooperative obligation.

Acquisition

A look at the API documentation of the Deribit exchange shows that Deribit's market interface is simply a pass-through to access the futures or options market.instrument_nameIf the parameters are different (instrument_name is set by the SetContractType function), then you can basically use the interface to access the market.GetTickerIn addition to the above, there are also a number of other options available.

Of course, the inventor of the QT platform packs the virtual disk of the Deribit exchange by default, so we first need to switch to the analogue disk using the following code:

exchange.IO("base", "https://test.deribit.com")

And then we set the current as an option contract.BTC-27DEC19-7000-PI'm not sure. This is a call option with a call date of 27DEC19 and a call price of 7000.

exchange.SetContractType("BTC-27DEC19-7000-P")

Then get, we write together, let the code run, test how to get this option contract.

function main () {
    exchange.IO("base", "https://test.deribit.com")
    exchange.SetContractType("BTC-27DEC19-7000-P")
    var ticker = exchange.GetTicker()
    Log(ticker)
}

It is easy to test using the debugger tool:imgAs you can see, the price is consistent with the price on the analogue disc.img

The following are some of the most common types of interfaces used in the industry: Options trading is not very active, and sometimes there is no bid or offer, at which point inventors quantify the bottom of the trading platform to detect a value of zero, which will return an error, which can be used.SetErrorFilter("Invalid ticker")I'm not going to go into detail about this, but I'm going to try to figure out how to do it.GetRawJSONThe function is used to capture raw information about the market and package the data, and here I write an example to implement a similar function:

function init() {
    SetErrorFilter("Invalid ticker")
}

$.GetTicker = function(e) {
    var ticker = e.GetTicker()
    if (!ticker) {
        try {
            var ret = JSON.parse(e.GetRawJSON())
            return {
                Info : ret,
                High : ret.result.stats.high,
                Low : ret.result.stats.low, 
                Buy : ret.result.best_bid_price,
                Sell : ret.result.best_ask_price,
                Last : ret.result.last_price, 
                Volume : ret.result.stats.volume,
                OpenInterest : 0,
                Time : new Date().getTime()
            }
        } catch (err) {
            Log(err)
        }
    }
    
    return ticker
}

He wrote on the call:Log($.GetTicker(exchange))

List below

The following procedure is very simple, as compared to futures trading, only buy and sell in two directions.Sell,BuyThe functions are listed here.

function main () {
    exchange.IO("base", "https://test.deribit.com")
    exchange.SetContractType("BTC-27DEC19-7000-P")
    
    var id = exchange.Buy(0.017, 1)
    Log(exchange.GetOrder(id))
}

img

The simulation displays the orders that have just been placed.img

andexchange.GetOrder(id)You can check the order information here.

Withdrawal

Withdrawals are also used.CancelOrderThe function is the same as withdrawal in futures trading.img

Acquire Accounts Available

Acquire an account with the same available assets as when trading futures, directly callGetAccountLet's say the function is.

Display on the simulated exchange pageimg

Running code obtained:img

Access to information on holdings

The packaging cannot be used directly for storage.GetPositionThis function is used only to access futures holdings, since Deribit is a futures trading, not an options trading, by default. So that's where we have to start wrapping up the option holding function ourselves.

Interface of the API documentation to obtain the hold function:img

$.GetPosition = function(e) {
    // /private/get_positions
    // currency  , kind 
    
    var positions = [] 
    var currency = e.GetCurrency()
    var arr = currency.split("_")
    var baseCurrency = arr[0]
    
    try {
        var ret = e.IO("api", "GET", "/api/v2/private/get_positions", "currency=" + baseCurrency + "&kind=option")
        for (var i in ret.result) {
            if (ret.result[i].size == 0 || ret.result[i].direction == "zero") {
                continue    
            } 
            
            var pos = {
                Info : ret.result[i], 
                Amount : ret.result[i].size,
                FrozenAmount : 0,
                Price : ret.result[i].average_price,
                Profit : ret.result[i].floating_profit_loss,
                MarginLevel : 0,
                Margin : 0,
                ContractType : ret.result[i].instrument_name,
                Type : ret.result[i].direction == "buy" ? ORDER_TYPE_BUY : ORDER_TYPE_SELL,
            }
            
            positions.push(pos)
        }
    } catch (err) {
        Log(err)
        positions = null
    }
    
    return positions
}

CallingLog($.GetPosition(exchange))You can print out the information you have stored.img img

In this way, the basic operations can be implemented, and the rest can be studied in options trading strategies.


Related

More