Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    When plotting the EMA12 and EMA26, indicate when the two EMA cross

    Indicators/Strategies/Analyzers
    2
    3
    756
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      data-analyst last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • Robin Dhillon
        Robin Dhillon last edited by

        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()
        
        1 Reply Last reply Reply Quote 0
        • Robin Dhillon
          Robin Dhillon last edited by

          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

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors