Similarities and differences between commodity futures and cryptocurrency exchanges API

Similarities and differences between commodity futures and cryptocurrency exchanges API

·

5 min read

Commodity futures CTP and cryptocurrency API exchange have significant differences. Familiar with cryptocurrency exchange programming trading doesn't mean familiar with commodity futures CTP programming. we can not simply copy the method and experience. This article will summarize the similarities and differences between them.

Historical data

The CTP interface does not provide historical market quote, and the historical market quote needs to be resolved through the market quote agency . If the market data is lost due to failure to log in, the CTP does not provide a market replenishment mechanism. Historical market quotes can only be obtained through third-party agency. Most cryptocurrency exchanges usually provides an interface for obtaining K-line and transaction history.

Different agreement

The cryptocurrency exchange API is generally a REST and websocket protocol. The CTP internally encapsulates the network-related logic and communicates with the CTP background using the FTD protocol based on the TCP protocol. Divided into three modes:

  • Request response mode: the client initiates a request, and the CTP background receives and responds to the request.

  • Broadcast communication mode: After the client subscribes to the contract market quote, the CTP pushes the market quotes through the broadcast.

  • Private communication mode: After the client commissions a contract, the order information, transaction return, etc. are pushed by the CTP point-to-point.

All quotes and order transactions of the CTP agreement will be notified after changes, while the inquiry orders, accounts, and positions are actively queried. The above three modes can find similar forms in the cryptocurrency exchange API.

Different levels of data

The depth of the CTP protocol are only the latest buying and selling price, the deeper market quotes such as five layer of buying and selling price are expensive, you need pay extra money to the futures exchange to obtain it. on the other hand, The cryptocurrency exchange can generally get full depth up to 200 layers of buying and selling prices.

Also, CTP does not push the real transactions quotes, it can only be reversed by position changes, and the cryptocurrency exchange API can obtain real detailed transaction quotes. The market data tick level of the CTP platform is 2 tick per second. Most of the cryptocurrency exchange websocket can do up to 10 tick per second.

Different access restrictions

cryptocurrency exchanges are generally limited to 10 ticks pre second. There are no special requirements for order withdrawals. CTP has strict restrictions on requests that need to be sent out actively. Generally, once per 2 second is rather safe, and there are also requirements for the number of withdrawals.

Stability

The CTP protocol is very stable and there are almost no errors or network problems. Due to the cryptocurrency exchange have less restriction, longer transaction time, system maintenance, data delay and network errors are very common.

FMZ Quant Platform CTP Protocol Best Practices

CTP default mode to get the market interface such as GetTicker, GetDepth, GetRecords are cached data to get the latest, if there is no data will wait until there is data, so the strategy can not use Sleep. When there is a market quote change, ticker, depth, and records will be updated. At this point, any interface will be returned immediately when it be called. Then the called interface state is set to wait for the update mode, When the next time the same interface is called, it will wait for new data to return.

Some unpopular trading contracts or the price reaches the daily limited price will occur for a long time without any trading activities, which is normal for the strategy to be stuck for a long time.

If you want to get the data every time you get the market quote, even the old data, you can switch to the market immediately update mode exchange.IO ("mode", 0). At this point, the strategy cannot be written as an event driver. You need to add a SLeep event to avoid a fast infinite loop. Some low-frequency strategies can use this mode, and the strategy design is simple. Use exchange.IO("mode", 1) to switch back to the default cache mode.

When operating a single contract, use the default mode will be fine. However, if there are multiple trading contracts, it is possible that one contract has not been updated, resulting in a blockage of the market interface, and other trading contract market quotes updates are not available. To solve this problem, you can use the immediate update mode, but it is not convenient to write a high frequency strategy. At this point, you can use the event push mode to get the push of orders and quotes. The setting method is exchange.IO("wait"). If multiple exchange objects are added, this is rare situation in commodity futures trading, you can use exchange.IO ("wait_any"), and the returned index will indicate the index of the returned exchange.

Market quote tick change push:

{Event:"tick", Index: exchange index (in the order of robot exchange added), Nano: event nanosecond time, Symbol: contract name}

Order Push:

{Event:"order", Index: Exchange Index, Nano: Event Nanosecond Time, Order: Order Information (consistent with GetOrder)}

At this point the strategy structure can be written as:

function on_tick(symbol){
     Log("symbol update")
     exchange.SetContractType(symbol)
     Log(exchange.GetTicker())
}

function on_order(order){
     Log("order update", order)
}

function main(){
     while(true){
         if(exchange.IO("status")){ //Determine the link status
             exchange.IO("mode", 0)
             _C (exchange.SetContractType, "MA888") // subscribe to the MA, only the first time is the real request to send a subscription, the following are just program switch, won't consume any time.
             _C(exchange.SetContractType, "rb888") // Subscribe rb
             While(True){
                 Var e = exchange.IO("wait")
                 If(e){
                     If(e.event == "tick"){
                         On_tick(e.Symbol)
                     }else if(e.event == "order"){
                         On_order(e.Order)
                     }
                 }
            }
         }else{
             Sleep(10*1000)
         }
     }
}