/
backtest_portfolio.py
254 lines (226 loc) · 10.9 KB
/
backtest_portfolio.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
#coding=utf8
import datetime
import numpy as np
import pandas as pd
import talib as ta
from zipline.api import order, record, symbol
from zipline.utils.factory import load_bars_from_yahoo
from zipline/utils/calendars/calendar_utils import get_calendar, register_calendar
def prepare_data(tickers, start='', end=''):
assert isinstance(tickers, list)
if not end:
now = datetime.datetime.now()
end = now.strftime('%Y%m%d')
if not start:
start = (datetime.datetime.strptime(end, '%Y%m%d')+datetime.timedelta(days=-365*5)).strftime('%Y%m%d')
tmpStart = datetime.datetime.strptime(start, '%Y%m%d')
ystart = datetime.datetime(tmpStart.year, tmpStart.month, tmpStart.day, 0, 0, 0 ,0, pytz.utc)
tmpEnd = datetime.datetime.strptime(end, '%Y%m%d')
yend = datetime.datetime(tmpEnd.year, tmpEnd.month, tmpEnd.day, 0, 0, 0, 0, pytz.utc)
stockDf = load_bars_from_yahoo(stocks=tickers, start=ystart, end=yend, adjusted=True)
return stockDf.dropna()
def predict(inputHisDf, windowSize=20):
closeSeries = inputHisDf.tail(windowSize)['close']
std = closeSeries.std()
stdRate = std/closeSeries.mean()
if std < 0.05 or std > 50 or stdRate < 0.005 or stdRate > 0.12:
return 0
trendWindow = 10
stdGap = 1.25 + (stdRate-0.005)/(0.12-0.005)
hisDf = inputHisDf.tail(windowSize+1)
pU, pM, pL = ta.BBANDS(hisDf['close'].head(windowSize).astype(float).values, timeperiod=trendWindow, nbdevup=stdGap, nbdevdn=stdGap)
volU, volM, volL = ta.BBANDS(hisDf['volume'].head(windowSize).astype(float).values, timeperiod=trendWindow, nbdevup=stdGap, nbdevdn=stdGap)
cci = ta.CCI(hisDf['high'].astype(float).values, hisDf['low'].astype(float).values, hisDf['close'].astype(float).values, timeperiod=trendWindow)
preP = hisDf['close'].iat[-2]
curP = hisDf['close'].iat[-1]
preV = hisDf['volume'].iat[-2]
curV = hisDf['volume'].iat[-1]
pUSlope = _array_slope(pU[-trendWindow:])
pMSlope = _array_slope(pM[-trendWindow:])
volUSlope = _array_slope(volU[-trendWindow:])
volMSlope = _array_slope(volM[-trendWindow:])
if cci[-1] < -100:
if curP > pL[-1] and preP < pL[-1]:
return 1
if curP < pU[-1] and preP > pU[-1]:
return -1
'''
if pUSlope > 0 and pMSlope > 0 and pUSlope-pMSlope > 0: #goes upper with larger std
if curP > pU[-1] and preP < pU[-1]:
return 1
elif curP < pU[-1] and preP > pU[-1]:
return -1
elif curP < pL[-1] and preP > pL[-1]:
return -1
elif curP > pL[-1] and preP < pL[-1]:
return 1
elif pUSlope < 0 and pMSlope < 0 and pUSlope-pMSlope < 0: #goes down with small std
if curP > pL[-1] and preP < pL[-1]:
return 1
elif curP < pL[-1] and preP > pL[-1]:
return -1
if volUSlope > 0 and volMSlope > 0 and volUSlope-volMSlope > 0:
if curP > pL[-1] and preP < pL[-1]:
return 1
if curP > pM[-1] and pMSlope > 0:
return 1
if curP < pU[-1] and preP > pU[-1]:
return -1
if curP < pM[-1] and pMSlope < 0:
return -1
elif volUSlope < 0 and volMSlope < 0 and volUSlope-volMSlope < 0:
if curP < pU[-1] and preP > pU[-1]:
return -1
'''
return 0
def _array_slope(series):
if isinstance(series, list) or isinstance(series, np.ndarray):
series = pd.Series(series)
assert isinstance(series, pd.Series)
X = pd.Series(range(len(series)))
return X.corr(series)
def initialize(context, xdata=None, xticker=None, xstart='20130101', xend='20161207', window=20, mincorr=0.9):
context.ticker = xticker
context.i = 0
context.window = window
context.mincorr = mincorr
context.pos = dict(zip(context.ticker, [0]*len(context.ticker)))
context.pos_bar = dict(zip(context.ticker, [0]*len(context.ticker)))
context.pos_price = dict(zip(context.ticker, [0]*len(context.ticker)))
context.max_profit = 0.1
context.max_loss = 0.05
context.max_hold = 3
context.max_position_rate = 0.9
if xdata is not None:
context.data = xdata
else:
context.data = prepare_data(context.ticker, start=xstart, end=xend)
def handle_data(context, data):
# Skip first 300 days to get full windows
context.i += 1
if context.i < 500:
return
for t in context.ticker:
handle_data_func(context, t, data)
def handle_data_func(context, t, data):
curSym = symbol(t)
df = context.data[t]
df = df[df.index <= np.datetime64(data.current_dt)]
pred = predict(df, windowSize=context.window)
curLot = int(context.portfolio.starting_cash*context.max_position_rate/len(context.ticker)/data.current(curSym, 'price'))
if pred > 0:
if context.pos[t] <= 0:
order(curSym, curLot)
context.pos[t] += curLot
context.pos_bar[t] = context.i
context.pos_price[t] = data.current(curSym, 'price')
elif pred < 0:
if context.pos[t] > 0:
order(curSym, -1*context.pos[t])
context.pos[t] = 0
context.pos_bar[t] = 0
context.pos_price[t] = 0
else:
if context.pos[t] > 0:
if context.i - context.pos_bar[t] > context.max_hold:
order(curSym, -1*context.pos[t])
context.pos[t] = 0
context.pos_bar[t] = 0
context.pos_price[t] = 0
if context.pos[t] > 0:
ret = data.current(curSym, 'high')/context.pos_price[t] -1
if ret > context.max_profit or ret < - context.max_loss:
order(curSym, -1*context.pos[t])
context.pos[t] = 0
context.pos_bar[t] = 0
context.pos_price[t] = 0
# Save values for later inspection
kargs = {t:data.current(curSym, "price"),
'pred':pred*5,
'pos':context.pos,
}
record(**kargs)
# Note: this function can be removed if running
# this algorithm on quantopian.com
def analyze(results=None, symbol=None):
import matplotlib.pyplot as plt
import logbook
logbook.StderrHandler().push_application()
log = logbook.Logger('Algorithm')
fig = plt.figure()
ax1 = fig.add_subplot(211)
results.portfolio_value.plot(ax=ax1)
ax1.set_ylabel('Portfolio value (USD)')
ax2 = fig.add_subplot(212)
ax2.set_ylabel('Price (USD)')
# If data has been record()ed, then plot it.
# Otherwise, log the fact that no data has been recorded.
if (symbol in results):
results[symbol].plot(ax=ax2)
#results[['short_mavg', 'long_mavg']].plot(ax=ax2)
trans = results.ix[[t != [] for t in results.transactions]]
buys = trans.ix[[t[0]['amount'] > 0 for t in
trans.transactions]]
sells = trans.ix[
[t[0]['amount'] < 0 for t in trans.transactions]]
#ax2.plot(buys.index, results.short_mavg.ix[buys.index], '^', markersize=10, color='m')
#ax2.plot(sells.index, results.short_mavg.ix[sells.index], 'v', markersize=10, color='k')
results.pred.plot(ax=ax2)
#results.pos.plot(ax=ax2)
ax2.plot(buys.index, results['pred'].ix[buys.index], '^', markersize=10, color='r')
ax2.plot(sells.index, results['pred'].ix[sells.index], 'v', markersize=10, color='g')
plt.legend(loc=0)
print results
else:
msg = 'short_mavg & long_mavg data not captured using record().'
ax2.annotate(msg, xy=(0.1, 0.5))
log.info(msg)
plt.show()
def load_t(trading_day, trading_days, bm_symbol):
# dates = pd.date_range('2001-01-01 00:00:00', periods=365, tz="Asia/Shanghai")
bm = pd.Series(data=np.random.random_sample(len(trading_days)), index=trading_days)
tr = pd.DataFrame(data=np.random.random_sample((len(trading_days), 7)), index=trading_days,
columns=['1month', '3month', '6month', '1year', '2year', '3year', '10year'])
return bm, tr
'''
equities_t = make_simple_equity_info(sids=[6001, 6002], start_date=pd.to_datetime("2001-01-01 00:00:00"),
end_date=pd.to_datetime("2002-01-01 00:00:00"), symbols=['AA', 'CC'])
exchanges_t = pd.DataFrame({'exchange': ['HS_SZ'], 'timezone': ['Asia/Shanghai']})
'''
trading.environment = TradingEnvironment(load=load_t, bm_symbol='^HSI', exchange_tz='Asia/Shanghai')
'''trading.environment.write_data(equities=equities_t, exchanges=exchanges_t)'''
# Bug in code doesn't set tz if these are not specified
# (finance/trading.py:SimulationParameters.calculate_first_open[close])
# .tz_localize("Asia/Shanghai").tz_convert("UTC")
a = pd.to_datetime("2001-01-01 00:00:00").tz_localize("Asia/Shanghai")
sim_params = create_simulation_parameters(
start=pd.to_datetime("2001-01-01 00:00:00").tz_localize("Asia/Shanghai"),
end=pd.to_datetime("2001-09-21 00:00:00").tz_localize("Asia/Shanghai"),
data_frequency="daily", emission_rate="daily", env=trading.environment)
algor_obj = TradingAlgorithm(initialize=initialize, handle_data=handle_data,
sim_params=sim_params, env=trading.environment)
if __name__ =='__main__':
import sys
import pytz
import matplotlib.pyplot as plt
from zipline import TradingAlgorithm
from zipline.utils.factory import load_from_yahoo
import argparse
parser = argparse.ArgumentParser(description='predict/test using similarity-prediction')
parser.add_argument('-t', '--ticker', action='store', default='AAPL', help='tickers to predict/test')
parser.add_argument('-m', '--mamethod', action='store', choices=['ema','ma'], default='ema', help='ma method to pre-process the Close/Volume')
parser.add_argument('-p', '--maperiod', action='store', type=int, default=20, help='period to ma Close/Volume')
parser.add_argument('-w', '--window', action='store', type=int, default=20, help='window size to match')
parser.add_argument('-a', '--lookahead', action='store', type=int, default=1, help='days to lookahead when predict')
parser.add_argument('-c', '--mincorr', action='store', type=float, default=0.9, help='days to lookahead when predict')
parser.add_argument('-b', '--begin', action='store', type=str, default='20100101', help='start of the market data')
parser.add_argument('-e', '--end', action='store', type=str, default='20161221', help='end of the market data')
args = parser.parse_args()
#start = datetime.datetime(1990, 1, 1, 0, 0, 0, 0, pytz.utc)
#end = datetime.datetime(2002, 1, 1, 0, 0, 0, 0, pytz.utc)
#data = load_from_yahoo(stocks=['AAPL'], indexes={}, start=start, end=end, adjusted=False)
tickers = [t.strip() for t in args.ticker.split(',') if t.strip()]
data = prepare_data(tickers, start=args.begin, end=args.end)
algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data, capital_base=50000, xdata=data, xticker=tickers, xstart=args.begin, xend=args.end, window=args.window)
res = algo.run(data).dropna()
analyze(res, tickers[0])