Alpha Vantage API: How do I get stock market indexes?

Viewed 701

I'm trying to get data for these indexes SPX,COMP,DJIA,DJT,RUT,DJU using this Python library

I'm doing this

data, meta_data = ts.get_intraday(symbol='DJIA',interval='60min', outputsize='full')

but I get

ValueError: Error getting data from the api, no return was given.

If I change DJIA to AAPL I get a response.
How do I get data for the indexes mentioned above? Is there another API that I can use?
I tried Yahoo Finance API but could only retrieve data for DJIA,DJT, DJU

1 Answers

You need to provide the API key each time you access the Alpha Vantage library.

from alpha_vantage.timeseries import TimeSeries
import pandas as pd
import time
import datetime as dt

ts = TimeSeries (key=keys, output_format = "pandas")

    ### STOCK TIME SERIES > DAILY ADJUSTED ###
        # Date / Open / High / Low / Close / Adjusted Close / Volume / Dividend / Split
data_daily, meta_data = ts.get_daily_adjusted(symbol=stock_ticker, outputsize ='compact')
        # data_daily['column name'][row number]
data_daily_lastOpenPrice = data_daily['1. open'][0]
data_daily_lastHighPrice = data_daily['2. high'][0]
data_daily_lastLowPrice = data_daily['3. low'][0]
data_daily_lastAdjustedClosingPrice = data_daily['5. adjusted close'][0]
data_daily_lastTradingVolume = data_daily['6. volume'][0]
data_daily_lastDividendAmount = data_daily['7. dividend amount'][0]

If you put this inside a for loop you can cycle through a list of your stocks and print out the results for the metrics you are looking for.

Related