5
5
from datetime import datetime
6
6
import time
7
7
import threading
8
+ import os
8
9
9
10
# docs
10
11
# https://polygon.io/docs/stocks/ws_stocks_am
76
77
total_cash_map : Dict [str , float ] = {}
77
78
78
79
80
+ def print_centered (s : str ):
81
+ term_width = os .get_terminal_size ().columns
82
+ print (s .center (term_width ))
83
+
84
+
79
85
def run_websocket_client ():
80
86
# client = WebSocketClient("XXXXXX") # hardcoded api_key is used
81
87
client = WebSocketClient () # POLYGON_API_KEY environment variable is used
@@ -124,20 +130,23 @@ def top_function():
124
130
print ("\033 c" , end = "" )
125
131
126
132
# 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} " )
135
143
136
144
# Separator
137
- print ("\n " + "-" * 100 + "\n " )
145
+ print ()
146
+ print_centered ("-" * 100 + "\n " )
138
147
139
148
# Print table header
140
- print (
149
+ print_centered (
141
150
"{:<15}{:<20}{:<20}{:<20}{:<20}" .format (
142
151
"Ticker" , "Trades (5s)" , "Cash (5s)" , "Total Trades" , "Total Cash"
143
152
)
@@ -150,7 +159,7 @@ def top_function():
150
159
cash_5s = cash_map_5s .get (ticker , 0 )
151
160
total_trades = total_string_map [ticker ]
152
161
total_cash = total_cash_map .get (ticker , 0.0 )
153
- print (
162
+ print_centered (
154
163
"{:<15}{:<20}{:<20,.2f}{:<20}{:<20,.2f}" .format (
155
164
ticker , trades , cash_5s , total_trades , total_cash
156
165
)
@@ -166,8 +175,9 @@ def top_function():
166
175
minutes , seconds = divmod (rem , 60 )
167
176
168
177
# 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"
171
181
)
172
182
173
183
# clear map and cash for next loop
0 commit comments