feat: Enhance trading system page with cash-aware position sizing
This commit is contained in:
parent
a643a1649e
commit
336afa8170
@ -1,20 +1,23 @@
|
||||
import streamlit as st
|
||||
from trading.journal import get_latest_portfolio_value
|
||||
from trading.journal import get_latest_portfolio_value, get_open_trades_summary
|
||||
from trading.position_calculator import PositionCalculator
|
||||
|
||||
def trading_system_page():
|
||||
st.header("Trading System")
|
||||
st.subheader("Position Calculator")
|
||||
|
||||
# Get latest portfolio value for default account size
|
||||
# Get latest portfolio value and open trades for cash calculation
|
||||
portfolio_data = get_latest_portfolio_value()
|
||||
default_account_size = portfolio_data['cash_balance'] if portfolio_data else 100000.0
|
||||
cash_balance = portfolio_data['cash_balance'] if portfolio_data else 0
|
||||
|
||||
# Display available cash
|
||||
st.info(f"Available Cash: ${cash_balance:,.2f}")
|
||||
|
||||
col1, col2 = st.columns(2)
|
||||
with col1:
|
||||
account_size = st.number_input("Account Size ($)",
|
||||
min_value=0.0,
|
||||
value=default_account_size,
|
||||
value=cash_balance,
|
||||
step=1000.0)
|
||||
risk_percentage = st.number_input("Risk Percentage (%)",
|
||||
min_value=0.1,
|
||||
@ -32,27 +35,49 @@ def trading_system_page():
|
||||
target_price = st.number_input("Target Price ($)", min_value=0.01, step=0.01)
|
||||
|
||||
if st.button("Calculate Position"):
|
||||
try:
|
||||
calculator = PositionCalculator(
|
||||
account_size=account_size,
|
||||
risk_percentage=risk_percentage,
|
||||
stop_loss_percentage=stop_loss_percentage
|
||||
)
|
||||
|
||||
position = calculator.calculate_position_size(entry_price, target_price)
|
||||
|
||||
col1, col2 = st.columns(2)
|
||||
with col1:
|
||||
try:
|
||||
calculator = PositionCalculator(
|
||||
account_size=account_size,
|
||||
risk_percentage=risk_percentage,
|
||||
stop_loss_percentage=stop_loss_percentage
|
||||
)
|
||||
|
||||
position = calculator.calculate_position_size(entry_price, target_price)
|
||||
|
||||
# Calculate maximum shares possible with available cash
|
||||
max_shares_by_cash = int(cash_balance / entry_price) if entry_price > 0 else 0
|
||||
|
||||
# Adjust shares based on available cash
|
||||
recommended_shares = min(position['shares'], max_shares_by_cash)
|
||||
|
||||
col1, col2 = st.columns(2)
|
||||
with col1:
|
||||
if recommended_shares < position['shares']:
|
||||
st.warning(f"Position size limited by available cash. Maximum affordable shares: {recommended_shares:,}")
|
||||
position_value = recommended_shares * entry_price
|
||||
risk_amount = position['risk_amount'] * (recommended_shares / position['shares'])
|
||||
|
||||
st.metric("Recommended Shares", f"{recommended_shares:,}")
|
||||
st.metric("Position Value", f"${position_value:,.2f}")
|
||||
st.metric("Risk Amount", f"${risk_amount:,.2f}")
|
||||
else:
|
||||
st.metric("Number of Shares", f"{position['shares']:,}")
|
||||
st.metric("Position Value", f"${position['position_value']:,.2f}")
|
||||
st.metric("Risk Amount", f"${position['risk_amount']:,.2f}")
|
||||
|
||||
with col2:
|
||||
st.metric("Stop Loss Price", f"${position['stop_loss']:.2f}")
|
||||
st.metric("Potential Loss", f"${position['potential_loss']:,.2f}")
|
||||
if 'potential_profit' in position:
|
||||
potential_profit = (target_price - entry_price) * recommended_shares
|
||||
risk_reward = abs(potential_profit / (position['stop_loss'] - entry_price) / recommended_shares) if recommended_shares > 0 else 0
|
||||
st.metric("Potential Profit", f"${potential_profit:,.2f}")
|
||||
st.metric("Risk/Reward Ratio", f"{risk_reward:.2f}")
|
||||
|
||||
# Show percentage of cash being used
|
||||
if recommended_shares > 0:
|
||||
cash_usage = (recommended_shares * entry_price / cash_balance) * 100
|
||||
st.info(f"This position would use {cash_usage:.1f}% of available cash")
|
||||
|
||||
with col2:
|
||||
st.metric("Stop Loss Price", f"${position['stop_loss']:.2f}")
|
||||
st.metric("Potential Loss", f"${position['potential_loss']:,.2f}")
|
||||
if 'potential_profit' in position:
|
||||
st.metric("Potential Profit", f"${position['potential_profit']:,.2f}")
|
||||
st.metric("Risk/Reward Ratio", f"{position['risk_reward_ratio']:.2f}")
|
||||
|
||||
except Exception as e:
|
||||
st.error(f"Error calculating position: {str(e)}")
|
||||
except Exception as e:
|
||||
st.error(f"Error calculating position: {str(e)}")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user