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

[Bug]: get_trades_async should accept limit=None #790

Open
1 task done
mj-z-ali opened this issue Oct 14, 2024 · 0 comments
Open
1 task done

[Bug]: get_trades_async should accept limit=None #790

mj-z-ali opened this issue Oct 14, 2024 · 0 comments

Comments

@mj-z-ali
Copy link

mj-z-ali commented Oct 14, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

I would like to retrieve all trades across several days for a given symbol. My strategy is to run get_trades_async for each date separately. Below, I test get_trades_async for a single date, where I attempt to retrieve all trades for SPY on October 11, 2024. When I run the following, I get a type error:

symb, tf =  asyncio.run(async_rest_data_client.get_trades_async('SPY','2024-10-11', '2024-10-11',limit=None))

>> raise TypeError(
TypeError: Invalid variable type: value should be str, int or float, got None of type <class 'NoneType'>

Expected Behavior

The underlying Alpaca API only accepts a limit between 1 and 10,000. It defaults to 1,000 if no limit is given. However, a "next_page_token" is provided in order to continue requesting more data where it left off in case of a request larger than 10,000 (as in my case). The _request function keeps requesting packets until "next_page_token" is no longer available. This is good. However, the purpose of this is defeated when limit is reached in the calling for-loop:

    async def _iterate_requests(self,
...
        async for packet in self._request(url, payload):
     ...
                if len(df) >= limit:
                    break

        return df


    async def _request(self, url, payload):
...
            while 1:
                async with session.get(url, **opts) as response:

                    response = await response.json()
                    page_token = response.get('next_page_token')
                    payload["page_token"] = page_token
                    yield response

                    if not page_token:
                        break

Entering a limit > 10,000 to avoid an early break will append this limit to the url and cause Alpaca to not return anything at all. Entering a limit of None will cause TypeError.

SDK Version I encountered this issue in

Alpaca Trade API version: 3.0.0

Steps To Reproduce

Run 

asyncio.run(async_rest_data_client.get_trades_async('SPY','2024-10-11', '2024-10-11',limit=None))

Produces TypeError.

Run

sym, tf = asyncio.run(async_rest_data_client.get_trades_async('SPY','2024-10-11', '2024-10-11',limit=10_001))

Produces empty dataframe.



### Filled out the Steps to Reproduce section?

- [X] I have entered valid steps to reproduce my issue or have attached a minimally reproducible case in code that shows my issue happening; and understand that without this my issue will be flagged as invalid and closed after 30 days.

### Anything else?

_No response_
mj-z-ali added a commit to mj-z-ali/alpaca-trade-api-python that referenced this issue Oct 14, 2024
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

1 participant