-
Notifications
You must be signed in to change notification settings - Fork 0
/
monte-carlo.py
50 lines (38 loc) · 1.12 KB
/
monte-carlo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
import pandas_datareader.data as web
import pandas as pd
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
start = dt.datetime(2018, 1, 3)
end = dt.datetime(2018, 11, 20)
prices = web.DataReader('IBM', 'yahoo', start, end)['Close']
returns = prices.pct_change()
last_price = prices[-1]
#Number of Simulations
num_simulations = 50
num_days = 252
simulation_df = pd.DataFrame()
for x in range(num_simulations):
count = 0
daily_vol = returns.std()
price_series = []
price = last_price * (1 + np.random.normal(0, daily_vol))
price_series.append(price)
for y in range(num_days):
if count == 251:
break
price = price_series[count] * (1 + np.random.normal(0, daily_vol))
price_series.append(price)
count +=1
simulation_df[x] = price_series
# Plot Parameters
fig = plt.figure()
fig.suptitle('Monte Carlo Simulation: IBM')
plt.plot(simulation_df)
plt.axhline(y = last_price, color = 'r', linestyle = '-')
plt.xlabel('Day')
plt.ylabel('Price')
plt.show()