nickmccullum / algorithmic-trading-python

The repository for freeCodeCamp's YouTube course, Algorithmic Trading in Python
2.35k stars 2.37k forks source link

In batch api --> JSONDecodeError #53

Open mig9mili opened 1 year ago

mig9mili commented 1 year ago

symbol_groups = list(chunks(stocks['Ticker'], 100)) symbol_strings = [] for i in range(0, len(symbol_groups)): symbol_strings.append(','.join(symbol_groups[i]))

final_dataframe = pd.DataFrame(columns=my_col)

for symbol_string in symbol_strings: batch_api_call_url = f'https://cloud.iexapis.com/stable/stock/market/batch/types=quote&symbols={symbol_string}&token={IEX_CLOUD_API_TOKEN}' data = requests.get(batch_api_call_url).json() for symbol in data: if data[symbol]['quote']: final_dataframe = final_dataframe.append( pd.Series([symbol, data[symbol]['quote']['latestPrice'], data[symbol]['quote']['marketCap'], 'N/A'], index=my_col), ignore_index=True)

final_dataframe Screenshot 2023-03-20 104621 Screenshot 2023-03-20 104719

SoartheCat717 commented 1 year ago

I found similar error too.

image

SoartheCat717 commented 1 year ago

Ok, I found the reason. Since I am using the free trial of the API, so they only allocate 5 requests per second for me. That's what caused the error.

What you can do is to reduce your batch call to 5.

I have tried to reduce the batch call to 20 or 10. But there are errors that occurred as well, though some of the batch calls may successfully requested.

To fix this problem, you can either upgrade your plan or reduce your batch call to 5(if you are using IEX Apperate API).

rohitx01 commented 1 year ago

Screenshot (267) Just use this piece of code

The request error can arise due to the requesting a large number of Batch API calls all at once Therefore for that use

[:i]

at end of

for symbol_string in symbol_strings:
for symbol_string in symbol_strings [:i]:

and you can change the call to 20 calls or less to see if it works.

But make sure to remove it in the final code piece when making requests so that it will loop over every single stock

Even after this, the main error occurs at data['symbol'] For that use this piece of code below the FOR loop; the reason for that is these 3 stocks have been delisted and calls to them cause errors in the Series -------->

 if symbol == 'HFC' or symbol == 'VIAC' or symbol == 'WLTW' or symbol == 'DISCA':
            continue
        else:
            final_dataframe =  final_dataframe.append(