2.8 Switching to a market data update mode (explanation)

Author: The Little Dream, Created: 2016-07-13 10:08:50, Updated: 2017-10-11 10:21:29

Switch to market data update mode

  • The following is an introduction in the API documentation:
// 只支持Websocket模式(huobi, okcoin.cn, BTCC支持)与商品期货CTP协议
exchange.IO("websocket"); // 切换行情通信协议到websocket(默认为rest), Ticker, Depth会切换为websocket协议来更新, 商品期货CTP无需切换
// 切换GetTicker, GetDepth数据更新模式
exchange.IO("mode", 0); // 立即返回模式, 如果当前还没有接收到交易所最新的行情数据推送, 就立即返回旧的行情数据, 如果有新的数据就返回新的数据
exchange.IO("mode", 1); // 缓存模式(默认模式), 如果当前还没有收到交易所最新的行情数据(同上一次api获取的数据比较), 就等待接收然后再返回, 
                        //如果调用该函数之前收到了最新的行情数据, 就立即返回最新的数据
exchange.IO("mode", 2); // 强制更新模式, 进入等待一直到接收到交易所下一次的最新推送数据后返回
// 如果想第一时间获取最新的行情可以切换到websocket后不Sleep的立即检测数据, GetTicker, GetDepth用缓存模式进行工作
exchange.IO("websocket");
while (true) {
    Log(exchange.GetTicker());
}
  • To make it easier for users to understand, here are some details:

    • 1, the default of the platform system is rest mode: once an API data request is made in rest mode, the exchange server returns one data ((currently up to date), and the rest of the data is returned to the server. The exchange server does not push the latest data to the user. Therefore, the policy requires frequent requests for data in the rest mode to ensure timely access to the latest data (not necessarily the first time new data is generated, because a question is answered).

    • 2. API supporting websocket mode has GetTicker, GetDepth.exchanges[0].IO("websocket");After the function, open the websocket mode. A careful student might see why exchanges[0] is written like this.imgFor example, if two exchanges (or more) are added to a policy, one of the global objects defined by the platform is an array that represents all the exchanges added to the policy, the array is exchanges. The array of exchange objects, the main exchange object is the first of the array: exchanges[0] token exchange object. The second exchange object is exchanges.

    • 3. websocket mode differs from rest: in websocket mode, the administrator sends a subscription request to the exchange server, and after the exchange server receives it, it is automatically pushed to the administrator whenever there is updated data. The administrator caches this data, which is quickly obtained when the strategic robot requests the data, and the data is updated.

    • 4. Once the websocket mode is enabled, there are three working modes that can be set:

      • <1> exchange.IO("mode",0);//Instant return mode: This mode immediately returns the current data ((currently updated)).

      • <2> exchange.IO("mode", 1);//caching mode ((default mode): This mode has 2 processing branches, each of which has a different processor. First, when the API requests data, the requested API waits for the data to be received and returns it if the current cached data is not up-to-date compared to the data obtained by the previous API. Second, when the API requests data, it immediately returns the most recent data if the current cached data is up to date compared to the previous API retrieved data (i.e. the latest transactional data was received before the API request data was called).

      • <3> exchange.IO("mode", 2);// Forced update mode: This mode may be confused with a logical branch of the caching mode at first glance, but it's understandable. The caching mode is used to determine if the cached data is up-to-date. The forced update mode is used to force customers to wait until the next time the latest market data is pushed.


More

hokshelatoYou know what? function main (() { exchange.IO (("websocket"); while (true) { Log ((exchange.GetTicker))); I'm not sure. I'm not sure. What's up? ReferenceError: setLastError is not defined, presumably because the retrieval system does not support asynchronous push data. 2. Analog disk error report, `Futures_OP 4: period not support `, then only one ticker data is received, and the robot cannot stop normally, constantly prompting the ticker to stop in the middle, requiring manual kill.

FangBeiHow many lines of data are cached? How long will it take to be cached before it is cleared?

hokshelatoUnderstood, thank you!

The Little Dream1, no rest in the retest, part of the websocket. 2, the analogue disk also does not have a websocket mode. The documentation says that only OKEX token pro supports the websocket protocol.

The Little DreamI'm going to check out the specifics.