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/

    Recursive indicator

    General Code/Help
    1
    1
    23
    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.
    • B
      BT.chuck last edited by

      Hi there,

      I am new to bt and need some help. I want self.move to be a trend indicator, and want its value always be the last non-zero value after triggering.

          def __init__(self):
      
              # keep track of close price in the series
              self.data_close = self.datas[0].close
      
      
              # keep track of pending orders/buy price/buy commission
              self.order = None
              self.price = None
              self.comm = None
              self.move = bt.LineNum(0.0)
      
              # add Bollinger Bands indicator and track the buy/sell signals
              self.b_band = bt.ind.BollingerBands(self.data_close, period=self.p.period, devfactor=self.p.devfactor)
              self.upbot = bt.ind.CrossOver(self.data_close, self.b_band.lines.bot)
              self.downtop = bt.ind.CrossOver(self.data_close, self.b_band.lines.top)
              self.move = bt.If(self.upbot>0, 1, bt.If(self.upbot<0, -2, bt.If(self.downtop<0, -1, self.move(-1))))
      

      Currently, it returns the following result to me

      uptop: -1.0, downtop: 0.0, move: -2.0
      uptop: 1.0, downtop: 0.0, move: 1.0
      uptop: 0.0, downtop: 0.0, move: 0.0
      uptop: 0.0, downtop: 0.0, move: 0.0
      

      What I wanted is

      uptop: -1.0, downtop: 0.0, move: -2.0
      uptop: 1.0, downtop: 0.0, move: 1.0
      uptop: 0.0, downtop: 0.0, move: 1.0
      uptop: 0.0, downtop: 0.0, move: 1.0
      

      Please help! Thank you in advance!

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