🥭 Mango Markets support is available at: Docs | Discord | Twitter | Github | Email
Run this code on Binder.
🏃♀️ To run this notebook press the ⏩ icon in the toolbar above.
This notebook shows how to stream the OrderBook
for a perp market via a websocket, showing new orders as they appear.
In the code below, the second parameter to market.on_orderbook_change()
is a lambda that is called when a new OrderBook
is available. (All the demonstration code below does is print the current OrderBook
out with a timestamp. It's here you probably want to insert your own functionality.)
Behind the scenes what the code does:
OrderBook
to start with.OrderBook
OrderBook
when it is updatedStreaming changes to an OrderBook
is complicated by the fact that an OrderBook
is based on two Solana accounts, not one. Bids and asks are held in separate accounts, and we want to update the OrderBook
when either of them is changed.
This code will exit after 60 seconds.
import datetime
import mango
import time
with mango.ContextBuilder.build(cluster_name="devnet") as context:
market = mango.market(context, "BTC-PERP")
subscription = market.on_orderbook_change(context, lambda ob: print("\n", datetime.datetime.now(), "\n", ob))
time.sleep(60)
subscription.dispose()
print("Example complete.")