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/

    Comparing current data with another at some point in time?

    General Code/Help
    2
    3
    73
    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.
    • V
      Vypy1 last edited by

      I have the below code and have added a custom pandas dataframe with a column called Peaks.

      What I want is to be able to compare the current close of a candle to a previous high. The previous high is set to 0 and is updated with every next and wherever Peaks is 1, previous high is updated to that candles high.

      Since next is called at least once on every candle, my way of comparing is incorrect and what I really want is to be able to compare current candles close to whatever the high of the candle was when Peaks == 1. I am not sure of how to do this, any help will be great.

          def next(self):
              
              prev_high = 0
              if self.data.Peaks[0] == 1.0:
                  prev_high = self.high[0]
              
              if self.close[0] > prev_high:
                  print(f'Date: {self.datas[0].datetime.date()}, Close: {self.close[0]}, Prev High: {prev_high}')
      
      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @Vypy1 last edited by

        @vypy1
        I think you need to set prev_high as a parameter of the strategy. This will allow it to carry forward to the next next. (that sounds odd)

        def __init__(self):
            self.prev_high = 0
        

        Then:

        def next(self):
            if self.data.Peaks[0] == 1.0:
                self.prev_high = self.high[0]
         
            if self.close[0] > self.prev_high:
                print(f'Date: {self.datas[0].datetime.date()}, Close: {self.close[0]}, Prev High: {self.prev_high}')
        

        RunBacktest.com

        V 1 Reply Last reply Reply Quote 1
        • V
          Vypy1 @run-out last edited by

          @run-out That did not sound odd at all :P

          Thanks this works, this is what I was expecting it to do.

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