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

Problem with YahooFinanceData

  • I am new to backtrader
    I want to reciev data with timeframe=bt.TimeFrame.Minutes

    from __future__ import (absolute_import, division, print_function,
    import datetime  # For datetime objects
    import os.path  # To manage paths
    import sys  # To find out the script name (in argv[0])
    # Import the backtrader platform
    import backtrader as bt
    # Create a Stratey
    class TestStrategy(bt.Strategy):
        def log(self, txt, dt=None):
            ''' Logging function fot this strategy'''
            dt = dt or self.datas[0]
            print('%s, %s' % (dt.isoformat(), txt))
        def __init__(self):
            # Keep a reference to the "close" line in the data[0] dataseries
            self.dataclose = self.datas[0].close
        def next(self):
            # Simply log the closing price of the series from the reference
            self.log('Close, %.2f' % self.dataclose[0])
            if self.dataclose[0] < self.dataclose[-1]:
                # current close less than previous close
                if self.dataclose[-1] < self.dataclose[-2]:
                    # previous close less than the previous close
                    # BUY, BUY, BUY!!! (with all possible default parameters)
                    self.log('BUY CREATE, %.2f' % self.dataclose[0])
    if __name__ == '__main__':
        # Create a cerebro entity
        cerebro = bt.Cerebro()
        # Add a strategy
        # Datas are in a subfolder of the samples. Need to find where the script is
        # because it could have been called from anywhere
        # Create a Data Feed
        data = bt.feeds.YahooFinanceData(
            # Do not pass values before this date
            fromdate=datetime.datetime(2016, 1, 1),
            # Do not pass values before this date
            todate=datetime.datetime(2016, 2, 1),
            timeframe = bt.TimeFrame.Minutes,
            # Do not pass values after this date
        # Add the Data Feed to Cerebro
        # Set our desired cash start
        # Print out the starting conditions
        print('Starting Portfolio Value: %.2f' %
        # Run over everything
        # Print out the final result
        print('Final Portfolio Value: %.2f' %

    I get an error.What is the error?

    KeyError                                  Traceback (most recent call last)
    <ipython-input-6-0bc68b8d700a> in <module>()
         69     # Run over everything
    ---> 70
         72     # Print out the final result
    ~\Anaconda3\envs\env_zipline\lib\site-packages\backtrader\ in run(self, **kwargs)
       1125             # let's skip process "spawning"
       1126             for iterstrat in iterstrats:
    -> 1127                 runstrat = self.runstrategies(iterstrat)
       1128                 self.runstrats.append(runstrat)
       1129                 if self._dooptimize:
    ~\Anaconda3\envs\env_zipline\lib\site-packages\backtrader\ in runstrategies(self, iterstrat, predata)
       1208                 if self._exactbars < 1:  # datas can be full length
       1209                     data.extend(size=self.params.lookahead)
    -> 1210                 data._start()
       1211                 if self._dopreload:
       1212                     data.preload()
    ~\Anaconda3\envs\env_zipline\lib\site-packages\backtrader\ in _start(self)
        202     def _start(self):
    --> 203         self.start()
        205         if not self._started:
    ~\Anaconda3\envs\env_zipline\lib\site-packages\backtrader\feeds\ in start(self)
        348     def start(self):
    --> 349         self.start_v7()
        351         # Prepared a "path" file -  CSV Parser can take over
    ~\Anaconda3\envs\env_zipline\lib\site-packages\backtrader\feeds\ in start_v7(self)
        319         }
    --> 321         urlargs.append('interval={}'.format(intervals[self.p.timeframe]))
        322         urlargs.append('events=history')
        323         urlargs.append('crumb={}'.format(crumb))

  • As I know Yahoo was providing only daily data. I really doubt that it was changed, but I didn't use them for long time.

  • administrators

    @ab_trader said in Problem with YahooFinanceData:

    As I know Yahoo was providing only daily data

    And even if Yahoo provides some other timeframes, that data feed is for sure only meant for the API which only deliver days.

  • @backtrader How to check algorithm for history data with minutes timeframe?

  • administrators

    By getting data from a provider which offers you data with intraday resolution.

    From other thread you are also trying to do things with Interactive Brokers but you have no data permissions. See: Interactive Brokers is very cheap as a data provider and will cost you nothing if you generate commissions.

    Some will argue about the quality of the data, but it is at least a starting point and in most cases more than enough. But you need to subscribe.

    You are not going to find intraday data (except maybe for Forex, which isn't of course centralized and is provider dependent) for free falling from the sky.

Log in to reply