refactor: Improve trade direction inference and add debug logging in position performance calculation
This commit is contained in:
parent
aedd96eef4
commit
0f8a75ca28
@ -21,7 +21,15 @@ def calculate_position_performance(trades):
|
|||||||
shares = float(trade['shares'])
|
shares = float(trade['shares'])
|
||||||
price = float(trade['entry_price'])
|
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':
|
if direction == 'buy':
|
||||||
total_bought += shares
|
total_bought += shares
|
||||||
@ -29,10 +37,17 @@ def calculate_position_performance(trades):
|
|||||||
elif direction == 'sell':
|
elif direction == 'sell':
|
||||||
total_sold += shares
|
total_sold += shares
|
||||||
total_proceeds += shares * price
|
total_proceeds += shares * price
|
||||||
|
|
||||||
|
print(f"Processing trade: Direction={direction}, Shares={shares}, Price={price}") # Debug
|
||||||
|
|
||||||
except (ValueError, TypeError) as e:
|
except (ValueError, TypeError) as e:
|
||||||
print(f"Error processing trade: {e}")
|
print(f"Error processing trade: {e}")
|
||||||
continue
|
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
|
# Avoid division by zero
|
||||||
if total_bought == 0:
|
if total_bought == 0:
|
||||||
return {
|
return {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user