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



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



  • 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:)



  • 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



  • @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.
    


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



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



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



  • 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
    


  • Great, Thanks, I appreciate it!


Log in to reply
 

});