feat: Add Monte Carlo target price calculation and projection parameters

This commit is contained in:
Bobby (aider) 2025-02-17 15:47:58 -08:00
parent cd9b6f7969
commit 2dda835a3f
2 changed files with 46 additions and 9 deletions

View File

@ -47,6 +47,27 @@ class MonteCarloSimulator:
return price_paths
def calculate_target_price(self, confidence_level: float = 95) -> float:
"""
Calculate target price based on Monte Carlo simulation and confidence level
Args:
confidence_level (float): Confidence level for target price (default: 95)
Returns:
float: Recommended target price
"""
# Run simulation
paths = self.run_simulation()
# Get the prices at specified timeframe
final_prices = paths[-1]
# Calculate the price that represents the upside potential
target_price = np.percentile(final_prices, confidence_level)
return target_price
def calculate_stop_loss(self, risk_percentage: float) -> float:
"""
Calculate stop loss price based on Monte Carlo simulation and desired risk percentage

View File

@ -57,11 +57,22 @@ def trading_system_page():
max_value=100.0,
value=1.0,
step=0.1)
use_monte_carlo = st.checkbox("Use Monte Carlo for Stop Loss", value=True)
use_monte_carlo = st.checkbox("Use Monte Carlo for Analysis", value=True)
if use_monte_carlo:
days_out = st.number_input("Days to Project",
min_value=1,
max_value=30,
value=5,
help="Number of days to project for target price")
confidence_level = st.slider("Confidence Level (%)",
min_value=80,
max_value=99,
value=95)
with col2:
ticker = st.text_input("Ticker Symbol", value="").upper()
entry_price = st.number_input("Entry Price ($)", min_value=0.01, step=0.01)
if not use_monte_carlo:
target_price = st.number_input("Target Price ($)", min_value=0.01, step=0.01)
if st.button("Calculate Position"):
@ -85,10 +96,12 @@ def trading_system_page():
return
# Initialize Monte Carlo simulator
simulator = MonteCarloSimulator(df, num_simulations=1000, time_horizon=5)
# Initialize Monte Carlo simulator
simulator = MonteCarloSimulator(df, num_simulations=1000, time_horizon=days_out)
# Calculate stop loss price
# Calculate stop loss and target prices
stop_loss_price = simulator.calculate_stop_loss(risk_percentage)
target_price = simulator.calculate_target_price(confidence_level)
# Calculate stop loss percentage
stop_loss_percentage = abs((stop_loss_price - entry_price) / entry_price * 100)
@ -150,13 +163,16 @@ def trading_system_page():
# Add Monte Carlo metrics if used
if use_monte_carlo:
st.subheader("Monte Carlo Analysis")
col1, col2 = st.columns(2)
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Calculated Stop Loss Price", f"${stop_loss_price:.2f}")
st.metric("Stop Loss Percentage", f"{stop_loss_percentage:.2f}%")
st.metric("Stop Loss Price", f"${stop_loss_price:.2f}")
st.metric("Stop Loss %", f"{stop_loss_percentage:.2f}%")
with col2:
st.metric("Based on Simulations", "1,000")
st.metric("Simulation Timeframe", "5 days")
st.metric("Target Price", f"${target_price:.2f}")
st.metric("Target %", f"{((target_price - entry_price) / entry_price * 100):.2f}%")
with col3:
st.metric("Days Projected", f"{days_out}")
st.metric("Confidence Level", f"{confidence_level}%")
except Exception as e:
st.error(f"Error calculating position: {str(e)}")