The resource loading... loading...

How to break through the Tick receiving limit of commodity futures

Author: ruby, Created: 2018-08-27 15:58:30, Updated:

What is Tick? For example, transaction data can be imagined as a river, and Tick is the data of a section of the river. The finest granularity of domestic futures is twice per second. In other words, domestic futures send up to one Tick at 500 milliseconds.

How do most domestic(means China in this article) software get Tick?

Then there is often more than one transaction in 500 milliseconds, and the specific situation in it is completely a black box. Especially in the high-frequency trading strategy of commodity futures, the receiving speed of the Tick market has a decisive influence on the profitability of the strategy.

Most trading frameworks on the market use a callback mode, which means that there is at most one Tick in 500 milliseconds in ideal situation. Under the real situation onBar/onTick, it is good not to miss Tick. why? Because you have to deal with the whole code logic in onBar/onTick function, which costs a lot of time. Whether you want it or not, your strategy logic must be interrupted, you must use the state idle, like this:

img

More advanced mechanism

FMZ quantitative trading platform does not adopt this backward callback mechanism, but adopts the main function mechanism that does not interrupt the strategy logic, allowing users to control the strategy flow more naturally. Using C++ and Golang as a strategy underlying level, the upper level of strategy uses JavaScript / Python to handle logic problems. Combined with the event trigger mechanism, the strategy can also be used to process the market at the fastest speed in the first time.

Don’t say that the scripting language is slow, unless you use it for neural network training, even if it was, it can be used in any occasion after adding Jit hot compilation. The entry-level strategy is not written here, and talk about the synthesis of futures high-frequency Tick. For example, if we connect to a futures company, we can only receive the market of this futures company. The speed and quality of our receiving are related to our own network, and also related to the load of the futures company’s front-end machine.

So, how can we get more accurate futures Tick data faster? Under the FMZ Quant’s strategy model, you can easily operate the accounts of different futures companies, and combine their prices to process orders at the fastest speed. Under normal circumstances, we can get two Ticks per second from the futures company, but through the technology of combining market, taking the MA801 as an example, we can get a Tick with up to six times per second and no repetition.

img

Code demo

This code can only be used in real market and cannot be backtested. If you don’t use it on FMZ Quant platform, you can reference the principle only. When adding the exchange, many futures companies can be added to carry out the concurrent fusion processing of the market. Here add two exchanges to state:

img

The code is as follows:

img

Demo effect

img

As shown above, at 21:24:44, the data of the first futures company is earlier than the second one. Adding two futures companies can show the effect, if you add more than 5 futures companies to merge together, then you basically have no possibility of missing Tick; if you are developing a high-frequency trading strategy, you’ve solved a very important and decisive step, namely the speed and stability of Tick receiving.

get the complete code:

function main() {
    log("Prepare to connect to the exchange and subscribe to the market")
    //step 1:All futures front-end machines are starting to subscribe to the variety
    _.each(exchanges, function(e){
        /*Waiting to connect to the exchange. The strategy is running without 
          interruption, and it is not the logic of the event callback. */
        while(!e.IO("status"))Sleep(1000);
        /*Use _C function to troubleshoot network errors. If subscribe to the market just 
        after connecting to exchanges, there may be a CTP unprepared error. */
        _C(e.SetContractType, "MA801")
        /*Switch the market receiving mode to the immediate return mode instead of the 
          event trigger mode. Refer to the API documentation on FMZ website. */
        e.IO("mode", 0)
    })
    Log("start fusing the data")
    //step 2: the important part begins
    var preVolume = 0
    while (true) {
        var ts = new Date().getTime()
        //Return if any exchanges occur tick event
        var ret = exchange.IO("wait_any")
        //Reset Volume at the right time
        if (ret.Nano/1000000 - ts > 60000) {
            preVolume = 0
        }
        //Target the exchange where the event occurred
        var e = exchanges[ret.Index]
        //Get ticker,  because of switching mode as return before, so here is the updated market, and GetTicker won't fail
        //Only the Tick with increasing volume is displayed. It no need to compare actual process, just process it.
        var ticker = e.GetTicker()
        if (ticker.Volume >= preVolume){
            Log(ret,ticker.Last, ticker.Volume)
            preVolume = ticker.Volume
        }
    }
}   



More