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

Alpha Vantage DataFeed

  • Hello,

    I'm pretty new to Python coding and Backtrader.
    However I have done some backtests using the Alpaca API and it worked without to much problems.

    I would to use the Alpha-Vantage API because they provide not only stock data, but also FOREX and Crypto.

    First I tried the Code I found here:

    from alpha_vantage.timeseries import TimeSeries
    import pandas as pd
    import numpy as np
    import backtrader as bt
    from datetime import datetime
    # ----------
    # Register for an API at:
    # Then insert it here.
    Apikey = 'XXXXX'
    def alpha_vantage_eod(symbol_list, compact=False, debug=False, *args, **kwargs):
        Helper function to download Alpha Vantage Data.
        This will return a nested list with each entry containing:
            [0] pandas dataframe
            [1] the name of the feed.
        data_list = list()
        size = 'compact' if compact else 'full'
        for symbol in symbol_list:
            if debug:
                print('Downloading: {}, Size: {}'.format(symbol, size))
            # Submit our API and create a session
            alpha_ts = TimeSeries(key=Apikey, output_format='pandas')
            data, meta_data = alpha_ts.get_daily(symbol=symbol, outputsize=size)
            #Convert the index to datetime.
            data.index = pd.to_datetime(data.index)
            data.columns = ['Open', 'High', 'Low', 'Close','Volume']
            if debug:
            data_list.append((data, symbol))
        return data_list
    class TestStrategy(bt.Strategy):
        def __init__(self):
        def next(self):
            for i, d in enumerate(self.datas):
                bar = len(d)
                dt = d.datetime.datetime()
                dn = d._name
                o =[0]
                h = d.high[0]
                l = d.low[0]
                c = d.close[0]
                v = d.volume[0]
                print('{} Bar: {} | {} | O: {} H: {} L: {} C: {} V:{}'.format(dt, bar,dn,o,h,l,c,v))
    # Create an instance of cerebro
    cerebro = bt.Cerebro()
    # Add our strategy
    # Download our data from Alpha Vantage.
    symbol_list = ['LGEN.L','LLOY.L']
    data_list = alpha_vantage_eod(
    for i in range(len(data_list)):
        data = bt.feeds.PandasData(
                    dataname=data_list[i][0], # This is the Pandas DataFrame
                    name=data_list[i][1], # This is the symbol
        #Add the data to Cerebro
    print('Starting to run')
    # Run the strategy

    Unfortunately it didn't work. I wasn't able to display any data with it.
    And yes, I used my API - key (which is working, i tested it in another code)

    I thought that the problem came from the Pandas dataframe from Alpha-Vantage (maybe not supported anymore)

    I tried different things and I done quiet a lot of research in the internet and I can't find any suitable solution.

    I also tried to request JSON datas and convert them to pandas or CSV, unfortunately this was also not working.

    But maybe someone can help with the code above.

    Thanks a lot in adavance

  • I've developed alphavantage data feed to bt some time ago. Not sure if it works now, you can try -

  • Hello ab_trader,

    thanks for the quick reply !
    did you have a sample code for using this datafeed ?

  • @rajanprabu I already test this code and its not working for me, that's why I opened this topic

  • I'm sorry for the oversight.

  • @marketwizard there is tests folder in the repo, which has examples.

Log in to reply