fix: Resolve timezone awareness issue in weekly metrics calculation
This commit is contained in:
parent
00d00fb06b
commit
18f689115e
@ -13,15 +13,19 @@ def calculate_weekly_metrics(trades) -> dict:
|
|||||||
"""Calculate weekly performance metrics"""
|
"""Calculate weekly performance metrics"""
|
||||||
now = datetime.now(pytz.timezone('US/Pacific'))
|
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 = (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_pl = 0
|
||||||
weekly_trades = []
|
weekly_trades = []
|
||||||
|
|
||||||
for trade in trades:
|
for trade in trades:
|
||||||
# For sells/exits that happened this week
|
# Get the trade date and ensure it's timezone aware
|
||||||
if (trade.get('direction') == 'sell' or trade.get('exit_price')) and \
|
trade_date = trade.get('exit_date', trade.get('entry_date'))
|
||||||
trade.get('exit_date', trade.get('entry_date')) >= week_start:
|
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'))
|
price = float(trade.get('exit_price') or trade.get('entry_price'))
|
||||||
shares = float(trade['shares'])
|
shares = float(trade['shares'])
|
||||||
position_id = trade['position_id']
|
position_id = trade['position_id']
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user