# Performance Attribution API

**URL:** <https://forum.alpaca.markets/t/performance-attribution-api/12413>\
**Category:** Alpaca Trading\
**Created:** [May 19, 2023, 10:45am UTC](https://forum.alpaca.markets/t/performance-attribution-api/12413 "2023-05-19T10:45:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul\_kara\_alphalayer](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@paul\_kara\_alphalayer](https://forum.alpaca.markets/u/paul_kara_alphalayer)\
**Post date:** [May 19, 2023, 10:45am UTC](https://forum.alpaca.markets/t/performance-attribution-api/12413/1 "2023-05-19T10:45:24Z")

</div>

Is there an API to gather performance attribution for various positions? I have a paper portfolio running with around 450 stock positions. The portfolio has recently experience an outsized loss and I’m trying to understand which security (or securities) on a particular day lead to this huge move.

Currently the web POSITIONS page leaves a lot to be desired in terms of your ability to understand how the aggregate portfolio P&L was driven each day, historically. As far as I can tell, the POSITIONS page, for example, while purporting to show TOTAL P&L doesn’t seem to be accurate, since if I add up all the losses they don’t come close to what the HOME page shows on the line chart (i’m assuming this line chart is unrealized P&L? But there’s no tittle so it’s not obvious either).

Thanks

---

<div class="post-metadata">

**Author:** ![Carlos\_Santana](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/carlos_santana/32/5269_2.png) [@Carlos\_Santana](https://forum.alpaca.markets/u/Carlos_Santana)\
**Post date:** [May 19, 2023, 1:28pm UTC](https://forum.alpaca.markets/t/performance-attribution-api/12413/2 "2023-05-19T13:28:43Z")

</div>

I would extract all orders and connect it via symbol.

---

<div class="post-metadata">

**Author:** ![paul\_kara\_alphalayer](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@paul\_kara\_alphalayer](https://forum.alpaca.markets/u/paul_kara_alphalayer)\
**Post date:** [May 19, 2023, 3:46pm UTC](https://forum.alpaca.markets/t/performance-attribution-api/12413/3 "2023-05-19T15:46:01Z")

</div>

Could you go into a bit more detail?

---

<div class="post-metadata">

**Author:** ![Carlos\_Santana](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/carlos_santana/32/5269_2.png) [@Carlos\_Santana](https://forum.alpaca.markets/u/Carlos_Santana)\
**Post date:** [May 19, 2023, 4:55pm UTC](https://forum.alpaca.markets/t/performance-attribution-api/12413/4 "2023-05-19T16:55:29Z")

</div>

1. You extract the closed orders:  
[Orders | Alpaca Docs](https://alpaca.markets/docs/api-references/trading-api/orders/)

2. Put the order values into a pandas data frame.

3. Calculate order value for each order: (qty \* filled\_avg\_price) (\*-1 for buy order)

4. Export it into a csv

5. Make a pivot table in Excel

6. Group by symbol and calculate PnL via order value

(5.+6. can also be done in python instead of Excel)

---

<div class="post-metadata">

**Author:** ![paul\_kara\_alphalayer](https://avatars.discourse-cdn.com/v4/letter/p/e47c2d/32.png) [@paul\_kara\_alphalayer](https://forum.alpaca.markets/u/paul_kara_alphalayer)\
**Post date:** [May 23, 2023, 2:04pm UTC](https://forum.alpaca.markets/t/performance-attribution-api/12413/5 "2023-05-23T14:04:48Z")

</div>

Thanks, so basically you’re saying to manually extract all the orders and avg fill prices, then merge this with price data to compute P&L.

---

<div class="post-metadata">

**Author:** ![Carlos\_Santana](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/carlos_santana/32/5269_2.png) [@Carlos\_Santana](https://forum.alpaca.markets/u/Carlos_Santana)\
**Post date:** [May 24, 2023, 10:43am UTC](https://forum.alpaca.markets/t/performance-attribution-api/12413/6 "2023-05-24T10:43:16Z")

</div>

Yes but no need to merge with price data because you getting all fill prices with the orders.
