Hello there!
This code obliges you to close all positions at the end of the trading session (10 minutes before the US Market closes). Docs: Clock | Alpaca Docs
current_time = api.get_clock().timestamp.timestamp()
closing_time = api.get_clock().next_close.timestamp()
future_time = current_time + (10 * 60) // 600 sec
if future_time > closing_time:
try:
portfolio = api.list_positions()
for position in portfolio:
self.alpaca.close_position(position.symbol)
time.sleep(1)
except Exception as e:
logger.error(f"--- ERROR --- {e} ---")
else:
// call to trade function
Output:
current_time: 1672443283
closing_time: 1672779600
future_time: 1672443883
At first glance, it seems that everything is in order, the metric data was received after the closing of the trading (market) session. Please help me understand why this is happening…
Thanks.