refactor: Improve trade direction inference and add debug logging in position performance calculation

This commit is contained in:
Bobby (aider) 2025-02-13 10:39:25 -08:00
parent aedd96eef4
commit 0f8a75ca28

View File

@ -21,7 +21,15 @@ def calculate_position_performance(trades):
shares = float(trade['shares'])
price = float(trade['entry_price'])
direction = str(trade.get('direction', '')).lower()
# If no direction field, infer from exit_price
if 'direction' not in trade or not trade['direction']:
# If no exit_price, assume it's a buy
if trade.get('exit_price') is None:
direction = 'buy'
else:
direction = 'sell'
else:
direction = str(trade.get('direction', '')).lower()
if direction == 'buy':
total_bought += shares
@ -29,10 +37,17 @@ def calculate_position_performance(trades):
elif direction == 'sell':
total_sold += shares
total_proceeds += shares * price
print(f"Processing trade: Direction={direction}, Shares={shares}, Price={price}") # Debug
except (ValueError, TypeError) as e:
print(f"Error processing trade: {e}")
continue
print(f"Performance calculation results:") # Debug
print(f"Total bought: {total_bought} shares at total cost: ${total_cost}") # Debug
print(f"Total sold: {total_sold} shares with total proceeds: ${total_proceeds}") # Debug
# Avoid division by zero
if total_bought == 0:
return {