REQUIRED
A Thetadata Index Standard Subscription is required to use this endpoint.
Trade Stream
Behavior
This stream returns every trade for a specified symbol reported on the CBOE CGIF feed. The Theta Terminal will continue to receive these messages unless it is terminated or you unsubscribe from the full trade stream.
NOTE
The trade object only the price field is updated and only reported if price has changed since last report. Indices are typically reported once every 1-second or few seconds. There is normally no 9:30:00 index price report, only 9:30:01.
Subscribe to Trade Stream
The id
field should be increased for each new stream request made. This ID is returned in a later message to verify that the request to stream trades was successful. This ID does not have any representation of contracts or unqiue streams. It only represents a way of tracking streaming requests made. Failure to increment the ID for each request will prevent the terminal from automatically resubscribing to streams you previously requested.
Contract Parameter
The contract in the payload example below is subscribing to all trades for SPX stock.
Payload
{
"msg_type": "STREAM",
"sec_type": "INDEX",
"req_type": "TRADE",
"add": true,
"id": 0,
"contract": {
"root": "SPX"
}
}
Sample Code
REQUIRED
The Theta Terminal must be running for this code to work.
import asyncio
import websockets
# This code has only been tested on Python 3.11. Other versions might require adjustments.
async def stream_trades():
async with websockets.connect('ws://127.0.0.1:25520/v1/events') as websocket:
req = {}
req['msg_type'] = 'STREAM'
req['sec_type'] = 'INDEX'
req['req_type'] = 'TRADE'
req['add'] = True
req['id'] = 0
req['contract'] = {}
req['contract']['root'] = "SPX"
await websocket.send(req.__str__())
while True:
response = await websocket.recv()
print(response)
asyncio.get_event_loop().run_until_complete(stream_trades())
Unsubscribe from the Trade Stream
Changing the add
field in the payload from true
to false
will end the trade stream subscription.
{
"msg_type": "STREAM",
"sec_type": "INDEX",
"req_type": "TRADE",
"add": false,
"id": 1,
"contract": {
"root": "SPX"
}
}
Sample output
{
"header": {
"type": "TRADE",
"status": "CONNECTED"
},
"contract": {
"security_type": "INDEX",
"root": "SPX"
},
"trade": {
"ms_of_day": 51952000,
"sequence": 0,
"size": 0,
"condition": 0,
"price": 5333.39,
"exchange": 5,
"date": 20240809
}
}
{
"header": {
"type": "OHLC",
"status": "CONNECTED"
},
"contract": {
"security_type": "INDEX",
"root": "SPX"
},
"ohlc": {
"ms_of_day": 51952000,
"open": 5314.66,
"high": 5358.67,
"low": 5300.84,
"close": 5333.39,
"volume": 0,
"count": 1135013,
"date": 20240809
}
}