forked from yhilpisch/py4at
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_data.py
57 lines (50 loc) · 1.54 KB
/
sample_data.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
51
52
53
54
55
56
57
#
# Python Module to Generate a
# Sample Financial Data Set
#
# Python for Algorithmic Trading
# (c) Dr. Yves J. Hilpisch
# The Python Quants GmbH
#
import numpy as np
import pandas as pd
r = 0.05 # constant short rate
sigma = 0.5 # volatility factor
def generate_sample_data(rows, cols, freq='1min'):
'''
Function to generate sample financial data.
Parameters
==========
rows: int
number of rows to generate
cols: int
number of columns to generate
freq: str
frequency string for DatetimeIndex
Returns
=======
df: DataFrame
DataFrame object with the sample data
'''
rows = int(rows)
cols = int(cols)
# generate a DatetimeIndex object given the frequency
index = pd.date_range('2021-1-1', periods=rows, freq=freq)
# determine time delta in year fractions
dt = (index[1] - index[0]) / pd.Timedelta(value='365D')
# generate column names
columns = ['No%d' % i for i in range(cols)]
# generate sample paths for geometric Brownian motion
raw = np.exp(np.cumsum((r - 0.5 * sigma ** 2) * dt +
sigma * np.sqrt(dt) *
np.random.standard_normal((rows, cols)), axis=0))
# normalize the data to start at 100
raw = raw / raw[0] * 100
# generate the DataFrame object
df = pd.DataFrame(raw, index=index, columns=columns)
return df
if __name__ == '__main__':
rows = 5 # number of rows
columns = 3 # number of columns
freq = 'D' # daily frequency
print(generate_sample_data(rows, columns, freq))