-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulate.py
42 lines (36 loc) · 1.43 KB
/
simulate.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
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from GBM import GBM
import numpy as np
import matplotlib.pyplot as plt
# Parse command line arguments
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("-i", "--initial_price", default=100, type=float, help="Initial price of an asset")
parser.add_argument("-d", "--drift", default=0.1, type=float, help="The drift or expected annual return")
parser.add_argument("-v", "--volatility", default=0.1, type=float, help="The volatility/spread of an asset")
parser.add_argument("-p", "--paths", default=100, type=int, help="The desired number of generated paths")
args = vars(parser.parse_args())
# Set parameters
initial_price = args['initial_price']
drift = args['drift']
volatility = args['volatility']
n = args['paths']
interval = 1/365. # set interval to one day
# Initialize each path
stochastic_processes = []
for _ in range(n):
stochastic_processes.append( GBM(initial_price,drift,interval,volatility) )
# Simulate each stochastic process
for process in stochastic_processes:
tte = 1 # a year for time to expiry
while tte - process.dt > 0:
process.time_step()
tte -= process.dt
# Graphing
for process in stochastic_processes:
x = np.arange(len(process.asset_prices))
y = process.asset_prices
plt.plot(x,y)
plt.title(f'{n} Price Paths : $\mu={drift},\ \sigma={volatility}$')
plt.xlabel('Days')
plt.ylabel('Price')
plt.show()