-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizer.py
84 lines (65 loc) · 2.49 KB
/
optimizer.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
# coding: utf8
# part of pybacktest package: https://github.com/ematvey/pybacktest
""" Optimizer class """
from cached_property import cached_property
from pybacktest.backtest import Backtest
import itertools
import pandas
import numpy
import multiprocessing
def _embedded_backtest(args_tuple):
params, strategy_fn, ohlc, metrics = args_tuple
bt = Backtest(strategy_fn(ohlc, **params))
r = {}
for m in metrics:
r[m] = getattr(bt.stats, m)
r.update(params)
return r
class Optimizer(object):
def __init__(self, strategy_fn, ohlc, params={},
metrics=['pf', 'sharpe', 'maxdd', 'mpi', 'average', 'trades'],
processes=None):
''' `strategy_fn` - Backtest-compatible strategy function.
`ohlc` - Backtest- and strategy-compatible dataframe.
`metrics` - Backtest-compatible set of metrics.
`processes` - pass 1 to use single (this) process, pass None to use
#processes = #cores, or specify exact number of processes to use.
'''
self.strategy_fn = strategy_fn
self.ohlc = ohlc
self.metrics = metrics
assert all([len(p) == 3 for p in list(params.values())]), 'Wrong params specified'
self.params = params.copy()
self.processes = processes
def add_param(self, param, start, stop, step):
self.params[param] = [start, stop, step]
@cached_property
def results(self):
p = self.params
pn = list(p.keys())
results = []
param_space = [
dict(list(zip(pn, pset))) for pset in
itertools.product(
*[numpy.arange(p[k][0], p[k][1]+.000001, p[k][2]) for k in pn]
)
]
args_gen = list(zip(param_space,
itertools.repeat(self.strategy_fn),
itertools.repeat(self.ohlc),
itertools.repeat(self.metrics)))
if self.processes != 1:
pool = multiprocessing.Pool(self.processes)
try:
results = pool.map(_embedded_backtest, args_gen)
except KeyboardInterrupt:
pool.close()
pool.join()
else:
results = []
for a in args_gen:
results.append(_embedded_backtest(a))
return pandas.DataFrame(results)
def best_by(self, name, depth=20):
res = self.results
return res[res[name].notnull()].sort(name, ascending=False).head(depth)