-1

After the deprecation of Yahoo Finance for pandas datareader, I have searched high and low after a replacement. After some thorough Googling, I decided I must try Alpha Vantage.

I found this video on how to get stockinfo from Alpha Vantage: https://www.youtube.com/watch?v=nipPiiNoqd4

And this is my inspired code trying to do just so.

import pandas as pd
import datetime
import requests

def dtr_alvan():
    try:
        data=requests.get('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&apikey=demo')
        data=data.json()
        print('\ndata before json parsing:\n',data)

        data=data('Time Series (Daily)')
        df=pd.DataFrame(columns=['date','open','high','low','close','adj_close','volume','dividend amount','split coeffiecient'])
        for d,p in data.items():
            date=datetime.datetime.strptime(d, '%Y-%m-%d %H:%M:%S')
            data_row=[date,float(p['1. open']),float(p['2. high']),float(p['3. low']),float(p['4. close']),float(p['5. adjusted close']),int(p['6. volume']),float(p['7. dividend amount']),float(p['8. split coefficient'])]
            df.loc[-1,:]=data_row
            df.index=df.index+1
        data=df.sort_values('date')
        print('\ndata after json parsing:\n',data)

        return data


    except Exception as e:
        print(str(e))
        pass


if __name__ == '__main__':
    print("== m04_get_stocks == is being run directly\n")

    data = dtr_alvan()

Apparantly I can get the data (first print statement), but parsing the json gives me the error dict object is not callable (second print statement is not run, as in the line data=data('Time Series (Daily)') the code jumps to the error exception with the error

'dict object is not callable'

Can anyone help with this - I am totally stumped, and need a hint from you guys?

Excaliburst
  • 143
  • 1
  • 4
  • 15

2 Answers2

2

Should this be in square brackets instead of ()?

data=data('Time Series (Daily)')

i.e. are you trying to access the key 'Time Series (Daily)' in the data dict?

If so, use:

data = data['Time Series (Daily)']

Otherwise you are trying to call a function data() with the argument 'Time Series (Daily)'

Ollie Graham
  • 124
  • 1
  • 8
1

you're getting this error because you're trying to call dictionary as a method. you should access the values of dictionary objects using third brackets '[]'.

it should be, data=data['Time Series (Daily)']

Minhaz
  • 66
  • 3