-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter_three.py
More file actions
51 lines (41 loc) · 1.97 KB
/
chapter_three.py
File metadata and controls
51 lines (41 loc) · 1.97 KB
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
'''
- We are going to learn , how to implement constraints while strategy optimization.
- As you can see from the code, we are trying to optimize the parameters for our RsiOscillator strategy.
- But as you can see, the upperBound values & lowerBound values are same. But it does not make sense, because we
want that upperBound value should be greater than lowerBound values for all combinations of upperBound & lowerBound.
Disadvantages of Over Parameter Optimization
--------------------------------------------
- Overfitting: One of the main challenges in parameter optimization is the risk of overfitting.
When you optimize your strategy parameters based on historical data, there's a possibility that your strategy becomes too specific to the historical dataset and may not perform well on new, unseen data.
- Data Snooping Bias: If you iterate over different combinations of parameters multiple times using the same dataset, you might unintentionally find a set of parameters that work well purely due to chance.
'''
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
class RsiOscillator(Strategy):
upperBound = 70
lowerBound = 30
rsiWindow = 14
def init(self):
self.rsi = self.I(ta.rsi, pd.Series(self.data.Close), self.rsiWindow)
def next(self):
if crossover(self.rsi, self.upperBound):
self.position.close()
elif crossover(self.lowerBound, self.rsi):
self.buy()
bt = Backtest(GOOG, RsiOscillator, cash=10000)
stats = bt.optimize(
upperBound=range(10, 45, 5),
lowerBound=range(10, 45, 5),
rsiWindow=range(10, 30, 2),
maximize='Sharpe Ratio',
# We are ensuring to only look the combination in which upperBound values are greater than lowerBound values.
# We can also make use of rsiWindow in it.
constraint=lambda param: param.upperBound > param.lowerBound
)
print(stats)
bt.plot()