-
Notifications
You must be signed in to change notification settings - Fork 12
/
stock_polygon.py
62 lines (47 loc) · 1.75 KB
/
stock_polygon.py
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
# Be sure to pip install polygon-api-client
import time
import json
import config
from kafka import KafkaProducer
from websocket_client import WebSocketClient, STOCKS_CLUSTER
"""
----------------------------------
instantiate Kafka producer
==================================
"""
producer = KafkaProducer(
bootstrap_servers=['localhost:29092'],
value_serializer=lambda x: json.dumps(x).encode('utf-8'),
)
""" This method below processes the one-minute aggregate bars from
the polygon.io websocket, and then sends them to the stock_min_bars
topic producer."""
def my_custom_process_message(message):
TICK_INSTANCE = json.loads(message)[0]['ev'] == 'AM'
try:
if TICK_INSTANCE:
message_str = (json.loads(message)[0])
# performs basic price normalization of candlesticks
message_str['price'] = message_str['c']
message_str['c'] = (message_str['c'] - message_str['o']) / message_str['o']
message_str['l'] = (message_str['l'] - message_str['o']) / message_str['o']
message_str['h'] = (message_str['h'] - message_str['o']) / message_str['o']
print(message_str)
producer.send('stock_min_bars',value=message_str)
else:
pass
except Exception as e:
logging.error("{}".format(e.args))
def my_custom_error_handler(ws, error):
print("this is my custom error handler", error)
def my_custom_close_handler(ws):
print("this is my custom close handler")
def main():
key = config.POLYGON_API
my_client = WebSocketClient(STOCKS_CLUSTER, key, my_custom_process_message)
my_client.run_async()
my_client.subscribe("AM.RIOT, AM.NET")
time.sleep(1)
#my_client.close_connection()
if __name__ == "__main__":
main()