How to back-date trades?

Hi All,

I’d like to use Alpaca to enter in past trades of an existing portfolio, and simulate some trading strategies. In looking at the orders endpoint, I don’t see the ability to back-date the trade or enter a cost basis. I’d like to enter in previous trades so I can use the portfolio history functionality. Is there something I’m missing or does this have to be done manually? Thanks for any direction

-J

2 Likes

Hey. Sorry, it is a little unclear what exactly you are after. However, if you are looking to get historical (filled) order / trade into, you can pass the status = 'closed' argument to the list_orders() call of the API. Assuming you are using python: e.g. api.list_orders(status = 'closed', limit = 100)). You can find relevant examples in other languages here under the Get a List of Existing Orders tab: https://alpaca.markets/docs/api-documentation/how-to/orders/. Hope this is helpful.

Oh. After re-reading your message, I think you want to backtest the P&L of your historical trades from another platform, or simulator using Alpaca’s simulation backend. I am new to Alpaca but I don’t think that’s possible. Depending on what you are trying to accomplish, you might have other options though. What exactly are you trying to check for these historical trades (P&L, execution?) If it is the former, you can download historical price data from Alpaca and use it to calculate the retrospective P&L locally on your machine. If it is the latter, I think it would be hard to simulate retrospectively.

Hi Sokol,

Thanks for the response. It was the former I was getting at. To clarify, my goal is to take an existing/actual portfolio with years of history and trades, and calculate things like daily portfolio value, weights, etc. I figured that it would not be a huge effort to just pull the data for the stocks and calculate it in Pandas, but it does get complicated when you want to do things like calculate CAGR for specific stocks. To my knowledge, the only free platform I’ve seen that lets you do this is Yahoo finance, which lets you input a stock, purchase date, lot, and cost per share. However, historical graphing tools are weak.

Maybe I misunderstood what Alpaca is meant for, but do find it surprising that this wouldn’t be a feature to focus on.

1 Like

This may not fit your use case exactly but have you looked at pyfolio (https://www.quantopian.com/docs/api-reference/pyfolio-api-reference)? It is good for putting in historical frames such as transaction data and outputting a bunch of statistics and graphs with a single api call

1 Like

Alpaca currenly does not offer built-in backteting functionality. We do understand the importance of backtesting when it comes to automated trading strategies, but use cases vary, and right now we rely on ecosystem that are out there. We have backtrader integration for example that could work for you. https://github.com/alpacahq/alpaca-backtrader-api