Backtrader Community

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

    YahooFinanceData

    General Code/Help
    4
    3
    4545
    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.
    • H
      hairyblackhole last edited by

      I'm trying to get started with Backtrader and I want to use YahooFinanceData to download some data. However, it seems to not be downloading anything. In my strategy, len(self.datas[0]) is equal to zero. I've pasted the full code below. My code is nearly identical to the Quickstart, but with YahooFinanceData instead of YahooFinanceCSVData. Any ideas?

      Thanks,

      from __future__ import (absolute_import, division, print_function, unicode_literals)
      import backtrader as bt
      import datetime
      
      class PrintPrices(bt.Strategy):
      
          def log(self, txt, dt=None):
              dt = dt or self.datas[0].datetime.date(0)
              print('%s, %s' % (dt.isoformat(), txt))
      
          def __init__(self):
              self.dataclose = self.datas[0].close
              print('%d' % len(self.datas[0]))
      
          def next(self):
              self.log('Close, %.2f' % self.dataclose[0])
      
      
      if __name__ == '__main__':
          cerebro = bt.Cerebro()
          cerebro.addstrategy(PrintPrices)
          cerebro.broker.setcash(100000.0)
      
          voo_data = bt.feeds.YahooFinanceData(
              dataname = 'VOO',
              name = 'VOO',
              fromdate = datetime.datetime(2015,1,1),
              todate = datetime.datetime(2017,3,1),
              reverse = False
          )
      
          cerebro.adddata(voo_data)
          cerebro.run()
      
      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        See this other post which probably will solve your situation: Community - Problem with the simple sample

        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          Additionally: at the start of backtesting the len of any data will be 0. The len increases with the delivery of each bar.

          Other platforms choose to give you an index to the current bar being delivered. Using len is meant to be pythonic.

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