Search code examples
pythonpackagequandl

How do I access Quandl web data with FinQuant on Python?


When trying to access quandl data through the FinQuant package I get an error


import pandas as pd
import datetime
import quandl
from finquant.portfolio import build_portfolio
quandl.api_config.api_key = "*************"
d = {
     0: {"Name": "BP.", "Allocation": 1000000},
     1: {"Name": "III", "Allocation": 1000000},
     2: {"Name": "GSK", "Allocation": 1000000},
     3: {"Name": "OCDO", "Allocation": 1000000},
     4: {"Name": "RBS", "Allocation": 2000000},
     5: {"Name": "SVT", "Allocation": 1000000},
     }

pf_allocation = pd.DataFrame.from_dict(d, orient="index")

#set list of names based on names
names = pf_allocation["Name"].values.tolist()

#start/end date
start = datetime.datetime(2018, 7, 1)
end = datetime.datetime(2019, 6, 30)

#building portfolio

pf = build_portfolio(
        names=names, pf_allocation=pf_allocation, start_date=start, end_date=end
)

get this:

wdir='C:/Users/Joe Shiafa Pierce/.spyder-py3')
Traceback (most recent call last):

  File "<ipython-input-15-*********>", line 1, in <module>
    runfile('C:/Users/Joe Shiafa Pierce/.spyder-py3/FinQuant POM project.py', wdir='C:/Users/Joe Shiafa Pierce/.spyder-py3')

  File "C:\Users\Joe Shiafa Pierce\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\Users\Joe Shiafa Pierce\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Joe Shiafa Pierce/.spyder-py3/FinQuant POM project.py", line 27, in <module>
    names=names, pf_allocation=pf_allocation, start_date=start, end_date=end

  File "C:\Users\Joe Shiafa Pierce\Anaconda3\lib\site-packages\finquant\portfolio.py", line 1153, in build_portfolio
    pf = _build_portfolio_from_api(**kwargs)

  File "C:\Users\Joe Shiafa Pierce\Anaconda3\lib\site-packages\finquant\portfolio.py", line 930, in _build_portfolio_from_api
    data = _quandl_request(names, start_date, end_date)

  File "C:\Users\Joe Shiafa Pierce\Anaconda3\lib\site-packages\finquant\portfolio.py", line 763, in _quandl_request
    raise Exception("Error during download of stock data from Quandl.")

Exception: Error during download of stock data from Quandl.

Any help is greatly appreciated.


Solution

  • I tried downloading stock price data for each of those tickers individually with

    import quandl
    quandl.get("WIKI/BP.")
    

    Having done that, it appears that the tickers/names as you used them are not found by Quandl. E.g. BP. is not a valid ticker/name on Quandl. You probably mean BP. Most of the others are also invalid.

    You might want to have a look at a list of ticker/names on Quandl: Quandl stock identifiers

    When used with valid tickers/names, e.g. III, FinQuant should return a portfolio object.