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


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


    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]
            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()
        voo_data = bt.feeds.YahooFinanceData(
            dataname = 'VOO',
            name = 'VOO',
            fromdate = datetime.datetime(2015,1,1),
            todate = datetime.datetime(2017,3,1),
            reverse = False

  • administrators

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

  • administrators

    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.

Log in to reply