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) 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.datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): self.dataclose = self.datas.close print('%d' % len(self.datas)) def next(self): self.log('Close, %.2f' % self.dataclose) 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()
See this other post which probably will solve your situation: Community - Problem with the simple sample
Additionally: at the start of backtesting the
lenof any data will be
lenincreases with the delivery of each bar.
Other platforms choose to give you an index to the current bar being delivered. Using
lenis meant to be pythonic.