All orders containing client_order_id

Hi all. I’m using a python script to trade on Alpaca. I give all my orders an id which looks like: {strategy name}_{timestamp}. At a certain time later in the month, I want to close all the positions made by this stategy. How do I fetch and close all those positions? I can’t figure out a way to only get the positions based on their client_order_id. In the documentation is stated that the use of client_order_id is helpfull if you use multiple strategies in the same account, so I guess I’m missing something.

For fellow traders Googling the same issue: I managed to do it!

# Get the last 10 closed orders
get_orders_data = GetOrdersRequest(
    status=QueryOrderStatus.CLOSED,
    limit=10,
    nested=True  # show nested multi-leg orders
)

orders = trading_client.get_orders(filter=get_orders_data)

# Filter the orders
filtered_orders = [
    order for order in orders
    if '{strategy name}' in order.client_order_id
1 Like