forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplesystem.py
262 lines (209 loc) · 7.71 KB
/
simplesystem.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
"""
Let's recap:
We got some data and created a trading rule
"""
from sysdata.sim.csv_futures_sim_data import csvFuturesSimData
data = csvFuturesSimData()
from systems.provided.example.rules import ewmac_forecast_with_defaults as ewmac
"""
Okay, I wonder how this would work for a number of instruments?
For this we need to build a system
A system is made up of SystemStages - essentially stages in the process, and it
needs data, and perhaps a configuration
The minimum stage you would have would be Rules - which is where you put
trading rules
"""
from systems.forecasting import Rules
"""
We can create rules in a number of different ways
Note that to make our rule work it needs to have
"""
my_rules = Rules(ewmac)
print(my_rules.trading_rules())
my_rules = Rules(dict(ewmac=ewmac))
print(my_rules.trading_rules())
from systems.basesystem import System
my_system = System([my_rules], data)
print(my_system)
print(my_system.rules.get_raw_forecast("EDOLLAR", "ewmac").tail(5))
"""
Define a TradingRule
"""
from systems.trading_rules import TradingRule
ewmac_rule = TradingRule(ewmac)
my_rules = Rules(dict(ewmac=ewmac_rule))
ewmac_rule
"""
... or two...
"""
ewmac_8 = TradingRule((ewmac, [], dict(Lfast=8, Lslow=32)))
ewmac_32 = TradingRule(dict(function=ewmac, other_args=dict(Lfast=32, Lslow=128)))
my_rules = Rules(dict(ewmac8=ewmac_8, ewmac32=ewmac_32))
print(my_rules.trading_rules()["ewmac32"])
my_system = System([my_rules], data)
my_system.rules.get_raw_forecast("EDOLLAR", "ewmac32").tail(5)
from sysdata.config.configdata import Config
my_config = Config()
my_config
empty_rules = Rules()
my_config.trading_rules = dict(ewmac8=ewmac_8, ewmac32=ewmac_32)
my_system = System([empty_rules], data, my_config)
my_system.rules.get_raw_forecast("EDOLLAR", "ewmac32").tail(5)
from systems.forecast_scale_cap import ForecastScaleCap
# we can estimate these ourselves
my_config.instruments = ["US10", "EDOLLAR", "CORN", "SP500_micro"]
my_config.use_forecast_scale_estimates = True
fcs = ForecastScaleCap()
my_system = System([fcs, my_rules], data, my_config)
my_config.forecast_scalar_estimate["pool_instruments"] = False
print(my_system.forecastScaleCap.get_forecast_scalar("EDOLLAR", "ewmac32").tail(5))
# or we can use the values from the book
my_config.forecast_scalars = dict(ewmac8=5.3, ewmac32=2.65)
my_config.use_forecast_scale_estimates = False
fcs = ForecastScaleCap()
my_system = System([fcs, my_rules], data, my_config)
print(my_system.forecastScaleCap.get_capped_forecast("EDOLLAR", "ewmac32").tail(5))
"""
combine some rules
"""
from systems.forecast_combine import ForecastCombine
# defaults
combiner = ForecastCombine()
my_system = System([fcs, my_rules, combiner], data, my_config)
print(my_system.combForecast.get_forecast_weights("EDOLLAR").tail(5))
print(my_system.combForecast.get_forecast_diversification_multiplier("EDOLLAR").tail(5))
# estimates:
from systems.accounts.accounts_stage import Account
from systems.rawdata import RawData
from systems.positionsizing import PositionSizing
my_account = Account()
combiner = ForecastCombine()
raw_data = RawData()
position_size = PositionSizing()
my_config.forecast_weight_estimate = dict(method="one_period")
my_config.use_forecast_weight_estimates = True
my_config.use_forecast_div_mult_estimates = True
my_system = System(
[my_account, fcs, my_rules, combiner, raw_data, position_size], data, my_config
)
# this is a bit slow, better to know what's going on
my_system.set_logging_level("on")
print(my_system.combForecast.get_forecast_weights("US10").tail(5))
print(my_system.combForecast.get_forecast_diversification_multiplier("US10").tail(5))
# fixed:
my_config.forecast_weights = dict(ewmac8=0.5, ewmac32=0.5)
my_config.forecast_div_multiplier = 1.1
my_config.use_forecast_weight_estimates = False
my_config.use_forecast_div_mult_estimates = False
combiner = ForecastCombine()
my_system = System(
[fcs, empty_rules, combiner, raw_data, position_size], data, my_config
) # no need for accounts if no estimation done
my_system.combForecast.get_combined_forecast("EDOLLAR").tail(5)
# size positions
possizer = PositionSizing()
my_config.percentage_vol_target = 25
my_config.notional_trading_capital = 500000
my_config.base_currency = "GBP"
my_system = System([fcs, my_rules, combiner, possizer, raw_data], data, my_config)
print(my_system.positionSize.get_price_volatility("EDOLLAR").tail(5))
print(my_system.positionSize.get_block_value("EDOLLAR").tail(5))
print(my_system.positionSize.get_underlying_price("EDOLLAR"))
print(my_system.positionSize.get_instrument_value_vol("EDOLLAR").tail(5))
print(my_system.positionSize.get_volatility_scalar("EDOLLAR").tail(5))
print(my_system.positionSize.get_vol_target_dict())
print(my_system.positionSize.get_subsystem_position("EDOLLAR").tail(5))
# portfolio - estimated
from systems.portfolio import Portfolios
portfolio = Portfolios()
my_config.use_instrument_weight_estimates = True
my_config.use_instrument_div_mult_estimates = True
my_config.instrument_weight_estimate = dict(method="shrinkage", date_method="in_sample")
my_system = System(
[my_account, fcs, my_rules, combiner, possizer, portfolio, raw_data],
data,
my_config,
)
my_system.set_logging_level("on")
print(my_system.portfolio.get_instrument_weights().tail(5))
print(my_system.portfolio.get_instrument_diversification_multiplier().tail(5))
# or fixed
portfolio = Portfolios()
my_config.use_instrument_weight_estimates = False
my_config.use_instrument_div_mult_estimates = False
my_config.instrument_weights = dict(US10=0.1, EDOLLAR=0.4, CORN=0.3, SP500=0.2)
my_config.instrument_div_multiplier = 1.5
my_system = System(
[fcs, my_rules, combiner, possizer, portfolio, raw_data], data, my_config
)
print(my_system.portfolio.get_notional_position("EDOLLAR").tail(5))
"""
Have we made some dosh?
"""
my_system = System(
[fcs, my_rules, combiner, possizer, portfolio, my_account, raw_data],
data,
my_config,
)
profits = my_system.accounts.portfolio()
print(profits.percent.stats())
# have costs data now
print(profits.gross.percent.stats())
print(profits.net.percent.stats())
"""
Another approach is to create a config object
"""
my_config = Config(
dict(
trading_rules=dict(ewmac8=ewmac_8, ewmac32=ewmac_32),
instrument_weights=dict(US10=0.1, EDOLLAR=0.4, CORN=0.3, SP500_micro=0.2),
instrument_div_multiplier=1.5,
forecast_scalars=dict(ewmac8=5.3, ewmac32=2.65),
forecast_weights=dict(ewmac8=0.5, ewmac32=0.5),
forecast_div_multiplier=1.1,
percentage_vol_target=25.00,
notional_trading_capital=500000,
base_currency="GBP",
)
)
print(my_config)
my_system = System(
[
Account(),
Portfolios(),
PositionSizing(),
ForecastCombine(),
ForecastScaleCap(),
Rules(),
RawData(),
],
data,
my_config,
)
print(my_system.portfolio.get_notional_position("EDOLLAR").tail(5))
"""
... or to import one
"""
my_config = Config("systems.provided.example.simplesystemconfig.yaml")
print(my_config)
my_system = System(
[
Account(),
Portfolios(),
PositionSizing(),
ForecastCombine(),
ForecastScaleCap(),
Rules(),
RawData(),
],
data,
my_config,
)
print(my_system.rules.get_raw_forecast("EDOLLAR", "ewmac32").tail(5))
print(my_system.rules.get_raw_forecast("EDOLLAR", "ewmac8").tail(5))
print(my_system.forecastScaleCap.get_capped_forecast("EDOLLAR", "ewmac32").tail(5))
print(my_system.forecastScaleCap.get_forecast_scalar("EDOLLAR", "ewmac32"))
print(my_system.combForecast.get_combined_forecast("EDOLLAR").tail(5))
print(my_system.combForecast.get_forecast_weights("EDOLLAR").tail(5))
print(my_system.positionSize.get_subsystem_position("EDOLLAR").tail(5))
print(my_system.portfolio.get_notional_position("EDOLLAR").tail(5))