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/

    How could I plot Crossover Signal on master window(plotinfo.plotmaster == self.datas[0]) as triangle indicating where it happens??

    General Code/Help
    3
    6
    503
    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.
    • xyshell
      xyshell last edited by xyshell

      Hi everyone.
      Currently I want to plot some Crossover Signal (bt.ind.CrossOver) on the price to gain some insight.
      I want to plot it as triangle (just like the buy and sell point) with probably different colors and directions (up-triangle indicating it's crossing up, down-triangle indicating it's crossing down), just like selling gives a red downside triangle and buying gives a green upside triangle.
      Is there anyone know how to do that?

      1 Reply Last reply Reply Quote 0
      • xyshell
        xyshell last edited by xyshell

        I think I found some part of solution in this article.

        https://www.backtrader.com/blog/posts/2016-12-10-buysellarrows/buysellarrows/

        But this only re-defines buy-sell signal pattern, not exactly what I want.

        1 Reply Last reply Reply Quote 0
        • A
          ab_trader last edited by

          You may want to create custom indicator with two lines up and down which has a CrossOver indicator inside. Something like this (not tested):

          class CrossOverWithArrows(bt.Indicator):
          
              lines = ('up', 'down')
          
              plotlines = dict(
                  up=dict(marker='$\u21E7$', markersize=12.0),
                  down=dict(marker='$\u21E9$', markersize=12.0)
          
              def __init__(self):
                  up = bt.ind.CrossUp(smthng, smthng)
                  down = bt.ind.CrossDown(smthng, smthng)
          
          • If my answer helped, hit reputation up arrow at lower right corner of the post.
          • Python Debugging With Pdb
          • New to python and bt - check this out
          xyshell 2 Replies Last reply Reply Quote 3
          • xyshell
            xyshell @ab_trader last edited by

            @ab_trader Cool, I'll try it latter!

            1 Reply Last reply Reply Quote 0
            • xyshell
              xyshell @ab_trader last edited by

              @ab_trader said in How could I plot Crossover Signal on master window(plotinfo.plotmaster == self.datas[0]) as triangle indicating where it happens??:

              plotlines = dict(
                  up=dict(marker='$\u21E7$', markersize=12.0),
                  down=dict(marker='$\u21E9$', markersize=12.0)
              

              Thanks for your advice, here is what I've done and it works exactly the same as expected.

              import datetime
              
              import backtrader as bt
              
              
              class DoubleSMAWithLoc(bt.Indicator):
              
                  lines = ('fast', 'slow', 'loc')
              
                  params = (
                      ('pfast', 10),
                      ('pslow', 30),
                  )
              
                  plotlines = dict(loc=dict(_plotskip='True',))
              
                  def __init__(self):
                      self.l.fast = bt.ind.SMA(self.data, period=self.p.pfast)
                      self.l.slow = bt.ind.SMA(self.data, period=self.p.pslow)
                      self.l.loc = self.data.close
              
              
              class CrossOverWithArrows(bt.Indicator):
              
                  lines = ('up', 'down')
              
                  plotlines = (
                      ('up', dict(ls='', marker='^', markersize=8.0, color='green', fillstyle='full')),
                      ('down', dict(ls='', marker='v', markersize=8.0, color='red', fillstyle='full'))
                  )
              
                  def __init__(self):
                      self.cross_up = bt.ind.CrossUp(self.data.fast, self.data.slow)
                      self.cross_down = bt.ind.CrossDown(self.data.fast, self.data.slow)
                      self.l.up = bt.If(self.cross_up, self.data.loc, -1)
                      self.l.down = bt.If(self.cross_down, self.data.loc, -1)
              
              
              class SMACross(bt.Strategy):
              
                  params = (
                      ('pfast', 10),
                      ('pslow', 30),
                  )
              
                  def __init__(self):
                      self.sma = DoubleSMAWithLoc(
                          self.datas[0],
                          pfast=self.p.pfast, pslow=self.p.pslow,
                          plotmaster=self.datas[0])
                      self.cross = CrossOverWithArrows(
                          self.sma,
                          plotmaster=self.datas[0])
              
              
              if __name__ == '__main__':
                  cerebro = bt.Cerebro(stdstats=False)
                  cerebro.addstrategy(SMACross)
                  datapath = 'datas/orcl-1995-2014.txt'
              
                  # Create a Data Feed
                  data = bt.feeds.YahooFinanceCSVData(
                      dataname=datapath,
                      # Do not pass values before this date
                      fromdate=datetime.datetime(2000, 1, 1),
                      # Do not pass values before this date
                      todate=datetime.datetime(2000, 12, 31),
                      # Do not pass values after this date
                      reverse=False)
              
                  cerebro.adddata(data)
                  cerebro.addwriter(bt.WriterFile, csv=True, out='temp.csv')
                  cerebro.run()
                  cerebro.plot()
              

              Screen Shot 2020-03-22 at 12.40.37 PM.png

              slashAmoment 1 Reply Last reply Reply Quote 0
              • slashAmoment
                slashAmoment @xyshell last edited by

                @xyshell I think this will be better
                ('up', dict(_skipnan=True, ls='', marker='^', markersize=8.0, color='green', fillstyle='full')),
                ('down', dict(_skipnan=True, ls='', marker='v', markersize=8.0, color='red', fillstyle='full')),
                and
                self.l.up = bt.If(self.cross_up, self.data.loc, float('NaN'))
                self.l.down = bt.If(self.cross_down, self.data.loc,float('NaN'))

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