Listen for events, it returns when there is any WebSocket
readable data or concurrent tasks, such as exchange.Go()
, HttpQuery_Go()
, etc. are completed.
If the returned object is not null value, the Event
contained in the return content is the event trigger type. For example, the following return value structure:
{"Seq":1,"Event":"Exchange_GetTrades","ThreadId":0,"Index":3,"Nano":1682068771309583400}
object
EventLoop() EventLoop(timeout)
The parameter timeout
is the timeout setting, in milliseconds. The parameter timeout
waits for an event to occur before returning if it is set to 0. If it is greater than 0, it sets the event to wait for a timeout, and returns the most recent event immediately if it is less than 0.
timeout
false
number
function main() {
var routine_getTicker = exchange.Go("GetTicker")
var routine_getDepth = exchange.Go("GetDepth")
var routine_getTrades = exchange.Go("GetTrades")
// Sleep(2000), if the Sleep statement is used here, it will cause the subsequent EventLoop function to miss the previous events, because after waiting for 2 seconds, the concurrent function has received the data, and the subsequent EventLoop listening mechanism started, it misses these events.
// These events will not be missed unless EventLoop(-1) is called at the beginning of the first line of code to first initialize the EventLoop's listening mechanism.
// Log("GetDepth:", routine_getDepth.wait()) If the wait function is called in advance to retrieve the result of a concurrent call to the GetDepth function, the event that the GetDepth function receives the result of the request will not be returned in the EventLoop function.
var ts1 = new Date().getTime()
var ret1 = EventLoop(0)
var ts2 = new Date().getTime()
var ret2 = EventLoop(0)
var ts3 = new Date().getTime()
var ret3 = EventLoop(0)
Log("The first concurrent task completed was:", _D(ts1), ret1)
Log("The second concurrent task completed was:", _D(ts2), ret2)
Log("The third concurrent task completed was:", _D(ts3), ret3)
Log("GetTicker:", routine_getTicker.wait())
Log("GetDepth:", routine_getDepth.wait())
Log("GetTrades:", routine_getTrades.wait())
}
import time
def main():
routine_getTicker = exchange.Go("GetTicker")
routine_getDepth = exchange.Go("GetDepth")
routine_getTrades = exchange.Go("GetTrades")
ts1 = time.time()
ret1 = EventLoop(0)
ts2 = time.time()
ret2 = EventLoop(0)
ts3 = time.time()
ret3 = EventLoop(0)
Log("The first concurrent task completed was:", _D(ts1), ret1)
Log("The second concurrent task completed was:", _D(ts2), ret2)
Log("The third concurrent task completed was:", _D(ts3), ret3)
Log("GetTicker:", routine_getTicker.wait())
Log("GetDepth:", routine_getDepth.wait())
Log("GetTrades:", routine_getTrades.wait())
void main() {
auto routine_getTicker = exchange.Go("GetTicker");
auto routine_getDepth = exchange.Go("GetDepth");
auto routine_getTrades = exchange.Go("GetTrades");
auto ts1 = Unix() * 1000;
auto ret1 = EventLoop(0);
auto ts2 = Unix() * 1000;
auto ret2 = EventLoop(0);
auto ts3 = Unix() * 1000;
auto ret3 = EventLoop(0);
Log("The first concurrent task completed was:", _D(ts1), ret1);
Log("The second concurrent task completed was:", _D(ts2), ret2);
Log("The third concurrent task completed was:", _D(ts3), ret3);
Ticker ticker;
Depth depth;
Trades trades;
routine_getTicker.wait(ticker);
routine_getDepth.wait(depth);
routine_getTrades.wait(trades);
Log("GetTicker:", ticker);
Log("GetDepth:", depth);
Log("GetTrades:", trades);
}
The first call to the EventLoop()
function in the code initializes the mechanism for that listened event, and if the first EventLoop()
call starts after the event callback, it will miss the previous events. The underlying system wraps a queue structure that caches a maximum of 500 event callbacks. If the EventLoop()
function is not called in time to take them out during program execution, later event callbacks outside the 500 cache will be lost. Calls to the EventLoop()
function do not affect the cache queue of the underlying system WebSocket or the caches of concurrent functions such as exchange.Go()
. For these caches, it is still necessary to use the respective methods to retrieve the data. No return event will be generated in the EventLoop()
function for data that has been retrieved before the EventLoop()
function returns. The main purpose of the EventLoop()
function is to notify the strategy layer that new network data has been received by the underlying system. The whole strategy is driven by events. When the EventLoop()
function returns an event, just traverses all data sources. For example, WebSocket connections, objects created by exchange.Go()
try to get data. The EventLoop()
function supports live trading only.
Listen for events in the main thread when called from the main function main()
. In strategies written in the JavaScript
language, the threading.Thread()
function creates a thread, which can also be called in the thread’s execution function, to listen for events in the current thread.
{@fun/Global/Dial Dial}, {@fun/Trade/exchange.Go exchange.Go}, {@fun/Global/HttpQuery_Go HttpQuery_Go}
UUID __Serve