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
-
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()
-
See this other post which probably will solve your situation: Community - Problem with the simple sample
-
Additionally: at the start of backtesting the
len
of any data will be0
. Thelen
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.