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/

    One for the cookbook: On Balance Volume

    Indicators/Strategies/Analyzers
    1
    1
    885
    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.
    • R
      Rudi last edited by

      @backtrader Not developed by myself but a valuable indicator. Credits go to backtest rookies

      class OnBalanceVolume(bt.Indicator):
          alias = 'OBV'
          lines = ('obv')
          params = (
            ('length', 12),
          )
      
          plotlines = dict(
              obv=dict(
                  _name='OBV',
                  color='purple',
                  alpha=0.50
              )
          )
      
          def __init__(self):
      
              # Plot a horizontal Line
              self.plotinfo.plotyhlines = [0]
      
          def next(self):
      
              # Aliases to avoid long lines
              c = self.data.close
              v = self.data.volume
              obv = self.lines.obv
      
              if c[0] > c[-1]:
                  obv[0] = obv[-1] + v[0]
              elif c[0] < c[-1]:
                  obv[0] = obv[-1] - v[0]
              else:
                  obv[0] = obv[-1]
      
      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors