REQUIRED
A Thetadata Stock Standard Subscription is required to use this endpoint.
Quote Stream
Behavior
This stream returns every BBO quote reported on the Nasdaq Basic feed for the specified symbol. The Theta Terminal will continue to receive these messages unless it is terminated or you unsubscribe from the Quote stream.
Subscribe to Quote 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 quotes 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 quotes for AAPL stock.
Payload
{
"msg_type": "STREAM",
"sec_type": "STOCK",
"req_type": "QUOTE",
"add": true,
"id": 0,
"contract": {
"root": "AAPL"
}
}
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_quotes():
async with websockets.connect('ws://127.0.0.1:25520/v1/events') as websocket:
req = {}
req['msg_type'] = 'STREAM'
req['sec_type'] = 'STOCK'
req['req_type'] = 'QUOTE'
req['add'] = True
req['id'] = 0
req['contract'] = {}
req['contract']['root'] = "AAPL"
await websocket.send(req.__str__())
while True:
response = await websocket.recv()
print(response)
asyncio.get_event_loop().run_until_complete(stream_quotes())
Unsubscribe from the Quote Stream
Changing the add
field in the payload from true
to false
will end the stream subscription.
{
"msg_type": "STREAM",
"sec_type": "STOCK",
"req_type": "QUOTE",
"add": false,
"id": 1,
"contract": {
"root": "AAPL"
}
}
Sample output
The condition and exchange values correspond to their respective Enums.
The strike price is in 1/10th of a cent. This means that a $140 strike price is represented as
140000
.
{
"header": {
"type": "QUOTE",
"status": "CONNECTED"
},
"contract": {
"security_type": "STOCK",
"root": "AAPL"
},
"quote": {
"ms_of_day": 38437457,
"bid_size": 235,
"bid_exchange": 29,
"bid": 184.49,
"bid_condition": 0,
"ask_size": 100,
"ask_exchange": 29,
"ask": 184.5,
"ask_condition": 0,
"date": 20240503
}
}