Skip to content

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

@mj-z-ali

Description

@mj-z-ali

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_

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions