Skip to content

Commit 3c08847

Browse files
authored
Add files via upload
1 parent 1e5de1a commit 3c08847

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import yfinance as yf
4+
5+
def get_stock_prices(ticker, start_date, end_date):
6+
stock_data = yf.download(ticker, start=start_date, end=end_date)
7+
return stock_data['Close'].values
8+
9+
def monte_carlo_simulation(starting_prices, num_simulations, num_days, returns_std):
10+
simulations = np.zeros((num_simulations, num_days))
11+
simulations[:, 0] = starting_prices
12+
13+
for i in range(1, num_days):
14+
daily_returns = np.random.normal(0, returns_std, num_simulations)
15+
simulations[:, i] = simulations[:, i - 1] * (1 + daily_returns)
16+
17+
return simulations
18+
19+
# Set parameters
20+
ticker = 'AAPL' # Apple stock symbol
21+
start_date = '2022-01-01'
22+
end_date = '2023-01-01'
23+
historical_prices = get_stock_prices(ticker, start_date, end_date)
24+
25+
num_simulations = 100 # Number of simulations
26+
num_days = 250 # Number of trading days in a year
27+
28+
starting_prices = historical_prices[-num_simulations:] # Use the last prices for the initial simulations
29+
returns_std = np.std(np.diff(np.log(historical_prices))) # Use historical daily log returns standard deviation
30+
31+
# Run the simulation
32+
simulations = monte_carlo_simulation(starting_prices, num_simulations, num_days, returns_std)
33+
34+
# Plot the results
35+
plt.figure(figsize=(10, 6))
36+
37+
# Plot simulations
38+
for i in range(num_simulations):
39+
plt.plot(simulations[i, :], color='blue', alpha=0.1)
40+
41+
# Highlight actual stock prices in a different color
42+
plt.plot(historical_prices, color='red', label='Actual Stock Price')
43+
44+
plt.title('Monte Carlo Simulation of Daily Stock Prices for AAPL')
45+
plt.xlabel('Days')
46+
plt.ylabel('Stock Price')
47+
plt.legend()
48+
plt.show()

0 commit comments

Comments
 (0)