-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstrategy_future_selection.py
209 lines (180 loc) · 7.86 KB
/
strategy_future_selection.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
# from models.numeric import (
# ArimaRaw,
# ArimaLinear,
# ArimaNoTrend,
# ArimaLinearNoTrend,
# )
from models.categorical import (
LogRegWrapper,
RFWrapper,
XGBWrapper
)
from strategy import (
basic_strategy,
long_only,
short_only,
fixed_threshold_strategy,
perc_threshold_strategy,
futures_only,
futures_hold,
cash_and_futures,
futures_subset
)
import utils
import pickle
import numpy as np
import pandas as pd
from tqdm import tqdm
# Load saved models
SAVED_MODELS = {
# 'logreg': LogRegWrapper,
# 'rf': RFWrapper,
'xgb': XGBWrapper
}
LOADED_MODELS = {}
for name, model in SAVED_MODELS.items():
print(f'loading {name} from {model.SAVED_DIR}...')
for future in utils.futuresList:
pickle_path = f'{model.SAVED_DIR}/{future}.p'
try:
with open(pickle_path, 'rb') as f:
LOADED_MODELS[name, future] = pickle.load(f)
except:
raise FileNotFoundError(f'No saved {name} for {future}!')
def myTradingSystem(DATE, OPEN, HIGH, LOW, CLOSE, VOL, USA_ADP, USA_EARN,\
USA_HRS, USA_BOT, USA_BC, USA_BI, USA_CU, USA_CF, USA_CHJC, USA_CFNAI,\
USA_CP, USA_CCR, USA_CPI, USA_CCPI, USA_CINF, USA_DFMI, USA_DUR,\
USA_DURET, USA_EXPX, USA_EXVOL, USA_FRET, USA_FBI, USA_GBVL, USA_GPAY,\
USA_HI, USA_IMPX, USA_IMVOL, USA_IP, USA_IPMOM, USA_CPIC, USA_CPICM,\
USA_JBO, USA_LFPR, USA_LEI, USA_MPAY, USA_MP, USA_NAHB, USA_NLTTF,\
USA_NFIB, USA_NFP, USA_NMPMI, USA_NPP, USA_EMPST, USA_PHS, USA_PFED,\
USA_PP, USA_PPIC, USA_RSM, USA_RSY, USA_RSEA, USA_RFMI, USA_TVS, USA_UNR,\
USA_WINV, exposure, equity, settings):
''' This system uses trend following techniques to allocate capital into the desired equities'''
# Load standardized data
date_index = pd.to_datetime(DATE, format='%Y%m%d')
data = dict()
# Data + preprocessing and indicators
for i, future in enumerate(utils.futuresList):
# Slice data by futures
df = pd.DataFrame({
'OPEN': OPEN[:, i],
'HIGH': HIGH[:, i],
'LOW': LOW[:, i],
'CLOSE': CLOSE[:, i],
'VOL': VOL[:, i],
}, index=date_index)
# Replace nan and 0 values with previous day's data (ffill)
df = df.replace(0, np.nan)
df = df.fillna(method="ffill")
# ARIMA: Velocity and acceleration terms for linearized data
df = utils.linearize(df, old_var='CLOSE', new_var='CLOSE_LINEAR')
df = utils.detrend(df, old_var='CLOSE_LINEAR', new_var='CLOSE_VELOCITY')
df = utils.detrend(df, old_var='CLOSE_VELOCITY', new_var='CLOSE_ACCELERATION')
# CATEGORICAL: Preprocessed features
df = utils.percentage_diff(df, old_var='CLOSE', new_var='CLOSE_PCT')
df = utils.diff(df, old_var='CLOSE', new_var='CLOSE_DIFF')
df = utils.percentage_diff(df, old_var='CLOSE_LINEAR', new_var='CLOSE_LINEAR_PCT')
df = utils.percentage_diff(df, old_var='VOL', new_var='VOL_PCT')
df = utils.diff(df, old_var='VOL', new_var='VOL_DIFF')
df = utils.linearize(df, old_var='VOL', new_var='VOL_LINEAR')
df = utils.detrend(df, old_var='VOL_LINEAR', new_var='VOL_VELOCITY')
df = utils.percentage_diff(df, old_var='VOL_LINEAR', new_var='VOL_LINEAR_PCT')
# CATEGORICAL: Y var
df = utils.long_short(df, old_var='CLOSE_DIFF', new_var='LONG_SHORT')
# TECHNICAL INDICATORS
df = utils.SMA(df, input='CLOSE', output='SMA20', periods=20)
df = utils.EMA(df, input='CLOSE', output='EMA20', periods=20)
df = utils.MACD(df, input='CLOSE', output='MACD')
df = utils.RSI(df, input='CLOSE', output='RSI14', periods=14)
df = utils.ATR(df, input=['HIGH', 'LOW', 'CLOSE'], output='ATR', periods=14)
df = utils.VPT(df, input=['CLOSE', 'VOL'], output='VPT')
df = utils.BBands(df, input='CLOSE', output=['BBANDS_HIGH', 'BBANDS_LOW'], periods=14)
df = utils.CCI(df, input=['HIGH', 'LOW', 'CLOSE'], output='CCI', periods=20)
df = df.replace(np.inf, np.nan)
df = df.fillna(method="ffill")
data[future] = df
# Economic indicators
indicators = pd.DataFrame(
data = np.hstack([
USA_ADP, USA_EARN,\
USA_HRS, USA_BOT, USA_BC, USA_BI, USA_CU, USA_CF, USA_CHJC, USA_CFNAI,\
USA_CP, USA_CCR, USA_CPI, USA_CCPI, USA_CINF, USA_DFMI, USA_DUR,\
USA_DURET, USA_EXPX, USA_EXVOL, USA_FRET, USA_FBI, USA_GBVL, USA_GPAY,\
USA_HI, USA_IMPX, USA_IMVOL, USA_IP, USA_IPMOM, USA_CPIC, USA_CPICM,\
USA_JBO, USA_LFPR, USA_LEI, USA_MPAY, USA_MP, USA_NAHB, USA_NLTTF,\
USA_NFIB, USA_NFP, USA_NMPMI, USA_NPP, USA_EMPST, USA_PHS, USA_PFED,\
USA_PP, USA_PPIC, USA_RSM, USA_RSY, USA_RSEA, USA_RFMI, USA_TVS, USA_UNR,\
USA_WINV,
]),
index = date_index,
columns = utils.keys,
)
indicators = indicators.fillna(method="ffill")
indicators = utils.percentage_diff(indicators,
old_var = utils.keys,
new_var = [key+"_PCT" for key in utils.keys],
)
indicators = utils.diff(
indicators,
old_var = utils.keys,
new_var = [key+"_DIFF" for key in utils.keys],
)
for future, df in data.items():
future_df = data[future].join(indicators)
future_df = future_df.fillna(method="ffill")
future_df = future_df.fillna(0)
data[future] = future_df
# Fit and predict
prediction = pd.DataFrame(index=utils.futuresList)
for (name, future), model in tqdm(settings['models'].items()):
prediction.loc[future, name] = model.predict(data, future)
sign = utils.sign(prediction)
magnitude = utils.magnitude(prediction)
# Futures strategy (Allocate position based on predictions)
model = prediction.columns[0] # Arbitrarily pick first model in case of multiple
position = basic_strategy(sign[model], magnitude[model])
# Cash-futures strategy
# if not settings['subset']:
# position = futures_only(position)
# else:
# position = futures_subset(position, subset_csv=settings['subset'])
position = futures_subset(position, subset_csv=settings['subset'])
# Update persistent data across runs
settings['sign'].append(sign)
settings['magnitude'].append(magnitude)
settings['previous_position'] = position
# Yay!
return position, settings
def mySettings():
''' Define your trading system settings here '''
settings= {}
settings['markets'] = utils.futuresAllList
settings['beginInSample'] = '20190123' # '20181020'
settings['endInSample'] = '20210331' # '20201231'
settings['lookback']= 504
settings['budget']= 10**6
settings['slippage']= 0.05
# Stuff to persist
settings['models'] = LOADED_MODELS
settings['sign'] = []
settings['magnitude'] = []
settings['previous_position'] = []
settings['subset'] = 'model_metrics/future_subset/xgb_pct_tech.csv' # None
return settings
# Evaluate trading system defined in current file.
if __name__ == '__main__':
import quantiacsToolbox
## FIRST RUN (COMMENT OUT AFTER FIRST RUN) ##
## CHANGE settings['subset'] TO None ##
# results = quantiacsToolbox.runts(__file__, plotEquity=False)
# futureResults = utils.market_stats(results)
# futureResults["trade"] = [1 if x > 0 else 0 for x in futureResults.sharpe]
# futureResults.to_csv(f'model_metrics/future_subset/xgb_pct_tech.csv')
## SECOND RUN ##
## CHANGE settings['subset'] TO FILE LOCATION e.g. 'model_metrics/future_subset/logreg_pct_macro.csv'
results = quantiacsToolbox.runts(__file__, plotEquity=True)
print("sharpe:", results["stats"]["sharpe"])
futureResults = utils.market_stats(results)
futureResults["trade"] = [1 if x > 0 else 0 for x in futureResults.sharpe]
futureResults.to_csv(f'model_metrics/future_subset/xgb_pct_tech_test.csv')