-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbatch.py
59 lines (54 loc) · 1.76 KB
/
batch.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
import itertools
import pandas as pd
from vix_strategy import Strategy
def main():
var_fields = {
'minLv':[.5,.8,1.1,1.3],
'maxLv':[1.5,1.8,2.1,2.5],
'rollD':[10,20,50,100,250,500],
'sma':[5,20,50,100],
}
combos = [{
'start': '20100101',
'end': None,
'stock': 'SPY',
'benchmark': 'SPY',
'indicator': '^VIX',
'minLv': minLv,
'maxLv': maxLv,
'rollD': rollD,
'sma': sma,
'ininav': 1.0e7,
} for minLv,maxLv,rollD,sma in itertools.product(*tuple(var_fields.values()))]
def trial(combo, i, n):
result = Strategy(**combo).run().evaluate()
cur = {n:result['config'][n] for n in var_fields.keys()}
print(f'{i+1: >5} / {n} | config: {cur}', end=' | ')
return result
results = []
for i,combo in enumerate(combos):
try:
results.append(trial(combo, i, len(combos)))
print('Success', end=' \r')
except Exception as e:
print(str(e))
continue
def present(results):
all = []
for res in results:
data = {}
for key,val in res['config'].items():
data[key] = val
for key,val in res['metrics']['portfolio'].items():
data[key] = val
for key,val in res['metrics']['benchmark'].items():
data[key+'_bm'] = val
all.append(data)
df = pd.DataFrame(all).sort_values(by='sharpe', ascending=False).reset_index(drop=True)
print(df.head(10))
df.to_excel('results.xlsx')
return df
df = present(results)
br()
if __name__ == '__main__':
main()