---
# System prepended metadata

title: Untitled

---

```javascript=

/* assumptions:
 * - for limit orders that cross to the other side, the event order is:
 * cross order (orderbook)
 * -> trade event (trades)
 * -> cross/top order (orderbook)
 * -> cross/top order (orderbook)
 *

if orderbook event:
    if limit order is cross order:
        book.cross_orders[event.orderId] = event
        return
        
    book.apply(event) // see below

elif trade event:
    if caused by limit orders crossing:
        cross_order = ...
        top_order = ...
        update quantity for both
        publish delta for top_order
        top_order = next top order
        if cross_order.qty != 0 and cross_order.prc doesn't cross top_order:
            publish cross_order new delta

    check bid and ask orders if they are in book.cross_orders:
        publish delta for current top # to update internally, for us to get next top
        check if cross order still stands and won't cross next top:
            then publish cross order
    
    
----

apply(event):
    if order_changed:
        ...
        if event.order_id not in bids/asks and in book.cross_orders:
            book.cross_orders[event.order_id].volume = event.volume
            if (book.cross_orders[event.order_id].price in below other side top order):
                publish_delta(book.cross_orders[event.orderId])
        
        ...
        


```
