forked from pst-group/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomdata.py
More file actions
289 lines (206 loc) · 7.87 KB
/
randomdata.py
File metadata and controls
289 lines (206 loc) · 7.87 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from sysdata.data import simData
from random import gauss
import numpy as np
from syscore.pdutils import create_arbitrary_pdseries
import pandas as pd
class RandomData(simData):
"""
Generates random data for testing with a saw tooth pattern
Unlike a normal data object this doesn't have any data when first created
You need to run generate_random_data which will generate, and then cache, the data
"""
def __init__(self):
super().__init__()
setattr(self, "_price_cache_random_data", dict())
def __repr__(self):
return "RandomData object with %d instruments" % len(
self.get_instrument_list())
def get_instrument_list(self):
return list(self._price_cache_random_data.keys())
def get_raw_price(self, instrument_code):
"""
Returns a pd.series of prices
:param instrument_code: instrument to get carry data for
:type instrument_code: str
:returns: pd.DataSeries
>>> ans=RandomData()
>>> ans.generate_random_data("wibble", 10, 5, 5, 0.0)
>>> ans.get_raw_price("wibble")
1980-01-01 0.0
1980-01-02 1.0
1980-01-03 2.0
1980-01-04 3.0
1980-01-07 4.0
1980-01-08 5.0
1980-01-09 4.0
1980-01-10 3.0
1980-01-11 2.0
1980-01-14 1.0
Freq: B, dtype: float64
"""
if instrument_code in self.get_instrument_list():
## must have been cached
return self._price_cache_random_data[instrument_code]
error_msg = "No price found for %s you need to run .generate_random_data(instrument_code=%s....)" % (
instrument_code, instrument_code)
self.log.critical(error_msg)
def generate_random_data(self,
instrument_code,
Nlength,
Tlength,
Xamplitude,
Volscale,
sines=False,
date_start=pd.datetime(1980, 1, 1)):
"""
Generates a trend of length N amplitude X, plus gaussian noise mean zero std. dev (vol scale * amplitude)
With an arbitrary datetime index
If sines=True then generates as a sine wave, otherwise straight line
:param Nlength: total number of returns to generate
:type Nlength: int
:param Tlength: Length of each trend
:type Tlength: int
:param Xamplitude: Amplitude of each trend
:type Xamplitude: float
:param Volscale: Ratio of volatility to amplitude
:type Volscale: float
:param sines: Generate a sine wave (if True), or a saw tooth (False). Default False.
:type sines: bool
:param date_start: Start date for arbitrary series
:type date_start: datetime
:param instrument_code: made up instrument to create data for
:type instrument_code: str
:returns: None
Also puts results into cache
"""
random_data = generate_trendy_pdseries(
Nlength,
Tlength,
Xamplitude,
Volscale,
sines=sines,
date_start=date_start)
self._price_cache_random_data[instrument_code] = random_data
return None
def generate_siney_trends(Nlength, Tlength, Xamplitude):
"""
Generates a price process, Nlength returns, underlying trend with length T and amplitude X
as a sine wave
:param Nlength: total number of returns to generate
:type Nlength: int
:param Tlength: Length of each trend
:type Tlength: int
:param Xamplitude: Amplitude of each trend
:type Xamplitude: float
:returns: returns a vector of numbers as a list, length Nlength
"""
halfAmplitude = Xamplitude / 2.0
cycles = Nlength / Tlength
cycles_as_pi = cycles * np.pi
increment = cycles_as_pi / Nlength
alltrends = [
np.sin(x) * halfAmplitude
for x in np.arange(0.0, cycles_as_pi, increment)
]
alltrends = alltrends[:Nlength]
return alltrends
def generate_trends(Nlength, Tlength, Xamplitude):
"""
Generates a price process, Nlength returns, underlying trend with length T and amplitude X
:param Nlength: total number of returns to generate
:type Nlength: int
:param Tlength: Length of each trend
:type Tlength: int
:param Xamplitude: Amplitude of each trend
:type Xamplitude: float
:returns: returns a vector of numbers as a list, length Nlength
"""
halfAmplitude = Xamplitude / 2.0
trend_step = Xamplitude / Tlength
cycles = int(np.ceil(Nlength / Tlength))
trendup = list(
np.arange(start=-halfAmplitude, stop=halfAmplitude, step=trend_step))
trenddown = list(
np.arange(start=halfAmplitude, stop=-halfAmplitude, step=-trend_step))
alltrends = [trendup + trenddown] * int(np.ceil(cycles))
alltrends = sum(alltrends, [])
alltrends = alltrends[:Nlength]
return alltrends
def generate_noise(Nlength, stdev):
"""
Generates a series of gaussian noise as a list Nlength
:param Nlength: total number of returns to generate
:type Nlength: int
:param stdev: Standard deviation of noise
:type stdev: float
:returns: returns a vector of numbers as a list, length Nlength
"""
return [gauss(0.0, stdev) for Unused in range(Nlength)]
def generate_trendy_pdseries(Nlength,
Tlength,
Xamplitude,
Volscale,
sines=False,
date_start=pd.datetime(1980, 1, 1)):
"""
Generates a trend of length N amplitude X, plus gaussian noise mean zero std. dev (vol scale * amplitude)
With an arbitrary datetime index
If sines=True then generates as a sine wave, otherwise straight line
:param Nlength: total number of returns to generate
:type Nlength: int
:param Tlength: Length of each trend
:type Tlength: int
:param Xamplitude: Amplitude of each trend
:type Xamplitude: float
:param Volscale: Ratio of volatility to amplitude
:type Volscale: float
:param sines: Generate a sine wave (if True), or a saw tooth (False). Default False.
:type sines: bool
:param date_start: Start date for arbitrary series
:type date_start: datetime
:returns: a pd.Series, length Nlength
>>> generate_trendy_pdseries(10, 5, 5, 0.0)
1980-01-01 0.0
1980-01-02 1.0
1980-01-03 2.0
1980-01-04 3.0
1980-01-07 4.0
1980-01-08 5.0
1980-01-09 4.0
1980-01-10 3.0
1980-01-11 2.0
1980-01-14 1.0
Freq: B, dtype: float64
>>> generate_trendy_pdseries(10, 5, 5, 0.0, True)
1980-01-01 0.000000e+00
1980-01-02 1.469463e+00
1980-01-03 2.377641e+00
1980-01-04 2.377641e+00
1980-01-07 1.469463e+00
1980-01-08 2.220446e-16
1980-01-09 -1.469463e+00
1980-01-10 -2.377641e+00
1980-01-11 -2.377641e+00
1980-01-14 -1.469463e+00
Freq: B, dtype: float64
"""
stdev = Volscale * Xamplitude
noise_returns_as_list = generate_noise(Nlength, stdev)
## Can use a different process here if desired
if sines:
process_as_list = generate_siney_trends(Nlength, Tlength, Xamplitude)
else:
process_as_list = generate_trends(Nlength, Tlength, Xamplitude)
pd_process = create_arbitrary_pdseries(
process_as_list, date_start=date_start)
noise_returns = create_arbitrary_pdseries(
noise_returns_as_list, date_start=date_start)
process_returns = pd_process.diff()
combined_returns = noise_returns + process_returns
combined_returns[0] = 0
combined_price = combined_returns.cumsum()
return combined_price
return pdseries
if __name__ == '__main__':
import doctest
doctest.testmod()