55from datetime import datetime
66import time
77import threading
8+ import os
89
910# docs
1011# https://polygon.io/docs/stocks/ws_stocks_am
7677total_cash_map : Dict [str , float ] = {}
7778
7879
80+ def print_centered (s : str ):
81+ term_width = os .get_terminal_size ().columns
82+ print (s .center (term_width ))
83+
84+
7985def run_websocket_client ():
8086 # client = WebSocketClient("XXXXXX") # hardcoded api_key is used
8187 client = WebSocketClient () # POLYGON_API_KEY environment variable is used
@@ -124,20 +130,23 @@ def top_function():
124130 print ("\033 c" , end = "" )
125131
126132 # 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} " )
133+ print ()
134+ print_centered ("--- Past 5 seconds ---" )
135+ print_centered (f"Tickers seen (5s): { len (string_map )} " )
136+ print_centered (f"Trades seen (5s): { sum (string_map .values ())} " )
137+ print_centered (f"Cash traded (5s): { cash_traded :,.2f} " )
138+ print ()
139+ print_centered ("--- Running Totals ---" )
140+ print_centered (f"Total Tickers seen: { total_tickers_seen } " )
141+ print_centered (f"Total Trades seen: { total_trades_seen } " )
142+ print_centered (f"Total Cash traded: { total_cash_traded :,.2f} " )
135143
136144 # Separator
137- print ("\n " + "-" * 100 + "\n " )
145+ print ()
146+ print_centered ("-" * 100 + "\n " )
138147
139148 # Print table header
140- print (
149+ print_centered (
141150 "{:<15}{:<20}{:<20}{:<20}{:<20}" .format (
142151 "Ticker" , "Trades (5s)" , "Cash (5s)" , "Total Trades" , "Total Cash"
143152 )
@@ -150,7 +159,7 @@ def top_function():
150159 cash_5s = cash_map_5s .get (ticker , 0 )
151160 total_trades = total_string_map [ticker ]
152161 total_cash = total_cash_map .get (ticker , 0.0 )
153- print (
162+ print_centered (
154163 "{:<15}{:<20}{:<20,.2f}{:<20}{:<20,.2f}" .format (
155164 ticker , trades , cash_5s , total_trades , total_cash
156165 )
@@ -166,8 +175,9 @@ def top_function():
166175 minutes , seconds = divmod (rem , 60 )
167176
168177 # 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"
178+ print ()
179+ print_centered (
180+ f"Current Time: { current_time } | App Uptime: { int (hours ):02} :{ int (minutes ):02} :{ int (seconds ):02} | Time taken: { end_time - start_time :.6f} seconds"
171181 )
172182
173183 # clear map and cash for next loop
0 commit comments