4

So essentially I have the free API key for Alpha Vantage in which I am using to obtain fundamental data on random stocks. To be clear I have connected to and used the API successfully and I am not in need of pulling stock price data older than 5 years.

Summed up, does anyone know if there is a way you can obtain income, balance sheet, and cash flow data on selected Tickers older than 5 years? Is this a premium feature? I couldn't find anything anywhere.

https://www.alphavantage.co/documentation/ <-- Documentation also shows only 5 years' worth of data in the 'fundamental data' examples.

import requests
import pandas as pd
from pandasgui import show
from alpha_vantage.alphavantage import AlphaVantage
from alpha_vantage.timeseries import TimeSeries

# ///////////////////////////////////////////////////////////
# FUNCTIONS FOR ALPHA VANTAGE
# ///////////////////////////////////////////////////////////
TIME_SERIES_INTRADAY = 'TIME_SERIES_INTRADAY'
TIME_SERIES_INTRADAY_EXTENDED = 'TIME_SERIES_INTRADAY_EXTENDED'
TIME_SERIES_DAILY = 'TIME_SERIES_DAILY'
TIME_SERIES_DAILY_ADJUSTED = 'TIME_SERIES_DAILY_ADJUSTED'
TIME_SERIES_WEEKLY = 'TIME_SERIES_WEEKLY'
TIME_SERIES_WEEKLY_ADJUSTED = 'TIME_SERIES_WEEKLY_ADJUSTED'
TIME_SERIES_MONTHLY = 'TIME_SERIES_MONTHLY'
TIME_SERIES_MONTHLY_ADJUSTED = 'TIME_SERIES_MONTHLY_ADJUSTED'
GLOBAL_QUOTE = 'GLOBAL_QUOTE'
SYMBOL_SEARCH = 'SYMBOL_SEARCH'
OVERVIEW = 'OVERVIEW'
EARNINGS = 'EARNINGS'
INCOME_STATEMENT = 'INCOME_STATEMENT'
BALANCE_SHEET = 'BALANCE_SHEET'
CASH_FLOW = 'CASH_FLOW'
LISTING_STATUS = 'LISTING_STATUS'
EARNINGS_CALENDAR = 'EARNINGS_CALENDAR'
IPO_CALENDAR = 'IPO_CALENDAR'
CURRENCY_EXCHANGE_RATE = 'CURRENCY_EXCHANGE_RATE'
FX_INTRADAY = 'FX_INTRADAY'
FX_DAILY = 'FX_DAILY'
FX_WEEKLY = 'FX_WEEKLY'
FX_MONTHLY = 'FX_MONTHLY'
CURRENCY_EXCHANGE_RATE = 'CURRENCY_EXCHANGE_RATE'
CRYPTO_RATING = 'CRYPTO_RATING'
CRYPTO_INTRADAY = 'CRYPTO_INTRADAY'
DIGITAL_CURRENCY_DAILY = 'DIGITAL_CURRENCY_DAILY'
DIGITAL_CURRENCY_WEEKLY = 'DIGITAL_CURRENCY_WEEKLY'
DIGITAL_CURRENCY_MONTHLY = 'DIGITAL_CURRENCY_MONTHLY'

# ///////////////////////////////////////////////////////////
# TIME SERIES FOR STOCK DATA
# ///////////////////////////////////////////////////////////
ONEMIN = 'Time Series (1min)'
FIVEMIN = 'Time Series (5min)'
FIVETEENMIN = 'Time Series (15min)'
THIRTYMIN = 'Time Series (30min)'
HOUR = 'Time Series (60min)'
FIVEMIN = 'Time Series (5min)'
DAILY = 'Time Series (Daily)' 
WEEKLY = 'Weekly Time Series'
WEEKLY_ADJ = 'Weekly Adjusted Time Series'
MONTLY = 'Monthly Time Series'
MONTLY_ADJ = 'Monthly Adjusted Time Series'

# /////////////////////////////////////////////////////////
# query fundamental stock data of selected type ///////////
# /////////////////////////////////////////////////////////
def query_fundamental_data(func, symbol, outputsize='full', datatype='json', apikey='TUBY75I2F4D58X6M'):
    data = {
        "function": func,
        "symbol": symbol,
        "outputsize": outputsize, # (full) and (compact) are accepted
        "datatype": datatype,
        "apikey": apikey
    }
    return requests.get("https://www.alphavantage.co/query", data).json()

# /////////////////////////////////////////////////////////
# convert fund stock data of selected type to json ////////
# /////////////////////////////////////////////////////////
def get_fundamental_dataframe(json):
    df = pd.DataFrame(json['annualReports'])
    df.set_index('fiscalDateEnding', inplace=True)
    return df

# /////////////////////////////////////////////////////////
# query timeseries stock data of selected type ////////////
# /////////////////////////////////////////////////////////
def query_timeseries_data(func, symbol, outputsize='full', datatype='json', apikey='TUBY75I2F4D58X6M'):
    data = {
        "function": func,
        "symbol": symbol,
        "outputsize": outputsize, # (full) and (compact) are accepted
        "datatype": datatype,
        "apikey": apikey
    }
    return requests.get("https://www.alphavantage.co/query", data).json()

# /////////////////////////////////////////////////////////
# convert time-series stock data of selected type to JSON//
# /////////////////////////////////////////////////////////
def get_timeseries_dataframe(json, type):
    df = pd.DataFrame.from_dict(json[type], orient= 'index')
    df.index =  pd.to_datetime(df.index, format='%Y-%m-%d')
    
    df = df.rename(columns={ '1. open': 'Open', '2. high': 'High', '3. low': 'Low', '4. close': 'Close', '5. volume': 'Volume'})
    df = df.astype({'Open': 'float64', 'High': 'float64', 'Low': 'float64','Close': 'float64','Volume': 'float64',})
    df = df[[ 'Open', 'High', 'Low', 'Close', 'Volume']]
    return df

# /////////////////////////////////////////////////////////
# how to create dataframes for matplotlib usage  //////////
# /////////////////////////////////////////////////////////

response_json = query_fundamental_data(INCOME_STATEMENT, 'MSFT')
INCOME_DATA = get_fundamental_dataframe(response_json)

#response_json = query_fundamental_data(BALANCE_SHEET, 'MSFT')
#BALANCE_DATA = get_fundamental_dataframe(response_json)

#response_json = query_fundamental_data(CASH_FLOW, 'MSFT')
#CASHFLOW_DATA = get_fundamental_dataframe(response_json)

#response_json = query_timeseries_data(TIME_SERIES_DAILY, 'MSFT')
#STOCKPRICE_DATA = get_timeseries_dataframe(response_json, DAILY)

show(INCOME_DATA)
#show(BALANCE_DATA)
#show(CASHFLOW_DATA)
#show(STOCKPRICE_DATA)

You will notice that the results are only a mere 5 years. pandas gui view of dataframe

Thanks in advance to anyone who can help or who can inform me if this is even possible currently or not. I have tried to find a way myself, I cannot seem to find one within the documentation or anywhere really.

TehBunk
  • 53
  • 6
  • I've looked at the demos in the [official reference](https://www.alphavantage.co/documentation/#fundamentals) and [demo API](https://www.alphavantage.co/query?function=EARNINGS&symbol=IBM&apikey=demo), and it looks like you can get more than 5 years, but I'm just guessing, can't you get it by setting the start year? – r-beginners May 12 '21 at 03:25
  • I looked in their documentation and have looked through a few other threads and can only seem to find a way to search dates on the Stock Prices. That's whats got me so confused, like they must have data going beyond five years as it can be viewed in earnings. But I cannot seem to find it for the income statement, balance sheet, or cashflow – TehBunk May 14 '21 at 02:42
  • I've tried the demo API, and it looks like it's over 5 years old. Also, the description of AlphaVantage in the pandas data reader says up to 5 years. For your [reference](https://pandas-datareader.readthedocs.io/en/latest/readers/alphavantage.html) – r-beginners May 14 '21 at 03:32
  • So it appears for price data such as Forex, Stock price, and Crypto it is very easy to get long-term data. However, even after reading through the alpha-vantage python module and using the methods supplied it doesn't appear to be possible for fundamental data. I am still limited to only 5 years. :( – TehBunk May 15 '21 at 23:30

0 Answers0