-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathexample_app_options.py
More file actions
152 lines (123 loc) · 5.97 KB
/
example_app_options.py
File metadata and controls
152 lines (123 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import threading
import signal
import time
import sys
import logging
from threading import Event, Lock
from intriniorealtime.options_client import IntrinioRealtimeOptionsClient
from intriniorealtime.options_client import OptionsQuote
from intriniorealtime.options_client import OptionsTrade
from intriniorealtime.options_client import OptionsRefresh
from intriniorealtime.options_client import OptionsUnusualActivity
from intriniorealtime.options_client import OptionsUnusualActivityType
from intriniorealtime.options_client import OptionsUnusualActivitySentiment
from intriniorealtime.options_client import log
from intriniorealtime.options_client import Config
from intriniorealtime.options_client import Providers
from intriniorealtime.options_client import LogLevel
options_trade_count = 0
options_trade_count_lock = Lock()
options_quote_count = 0
options_quote_count_lock = Lock()
options_refresh_count = 0
options_refresh_count_lock = Lock()
options_ua_block_count = 0
options_ua_block_count_lock = Lock()
options_ua_sweep_count = 0
options_ua_sweep_count_lock = Lock()
options_ua_large_trade_count = 0
options_ua_large_trade_count_lock = Lock()
options_ua_unusual_sweep_count = 0
options_ua_unusual_sweep_count_lock = Lock()
def on_quote(quote: OptionsQuote):
global options_quote_count
global options_quote_count_lock
with options_quote_count_lock:
options_quote_count += 1
def on_trade(trade: OptionsTrade):
global options_trade_count
global options_trade_count_lock
with options_trade_count_lock:
options_trade_count += 1
def on_refresh(refresh: OptionsRefresh):
global options_refresh_count
global options_refresh_count_lock
with options_refresh_count_lock:
options_refresh_count += 1
def on_unusual_activity(ua: OptionsUnusualActivity):
global options_ua_block_count
global options_ua_block_count_lock
global options_ua_sweep_count
global options_ua_sweep_count_lock
global options_ua_large_trade_count
global options_ua_large_trade_count_lock
global options_ua_unusual_sweep_count
global options_ua_unusual_sweep_count_lock
if ua.activity_type == OptionsUnusualActivityType.BLOCK:
with options_ua_block_count_lock:
options_ua_block_count += 1
elif ua.activity_type == OptionsUnusualActivityType.SWEEP:
with options_ua_sweep_count_lock:
options_ua_sweep_count += 1
elif ua.activity_type == OptionsUnusualActivityType.LARGE:
with options_ua_large_trade_count_lock:
options_ua_large_trade_count += 1
elif ua.activity_type == OptionsUnusualActivityType.UNUSUAL_SWEEP:
with options_ua_unusual_sweep_count_lock:
options_ua_unusual_sweep_count += 1
else:
log("on_unusual_activity - Unknown activity_type {0}", ua.activity_type)
class Summarize(threading.Thread):
def __init__(self, stop_flag: threading.Event, intrinio_client: IntrinioRealtimeOptionsClient):
threading.Thread.__init__(self, group=None, args=(), kwargs={}, daemon=True)
self.__stop_flag: threading.Event = stop_flag
self.__client = intrinio_client
def run(self):
while not self.__stop_flag.is_set():
time.sleep(30.0)
(dataMsgs, txtMsgs, queueDepth) = self.__client.get_stats()
log("Client Stats - Data Messages: {0}, Text Messages: {1}, Queue Depth: {2}".format(dataMsgs, txtMsgs, queueDepth))
log(
"App Stats - Trades: {0}, Quotes: {1}, Refreshes: {2}, Blocks: {3}, Sweeps: {4}, Large Trades: {5}, Unusual Sweeps: {6}"
.format(
options_trade_count,
options_quote_count,
options_refresh_count,
options_ua_block_count,
options_ua_sweep_count,
options_ua_large_trade_count,
options_ua_unusual_sweep_count))
# Your config object MUST include the 'api_key' and 'provider', at a minimum
config: Config = Config(
api_key="API_KEY_HERE",
provider=Providers.OPRA, # or Providers.OPTIONS_EDGE
num_threads=8,
symbols=["AAPL", "BRKB__230217C00300000"], # this is a static list of symbols (options contracts or option chains) that will automatically be subscribed to when the client starts
log_level=LogLevel.INFO,
delayed=False) #set delayed parameter to true if you have realtime access but want the data delayed 15 minutes anyway
# Register only the callbacks that you want.
# Take special care when registering the 'on_quote' handler as it will increase throughput by ~10x
intrinioRealtimeOptionsClient: IntrinioRealtimeOptionsClient = IntrinioRealtimeOptionsClient(config, on_trade=on_trade, on_quote=on_quote, on_refresh=on_refresh, on_unusual_activity=on_unusual_activity)
stop_event = Event()
def on_kill_process(sig, frame):
log("Sample Application - Stopping")
stop_event.set()
intrinioRealtimeOptionsClient.stop()
sys.exit(0)
signal.signal(signal.SIGINT, on_kill_process)
summarize_thread = Summarize(stop_event, intrinioRealtimeOptionsClient)
summarize_thread.start()
intrinioRealtimeOptionsClient.start()
#use this to join the channels already declared in your config
intrinioRealtimeOptionsClient.join()
# Use this to subscribe to the entire universe of symbols (option contracts). This requires special permission.
# intrinioRealtimeOptionsClient.join_firehose()
# Use this to subscribe, dynamically, to an option chain (all option contracts for a given underlying contract).
# intrinioRealtimeOptionsClient.join("AAPL")
# Use this to subscribe, dynamically, to a specific option contract.
# intrinioRealtimeOptionsClient.join("AAP___230616P00250000")
# Use this to subscribe, dynamically, a list of specific option contracts or option chains.
# intrinioRealtimeOptionsClient.join("GOOG__220408C02870000", "MSFT__220408C00315000", "AAPL__220414C00180000", "TSLA", "GE")
time.sleep(60 * 60 * 24)
# sigint, or ctrl+c, during the thread wait will also perform the same below code.
on_kill_process(None, None)