-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackTest.py
477 lines (359 loc) · 12.9 KB
/
BackTest.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
import pandas
import numpy as np
import talib
class STree():
leaves={}
def __init__(self,name,type='0',*strategies,**typeStrategies):
'''
:param name: name of the tree
:param strategies: list of strategies names:['entry1','entry2',...]
:return:
'''
self.name=name
self.type=type
self.create(type,*strategies,**typeStrategies)
def createParamBranch(self,params,funcParams,**need):
inneedParam=need.copy()
pa=params.copy()
if self.name in funcParams.keys():
for p in funcParams[self.name]:
if p in params:
inneedParam[p]=params[p]
pa.pop(p)
for name in self.leaves.keys():
tree=self.leaves[name]
if isinstance(tree,STree):
tree.createParamBranch(pa,funcParams,**inneedParam)
if len(self.leaves)==0:
self.__init__(self.name,self.type,**inneedParam)
pass
def create(self,type,*strategies,**ts):
leaves={}
nextType= str(int(type)+1) if type.isdigit() else type
if len(strategies)>1:
for s in strategies[0]:
leaves[s] = STree(s,nextType,*strategies[1:],**ts)
elif len(strategies)==1:
for s in strategies[0]:
leaves[s]= STree(s,nextType,**ts)
else:
if len(ts)>0:
t,leave=ts.popitem()
for s in leave:
leaves[s]=STree(s,t,**ts)
self.leaves=leaves.copy()
def showBranchesDict(self,tree,**parentDict):
thisDict=parentDict.copy()
thisDict[tree.type]=tree.name
thisList=[]
for name in tree.leaves.keys():
if isinstance(tree.leaves[name],STree):
next=tree.showBranchesDict(tree.leaves[name],**thisDict)
thisList.extend(next)
else :
thisList.append(thisDict)
if len(tree.leaves)==0:
thisList.append(thisDict)
return thisList
def showBranches(self,tree,*nameList):
thisList=[]
for name in tree.leaves.keys():
if isinstance(tree.leaves[name],STree):
leaveList=list(nameList).copy()
leaveList.append(name)
thisList.extend(tree.showBranches(tree.leaves[name],*leaveList))
else :
thisList.append(nameList)
if len(tree.leaves)<1:
thisList.append(nameList)
return thisList
def showAllCombination(self):
combList=[]
for name in self.leaves.keys():
tree=self.leaves[name]
combList.extend(self.showBranchesDict(tree))
return combList
class Order():
def __init__(self,ticket,code,openPrice,lots,deposit,time,stoplost=0,takeprofit=0,comment=None):
self.code=code
self.openPrice=openPrice
self.lots=lots
self.stoplost=stoplost
self.takeprofit=takeprofit
self.ticket=ticket
self.deposit=deposit
self.openTime=time
self.comment=comment
def close(self,price,time,cls=None):
self.closeTime=time
self.closePrice=price
self.profit=(self.closePrice-self.openPrice)*self.lots
def refresh(self,price,cls):
profit=(price-self.openPrice)*self.lots
if self.takeprofit != 0:
if profit>(self.takeprofit-self.openPrice)*self.lots:
cls.closeOrder(ticket=self.ticket,price=self.takeprofit)
return
if self.stoplost != 0:
if profit<(self.stoplost-self.openPrice)*self.lots:
cls.closeOrder(ticket=self.ticket,price=self.stoplost)
return
self.closePrice=price
self.profit=profit
class Account():
orders=[]
ordersHistory=[]
Time=0
capital=[]
def __init__(self,initCash=1000000,lever=1):
'''
:param initCash: initial cash
:param lever: >1
:return:
How to use:
# create an account:
acc=Account()
'''
self.lever=lever
self.cash=initCash
self.initCash=initCash
self.nextTicket=0
pass
def closeOrder(self,price,order):
'''
:param price:
:param order:
:return:
'''
order.close(price,self.Time)
self.cash=self.cash+order.profit+order.deposit
self.ordersHistory.append(order)
self.orders.remove(order)
def openOrder(self,code,price,lots,stoplost=0,takeprofit=0,ticket=None,comment=None):
'''
open an order
:param price:
:param lots:
:param stoplost:
:param takeprofit:
:param ticket:
:return:
'''
if ticket is None:
ticket=self.nextTicket
deposit=abs(lots*price/self.lever)
order=Order(ticket,code,price,lots,deposit,self.Time,stoplost,takeprofit,comment)
self.cash=self.cash-deposit
self.orders.append(order)
self.nextTicket=ticket+1
def getOrders(self):
attrs=['ticket','code','openPrice','lots','stoplost','takeprofit','deposit']
orders=[]
for o in self.orders:
order=[]
for a in attrs:
order.append(getattr(o,a))
orders.append(order)
return pandas.DataFrame(orders,columns=attrs)
def getHistoryOrders(self):
attrs=['ticket','code','openTime','closeTime','openPrice','closePrice','lots','stoplost','takeprofit','deposit','profit']
orders=[]
for o in self.ordersHistory:
order=[]
for a in attrs:
order.append(getattr(o,a))
orders.append(order)
return pandas.DataFrame(orders,columns=attrs)
def marginLog(self):
log=[self.initCash]
for o in self.ordersHistory:
log.append(log[-1]+o.profit)
return(log)
def refresh(self,time,price):
self.Time=time
for o in self.orders:
o.refresh(price,self)
capital=self.cash
for o in self.orders:
capital=capital+o.profit+o.deposit
self.capital.append([time,capital])
def findOrder(self,value,searchBy='comment',mode='orders'):
for o in getattr(self,mode):
if getattr(o,searchBy)==value:
return o
def findOrders(self,mode='orders',**how):
out=[]
for o in getattr(self,mode):
isOrder=True
for k in how.keys():
if getattr(o,k)!=how[k]:
isOrder=False
break
if isOrder:
out.append(o)
return out
def mergeOrders(self,*args,stoplost=0,takeprofit=0):
code = args[0].code
lots=0
price=0
comment=''
deposit=0
for order in args:
if code != order.code:
print('Cannot merge orders with different code')
return
lots=lots+order.lots
price=price+order.lots*order.price
deposit+=order.deposit
comment='%s_%s_' % (comment,order.comment)
self.orders.remove(order)
newOrder=Order(args[0].ticket,code,price/lots,lots,deposit,self.Time,stoplost,takeprofit,comment)
self.orders.append(newOrder)
class System():
data={}
params={}
funcparam={}
selectorlist=[]
def __init__(self,default='close',lever=1,account=Account()):
self.acc=account
self.setCustomSelector()
self._set_selector()
self._set_funcparam()
self.default=default
self.lever=lever
self.init()
def getPrice(self,name,column,shift=0):
data=self.timeData[name]
return data.get_value(shift,column) if len(data.index)>shift else 0
def getPrices(self,name,array,*columns):
data=self.timeData[name]
return data.loc[array,columns] if len(data.index)>max(array) else 0
def setParams(self,**kwds):
for k in kwds.keys():
self.params[k]=kwds[k]
def importData(self,name,data,maincode=False):
if maincode:
self.code=name
self.data[name]=data
def makeIndicator(self,indicator,time,*values,**params):
value=[]
for v in values:
if isinstance(v,np.ndarray):
value.append(v)
else:
value.append(np.array(v))
ind=getattr(talib,indicator)(*value,**params)
data={'time':time}
if isinstance(ind,tuple):
count=0
for i in ind:
data[count]=i
count+=1
else:
data[0]=ind
ind=pandas.DataFrame(data).dropna()
return ind
def refreshAccount(self,time,price):
self.acc.Time=time
for i in range(0,len(self.acc.orders)):
for k in self.data.keys():
if self.acc.orders[i].code==k:
pass
def runSelector(self,start=None,end=None,**kwds):
'''
:param kwds:
functions: filter='', entry='', exit='', stoplost='', takeprofit='', ...
params: fast=10, slow=20, ...
:return:
'''
for k in kwds.keys():
if k not in self.selectorlist:
setattr(self,k,kwds[k])
self.makeIndicators()
Filter=getattr(self,kwds['Filter'])
Entry=getattr(self,kwds['Entry'])
Exit=getattr(self,kwds['Exit'])
StopLost=getattr(self,kwds['StopLost'],0)
TakeProfit=getattr(self,kwds['TakeProfit'],0)
basicData=self.data[self.code]
for i in basicData.index[start:end]:
price=self.getPrice(self.code,self.default)
self.time=basicData.get_value(i,'time')
self._set_Time_Data()
self.acc.refresh(self.time,price)
for o in self.acc.orders:
if Exit()*o.losts>0:
self.acc.closeOrder(price,o)
direct=Filter()
if direct==0:
continue
if Entry()*direct>0:
self.acc.openOrder(self.code,price,abs(Entry())*direct,StopLost(),TakeProfit())
def simpleStrategy(self):
pass
def runSimple(self,start=None,end=None,**kwds):
for k in kwds.keys():
if k not in self.selectorlist:
setattr(self,k,kwds[k])
self.makeIndicators()
basicData=self.data[self.code]
for i in basicData.index[start:end]:
self.time=basicData.get_value(i,'time')
self._set_Time_Data()
self.acc.refresh(self.time,self.getPrice(self.code,'closeBid')*10000)
self.simpleStrategy()
def optimalizeSimple(self,**params):
paramsTree=STree('params',**params)
paramsComb=paramsTree.showAllCombination()
for pc in paramsComb:
self.runSimple(**pc)
def optimalize(self,start=None,end=None,params=None,funcparam=None,selector=None):
params=self.params if params is None else params
funcparam=self.funcparam if funcparam is None else funcparam
if selector is None:
selector={}
for select in self.selectorlist:
selector[select]=list(getattr(self,select).keys())
sTree=STree('tree',**selector)
sTree.createParamBranch(params,funcparam)
sa=sTree.showAllCombination()
for comb in sa:
self.runSelector(start,end,**comb)
def _set_funcparam(self):
selfAttrs=list(filter(self.pop__,self.__dir__()))
for s in self.selectorlist:
selector=getattr(self,s)
for name in selector.keys():
if '%s_param' % name in selfAttrs:
self.funcparam[name]=getattr(self,'%s_param' % name)
def _set_selector(self):
if self.selectorlist.__len__()==0:
self.selectorlist=['Filter','Entry','Exit']
selfAttrs=list(filter(self.pop__,self.__dir__()))
for s in self.selectorlist:
if not hasattr(self,s):
setattr(self,s,{})
sdict=getattr(self,s)
for a in selfAttrs:
if s in a and s != a :
attr=getattr(self,a)
if isinstance(getattr(self,a),type(self.__init__)):
sdict[a]=attr
def _set_Time_Data(self):
self.timeData={}
for name in self.data.keys():
data=self.data[name]
if isinstance(data,pandas.DataFrame):
data=data[data['time']<=self.time]
data.index=reversed(np.arange(0,data.index.size))
self.timeData[name]=data
def pop__(self,x):
return '__' not in x
def init(self):
pass
def setCustomSelector(self):
pass
def makeIndicators(self):
pass
if __name__ == '__main__':
pass