forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
positionsizing.py
576 lines (437 loc) · 19.4 KB
/
positionsizing.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import pandas as pd
from syscore.dateutils import ROOT_BDAYS_INYEAR
from syscore.exceptions import missingData
from sysdata.config.configdata import Config
from sysdata.sim.sim_data import simData
from sysquant.estimators.vol import robust_vol_calc
from systems.buffering import (
calculate_buffers,
calculate_actual_buffers,
apply_buffers_to_position,
)
from systems.stage import SystemStage
from systems.system_cache import input, diagnostic, output
from systems.forecast_combine import ForecastCombine
from systems.rawdata import RawData
class PositionSizing(SystemStage):
"""
Stage for position sizing (take combined forecast; turn into subsystem positions)
KEY INPUTS: a) system.combForecast.get_combined_forecast(instrument_code)
found in self.get_combined_forecast
b) system.rawdata.get_daily_percentage_volatility(instrument_code)
found in self.get_price_volatility(instrument_code)
If not found, uses system.data.daily_prices to calculate
c) system.rawdata.daily_denominator_price((instrument_code)
found in self.get_instrument_sizing_data(instrument_code)
If not found, uses system.data.daily_prices
d) system.data.get_value_of_block_price_move(instrument_code)
found in self.get_instrument_sizing_data(instrument_code)
e) system.data.get_fx_for_instrument(instrument_code, base_currency)
found in self.get_fx_rate(instrument_code)
KEY OUTPUT: system.positionSize.get_subsystem_position(instrument_code)
Name: positionSize
"""
@property
def name(self):
return "positionSize"
@output()
def get_buffers_for_subsystem_position(self, instrument_code: str) -> pd.Series:
"""
Get buffers for subsystem
"""
position = self.get_subsystem_position(instrument_code)
buffer = self.get_subsystem_buffers(instrument_code)
pos_buffers = apply_buffers_to_position(position=position, buffer=buffer)
return pos_buffers
@diagnostic()
def get_subsystem_buffers(self, instrument_code: str) -> pd.Series:
position = self.get_subsystem_position(instrument_code)
vol_scalar = self.get_average_position_at_subsystem_level(instrument_code)
log = self.log
config = self.config
buffer = calculate_buffers(
instrument_code=instrument_code,
position=position,
log=log,
config=config,
vol_scalar=vol_scalar,
)
return buffer
@output()
def get_subsystem_position(self, instrument_code: str) -> pd.Series:
"""
Get scaled position (assuming for now we trade our entire capital for one instrument)
KEY OUTPUT
:param instrument_code: instrument to get values for
:type instrument_code: str
:returns: Tx1 pd.DataFrame
>>> from systems.tests.testdata import get_test_object_futures_with_comb_forecasts
>>> from systems.basesystem import System
>>> (comb, fcs, rules, rawdata, data, config)=get_test_object_futures_with_comb_forecasts()
>>> system=System([rawdata, rules, fcs, comb, PositionSizing()], data, config)
>>>
>>> system.positionSize.get_subsystem_position("EDOLLAR").tail(2)
ss_position
2015-12-10 1.811465
2015-12-11 2.544598
>>>
>>> system2=System([rawdata, rules, fcs, comb, PositionSizing()], data, config)
>>> system2.positionSize.get_subsystem_position("EDOLLAR").tail(2)
ss_position
2015-12-10 1.811465
2015-12-11 2.544598
"""
self.log.debug(
"Calculating subsystem position for %s" % instrument_code,
instrument_code=instrument_code,
)
"""
We don't allow this to be changed in config
"""
avg_abs_forecast = self.avg_abs_forecast()
vol_scalar = self.get_average_position_at_subsystem_level(instrument_code)
forecast = self.get_combined_forecast(instrument_code)
vol_scalar = vol_scalar.reindex(forecast.index, method="ffill")
subsystem_position_raw = vol_scalar * forecast / avg_abs_forecast
subsystem_position = self._apply_long_only_constraint_to_position(
position=subsystem_position_raw, instrument_code=instrument_code
)
return subsystem_position
def _apply_long_only_constraint_to_position(
self, position: pd.Series, instrument_code: str
) -> pd.Series:
instrument_long_only = self._is_instrument_long_only(instrument_code)
if instrument_long_only:
position[position < 0.0] = 0.0
return position
@diagnostic()
def _is_instrument_long_only(self, instrument_code: str) -> bool:
list_of_long_only_instruments = self._get_list_of_long_only_instruments()
return instrument_code in list_of_long_only_instruments
@diagnostic()
def _get_list_of_long_only_instruments(self) -> list:
config = self.config
long_only = config.get_element_or_default("long_only_instruments", [])
return long_only
def avg_abs_forecast(self) -> float:
return self.config.average_absolute_forecast
@property
def config(self) -> Config:
return self.parent.config
@diagnostic()
def get_average_position_at_subsystem_level(
self, instrument_code: str
) -> pd.Series:
"""
Get ratio of required volatility vs volatility of instrument in instrument's own currency
:param instrument_code: instrument to get values for
:type instrument_code: str
:returns: Tx1 pd.DataFrame
>>> from systems.tests.testdata import get_test_object_futures_with_comb_forecasts
>>> from systems.basesystem import System
>>> (comb, fcs, rules, rawdata, data, config)=get_test_object_futures_with_comb_forecasts()
>>> system=System([rawdata, rules, fcs, comb, PositionSizing()], data, config)
>>>
>>> system.positionSize.get_average_position_at_subsystem_level("EDOLLAR").tail(2)
vol_scalar
2015-12-10 11.187869
2015-12-11 10.332930
>>>
>>> ## without raw data
>>> system2=System([ rules, fcs, comb, PositionSizing()], data, config)
>>> system2.positionSize.get_average_position_at_subsystem_level("EDOLLAR").tail(2)
vol_scalar
2015-12-10 11.180444
2015-12-11 10.344278
"""
self.log.debug(
"Calculating volatility scalar for %s" % instrument_code,
instrument_code=instrument_code,
)
instr_value_vol = self.get_instrument_value_vol(instrument_code)
cash_vol_target = self.get_daily_cash_vol_target()
vol_scalar = cash_vol_target / instr_value_vol
return vol_scalar
@diagnostic()
def get_instrument_value_vol(self, instrument_code: str) -> pd.Series:
"""
Get value of volatility of instrument in base currency (used for account value)
:param instrument_code: instrument to get values for
:type instrument_code: str
:returns: Tx1 pd.DataFrame
>>> from systems.tests.testdata import get_test_object_futures_with_comb_forecasts
>>> from systems.basesystem import System
>>> (comb, fcs, rules, rawdata, data, config)=get_test_object_futures_with_comb_forecasts()
>>> system=System([rawdata, rules, fcs, comb, PositionSizing()], data, config)
>>>
>>> system.positionSize.get_instrument_value_vol("EDOLLAR").tail(2)
ivv
2015-12-10 89.382530
2015-12-11 96.777975
>>>
>>> system2=System([rawdata, rules, fcs, comb, PositionSizing()], data, config)
>>> system2.positionSize.get_instrument_value_vol("EDOLLAR").tail(2)
ivv
2015-12-10 89.382530
2015-12-11 96.777975
"""
self.log.debug(
"Calculating instrument value vol for %s" % instrument_code,
instrument_code=instrument_code,
)
instr_ccy_vol = self.get_instrument_currency_vol(instrument_code)
fx_rate = self.get_fx_rate(instrument_code)
fx_rate = fx_rate.reindex(instr_ccy_vol.index, method="ffill")
instr_value_vol = instr_ccy_vol.ffill() * fx_rate
return instr_value_vol
@diagnostic()
def get_instrument_currency_vol(self, instrument_code: str) -> pd.Series:
"""
Get value of volatility of instrument in instrument's own currency
:param instrument_code: instrument to get values for
:type instrument_code: str
:returns: Tx1 pd.DataFrame
>>> from systems.tests.testdata import get_test_object_futures_with_comb_forecasts
>>> from systems.basesystem import System
>>> (comb, fcs, rules, rawdata, data, config)=get_test_object_futures_with_comb_forecasts()
>>> system=System([rawdata, rules, fcs, comb, PositionSizing()], data, config)
>>>
>>> system.positionSize.get_instrument_currency_vol("EDOLLAR").tail(2)
icv
2015-12-10 135.272415
2015-12-11 146.464756
>>>
>>> system2=System([ rules, fcs, comb, PositionSizing()], data, config)
>>> system2.positionSize.get_instrument_currency_vol("EDOLLAR").tail(2)
icv
2015-12-10 135.362246
2015-12-11 146.304072
"""
self.log.debug(
"Calculating instrument currency vol for %s" % instrument_code,
instrument_code=instrument_code,
)
block_value = self.get_block_value(instrument_code)
daily_perc_vol = self.get_price_volatility(instrument_code)
## FIXME WHY NOT RESAMPLE?
(block_value, daily_perc_vol) = block_value.align(daily_perc_vol, join="inner")
instr_ccy_vol = block_value.ffill() * daily_perc_vol
return instr_ccy_vol
@diagnostic()
def get_block_value(self, instrument_code: str) -> pd.Series:
"""
Calculate block value for instrument_code
:param instrument_code: instrument to get values for
:type instrument_code: str
:returns: Tx1 pd.DataFrame
>>> from systems.tests.testdata import get_test_object_futures_with_comb_forecasts
>>> from systems.basesystem import System
>>> (comb, fcs, rules, rawdata, data, config)=get_test_object_futures_with_comb_forecasts()
>>> system=System([rawdata, rules, fcs, comb, PositionSizing()], data, config)
>>>
>>> system.positionSize.get_block_value("EDOLLAR").tail(2)
bvalue
2015-12-10 2447.0000
2015-12-11 2449.6875
>>>
>>> system=System([rules, fcs, comb, PositionSizing()], data, config)
>>> system.positionSize.get_block_value("EDOLLAR").tail(2)
bvalue
2015-12-10 2447.0000
2015-12-11 2449.6875
"""
underlying_price = self.get_underlying_price(instrument_code)
value_of_price_move = self.parent.data.get_value_of_block_price_move(
instrument_code
)
block_value = underlying_price.ffill() * value_of_price_move * 0.01
return block_value
@diagnostic()
def get_underlying_price(self, instrument_code: str) -> pd.Series:
"""
Get various things from data and rawdata to calculate position sizes
KEY INPUT
:param instrument_code: instrument to get values for
:type instrument_code: str
:returns: Tx1 pd.DataFrame: underlying price [as used to work out % volatility],
>>> from systems.tests.testdata import get_test_object_futures_with_comb_forecasts
>>> from systems.basesystem import System
>>> (comb, fcs, rules, rawdata, data, config)=get_test_object_futures_with_comb_forecasts()
>>> system=System([rawdata, rules, fcs, comb, PositionSizing()], data, config)
>>>
>>> ans=system.positionSize.get_underlying_price("EDOLLAR")
>>> ans[0].tail(2)
price
2015-12-10 97.8800
2015-12-11 97.9875
>>>
>>> ans[1]
2500
>>>
>>> system=System([rules, fcs, comb, PositionSizing()], data, config)
>>>
>>> ans=system.positionSize.get_underlying_price("EDOLLAR")
>>> ans[0].tail(2)
price
2015-12-10 97.8800
2015-12-11 97.9875
>>>
>>> ans[1]
2500
"""
try:
rawdata = self.rawdata_stage
except missingData:
underlying_price = self.data.daily_prices(instrument_code)
else:
underlying_price = rawdata.daily_denominator_price(instrument_code)
return underlying_price
@property
def rawdata_stage(self) -> RawData:
try:
rawdata_stage = getattr(self.parent, "rawdata")
except AttributeError as e:
raise missingData from e
return rawdata_stage
@property
def data(self) -> simData:
return self.parent.data
@input
def get_price_volatility(self, instrument_code: str) -> pd.Series:
"""
Get the daily % volatility; If a rawdata stage exists from there; otherwise work it out
:param instrument_code: instrument to get values for
:type instrument_code: str
:returns: Tx1 pd.DataFrame
KEY INPUT
Note as an exception to the normal rule we cache this, as it sometimes comes from data
>>> from systems.tests.testdata import get_test_object_futures_with_comb_forecasts
>>> from systems.basesystem import System
>>> (comb, fcs, rules, rawdata, data, config)=get_test_object_futures_with_comb_forecasts()
>>> system=System([rawdata, rules, fcs, comb, PositionSizing()], data, config)
>>>
>>> system.positionSize.get_price_volatility("EDOLLAR").tail(2)
vol
2015-12-10 0.055281
2015-12-11 0.059789
>>>
>>> system2=System([ rules, fcs, comb, PositionSizing()], data, config)
>>>
>>> system2.positionSize.get_price_volatility("EDOLLAR").tail(2)
vol
2015-12-10 0.055318
2015-12-11 0.059724
"""
daily_perc_vol = self.rawdata_stage.get_daily_percentage_volatility(
instrument_code
)
return daily_perc_vol
@diagnostic()
def get_vol_target_dict(self) -> dict:
# FIXME UGLY REPLACE WITH COMPONENTS
"""
Get the daily cash vol target
Requires: percentage_vol_target, notional_trading_capital, base_currency
To find these, look in (a) in system.config.parameters...
(b).... if not found, in systems.get_defaults.py
:Returns: tuple (str, float): str is base_currency, float is value
>>> from systems.tests.testdata import get_test_object_futures_with_comb_forecasts
>>> from systems.basesystem import System
>>> (comb, fcs, rules, rawdata, data, config)=get_test_object_futures_with_comb_forecasts()
>>> system=System([rawdata, rules, fcs, comb, PositionSizing()], data, config)
>>>
>>> ## from config
>>> system.positionSize.get_vol_target_dict()['base_currency']
'GBP'
>>>
>>> ## from defaults
>>> del(config.base_currency)
>>> system=System([rawdata, rules, fcs, comb, PositionSizing()], data, config)
>>> system.positionSize.get_vol_target_dict()['base_currency']
'USD'
>>>
"""
self.log.debug("Getting vol target")
percentage_vol_target = self.get_percentage_vol_target()
notional_trading_capital = self.get_notional_trading_capital()
base_currency = self.get_base_currency()
annual_cash_vol_target = self.annual_cash_vol_target()
daily_cash_vol_target = self.get_daily_cash_vol_target()
vol_target_dict = dict(
base_currency=base_currency,
percentage_vol_target=percentage_vol_target,
notional_trading_capital=notional_trading_capital,
annual_cash_vol_target=annual_cash_vol_target,
daily_cash_vol_target=daily_cash_vol_target,
)
return vol_target_dict
@diagnostic()
def get_daily_cash_vol_target(self) -> float:
annual_cash_vol_target = self.annual_cash_vol_target()
daily_cash_vol_target = annual_cash_vol_target / ROOT_BDAYS_INYEAR
return daily_cash_vol_target
@diagnostic()
def annual_cash_vol_target(self) -> float:
notional_trading_capital = self.get_notional_trading_capital()
percentage_vol_target = self.get_percentage_vol_target()
annual_cash_vol_target = (
notional_trading_capital * percentage_vol_target / 100.0
)
return annual_cash_vol_target
@diagnostic()
def get_notional_trading_capital(self) -> float:
notional_trading_capital = float(self.config.notional_trading_capital)
return notional_trading_capital
@diagnostic()
def get_percentage_vol_target(self):
return float(self.config.percentage_vol_target)
@diagnostic()
def get_base_currency(self) -> str:
base_currency = self.config.base_currency
return base_currency
@input
def get_fx_rate(self, instrument_code: str) -> pd.Series:
"""
Get FX rate to translate instrument volatility into same currency as account value.
KEY INPUT
:param instrument_code: instrument to get values for
:type instrument_code: str
:returns: Tx1 pd.DataFrame: fx rate
>>> from systems.tests.testdata import get_test_object_futures_with_comb_forecasts
>>> from systems.basesystem import System
>>> (comb, fcs, rules, rawdata, data, config)=get_test_object_futures_with_comb_forecasts()
>>> system=System([rawdata, rules, fcs, comb, PositionSizing()], data, config)
>>>
>>> system.positionSize.get_fx_rate("EDOLLAR").tail(2)
fx
2015-12-09 0.664311
2015-12-10 0.660759
"""
base_currency = self.get_base_currency()
fx_rate = self.data.get_fx_for_instrument(instrument_code, base_currency)
return fx_rate
@input
def get_combined_forecast(self, instrument_code: str) -> pd.Series:
"""
Get the combined forecast from previous module
:param instrument_code: instrument to get values for
:type instrument_code: str
:returns: Tx1 pd.DataFrame
KEY INPUT
>>> from systems.tests.testdata import get_test_object_futures_with_comb_forecasts
>>> from systems.basesystem import System
>>> (comb, fcs, rules, rawdata, data, config)=get_test_object_futures_with_comb_forecasts()
>>> system=System([rawdata, rules, fcs, comb, PositionSizing()], data, config)
>>>
>>> system.positionSize.get_combined_forecast("EDOLLAR").tail(2)
comb_forecast
2015-12-10 1.619134
2015-12-11 2.462610
"""
return self.comb_forecast_stage.get_combined_forecast(instrument_code)
@property
def comb_forecast_stage(self) -> ForecastCombine:
return self.parent.combForecast
if __name__ == "__main__":
import doctest
doctest.testmod()