Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Looping Through The Tickers in Our List of Stocks doesn't work #44

Open
mpquochung opened this issue Nov 18, 2022 · 1 comment
Open

Comments

@mpquochung
Copy link

mpquochung commented Nov 18, 2022

I got this error when running the code:
image
I have tried several things but doesn't work. Anyone help me pls!
P/S: this loop can only create 134 tickers, does this mean something happened with the 135th?

@npomfret
Copy link

npomfret commented Jan 6, 2023

The sample code isn't ideal as the response to the batch request may have symbols missing. I think it's better to loop over the symbols in the response rather than the symbols in the request. Something like...

stocks = pd.read_csv('sp_500_stocks.csv')
all_symbols = stocks['Ticker']
step = 100

my_columns = [... whatever]
df = pd.DataFrame(columns=my_columns)

for i in range(0, len(all_symbols), step):
    symbols = all_symbols[i:i + step]
    symbols_text = ','.join(symbols)

    url = f'{base_url}/stable/whatever...'
    response = session.get(url)
    data = response.json()
    for symbol in data:
        quote = data[symbol]['quote']
        ... etc

(also note, the above code has a greatly simplified 'chunking' mechanism and achieves everything in a single loop)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants