Skip to content

Instantly share code, notes, and snippets.

@Fyzu
Last active March 25, 2024 13:38
Show Gist options
  • Save Fyzu/8739efaa76dd034f1bea264fc365e5a3 to your computer and use it in GitHub Desktop.
Save Fyzu/8739efaa76dd034f1bea264fc365e5a3 to your computer and use it in GitHub Desktop.
Candle Snapshot Reconstruction

Our system (QD) incorporates a mechanism designed for transferring indexed states for events such as: Candle, Order.

The fundamental operation of this mechanism involves transferring a snapshot initially and subsequently applying incremental updates to this snapshot.

Candle event is an indexed event in our system that contains specific fields such as:

  • flags - flags for processing updates
  • index - event index, which represents order of the event

Note: Events arrive in a special order in which order these events need to be handled so as not to break the sequence of state.

Here is a description of the algorithm how to work with our indexed events:

  1. Set up a pending queue for indexed market events.

  2. Create variables to control state:

    • Transaction: Indicates transaction processing.
    • SnapshotFull: Indicates full snapshot acquisition in the queue.
    • SnapshotPart: Indicates the process of getting a snapshot in the queue.
  3. Set up a listener for incoming events.

  4. Subscribe to events for the required instrument.

  5. For each indexed event received, in order:

    5.1. If the eventFlags field value has SNAPSHOT_BEGIN, then: - Clear the pending queue. - Set SnapshotPart = true and SnapshotFull = false.

    5.2. If the eventFlags field value has SNAPSHOT_END and SnapshotPart is true, then: - Set SnapshotPart = false and SnapshotFull = true.

    5.3. If the eventFlags field value has TX_PENDING, then: - Set Transaction = true. Otherwise, set Transaction = false.

    5.4. Add the event to the pending queue.

  6. If SnapshotPart is true or Transaction is true, proceed to step 10.

  7. If SnapshotFull is true, then:

    • Remove state (previous events).
    • Set SnapshotFull = false.
  8. For each event in the pending queue:

    8.1. If the event has the REMOVE_EVENT flag, then: - Remove the event with this index from the state.

    8.2. Otherwise, add or update the event with this index to the state.

  9. Clear the pending queue.

  10. Wait for the next events, then go back to step 5.

Our system (QD) incorporates a mechanism designed for transferring indexed states for events such as: Candle, Order.

The fundamental operation of this mechanism involves transferring a snapshot initially and subsequently applying incremental updates to this snapshot.

Candle event is an indexed event in our system that contains specific fields such as:

  • flags - flags for processing updates
  • index - event index, which represents order of the event

Note: Events arrive in a special order in which order these events need to be handled so as not to break the sequence of state.

Here is a description of the algorithm how to work with our indexed events:

  1. Set up a pending queue for indexed market events

  2. Create Transaction, SnapshotFull, SnapshotPart vars to control state, where:

  • Transaction is variable indicating transaction processing

  • SnapshotFull is variable indicating full snapshot acquisition in the queue

  • SnapshotPart is variable indicating the process of getting a snapshot in the queue

  1. Set up a listener for incoming events

  2. Subscribe to events for required instrument

  3. Apply the following steps for each indexed event in order of receiving:

5.1. If eventFlags field value has SNAPSHOT_BEGIN, then clear the pending queue, set SnapshotPart = true, SnapshotFull = false

5.2. If eventFlags field value has SNAPSHOT_END and SnapshotPart is true, then set SnapshotPart = false and SnapshotFull = true

5.3. If eventFlags field value has TX_PENDING, then set Transaction = true, otherwise set set Transaction = false

5.4. Add the event to the pending queue

  1. If SnapshotPart is true or Transaction is true, go to the step 11

  2. If SnapshotFull is true, then remove state (previous events) and set SnapshotFull = false

  3. Go through the pending queue and apply each event to state:

    8.1. If this event has REMOVE_EVENT flag, then remove the event with this index from the state

    8.2. Else add or update event with this index to the state

  4. Clear pending queue

  5. Wait for the next events, then go to step 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment