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/

    Printing High and Low

    General Code/Help
    high low printing
    3
    9
    112
    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.
    • T
      ThisGuySold last edited by

      I need help accessing the high and low values for a current data and N in the past.

      Code:

          print(self.datas[0].low)
          print(self.datas[0].high)
      

      Output:
      <backtrader.linebuffer.LineBuffer object at 0x0000016E46240BC8>
      <backtrader.linebuffer.LineBuffer object at 0x0000016E46240B08>

      Also is aside from the documentation is there a list of the keywords explaining what the different methods do?

      1 Reply Last reply Reply Quote 0
      • Yaroslav Horyslavets
        Yaroslav Horyslavets last edited by

        If you need help you must send all of your code, because i am not magic and can't understand where are you try write these lines:)

        1 Reply Last reply Reply Quote 0
        • T
          ThisGuySold last edited by

          Thanks, I am new to this, here is the link to my files, it is pretty simple since I am starting out. See lines 109 and 110

          https://drive.google.com/open?id=1-iymxk-Y7iAPOtU0X17MxtpNx7PszQFT

          Thanks

          1 Reply Last reply Reply Quote 0
          • run-out
            run-out last edited by

            @ThisGuySold said in Printing High and Low:

            print(self.datas[0].low)
            print(self.datas[0].high)

            Try using :

            print(self.datas[0].low[0])  # For current bar, and 
            print(self.datas[0].low[-1])  # For previous bar an d-2, -3 etc for further back.
            
            1 Reply Last reply Reply Quote 1
            • T
              ThisGuySold last edited by

              I run the code with the changes ([I have updated the drive also), I placed a debug point (breakpoint) on line 112. This is my results after running.

              2000-01-11, SELL CREATE, 24.99
              2000-01-11, 24.35
              2000-01-11, 25.52

              But from my csv file
              24.35 should be 27.36
              25.52 should be 28.69

              Is there something about how backtrader runs that I am missing here?

              1 Reply Last reply Reply Quote 0
              • run-out
                run-out last edited by

                Here is the code from your file.

                 if not self.position: #If we dont have a postition we will then buy
                            if self.dataclose[0] < self.dataclose[-1]:
                                # current close less than previous close
                    
                                if self.dataclose[-1] < self.dataclose[-2]:
                                    # previous close less than the previous previous close
                    
                                    # BUY, BUY, BUY!!! (with all possible default parameters)
                                    self.log('BUY CREATE, %.2f' % self.dataclose[0])
                                    self.order = self.buy() #we store the order status
                                    
                        else: 
                            #Else if we have a position then we need to specify when to sell
                            #Here we choose 5 days after 
                            
                            
                            
                            if len(self) >= (self.bar_executed +  self.params.exitbars): 
                                self.log('SELL CREATE, %.2f' % self.dataclose[0]) #console log
                                self.order = self.sell()
                                
                                
                                
                                
                                self.log(self.datas[0].low[0]) 
                                self.log(self.datas[0].high[0])
                                
                                a1 = 1
                

                Your code

                self.log(self.datas[0].low[0]) 
                self.log(self.datas[0].high[0])
                

                are in a different part of the if statement, so you will get a different bar's data. Please include your code next time, thanks.

                1 Reply Last reply Reply Quote 0
                • T
                  ThisGuySold last edited by

                  Oh okay, Thanks for letting me know, So which part do I need to be to access to the current high, low values? Also suppose I am anywhere in the script is there a way to retrieve the current high, low values? Thanks.

                  1 Reply Last reply Reply Quote 0
                  • run-out
                    run-out last edited by

                    Your code is right, but in the wrong level, it is contained in a second if statement so you willl only get the value when that if condition is met. Just put it on the first line, better when together.

                    def next(): 
                        self.log("High {:.2f}, Low {:.2f}".format(self.datas[0].high[0], self.datas[0].low[0]))
                    

                    Results in:

                    2005-02-02 High 36.34, Low 35.29
                    2005-02-03 High 35.67, Low 35.00
                    2005-02-04 High 35.30, Low 34.71
                    2005-02-07 High 35.19, Low 34.36
                    2005-02-08 High 34.91, Low 34.32
                    2005-02-09 High 34.66, Low 33.45
                    2005-02-10 High 33.72, Low 32.47
                    2005-02-11 High 34.70, Low 33.31
                    2005-02-14 High 34.41, Low 33.78
                    
                    1 Reply Last reply Reply Quote 0
                    • T
                      ThisGuySold last edited by

                      Great, Thanks, I appreciate it!

                      1 Reply Last reply Reply Quote 1
                      • 1 / 1
                      • First post
                        Last post
                      Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                      $(document).ready(function () { app.coldLoad(); }); }