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:
https://backtest-rookies.com/2019/01/04/backtrader-alpha-vantage-data-direct-ingest/from alpha_vantage.timeseries import TimeSeries import pandas as pd import numpy as np import backtrader as bt from datetime import datetime # IMPORTANT! # ---------- # Register for an API at: # https://www.alphavantage.co/support/#api-key # 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: print(data) data_list.append((data, symbol)) return data_list class TestStrategy(bt.Strategy): def __init__(self): pass def next(self): for i, d in enumerate(self.datas): bar = len(d) dt = d.datetime.datetime() dn = d._name o = d.open[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 cerebro.addstrategy(TestStrategy) # Download our data from Alpha Vantage. symbol_list = ['LGEN.L','LLOY.L'] data_list = alpha_vantage_eod( symbol_list, compact=False, debug=False) 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 timeframe=bt.TimeFrame.Days, compression=1, fromdate=datetime(2018,1,1), todate=datetime(2019,1,1) ) #Add the data to Cerebro cerebro.adddata(data) print('Starting to run') # Run the strategy cerebro.run()
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
marketwizard
-
I've developed alphavantage data feed to
bt
some time ago. Not sure if it works now, you can try -
https://github.com/ab-trader/backtrader_addons
-
Hello ab_trader,
thanks for the quick reply !
did you have a sample code for using this datafeed ?
-
https://backtest-rookies.com/2019/01/04/backtrader-alpha-vantage-data-direct-ingest/
this could help..
-
@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.