11from polygon import WebSocketClient
2- from polygon .websocket .models import WebSocketMessage
2+ from polygon .websocket .models import WebSocketMessage , EquityTrade
33from typing import List
44from typing import Dict
5+ from datetime import datetime
56import time
67import threading
78
8-
99# docs
1010# https://polygon.io/docs/stocks/ws_stocks_am
1111# https://polygon-api-client.readthedocs.io/en/latest/WebSocket.html#
1616# program then prints the map, which gives a readout of the top stocks
1717# traded in the past 5 seconds.
1818
19- # aggregates
20- # client.subscribe("AM.*") # aggregates (per minute)
21- # client.subscribe("A.*") # aggregates (per second)
22-
23- # trades
24- # client.subscribe("T.*") # all trades
25- # client.subscribe("T.TSLA", "T.UBER") # limited trades
26-
27- # quotes
28- # client.subscribe("Q.*") # all quotes
29- # client.subscribe("Q.TSLA", "Q.UBER") # limited quotes
30-
3119
3220def run_websocket_client ():
3321 # client = WebSocketClient("XXXXXX") # hardcoded api_key is used
@@ -38,32 +26,73 @@ def run_websocket_client():
3826
3927string_map : Dict [str , int ]
4028string_map = {} #
29+ cash_traded = float (0 )
4130
4231
4332def handle_msg (msgs : List [WebSocketMessage ]):
4433 for m in msgs :
4534 # print(m)
4635
47- # verify this is a string
48- if isinstance (m , str ):
36+ if type (m ) == EquityTrade :
4937
50- if m .symbol in string_map :
51- string_map [m .symbol ] += 1
52- else :
53- string_map [m .symbol ] = 1
38+ # verify this is a string
39+ if isinstance (m .symbol , str ):
5440
41+ if m .symbol in string_map :
42+ string_map [m .symbol ] += 1
43+ else :
44+ string_map [m .symbol ] = 1
5545
56- # print messages
57- # client.run(handle_msg)
46+ # verify these are float
47+ if isinstance (m .price , float ) and isinstance (m .size , int ):
48+
49+ global cash_traded
50+ cash_traded += m .price * m .size
51+ # print(cash_traded)
5852
5953
6054def top_function ():
55+
56+ # start timer
57+ start_time = time .time ()
58+
6159 sorted_string_map = sorted (string_map .items (), key = lambda x : x [1 ], reverse = True )
6260 print ("\033 c" , end = "" ) # ANSI escape sequence to clear the screen
6361
64- for index , item in sorted_string_map [:10 ]:
62+ for index , item in sorted_string_map [:25 ]:
6563 print ("{:<15}{:<15}" .format (index , item ))
66- string_map .clear () # clear map for next loop
64+
65+ # end timer
66+ end_time = time .time ()
67+
68+ # print stats
69+ print ()
70+
71+ # current time
72+ current_time = datetime .now ()
73+ print (f"Time: { current_time } " )
74+
75+ # how many tickers seen
76+ ticker_count = len (sorted_string_map )
77+ print (f"Tickers seen: { ticker_count } " )
78+
79+ # how many trades seen
80+ trade_count = 0
81+ for index , item in sorted_string_map :
82+ trade_count += item
83+ print (f"Trades seen: { trade_count } " )
84+
85+ # cash traded
86+ global cash_traded
87+ formatted_number = "{:,.2f}" .format (cash_traded )
88+ print ("Roughly " + formatted_number + " cash changed hands" )
89+
90+ # performance?
91+ print (f"Time taken: { end_time - start_time :.6f} seconds" )
92+
93+ # clear map and cash for next loop
94+ string_map .clear ()
95+ cash_traded = 0
6796
6897
6998def run_function_periodically ():
0 commit comments