When plotting the EMA12 and EMA26, indicate when the two EMA cross
-
Hello, I based my current code on the quickstart code. For now I plotted the two EMA, but for my research I would need to indicate on the graph when the two lines are crossing. I checked the documentation but I was unable to find my answer. Is there any solution to this? Thanks!
-
Hello, Data-analyst. For your problem, I would suggest making some changes in your 'method'. Next is initiated to iterate over the lines of data and execute your logic at those data points. This should highlight all the cross over points. The important thing to remember is that BT is more of a logic manipulator than a chart manipulator so your focus should be towards manipulating data to highlight these areas.
self.log('Close, %.2f' % self.dataclose[0])
if self.order: # checking for pending orders return if not self.position: if self.sma[0] < self.sma2[0]: # fast ema12 crossover slow ema26 self.log('BUY CREATE, %.2f' % self.dataclose[0]) #just a log function self.order = self.buy() #initiate the buy else: if self.sma[0] > self.sma2[0]: #and then sell the position when the slow crosses over the fast self.order = self.sell()
-
You can also use the MA_CrossOver strategy class in the bt. Strategy parent. Just change the init attributes to accommodate for the EMA function. https://github.com/backtrader/backtrader/blob/master/backtrader/strategies/sma_crossover.py