forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rawdata.py
231 lines (181 loc) · 7.33 KB
/
rawdata.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
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
from systems.stage import SystemStage
from copy import copy
from syscore.objects import resolve_function
from systems.system_cache import input, diagnostic, output
class RawData(SystemStage):
"""
A SystemStage that does some fairly common calculations before we do
forecasting and which gives access to some widely used methods.
This is optional; forecasts can go straight to system.data
The advantages of using RawData are:
- preliminary calculations that are reused can be cached, to
save time (eg volatility)
- preliminary calculations are available for inspection when
diagnosing what is going on
Name: rawdata
"""
def _name(self):
return "rawdata"
@input
def get_daily_prices(self, instrument_code):
"""
Gets daily prices
:param instrument_code: Instrument to get prices for
:type trading_rules: str
:returns: Tx1 pd.DataFrame
KEY OUTPUT
"""
self.log.msg(
"Calculating daily prices for %s" %
instrument_code,
instrument_code=instrument_code)
dailyprice = self.parent.data.daily_prices(instrument_code)
return dailyprice
@output()
def daily_denominator_price(self, instrument_code):
"""
Gets daily prices for use with % volatility
This won't always be the same as the normal 'price' which is normally a cumulated total return series
:param instrument_code: Instrument to get prices for
:type trading_rules: str
:returns: Tx1 pd.DataFrame
>>> from systems.tests.testdata import get_test_object
>>> from systems.basesystem import System
>>>
>>> (rawdata, data, config)=get_test_object()
>>> system=System([rawdata], data)
>>> system.rawdata.daily_denominator_price("EDOLLAR").head(2)
price
1983-09-26 71.241192
1983-09-27 71.131192
"""
dem_returns = self.get_daily_prices(instrument_code)
return dem_returns
@output()
def daily_returns(self, instrument_code):
"""
Gets daily returns (not % returns)
:param instrument_code: Instrument to get prices for
:type trading_rules: str
:returns: Tx1 pd.DataFrame
>>> from systems.tests.testdata import get_test_object
>>> from systems.basesystem import System
>>>
>>> (rawdata, data, config)=get_test_object()
>>> system=System([rawdata], data)
>>> system.rawdata.daily_returns("EDOLLAR").tail(2)
price
2015-12-10 -0.0650
2015-12-11 0.1075
"""
instrdailyprice = self.get_daily_prices(instrument_code)
dailyreturns = instrdailyprice.diff()
return dailyreturns
@output()
def daily_returns_volatility(self, instrument_code):
"""
Gets volatility of daily returns (not % returns)
This is done using a user defined function
We get this from:
the configuration object
or if not found, system.defaults.py
The dict must contain func key; anything else is optional
:param instrument_code: Instrument to get prices for
:type trading_rules: str
:returns: Tx1 pd.DataFrame
>>> from systems.tests.testdata import get_test_object
>>> from systems.basesystem import System
>>>
>>> (rawdata, data, config)=get_test_object()
>>> system=System([rawdata], data)
>>> ## uses defaults
>>> system.rawdata.daily_returns_volatility("EDOLLAR").tail(2)
vol
2015-12-10 0.054145
2015-12-11 0.058522
>>>
>>> from sysdata.configdata import Config
>>> config=Config("systems.provided.example.exampleconfig.yaml")
>>> system=System([rawdata], data, config)
>>> system.rawdata.daily_returns_volatility("EDOLLAR").tail(2)
vol
2015-12-10 0.054145
2015-12-11 0.058522
>>>
>>> config=Config(dict(volatility_calculation=dict(func="syscore.algos.robust_vol_calc", days=200)))
>>> system2=System([rawdata], data, config)
>>> system2.rawdata.daily_returns_volatility("EDOLLAR").tail(2)
vol
2015-12-10 0.057946
2015-12-11 0.058626
"""
self.log.msg(
"Calculating daily volatility for %s" %
instrument_code,
instrument_code=instrument_code)
system = self.parent
dailyreturns = self.daily_returns(instrument_code)
volconfig = copy(system.config.volatility_calculation)
# volconfig contains 'func' and some other arguments
# we turn func which could be a string into a function, and then
# call it with the other ags
volfunction = resolve_function(volconfig.pop('func'))
vol = volfunction(dailyreturns, **volconfig)
return vol
@output()
def get_daily_percentage_volatility(self, instrument_code):
"""
Get percentage returns normalised by recent vol
Useful statistic, also used for some trading rules
This is an optional subsystem; forecasts can go straight to system.data
:param instrument_code: Instrument to get prices for
:type trading_rules: str
:returns: Tx1 pd.DataFrame
>>> from systems.tests.testdata import get_test_object
>>> from systems.basesystem import System
>>>
>>> (rawdata, data, config)=get_test_object()
>>> system=System([rawdata], data)
>>> system.rawdata.get_daily_percentage_volatility("EDOLLAR").tail(2)
vol
2015-12-10 0.055281
2015-12-11 0.059789
"""
denom_price = self.daily_denominator_price(instrument_code)
return_vol = self.daily_returns_volatility(instrument_code)
(denom_price, return_vol) = denom_price.align(
return_vol, join="right")
perc_vol = 100.0 * \
(return_vol / denom_price.shift(1))
return perc_vol
@diagnostic()
def norm_returns(self, instrument_code):
"""
Get returns normalised by recent vol
Useful statistic, also used for some trading rules
This is an optional subsystem; forecasts can go straight to system.data
:param instrument_code: Instrument to get prices for
:type trading_rules: str
:returns: Tx1 pd.DataFrame
>>> from systems.tests.testdata import get_test_object
>>> from systems.basesystem import System
>>>
>>> (rawdata, data, config)=get_test_object()
>>> system=System([rawdata], data)
>>> system.rawdata.norm_returns("EDOLLAR").tail(2)
norm_return
2015-12-10 -1.219510
2015-12-11 1.985413
"""
self.log.msg(
"Calculating normalised prices for %s" %
instrument_code,
instrument_code=instrument_code)
returnvol = self.daily_returns_volatility(
instrument_code).shift(1)
dailyreturns = self.daily_returns(instrument_code)
norm_return = dailyreturns / returnvol
return norm_return
if __name__ == '__main__':
import doctest
doctest.testmod()