8000 Example code fails to run fully · Issue #594 · polygon-io/client-python · GitHub
[go: up one dir, main page]

Skip to content

Example code fails to run fully #594

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

Closed
daxm opened this issue Jan 16, 2024 · 7 comments · Fixed by #601
Closed

Example code fails to run fully #594

daxm opened this issue Jan 16, 2024 · 7 comments · Fixed by #601
Assignees
Labels
bug Something isn't working

Comments

@daxm
Copy link
daxm commented Jan 16, 2024

Describe the bug
The example code listed in the README runs BUT will fail near the end. My guess is because my "free" API key is hitting its query limit.

To Reproduce

  1. Make a python file with the example code.
  2. Run the file.

Expected behavior
The file should run without error/crashing.

Screenshots

This code will error using a "free" API key:

import os

from dotenv import load_dotenv
from polygon import RESTClient

load_dotenv()


def main():
    client = RESTClient(api_key=os.getenv("API_KEY"))
    ticker = "AAPL"

    # List Aggregates (Bars)
    aggs = []
    for a in client.list_aggs(
        ticker=ticker,
        multiplier=1,
        timespan="minute",
        from_="2023-01-01",
        to="2023-06-13",
        limit=50000,
    ):
        aggs.append(a)

    print(aggs)

    # Get Last Trade
    trade = client.get_last_trade(ticker=ticker)
    print(trade)

    # List Trades
    trades = client.list_trades(
        ticker=ticker,
        timestamp="2022-01-04",
    )
    for trade in trades:
        print(trade)

    # Get Last Quote
    quote = client.get_last_quote(ticker=ticker)
    print(quote)

    # List Quotes
    quotes = client.list_quotes(
        ticker=ticker,
        timestamp="2022-01-04",
    )
    for quote in quotes:
        print(quote)


if __name__ == "__main__":
    main()

I'm guessing that reducing the 'limit' value from 50,000 to just 5000 would fix the problem. I cannot confirm this as now I can't run any queries. (I waited over 30 minutes to try again but am still getting 429 errors.)

@daxm daxm added the bug Something isn't working label Jan 16, 2024
@justinpolygon
Copy link
Contributor

Hi @daxm, I was able to run the example code without error using a Starter API key. This appears to be happening because you're making more than 5 API calls per minute on the Free tier. If you break this script down into individuals calls client.get_last_trade, client.get_last_quote, etc they should all work on their own. One thing to keep in mind is that client.list_aggs supports pagination behind the scenes so it might actually be making many API calls to support the request your asking for.

To help with debugging, you can see what requests are being made by turning on request tracing, update your client to client = RESTClient(api_key=os.getenv("API_KEY"), trace=True). Basically, this isn't a bug you're just hitting the API request limit.

@justinpolygon justinpolygon self-assigned this Jan 16, 2024
@daxm
Copy link
Author
daxm commented Jan 16, 2024

Yes, that was my guess too. My point is that maybe the example should be set up such that free API key users can at least run the example without error. Maybe have a super simple example and then a more advanced one (with a warning to the free tier people that their code might fail). Or, write into the example code to deal with 429 retry situations. (I've written them into my requests method in my API SDK packages I've written in the past so maybe a code update in polygon?)

@justinpolygon
Copy link
Contributor
justinpolygon commented Jan 16, 2024

Yeah, I totally agree. Can you link to that example you're talking about (and I'll update it)? We have the easy one off example scripts here https://github.com/polygon-io/client-python/tree/master/examples/rest for everyone to try.

@daxm
Copy link
Author
daxm commented Jan 16, 2024

It is in the readme. Basically I copy/pasted it into my def main() function.

@justinpolygon
Copy link
Contributor

It is in the readme. Basically I copy/pasted it into my def main() function.

Hey @daxm the script you posted isn't in the https://github.com/polygon-io/client-python/blob/master/README.md. Or, did you stick the examples together just by copy/pasting the sections?

@daxm
Copy link
Author
daxm commented Jan 16, 2024

It is there. In the "Using the Client" section. Not my WHOLE script but the code in the main() function.

@justinpolygon
Copy link
Contributor

Ah, I see what you're saying. Yeah, I'll add a note to the readme about API keys and what to expect. Thanks for reporting this. Sorry about the confusion here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0