For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Having trouble printing daily high, low, and close



  • I am running through the program, but am struggling with something simple.
    I want to print the high, low, and close for a day. But only close is appearing correctly.
    Pointing me at some reading explicitly about the data[] or datas[] constructs would also be appreciated.

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    
    import datetime  # For datetime objects
    import os.path  # To manage paths
    import sys  # To find out the script name (in argv[0])
    
    # Import the backtrader platform
    import backtrader as bt
    
    
    # Create a Stratey
    class TestStrategy(bt.Strategy):
    
        def log(self, txt, dt=None):
            ''' Logging function for this strategy'''
            dt = dt or self.datas[0].datetime.date(0)
            print('%s, %s' % (dt.isoformat(), txt))
    
        def __init__(self):
            # Keep a reference to the "close" line in the data[0] dataseries
            self.dataclose = self.datas[0].close
            self.datahigh = self.datas[0].high
            self.datalow = self.datas[0].low
    
        def next(self):
            # Simply log the closing price of the series from the reference
            self.log('Close: %.2f High: %.2f Low: %.2f' % (self.dataclose[0],self.datahigh[0],self.datalow[0]))
    
    
    
    if __name__ == '__main__':
        # Create a cerebro entity
        cerebro = bt.Cerebro()
    
        # Add a strategy
        cerebro.addstrategy(TestStrategy)
    
        # Datas are in a subfolder of the samples. Need to find where the script is
        # because it could have been called from anywhere
        modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
        datapath = os.path.join(modpath, '../../data/backtrader/datas/orcl-1995-2014.txt')
    
        # Create a Data Feed
        data = bt.feeds.YahooFinanceCSVData(
            dataname=datapath,
            # Do not pass values before this date
            fromdate=datetime.datetime(2000, 1, 1),
            # Do not pass values before this date
            todate=datetime.datetime(2000, 12, 31),
            # Do not pass values after this date
            reverse=False)
    
        # Add the Data Feed to Cerebro
        cerebro.adddata(data)
    
        # Set our desired cash start
        cerebro.broker.setcash(100000.0)
    
        # Print out the starting conditions
        print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
    
        # Run over everything
        cerebro.run()
    
        # Print out the final result
        print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())

  • administrators

    @bullyellis said in Having trouble printing daily high, low, and close:

    I want to print the high, low, and close for a day. But only close is appearing correctly.

    May we know what's not actually appearing correctly?

    @bullyellis said in Having trouble printing daily high, low, and close:

    Pointing me at some reading explicitly about the data[] or datas[] constructs would also be appreciated.

    Docs - Platform Concepts



  • Derp. Sorry. My output is below. When I compare this to the actual values in the orcl-1995-2014.txt file, only the close is correct.
    High and low are incorrect and I have no idea where they are coming from.

    2000-12-20, Close: 25.35 High: 26.52 Low: 24.46
    2000-12-21, Close: 26.24 High: 26.91 Low: 24.29
    2000-12-22, Close: 28.35 High: 28.45 Low: 26.68
    2000-12-26, Close: 27.52 High: 28.63 Low: 26.68
    2000-12-27, Close: 27.30 High: 27.63 Low: 26.13
    2000-12-28, Close: 27.63 High: 28.13 Low: 27.02
    2000-12-29, Close: 25.85 High: 27.85 Low: 25.52
    Final Portfolio Value: 100000.00

    Here are the rows from the .txt file
    Date,Open,High,Low,Close,Adj Close,Volume
    2000-12-20,28.062500,29.812500,27.500000,28.500000,25.350471,54440500
    2000-12-21,27.812500,30.250000,27.312500,29.500000,26.239960,46719700
    2000-12-22,30.375000,31.984301,30.000000,31.875000,28.352501,35568200
    2000-12-26,31.500000,32.187500,30.000000,30.937500,27.518599,20589500
    2000-12-27,30.375000,31.062500,29.375000,30.687500,27.296230,26437500
    2000-12-28,30.562500,31.625000,30.375000,31.062500,27.629786,25053600
    2000-12-29,30.875000,31.312500,28.687500,29.062500,25.850811,31702200

    I am using below code to read high low and close:

            self.dataclose = self.datas[0].close
            self.datahigh = self.datas[0].high
            self.datalow = self.datas[0].low

  • administrators

    You obviously don't know what adjusted close is. Check the data again:

    @bullyellis said in Having trouble printing daily high, low, and close:

    Date,Open,High,Low,Close,Adj Close,Volume
    2000-12-20,28.062500,29.812500,27.500000,28.500000,25.350471,54440500
    
    • The close is the 5th value from the left, i.e.: 28.50.
    • The adjusted close is the 6th value from the left, i.e.: 25.35

    With that in mind, I can only suggest you read about what adjusted close is and why it is used (usually what people want) or you want to deactivate that in the data feed, which you can do:

    Quick note: The adjusted close price takes into account splits (in both directions) and dividends, but please ... read and inform yourself.



  • I know what adjusted close is - I took Tucker Bulch's Machine Learning for trading at georgia tech.

    It is the close adjusted for splits, dividends (I think), etc. I just kind of assumed your program used Adj Close by default since that is more useful.
    This isn't helping me though as to why the High and Lows for a given day are different.

    Edit: Oh I see - it modifies the high and low in the same fashion as the adjusted close is versus the close.


  • administrators

    @bullyellis said in Having trouble printing daily high, low, and close:

    Edit: Oh I see - it modifies the high and low in the same fashion as the adjusted close is versus the close.

    Glad you figured it out.

    @bullyellis said in Having trouble printing daily high, low, and close:

    Tucker Bulch's Machine Learning for trading at georgia tech.

    That may be very american, but it has no meaning for me. The only Tucker I know is this one: https://www.imdb.com/title/tt0096316/.

    In any case it would seem the Machine Learning course skips the obvious parts about data input, like how the real world data looks like.



  • Thanks for your help, I really appreciate it.
    The course basically walks you through writing a primitive back tester and explains some basic machine learning and pandas.

    Bad news though, I will continue peppering your forum with questions, but I will make a greater effort to read first.