refactor: Improve timezone handling in calculate_weekly_metrics function
This commit is contained in:
parent
18f689115e
commit
bd4dce4f42
@ -11,9 +11,13 @@ from trading.journal import (
|
||||
|
||||
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)
|
||||
pacific_tz = pytz.timezone('US/Pacific')
|
||||
now = datetime.now(pacific_tz)
|
||||
|
||||
# Create week_start without timezone first, then localize it
|
||||
week_start = (now - timedelta(days=now.weekday())).replace(
|
||||
hour=0, minute=0, second=0, microsecond=0
|
||||
).astimezone(pacific_tz)
|
||||
|
||||
weekly_pl = 0
|
||||
weekly_trades = []
|
||||
@ -21,8 +25,12 @@ def calculate_weekly_metrics(trades) -> dict:
|
||||
for trade in trades:
|
||||
# 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)
|
||||
if trade_date:
|
||||
# Convert to Pacific timezone if needed
|
||||
if trade_date.tzinfo is None:
|
||||
trade_date = pacific_tz.localize(trade_date)
|
||||
else:
|
||||
trade_date = trade_date.astimezone(pacific_tz)
|
||||
|
||||
# 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:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user