No error shown but no results, simply double crossover
-
Hello again friends,
I tried a simple strategy with 4 moving averages . MA1 e MA2 for a buy crossover and MA3 and MA4 for a sell/closing crossover .
The main reason is that I want to find eventually optimezed parameters of this 4 moving average.
But when I run no errors and no results... Where I can put some feedback like print or log to see If it is doing backtest or not? I use def stop() but nothing in the output..
code:# Create a Stratey class TestStrategy(bt.Strategy): params = ( ('ma1period', 15), ('ma2period', 50), ('ma3period', 15), ('ma4period', 50)) def log(self, txt, dt=None, doprint=False): ''' Logging function fot this strategy''' if self.params.printlog or doprint: dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close # To keep track of pending orders and buy price/commission self.order = None self.buyprice = None self.buycomm = None # Add a MovingAverageSimple indicator sma1 = bt.indicators.SimpleMovingAverage( self.datas[0], period=self.params.ma1period) sma2 = bt.indicators.SimpleMovingAverage( self.datas[0], period=self.params.ma2period) sma3 = bt.indicators.SimpleMovingAverage( self.datas[0], period=self.params.ma3period) sma4 = bt.indicators.SimpleMovingAverage( self.datas[0], period=self.params.ma4period) self.crossover_b= bt.indicators.CrossOver(sma1, sma2) self.crossover_s= bt.indicators.CrossOver(sma3, sma4)
def next(self): print("sto facendo") # Simply log the closing price of the series from the reference self.log('Close, %.2f' % self.dataclose[0]) # Check if an order is pending ... if yes, we cannot send a 2nd one if self.order: return # Check if we are in the market if not self.position: #if self.rsi[0]< self.params.rsi_treshold: if self.crossover_b > 0: # Not yet ... we MIGHT BUY if ... #if self.dataclose[0] > self.sma1[0]: # BUY, BUY, BUY!!! (with all possible default parameters) self.log('BUY CREATE, %.2f' % self.dataclose[0]) self.buy() else: if self.crossover_s < 0: # in the market & cross to the downside the other two moving average self.close() # close long position
if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() # Add a strategy strats = cerebro.optstrategy( TestStrategy, ma1period=range(10, 12), ma2period=range(20, 22), ma3period=range(10, 12), ma4period=range(20, 22) ) # Add the Data Feed to Cerebro cerebro.adddata(data) # Set our desired cash start cerebro.broker.setcash(1000000) cerebro.addanalyzer(btanalyzers.SharpeRatio, _name = "sharpe") cerebro.addanalyzer(btanalyzers.DrawDown, _name = "drawdown") cerebro.addanalyzer(btanalyzers.Returns, _name = "returns") # Add a FixedSize sizer according to the stake cerebro.addsizer(bt.sizers.FixedSize, stake=0.5) # Set the commission cerebro.broker.setcommission(commission=0.001) # Run over everything back=cerebro.run()
-
I'm new to Backtrader, too, but I may be able to help with this. (It helps my learning, too.)
I managed to get your code to run by doing these things:
Firstly, to get logging to work, add(printLog, True)
to your strategy parameters, or callself.log(txt, True)
.
I reverted to non-optimisation to start with:- Change your strategy parameter default values to integers (not ranges).
- Change
cerebro.optstrategy()
tocerebro.addstrategy()
. - Then if you uadd cerebro.plot() at the end you should see some output.
For optimisation:
- Add the
maxcpus=1
to your Cerebro instance, i.e.back=cerebro.run(maxcpus=1)
. At least my system needs it.
I hope that helps.
-
I just spotted a couple of error in my response:
The parameter to add to the strategy should be('printlog', True)
, notprintLog
.
The call toself.log(txt, doprint=True)
.
T -
@tony-shacklock Hi , Thank you.
Now it works fine. But I thought when it runs blank it suggests some problems with the condition of buy or sell or in position, but those were right . I fell like sometimes backtrader don' t want to work proprely ahah maybe is jupyter notebook.
Anyway now I will do optimazation for this 4 parameters , I' ll let you know results.