Continuing my Prediction Markets series, I spent some time recently writing up some basics on managing state in trading bots that work in markets that close.
If you’re used to working in crypto, you’re likely accustomed to the the ‘always open’ casino. In prediction markets like Kalshi, the exchange isn’t 24 hours. Integrating state management can mitigate risks, reduce unnecessary losses, and ultimately make more informed trading decisions.
Getting Exchange Status via API
On Kalshi, there are a few convenient endpoints for querying the exchange state.
GET /exchange/schedule
: Queries the exchange “trading schedule”. In the JSON response, this is referred to as the “standard_hours”. Additionally, it will provide any upcoming “maintenance_windows”.GET /exchange/status
: Queries the current state of the exchange and trading status. Returns too booleans, “trading_active” and “exchange_active”. This endpoint isn’t actually documented in Kalshi’s Exchange Documentation.GET /exchange/announcements
: Queries any exchange announcements; unlikely to be useful programmatically.
And if you have some kind of REST client like this:
It can be quite easy to create a state manager for your trading bot(s).
Understanding Exchange States
The easiest way to manage exchange state is the break down the available status options. We can use a table to determine the possible states:
This easily breaks down our available exchange states, and thus trading options.
ACTIVE_TRADING_ENABLED: All systems go.
ACTIVE_TRADING_DISABLED: The only trading action available when the exchange is active but trading is disabled is to send CANCEL messages for existing orders. This happens overnight and into the morning before open.
INVALID_STATE: In theory, this should never happen. Personally, this initiates a complete shutdown of code.
INACTIVE_TRADING_DISABLED: Exchange is completely shutdown, and no trading is possible. This happens during unexpected downtimes or during maintenance windows.
Managing Exchange State
I wrote a simple exchange status polling class that checks for, and determines the current exchange state on a regular polling interval.
You can use something like the above to programmatically access the possible states. For example:
What to do with your State Manager
It’s important in trading tasks to CYA in as many ways as possible. You’re moving money around, be smart. Here are some ways you can make use of the state manager:
Know when to move your market-making bots into “CANCEL-ONLY” mode (status is ACTIVE_TRADING_DISABLED).
Know when to shutdown trading operations (status is INACTIVE_TRADING_DISABLED).
Know when to go full-boar (status is ACTIVE_TRADING_ENABLED).
References & Links
Kalshi Exchange Documentation — Unfortunately, the exchange docs are out of date. Their discord #dev channel is pretty helpful though.
Pydiction - This is my FOSS project for trading prediction markets programmatically. This is still heavily WIP, but will eventually make for solid rules-based trading platform.
KalshiRestClient — REST client code shared above.
KalshiStatus — Status manager code shared above.