I am getting a keyError when using Alpha Vantage API to get Stochastic Oscillator data for an algorithmic trader

111 Views Asked by At

I am using the Alpha Vantage API to get Stochastic Oscillator data to build an algorithmic trader, but I get a keyError when trying to pull the values for NFLX from the Alpha Vantage API

This is the code:

def get_stoch(symbol, k_period, d_period, start_date):
    api_key = open(r'apikey.txt')
    url = f'https://www.alphavantage.co/query?function=STOCH&symbol={symbol}&interval=daily&fastkperiod={k_period}&slowdperiod={d_period}&apikey={api_key}'
    raw = requests.get(url).json()
    df = pd.DataFrame(raw[f'Technical Analysis: STOCH']).T.iloc[::-1]
    df = df[df.index >= start_date]
    df.index = pd.to_datetime(df.index)
    df = df.astype(float)
    return df['SlowK'], df['SlowD']

nflx['%k'], nflx['%d'] = get_stoch('NFLX', 14, 3, '2020-01-01')

This is the error I am getting:

Traceback (most recent call last):
  File "c:\Users\Hemil Thakkar\Downloads\algotrader\algotraderstoch.py", line 38, in <module>
    nflx['%k'], nflx['%d'] = get_stoch('NFLX', 14, 3, '2020-01-01')
  File "c:\Users\Hemil Thakkar\Downloads\algotrader\algotraderstoch.py", line 32, in get_stoch
    df = pd.DataFrame(raw[f'Technical Analysis: STOCH']).T.iloc[::-1]
KeyError: 'Technical Analysis: STOCH'

I tried to get the json from the url above, but when I try to look for a certain in key in the json(variable: raw) I get a keyError.

0

There are 0 best solutions below