Chapter 01 — Python and Algorithmic Trading
%%time
import random
from math import exp, sqrt
S0 = 100
r = 0.05
T = 1.0
sigma = 0.2
values = []
for _ in range(1000000):
ST = S0 * exp((r - 0.5 * sigma ** 2) * T +
sigma * random.gauss(0, 1) * sqrt(T))
values.append(ST)
CPU times: user 923 ms, sys: 10.2 ms, total: 933 ms Wall time: 932 ms
%%time
import numpy as np
S0 = 100
r = 0.05
T = 1.0
sigma = 0.2
ST = S0 * np.exp((r - 0.5 * sigma ** 2) * T +
sigma * np.random.standard_normal(1000000) * np.sqrt(T))
CPU times: user 97.3 ms, sys: 22 ms, total: 119 ms Wall time: 119 ms
%matplotlib inline
from pylab import mpl, plt
plt.style.use('seaborn')
mpl.rcParams['font.family'] = 'serif'
import configparser
c = configparser.ConfigParser()
c.read('../pyalgo.cfg')
['../pyalgo.cfg']
import quandl as q
q.ApiConfig.api_key = c['quandl']['api_key']
d = q.get('BCHAIN/MKPRU')
d['SMA'] = d['Value'].rolling(100).mean()
d.loc['2013-1-1':].plot(title='BTC/USD exchange rate',
figsize=(10, 6));
# plt.savefig('../../images/ch01/bitcoin_xr.png')
d.tail()
Value | SMA | |
---|---|---|
Date | ||
2020-04-29 | 7790.66 | 7990.4138 |
2020-04-30 | 7765.33 | 7981.0335 |
2020-05-01 | 8777.63 | 7982.5451 |
2020-05-02 | 8628.77 | 7981.6102 |
2020-05-03 | 8824.66 | 7983.2674 |