-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path03-custom-optimization-metrics.py
46 lines (37 loc) · 1.18 KB
/
03-custom-optimization-metrics.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
import datetime
import pandas_ta as ta
import pandas as pd
from backtesting import Backtest
from backtesting import Strategy
from backtesting.lib import crossover
from backtesting.test import GOOG
def optim_func(series):
if series['# Trades'] < 10:
return -1
else:
return series['Equity Final [$]']/series['Exposure Time [%]']
class RsiOscillator(Strategy):
upper_bound = 70
lower_bound = 30
rsi_window = 14
# Do as much initial computation as possible
def init(self):
self.rsi = self.I(ta.rsi, pd.Series(self.data.Close), self.rsi_window)
# Step through bars one by one
# Note that multiple buys are a thing here
def next(self):
if crossover(self.rsi, self.upper_bound):
self.position.close()
elif crossover(self.lower_bound, self.rsi):
self.buy()
bt = Backtest(GOOG, RsiOscillator, cash=10_000, commission=.002)
stats = bt.optimize(
upper_bound = range(55, 85, 5),
lower_bound = range(10, 45, 5),
rsi_window = range(10, 30, 2),
maximize = optim_func,
constraint = lambda param: param.upper_bound > param.lower_bound,
# Random Grid search
#max_tries = 100
)
bt.plot()