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/

    Datafeed logging starts late

    General Code/Help
    1
    2
    54
    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.
    • G
      Gleetche last edited by

      Hello there,

      I don't know when did this start happening. The script logs the data feed like 50+ bars late. I have tested from 2 other data feed, the problem persists.

      Here's the data settings (As you can see the data should start from at 1st of January 2000:

      data = bt.feeds.YahooFinanceCSVData(
          dataname="data/oracle.csv",
          dtformat=2,
          fromdate=datetime.datetime(2000,1,10),
          todate=datetime.datetime(2001,12,29),
          reverse=False,
          adjclose=False)
      

      Here's the log (You can see that it starts from 22 April 2000):
      a8ff4210-38c9-4970-bd81-034ad1f80af6-image.png

      When I check the chart, it starts from 3rd of January (Close: 29.5) which is the datetime it should start logging from, because there was no 1-2 January 2020 data in the datafeed:
      355aaa42-268b-4ff3-b7df-974dfcdb7339-image.png
      f96a118f-cf63-4450-9005-b16eb1c8a2a3-image.png

      Here's the def next(self) #Location of the logging function

      class TestStrategy(bt.Strategy):
          tradeperiod = 0
          tradecount = 0
      
          params = dict(
              pfast=10,
              pslow=20,
          )
      
          def __init__(self):
              # Keep a reference to the "close" line in the data[0] dataseries
              self.dataclose = self.datas[0].close
      
              self.dc = DonchianChannels()
              # To keep track of pending orders and buy price/commission
              self.order = None
              self.buyprice = None
              self.buycomm = None
              self.mai = None
      
              # SMA use
              sma1 = bt.ind.SMA(period=self.p.pfast)  # fast moving average
              sma2 = bt.ind.SMA(period=self.p.pslow)  # slow moving average
              self.crossover = bt.ind.CrossOver(sma1, sma2)  # crossover signal
      
              # ATR use
              self.dataclose = self.datas[0].close
              self.datahigh = self.datas[0].high
              self.datalow = self.datas[0].low
              self.datapreviousclose = self.datas[0].close(-1)
              self.atr = 0
              # self.my_atr = bt.ind.ATR(period = 14,)
      
          # LOGGING FUNCTION
          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 next(self):
              cash = self.broker.get_cash()
      
              # ATR FUNCTION
              range_total = 0
              for i in range(-13, 1):
                  x = self.datahigh[i] - self.datalow[i]
                  y = self.datahigh[i] - self.datapreviousclose[i]
                  z = self.datalow[i] - self.datapreviousclose[i]
                  if x > y:
                      temp_truerange = x
                  else:
                      temp_truerange = y
      
                  if temp_truerange > z:
                      true_range = temp_truerange
                  else:
                      true_range = z
      
                  range_total += true_range
              self.atr = range_total / 14
      
              # Simply log the closing price of the series from the reference
              self.log('Close: %.2f , ATRx1: %.2f ,Trailing: %.2f' % (
              self.dataclose[0], (self.atr * 1), (self.datapreviousclose - (self.atr * 1))))
      
              # count how many days in tradecount
              if self.position:
                  self.tradeperiod += 1
              else:
                  self.tradeperiod = 0
      
              # Check if an order is pending.. if yes, cant send a 2nd order.
              if self.order:
                  return
      
      

      Anyone knows what is the problem here?

      1 Reply Last reply Reply Quote 0
      • G
        Gleetche last edited by

        62456657-4bb5-40c6-8ebe-7e72f3d3c45c-image.png
        I meant "22 March 2000" not April.

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