Simulated Exchange Communication Design Proposal
Source: Notion | Last edited: 2022-10-26 | ID: d6012ca4-ba5...
Between Simulated Exchange (SE) and Execution Service (EXEC)
Proposal bullet points:
Section titled “Proposal bullet points:”- No built-in clock is used for time. The timestamp(ts) of the orderbook data in Simulated Exchange (SE) is used as the single source of truth for the time for both Simulated Exchange (SE) and Execution (EXEC). When a simulation starts, SE will replay the orderbook data between two timestamps (ts1 and ts2) with a 100ms step-size, which moves the time forward.
- Functions in SE that take more than 0.1ms to run, such as network calls or loops >10^5 iterations, are considered blocking. All Websocket(ws) data, including orderbook, should be paused (pause looping) to wait for them. Otherwise, it may block normal time-sensitive commands, such as order-fill.
- These blocking function calls can be tracked by a data structure such as a set or a map in SE. Only when it is empty will ws start sending data again, which will move time forward.
- [Optional] We might not need this at the moment, but it’s a good-to-have in case we want to test other strategies: EXEC should be able to add to the data structure in SE as well, for example, through a POST call. In a normal-speed simulation where we wait 100ms between each 100ms step, function calls in EXEC that are time-consuming don’t need any special handling since they mimic the scenario in production. In an accelerated simulation, we wait a much smaller time (for example, the avg network delay between SE and EXEC) between each 100ms step. In this case, function calls in EXEC that are time-consuming would take longer than real-scenario since SE data feed is accelerated. We want EXEC to inform the data structure in SE about it so that ws can be paused.
Scenarios:
Section titled “Scenarios:”

Another Communication Proposal 😁
Section titled “Another Communication Proposal 😁”In contrast of what proposed above where Simulated Exchange is in charge of moving the clock forward, this proposal nominate Execution in charge of moving the clock forward by send a increase clock request to the Simulated Exchange.
sequenceDiagramExecution->>Simulator: subscribe simulator websocket "clock" stream (no need if it's REST call)Simulator-->>Execution: resp: success
Execution->>Simulator: subscribe binance websocket steam (order, kline, etc) in ccxt waySimulator-->>Execution: resp: success
loop tick1, time=111111 Execution->>Simulator: increase clock: REST CALL: time 111111 ? (maybe weboscket?) Simulator-->>Execution: binance event { "e":"ORDER_TRADE_UPDATE, E:000134 ... } Simulator-->>Execution: another binance event { "e":"ORDER_TRADE_UPDATE, E:012456 ... } Simulator-->>Execution: clock increase success: { e: "CLOCK_UPDATE_RESP", E: 111111 } Execution->>Simulator: fetch orderbook Simulator-->>Execution: resp: orderbook data Execution->>Simulator: create order Simulator-->>Execution: resp: new order data Execution->>Simulator: fetch positionRisk Simulator-->>Execution: resp: position dataend
loop tick2, time=222222 Execution->>Simulator: increase clock: REST CALL: time 222222 Simulator-->>Execution: clock increase success: { e: "CLOCK_UPDATE_RESP", E: 222222 }end
loop tick3, time=333333 Execution->>Simulator: increase clock: REST CALL: time 333333 Simulator-->>Execution: clock increase success: { e: "CLOCK_UPDATE_RESP", E: 333333 } Execution->>Simulator: fetch account Simulator-->>Execution: resp: account dataend