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/

    Observing some difference between actual value and LinesOperation var

    Indicators/Strategies/Analyzers
    1
    2
    99
    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.
    • J
      jsyadav last edited by

      Hi,
      My first post here..

      I am seeing some rounding error when comparing the original data from the csv file vs the value returned from LineOperations in "next()".

      I tried to validate the High, Low, Open, all the fields are showing some delta between the two (original Data and Line Operation)

      import backtrader as bt
      from datetime import datetime
      
      filename = 'AAPL1.csv'
      
      csv_in = open(filename, "r")
      d = []
      for line in csv_in:
          field_list = line.split(',')  # split the line at commas
          d.append(field_list[2])
      csv_in.close()
      
      class SmaCross(bt.Strategy):
      
          def __init__(self):
              pass
      
          def next(self):
              print len(self.data.high),(round(float(d[len(self.data.high)]), 2) - self.data.high[0])
      
      
      cerebro = bt.Cerebro()  # create a "Cerebro" engine instance
      data = bt.feeds.YahooFinanceCSVData(dataname=filename,
                                       fromdate=datetime(2020, 2, 3),
                                       todate=datetime(2020, 2, 19))
      
      cerebro.adddata(data)  # Add the data feed
      cerebro.addstrategy(SmaCross)  # Add the trading strategy
      
      cerebro.run()  # run it all
      

      the AAPL1.CSV file used for this testing contains the following

      Date,Open,High,Low,Close,Adj Close,Volume
      2020-02-03,76.07499694824219,78.37249755859375,75.55500030517578,77.16500091552734,76.63629913330078,173985600
      2020-02-04,78.82749938964844,79.91000366210938,78.40750122070312,79.7125015258789,79.16633605957031,136616400
      2020-02-05,80.87999725341797,81.19000244140625,79.73750305175781,80.36250305175781,79.81189727783203,118826800
      2020-02-06,80.64250183105469,81.30500030517578,80.06500244140625,81.30249786376953,80.74544525146484,105425600
      2020-02-07,80.59249877929688,80.8499984741211,79.5,80.00749969482422,79.64789581298828,117684000
      2020-02-10,78.54499816894531,80.38749694824219,78.4625015258789,80.38749694824219,80.02619171142578,109348800
      2020-02-11,80.9000015258789,80.9749984741211,79.67749786376953,79.90249633789062,79.54336547851562,94323200
      2020-02-12,80.36750030517578,81.80500030517578,80.36750030517578,81.80000305175781,81.4323501586914,113730400
      2020-02-13,81.04750061035156,81.55500030517578,80.8375015258789,81.21749877929688,80.85246276855469,94747600
      2020-02-14,81.18499755859375,81.49500274658203,80.7125015258789,81.23750305175781,80.87237548828125,80113600
      2020-02-18,78.83999633789062,79.9375,78.65249633789062,79.75,79.39155578613281,152531200
      
      

      And the result I see is

      1 0.53
      2 0.55
      3 0.56
      4 0.56
      5 0.36
      6 0.36
      7 0.36
      8 0.37
      9 0.37
      10 0.37
      11 0.36
      
      
      1 Reply Last reply Reply Quote 0
      • J
        jsyadav last edited by

        I found out that the price was divided by the adjfactor that is

        adjfactor = c / adjustedclose
        

        After setting the adjclose as False, the values are what I was looking for.

        data = bt.feeds.YahooFinanceCSVData(dataname=filename,adjclose=False)
        

        All good now.
        Thanks.

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