Account Activities with new Alpaca-py python API

I am updating code to use the new Alpaca-py python API, and am trying to access account activities, but it is not obvious how, or if that capability exists yet. It used to be get_activities() on the api object. Examing the TradingClient object and the account object (via get_account()) shows no obvious methods.

Thanks

1 Like

I have having the same issues. I would like to grab all the filled buy and sell orders, not the canceled one. If I use the TradingClient object, calling the ‘get_orders’ function is limited to 500 and I am not sure I am getting all the data. The GetOrdersRequest’s enum QueryOrderStatus is not granular enough. I think it needs FILLED in addition to CLOSED, CANCELED, and ALL.

1 Like

is there any update from Alpaca? I have been looking around for the solution too!

I seached the docs and source code but did’t find any implementation. Just to be sure I searched the forum and ended up here.
So, for the lack of functioality in the python API I coded a small fuction that retrieves account activities based on the API reference because I needed it for my algorithm. And since it looks like other people have been looking for it too, here’s my code:

from alpaca.trading.client import TradingClient


def get_account_activities(client: TradingClient,
                           activity_types: list[str] | str | None = None, # str with one type or list of types
                           category: str | None = None,

                           # region dates
                           # Both formats YYYY-MM-DD and YYYY-MM-DDTHH:MM:SSZ are supported
                           date: str | None = None,
                           until: str | None = None,
                           after: str | None = None,
                           # endregion

                           direction: str | None = None,  # 'desc' or 'asc', default=desc
                           page_size: int | None = None,  # 1 to 100 Defaults to 100
                           page_token: str | None = None, # Token used for pagination. Provide the ID of the last activity from the last page to retrieve the next set of results.
                           ):
    if isinstance(activity_types, list):
        activity_types = ','.join(activity_types)
    data = dict()
    for opt in [
        'activity_types',
        'category',
        'date',
        'until',
        'after',
        'direction',
        'page_size',
        'page_token',
    ]:
        if val := locals().get(opt):
            data[opt] = val
    return client.get('/account/activities', data=data or None)


client = TradingClient('api key', 'api secret')
activities = get_account_activities(client, activity_types='FILL', page_size=5)

This gives us a raw list of dicts which we can also read with pandas as in pd.DataFrame(activities).

I hope it helps someone - and look forward for a proper method to be implemented in TradingClient :slight_smile:

1 Like

You can grab all the trades done, 500 at a time (do it in a loop until you are done). Pretty much easy to do that in Python.