refactor: Improve trade direction handling and add debug output
This commit is contained in:
parent
3bc111288e
commit
58c59a4633
@ -17,17 +17,25 @@ def calculate_position_performance(trades):
|
||||
total_proceeds = 0
|
||||
|
||||
for trade in trades:
|
||||
if trade['direction'] == 'buy':
|
||||
# Debug print to see what we're getting
|
||||
print(f"Processing trade: {trade}")
|
||||
|
||||
# Convert direction to lowercase and strip whitespace
|
||||
direction = str(trade.get('direction', '')).lower().strip()
|
||||
|
||||
try:
|
||||
shares = float(trade['shares'])
|
||||
price = float(trade['entry_price'])
|
||||
total_bought += shares
|
||||
total_cost += shares * price
|
||||
elif trade['direction'] == 'sell':
|
||||
shares = float(trade['shares'])
|
||||
# For sells, use exit_price if available, otherwise use entry_price
|
||||
price = float(trade['exit_price']) if trade.get('exit_price') else float(trade['entry_price'])
|
||||
total_sold += shares
|
||||
total_proceeds += shares * price
|
||||
|
||||
if direction == 'buy':
|
||||
total_bought += shares
|
||||
total_cost += shares * price
|
||||
elif direction == 'sell':
|
||||
total_sold += shares
|
||||
total_proceeds += shares * price
|
||||
except (ValueError, TypeError) as e:
|
||||
print(f"Error processing trade: {e}")
|
||||
continue
|
||||
|
||||
# Avoid division by zero
|
||||
if total_bought == 0:
|
||||
@ -40,7 +48,7 @@ def calculate_position_performance(trades):
|
||||
'remaining_shares': 0
|
||||
}
|
||||
|
||||
avg_entry = total_cost / total_bought
|
||||
avg_entry = total_cost / total_bought if total_bought > 0 else 0
|
||||
avg_exit = total_proceeds / total_sold if total_sold > 0 else 0
|
||||
realized_pl = total_proceeds - (total_sold / total_bought * total_cost) if total_sold > 0 else 0
|
||||
remaining_shares = total_bought - total_sold
|
||||
@ -381,10 +389,23 @@ def trading_journal_page():
|
||||
|
||||
st.markdown("---")
|
||||
|
||||
# Debug output
|
||||
st.write("Raw trade data:")
|
||||
for trade in trades:
|
||||
st.write({
|
||||
'direction': trade.get('direction'),
|
||||
'shares': trade.get('shares'),
|
||||
'entry_price': trade.get('entry_price'),
|
||||
'entry_date': trade.get('entry_date')
|
||||
})
|
||||
|
||||
# Show buy trades
|
||||
st.subheader("Buy Orders")
|
||||
for trade in trades:
|
||||
if trade['direction'] == 'buy':
|
||||
# Convert direction to lowercase and strip whitespace
|
||||
direction = str(trade.get('direction', '')).lower().strip()
|
||||
|
||||
if direction == 'buy':
|
||||
col1, col2, col3 = st.columns(3)
|
||||
with col1:
|
||||
st.text(f"Date: {format_datetime(trade['entry_date'])}")
|
||||
@ -404,7 +425,10 @@ def trading_journal_page():
|
||||
# Show sell trades
|
||||
st.subheader("Sell Orders")
|
||||
for trade in trades:
|
||||
if trade['direction'] == 'sell':
|
||||
# Convert direction to lowercase and strip whitespace
|
||||
direction = str(trade.get('direction', '')).lower().strip()
|
||||
|
||||
if direction == 'sell':
|
||||
col1, col2 = st.columns(2)
|
||||
with col1:
|
||||
st.text(f"Date: {format_datetime(trade['entry_date'])}")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user