fix: Resolve timezone awareness issue in weekly metrics calculation

This commit is contained in:
Bobby (aider) 2025-02-13 12:29:05 -08:00
parent 00d00fb06b
commit 18f689115e

View File

@ -13,15 +13,19 @@ def calculate_weekly_metrics(trades) -> dict:
"""Calculate weekly performance metrics"""
now = datetime.now(pytz.timezone('US/Pacific'))
week_start = (now - timedelta(days=now.weekday())).replace(hour=0, minute=0, second=0, microsecond=0)
week_start = pytz.timezone('US/Pacific').localize(week_start)
weekly_pl = 0
weekly_trades = []
for trade in trades:
# For sells/exits that happened this week
if (trade.get('direction') == 'sell' or trade.get('exit_price')) and \
trade.get('exit_date', trade.get('entry_date')) >= week_start:
# Get the trade date and ensure it's timezone aware
trade_date = trade.get('exit_date', trade.get('entry_date'))
if trade_date and not trade_date.tzinfo:
trade_date = pytz.timezone('US/Pacific').localize(trade_date)
# For sells/exits that happened this week
if (trade.get('direction') == 'sell' or trade.get('exit_price')) and trade_date and trade_date >= week_start:
price = float(trade.get('exit_price') or trade.get('entry_price'))
shares = float(trade['shares'])
position_id = trade['position_id']