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/

    Backtest is feeding in data from a future date

    General Code/Help
    3
    4
    139
    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.
    • W
      walt last edited by

      I'm doing some testing on how prices are being loaded in. I have two stocks, one which starts 1/2/2004 and another starts 1/9/2004. Once I load them in to Backtrader, when I step through the code I see that the first time prenext is being called is on date 1/2/2004, which is fine. For the first stock the closing price is self.datas is correct. However for the 2nd stock, which starts 1/9/2004, the closing price which is being shown is the last price in the pricing file, which is from 5/25/2004. Shouldn't it show as NaN?

      vladisld 1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld @walt last edited by

        @walt It would be great if you could share the code, logs and a sample of the data that shows the problem.

        1 Reply Last reply Reply Quote 0
        • W
          walt last edited by

          Sure here's the code

          import backtrader as bt
          import datetime as dt
          import os
          
          class OptionData(bt.feeds.GenericCSVData):
              lines = ('delta',)
          
              params = (
                  ('open',-1),
                  ('high',-1),
                  ('low',-1),
                  ('volume',-1),
                  ('openinterest',-1),
                  ('close',1),
                  ('delta',2),
                  ('dtformat',('%m/%d/%Y')),
              )
          
          # Create a Strategy
          class Screener(bt.Strategy):
              def __init__(self):
                  print('Starting Portfolio Value: %.2f' % self.broker.getvalue())
          
              def prenext(self):
                  # Need to run strategy here since not all datas will have data at the beginning
                  # ! Be mindful that options which have no data will have prices filled in from the future
                  px1 = self.datas[0].close[0] # Value is 695.8 which is wr
                  px2 = self.datas[1].close[0] # Value is 712.04 which is wrong, correct price is 716.6
                                               # 712.04 is the price from 5/25/04
          
          data_folder = r'C:\Users\walte\OneDrive - K Squared Capital\K Squared Capital\Trading Models\Code\Backtrader\Data/'
          all_files = os.listdir(data_folder)
          
          # Create a cerebro entity
          cerebro = bt.Cerebro()
          data = OptionData(dataname = data_folder + 'SPX_C_400_20041218.csv')
          cerebro.adddata(data, name = 'SPX_C_400_20041218.csv')
          data = OptionData(dataname = data_folder + 'SPX_C_400_20040619.csv')
          cerebro.adddata(data, name = 'SPX_C_400_20040619.csv',)
          
          # Add a strategy
          cerebro.addstrategy(Screener)
          
          # Run over everything
          results = cerebro.run()
          

          Data for SPX_C_400_20041218.csv looks like this:
          716bf626-ae4a-43ed-9870-84c574b154a2-image.png

          And for SPX_C_400_20040619.csv:
          6298e85e-e686-43bd-a0a0-041d7c9814b4-image.png
          8e5606ea-9b57-42b5-b478-0378a20634c9-image.png

          vladisld 1 Reply Last reply Reply Quote 0
          • vladisld
            vladisld @walt last edited by

            @walt This behavior is by design although I would agree is somewhat unexpected.

            During first few prenext calls only the first data datas[0] is actually iterated ( since the second data datas[1] is still not started). Getting the 'current' value of the datas[1] doesn't make sense at this point - however the current implementation of GenericCSVData will just return the latest loaded bar value. Other data feed classes may just throw exception for example.

            To be on the safe side I'd suggest to just test that the data was actually iterated before trying to access the current bar:

            if len(self.datas[1]):
               px2 = self.datas[1].close[0]
            

            the len(self.datas[1]) will be zero unless the first bar of the data will be iterated.

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