1
1
from polygon import WebSocketClient
2
- from polygon .websocket .models import WebSocketMessage
2
+ from polygon .websocket .models import WebSocketMessage , EquityTrade
3
3
from typing import List
4
4
from typing import Dict
5
+ from datetime import datetime
5
6
import time
6
7
import threading
7
8
8
-
9
9
# docs
10
10
# https://polygon.io/docs/stocks/ws_stocks_am
11
11
# https://polygon-api-client.readthedocs.io/en/latest/WebSocket.html#
16
16
# program then prints the map, which gives a readout of the top stocks
17
17
# traded in the past 5 seconds.
18
18
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
-
31
19
32
20
def run_websocket_client ():
33
21
# client = WebSocketClient("XXXXXX") # hardcoded api_key is used
@@ -38,32 +26,73 @@ def run_websocket_client():
38
26
39
27
string_map : Dict [str , int ]
40
28
string_map = {} #
29
+ cash_traded = float (0 )
41
30
42
31
43
32
def handle_msg (msgs : List [WebSocketMessage ]):
44
33
for m in msgs :
45
34
# print(m)
46
35
47
- # verify this is a string
48
- if isinstance (m , str ):
36
+ if type (m ) == EquityTrade :
49
37
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 ):
54
40
41
+ if m .symbol in string_map :
42
+ string_map [m .symbol ] += 1
43
+ else :
44
+ string_map [m .symbol ] = 1
55
45
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)
58
52
59
53
60
54
def top_function ():
55
+
56
+ # start timer
57
+ start_time = time .time ()
58
+
61
59
sorted_string_map = sorted (string_map .items (), key = lambda x : x [1 ], reverse = True )
62
60
print ("\033 c" , end = "" ) # ANSI escape sequence to clear the screen
63
61
64
- for index , item in sorted_string_map [:10 ]:
62
+ for index , item in sorted_string_map [:25 ]:
65
63
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
67
96
68
97
69
98
def run_function_periodically ():
0 commit comments