Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Commit ccd5ec4

Browse files
committed
Streaming docs & improvements.
1 parent 26ba538 commit ccd5ec4

File tree

5 files changed

+133
-1
lines changed

5 files changed

+133
-1
lines changed

docs/options/trade_streaming.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from datetime import date
2+
from thetadata import ThetaClient, OptionRight, StreamMsg, StreamMsgType
3+
4+
5+
def streaming():
6+
# Credentials now required because get_last is only available to ThetaData Standard & Pro subscribers.
7+
client = ThetaClient(username="MyThetaDataEmail", passwd="MyThetaDataPassword")
8+
9+
with client.connect_stream(callback):
10+
client.req_trade_stream_opt("NVDA", date(2022, 12, 23), 150, OptionRight.CALL)
11+
client.req_trade_stream_opt("NVDA", date(2022, 12, 23), 150, OptionRight.PUT)
12+
client.req_trade_stream_opt("NVDA", date(2022, 12, 23), 145, OptionRight.CALL)
13+
client.req_trade_stream_opt("NVDA", date(2022, 12, 23), 145, OptionRight.PUT)
14+
15+
16+
# User generated method that gets called each time a message from the stream arrives.
17+
def callback(msg: StreamMsg):
18+
msg.type = msg.type
19+
20+
if msg.type == StreamMsgType.TRADE:
21+
print('con:' + msg.contract.to_string())
22+
print('trade: ' + msg.trade.to_string())
23+
24+
25+
if __name__ == "__main__":
26+
streaming()

docs/options/trade_streaming_full.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from datetime import date
2+
from thetadata import ThetaClient, OptionRight, StreamMsg, StreamMsgType
3+
4+
5+
def streaming():
6+
# Credentials now required because get_last is only available to ThetaData Standard & Pro subscribers.
7+
client = ThetaClient(username="MyThetaDataEmail", passwd="MyThetaDataPassword")
8+
9+
with client.connect_stream(callback):
10+
# requests every option trade
11+
client.req_full_trade_stream_opt()
12+
13+
14+
# User generated method that gets called each time a message from the stream arrives.
15+
def callback(msg: StreamMsg):
16+
msg.type = msg.type
17+
18+
if msg.type == StreamMsgType.TRADE:
19+
print('con:' + msg.contract.to_string())
20+
print('trade: ' + msg.trade.to_string())
21+
22+
23+
if __name__ == "__main__":
24+
streaming()

docs/tutorials.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,80 @@ Let's try a low-latency snapshot request for the most recent option quote availa
236236
0 57599990 1 1 6.05 50 63 46 6.55 50 2022-12-16
237237
```
238238

239+
## Streaming
240+
241+
### Preface
242+
243+
#### Definition
244+
245+
Streaming is defined as receiving continuous updates for trades or quotes throughout
246+
the trading day. It is much more performant when compared to snapshots.
247+
248+
#### Responses
249+
250+
You must implement a callback method as shown in the first example below. Each time a
251+
``StreamMsg`` is received, this method will get called. A ``StreamMsg`` can either be
252+
a trade or quote.
253+
254+
#### Limitations
255+
256+
The Pro tier has the ability to request a trade stream for every option contract in
257+
existence and is able to have up to 15K quote streams. The Standard tier is only allowed
258+
5K combined quote / trade streams. Other tiers will not have access to streaming at this time.
259+
260+
### Option Trades
261+
262+
Below requests to receive continuous updates for trades for 4 NVDA option contracts. Notice
263+
that these options expire today, so you may need to change the expiration dates to a date
264+
further in the future.
265+
266+
=== "trade_streaming.py"
267+
268+
```python
269+
--8<-- "docs/options/trade_streaming.py"
270+
```
271+
272+
```bash
273+
> python trade_streaming.py
274+
con:root: NVDA isOption: True exp: 2022-12-23 strike: 150.0 isCall: False
275+
trade: ms_of_day: 35319636 sequence: 1297770003 size: 1 condition: 18 price: 2.29 exchange: ARCX date: 2022-12-23
276+
con:root: NVDA isOption: True exp: 2022-12-23 strike: 145.0 isCall: False
277+
trade: ms_of_day: 35319722 sequence: 1297772550 size: 1 condition: 18 price: 0.59 exchange: XBOS date: 2022-12-23
278+
con:root: NVDA isOption: True exp: 2022-12-23 strike: 145.0 isCall: False
279+
trade: ms_of_day: 35320534 sequence: 1297793907 size: 1 condition: 18 price: 0.59 exchange: XISX date: 2022-12-23
280+
con:root: NVDA isOption: True exp: 2022-12-23 strike: 145.0 isCall: False
281+
trade: ms_of_day: 35321269 sequence: 1297821400 size: 1 condition: 18 price: 0.6 exchange: ARCX date: 2022-12-23
282+
con:root: NVDA isOption: True exp: 2022-12-23 strike: 145.0 isCall: False
283+
trade: ms_of_day: 35321269 sequence: 1297821467 size: 1 condition: 18 price: 0.6 exchange: EDGX date: 2022-12-23
284+
con:root: NVDA isOption: True exp: 2022-12-23 strike: 145.0 isCall: False
285+
trade: ms_of_day: 35321269 sequence: 1297821468 size: 1 condition: 18 price: 0.6 exchange: BATS date: 2022-12-23
286+
```
287+
288+
### Every Option Trade
289+
290+
Below requests to receive continuous updates for every option trade. This method is only
291+
available to PRO subscribers.
292+
293+
=== "trade_streaming_full.py"
294+
295+
```python
296+
--8<-- "docs/options/trade_streaming_full.py"
297+
```
298+
299+
```bash
300+
> python trade_streaming_full.py
301+
con:root: SPXW isOption: True exp: 2023-01-06 strike: 3780.0 isCall: False
302+
trade: ms_of_day: 40566197 sequence: 81076302 size: 4 condition: 4294967170 price: 35.15 exchange: XCBO date: 2022-12-23
303+
con:root: CHWY isOption: True exp: 2023-01-20 strike: 42.5 isCall: False
304+
trade: ms_of_day: 57375126 sequence: 3619080181 size: 1 condition: 4294967178 price: 4.55 exchange: XMIO date: 2022-12-23
305+
con:root: TSLA isOption: True exp: 2022-12-23 strike: 132.0 isCall: True
306+
trade: ms_of_day: 40566210 sequence: 796495932 size: 6 condition: 18 price: 0.15 exchange: XCBO date: 2022-12-23
307+
con:root: TSLA isOption: True exp: 2022-12-23 strike: 132.0 isCall: True
308+
trade: ms_of_day: 40566210 sequence: 796495933 size: 6 condition: 18 price: 0.15 exchange: XCBO date: 2022-12-23
309+
con:root: SPY isOption: True exp: 2022-12-23 strike: 381.0 isCall: True
310+
trade: ms_of_day: 41027015 sequence: 716643310 size: 6 condition: 125 price: 1.07 exchange: EDGX date: 2022-12-23
311+
```
312+
239313
## Historical NBBO Quotes
240314

241315
This tutorial will provide examples for requesting historical & intraday

thetadata/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
from .client import ThetaClient
2+
from .client import StreamMsg
3+
from .client import Trade
4+
from .client import Quote
5+
from .client import Contract
26
from .enums import *
37
from .parsing import *
48
from .exceptions import *

thetadata/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ def from_bytes(self, data: bytearray):
8585
date_raw = str(parse_int(view[28:32]))
8686
self.date = date(year=int(date_raw[0:4]), month=int(date_raw[4:6]), day=int(date_raw[6:8]))
8787

88+
def to_string(self) -> str:
89+
return 'ms_of_day: ' + str(self.ms_of_day) + ' sequence: ' + str(self.sequence) + ' size: ' + str(self.size) + \
90+
' condition: ' + str(self.condition) + ' price: ' + str(self.price) + ' exchange: ' + \
91+
str(self.exchange.value[1]) + ' date: ' + str(self.date)
92+
8893

8994
class Quote:
9095
"""Quote"""
@@ -325,7 +330,6 @@ def _read_stream(self, n_bytes: int) -> bytearray:
325330
continue
326331
total += part.__len__()
327332
buffer.extend(part)
328-
329333
return buffer
330334

331335
def _send_ver(self):

0 commit comments

Comments
 (0)