NSE_AI_Stock_Analyzer / monte_carlo.py
nitishkarvekar's picture
Create monte_carlo.py
09c1fbc verified
raw
history blame contribute delete
875 Bytes
import numpy as np
def monte_carlo_probability(df, target, stop, simulations=800, horizon=10):
close = df["Close"].squeeze()
price = float(close.iloc[-1])
returns = close.pct_change().dropna()
mu = returns.mean()
sigma = returns.std()
target_hits = 0
stop_hits = 0
for _ in range(simulations):
simulated_price = price
for _ in range(horizon):
random_return = np.random.normal(mu, sigma)
simulated_price = simulated_price * (1 + random_return)
if simulated_price >= target:
target_hits += 1
break
if simulated_price <= stop:
stop_hits += 1
break
total = target_hits + stop_hits
if total == 0:
return 50
probability = (target_hits / total) * 100
return round(probability, 2)