Backtrader Community

    • 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/

    Plot with filled area

    Indicators/Strategies/Analyzers
    observers plotting
    2
    7
    2717
    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.
    • A
      ab_trader last edited by ab_trader

      @backtrader

      I was trying to create filled area plot for the broker value observer. I've sub-classed existing value observer, add zero line, and filled area between value and zero lines, same approach as you used for ichimoku indicator. Finally ended with the following code:

      class ValueFilled(bt.Observer):
      
          lines = ('pnl', 'zero',)
      
          params = (('initial_cash', None),)
      
          plotinfo = dict(plot=True, subplot=True, plotname='Cumulative P/L')
      
          plotlines = dict(pnl=dict(ls='-', linewidth=1.0, color='black',
                                    _fill_gt=('zero', 'green'), _fill_lt=('zero', 'red'),),
                           zero=dict(_plotskip=True,),
          )
      
          def next(self):
      
              self.lines.pnl[0] = self._owner.broker.getvalue() - self.p.initial_cash
              self.lines.zero[0] = 0.0
      

      Is there any easier way to draw filled area under/below the line? In the plot.py I've fond only ax.fill_between method, so created second line above to use it.

      Here is the link to the picture picture
      Somehow pictures inserted into the post usually don't show up.

      • 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
      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        Not now. But it would seem sensible to support not only a line name as the reference to fill (above/below) but also a numeric value.

        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          You may try this commit: https://github.com/mementum/backtrader/commit/8ef8555d4fd5685b4811c848009bcd19e553e4a4

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

            Thank you!

            Since this is re-written, could you please get hard-coded alpha (line 471 of the plot.py) for the fill to 'plotlines' parameter level?

            • 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
            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators last edited by backtrader

              The latest commit to development allows that

              The Docs - Plotting has been updated to reflect the possibilities (you may need to use Shift + F5 to bypass the browser cache)

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

                @backtrader Thank you!

                It seems that you have a typo in the docs: _fill_gt / _less_gt should be _fill_gt / _fill_lt.
                At least latter works.

                • 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
                1 Reply Last reply Reply Quote 0
                • B
                  backtrader administrators last edited by

                  Better in the docs as in the code. Updated

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