Created
November 14, 2021 04:37
-
-
Save dkav6/2901393f2c416a867a37238ffc0ab2d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from backtesting import Backtest, Strategy | |
from backtesting.lib import crossover | |
from backtesting.test import SMA | |
class MLStrategy(Strategy): | |
n1=5 | |
n2=10 | |
def init(self): | |
self.sma1 = self.I(SMA, self.data.Close, self.n1) | |
self.sma2 = self.I(SMA, self.data.Close, self.n2) | |
def next(self): | |
if crossover(self.sma1, self.sma2) and self.data.target == 1: | |
self.buy() | |
elif crossover(self.sma2, self.sma1) and self.data.target == 0: | |
self.position.close() | |
bt = Backtest(data=backtest_df, | |
strategy=MLStrategy, | |
commission=.002, | |
cash=100000) | |
stats = bt.optimize(n1=range(5, 100, 5), | |
n2=range(10, 160, 5), | |
maximize='SQN', | |
constraint=lambda param: param.n1 < param.n2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment