1616# program then prints the map, which gives a readout of the top stocks
1717# traded in the past 5 seconds.
1818
19+ # Here's what the output looks like after running it for a couple hours:
20+
21+ """
22+ --- Past 5 seconds ---
23+ Tickers seen (5s): 1697
24+ Trades seen (5s): 12335
25+ Cash traded (5s): 88,849,414.33
26+
27+ --- Running Totals ---
28+ Total Tickers seen: 13848
29+ Total Trades seen: 22775838
30+ Total Cash traded: 178,499,702,488.96
31+
32+ ----------------------------------------------------------------------------------------------------
33+
34+ Ticker Trades (5s) Cash (5s) Total Trades Total Cash
35+ NVDA 445 6,933,283.61 632550 18,291,747,596.36
36+ TSLA 279 8,144,556.76 639585 11,319,594,268.07
37+ NVDL 277 3,748,806.85 10451 99,902,192.88
38+ TELL 171 78,424.03 6154 3,710,200.38
39+ AFRM 163 968,984.99 224338 745,895,134.93
40+ AAPL 134 2,359,278.02 304572 2,932,389,741.58
41+ QQQ 132 5,788,859.71 246679 11,003,577,730.48
42+ NVDS 130 598,047.04 7846 48,854,967.44
43+ SOXL 127 786,026.38 189184 719,639,349.26
44+ AMD 116 1,549,180.08 304704 3,713,351,432.39
45+ SPY 113 6,628,554.14 278926 15,435,607,506.98
46+ MSFT 109 1,600,861.75 148047 2,396,824,971.18
47+ SQQQ 88 1,006,330.83 173406 2,065,760,858.90
48+ TQQQ 83 717,574.40 296021 2,580,097,288.27
49+ PLUG 82 106,542.65 31921 53,825,007.27
50+ ITB 75 455,902.33 23369 185,892,273.60
51+ AVGO 71 1,955,826.79 31586 633,629,812.65
52+ STX 71 273,681.77 8420 34,141,139.17
53+ XPEV 68 234,765.41 41284 127,781,104.54
54+ OIH 55 662.12 2964 65,848,514.45
55+ XEL 54 197,642.42 18524 103,054,857.37
56+ XLU 53 850,017.20 35963 291,891,266.17
57+ ARRY 52 164,056.54 11354 23,001,537.49
58+ META 52 1,457,535.82 150793 2,717,344,906.63
59+ PLTR 52 147,743.93 86456 396,851,801.06
60+
61+ Current Time: 2023-08-25 08:27:14.602075 | App Uptime: 04:49:40 | Time taken: 0.003417 seconds
62+ """
63+
64+ app_start_time = time .time ()
65+ string_map : Dict [str , int ] = {}
66+ cash_map_5s : Dict [str , float ] = {}
67+ cash_traded = float (0 )
68+
69+ # totals
70+ total_tickers_seen = 0
71+ total_trades_seen = 0
72+ total_cash_traded = 0.0
73+
74+ # These dictionaries will keep track of the running total of trades and cash per ticker.
75+ total_string_map : Dict [str , int ] = {}
76+ total_cash_map : Dict [str , float ] = {}
77+
1978
2079def run_websocket_client ():
2180 # client = WebSocketClient("XXXXXX") # hardcoded api_key is used
@@ -24,70 +83,96 @@ def run_websocket_client():
2483 client .run (handle_msg )
2584
2685
27- string_map : Dict [str , int ]
28- string_map = {} #
29- cash_traded = float (0 )
30-
31-
3286def handle_msg (msgs : List [WebSocketMessage ]):
87+ global cash_traded
88+ global total_tickers_seen , total_trades_seen , total_cash_traded
3389 for m in msgs :
34- # print(m)
35-
36- if type (m ) == EquityTrade :
37- # verify this is a string
90+ if isinstance (m , EquityTrade ):
91+ # Update total trades and cash for the past 5 seconds
3892 if isinstance (m .symbol , str ):
39- if m .symbol in string_map :
40- string_map [m .symbol ] += 1
41- else :
42- string_map [m .symbol ] = 1
93+ string_map [m .symbol ] = string_map .get (m .symbol , 0 ) + 1
94+ total_string_map [m .symbol ] = total_string_map .get (m .symbol , 0 ) + 1
4395
44- # verify these are float
96+ # Update cash traded
4597 if isinstance (m .price , float ) and isinstance (m .size , int ):
46- global cash_traded
47- cash_traded += m .price * m .size
48- # print(cash_traded)
98+ cash_value = m .price * m .size
99+ cash_traded += cash_value
100+ total_cash_map [m .symbol ] = ( # type: ignore
101+ total_cash_map .get (m .symbol , 0 ) + cash_value # type: ignore
102+ )
103+
104+ # Update cash for the past 5 seconds
105+ cash_map_5s [m .symbol ] = ( # type: ignore
106+ cash_map_5s .get (m .symbol , 0 ) + cash_value # type: ignore
107+ ) # Okay!
108+
109+ # Update totals
110+ total_tickers_seen = len (total_string_map )
111+ total_trades_seen += 1
112+ total_cash_traded += cash_value
49113
50114
51115def top_function ():
52116 # start timer
53117 start_time = time .time ()
118+ global cash_traded
54119
55- sorted_string_map = sorted (string_map .items (), key = lambda x : x [1 ], reverse = True )
56- print ("\033 c" , end = "" ) # ANSI escape sequence to clear the screen
57-
58- for index , item in sorted_string_map [:25 ]:
59- print ("{:<15}{:<15}" .format (index , item ))
60-
61- # end timer
120+ # Only sort the string_map once
121+ sorted_trades_5s = sorted (string_map .items (), key = lambda x : x [1 ], reverse = True )
122+
123+ # Clear screen
124+ print ("\033 c" , end = "" )
125+
126+ # Print 5-second snapshot
127+ print ("\n --- Past 5 seconds ---" )
128+ print (f" Tickers seen (5s): { len (string_map )} " )
129+ print (f" Trades seen (5s): { sum (string_map .values ())} " )
130+ print (f" Cash traded (5s): { cash_traded :,.2f} " )
131+ print ("\n --- Running Totals ---" )
132+ print (f" Total Tickers seen: { total_tickers_seen } " )
133+ print (f" Total Trades seen: { total_trades_seen } " )
134+ print (f" Total Cash traded: { total_cash_traded :,.2f} " )
135+
136+ # Separator
137+ print ("\n " + "-" * 100 + "\n " )
138+
139+ # Print table header
140+ print (
141+ "{:<15}{:<20}{:<20}{:<20}{:<20}" .format (
142+ "Ticker" , "Trades (5s)" , "Cash (5s)" , "Total Trades" , "Total Cash"
143+ )
144+ )
145+
146+ # Print table content
147+ for ticker , trades in sorted (string_map .items (), key = lambda x : x [1 ], reverse = True )[
148+ :25
149+ ]:
150+ cash_5s = cash_map_5s .get (ticker , 0 )
151+ total_trades = total_string_map [ticker ]
152+ total_cash = total_cash_map .get (ticker , 0.0 )
153+ print (
154+ "{:<15}{:<20}{:<20,.2f}{:<20}{:<20,.2f}" .format (
155+ ticker , trades , cash_5s , total_trades , total_cash
156+ )
157+ )
158+
159+ # Print times
62160 end_time = time .time ()
63-
64- # print stats
65- print ()
66-
67- # current time
68161 current_time = datetime .now ()
69- print (f"Time: { current_time } " )
70-
71- # how many tickers seen
72- ticker_count = len (sorted_string_map )
73- print (f"Tickers seen: { ticker_count } " )
74162
75- # how many trades seen
76- trade_count = 0
77- for index , item in sorted_string_map :
78- trade_count += item
79- print (f"Trades seen: { trade_count } " )
80-
81- # cash traded
82- global cash_traded
83- formatted_number = "{:,.2f}" .format (cash_traded )
84- print ("Roughly " + formatted_number + " cash changed hands" )
163+ # Print elapsed the since we started
164+ elapsed_time = time .time () - app_start_time
165+ hours , rem = divmod (elapsed_time , 3600 )
166+ minutes , seconds = divmod (rem , 60 )
85167
86- # performance?
87- print (f"Time taken: { end_time - start_time :.6f} seconds" )
168+ # Print the time and quick stats
169+ print (
170+ f"\n Current Time: { current_time } | App Uptime: { int (hours ):02} :{ int (minutes ):02} :{ int (seconds ):02} | Time taken: { end_time - start_time :.6f} seconds"
171+ )
88172
89173 # clear map and cash for next loop
90174 string_map .clear ()
175+ cash_map_5s .clear ()
91176 cash_traded = 0
92177
93178
0 commit comments