Closing individual lots within a profitable position

I built a dashboard into my AI javascript trader which opens and closes positions fine once they have reached profitability - however, let’s say I buy 10 separate lots of any given crypto or equity with positions opened at different prices all of those positions get lumped into one so there isn’t any way to close a lot which was bought at a cheaper price to other lots and already attained profitability.

I had found a work around I created a lot tracking function in jscript which now allows me to close seperate profitable lots but there is a problem with this it’s not persistent when i close my javascript web app it loses memory of the lots since they were held in browser memory, to fix it I will likely have to create some sort of caching process were lots are stored locally until spent/unspent but I was just wondering whether I was overthinking it all together and thought i’d ask whether there is a simpler solution.

^ How it currently works notice the position is split into 3 sub orders of $1.00 BTC

@conda You can, of course, create and track ‘lots’ however you wish. But realize Alpaca currently doesn’t have any mechanism for lot tracking or specifying which lots to trade. Buys and sells are always in FIFO order, and end-of-year gains and losses are calculated that way, too.

Thanks dan, I would love to see native support for this through the API. I know it’s a big ask, but it allows for positions to have a bit more flexibility instead of for example spending $100 on 10 positions you could spend $100 on 100 x $1 positions instead each filled at different prices allowing positions to scale horizontally to some extent and by only closing the lots which have reached profitability as opposed to closing the whole lot higher profits could be made naturally going short would be the same you could scale those horizontally too.

Here is what happens to my lots once reloading the trading web app, it would just be nice if this could all be done through the API so we don’t have to create a separate function I imagine other users have encountered a similar problem when creating their own positions. I know it is supported on some other brokers I think Etoro allows for it as if I open several positions I can close the profitable lots individually would be nice if Alpaca had the same sort of lot tracking built in.

The workaround this code uses storing lots in a JS object keyed by normalised symbol, with a notional fraction basded sell calculation is a reasonable client-side approximation, but it can drift from reality if orders partially fill or get cancelled or otherwise delayed after recording.

cleanest solution probs would be a /v2/positions/{symbol}/lots sub-resource. When a buy order fills, Alpaca would internally tag the filled quantity with the order’s client_order_id and entry price, creating a lot record attached to the position. The endpoint would return an array of those lot records.

bakin it into the API would also open the door to proper tax lot selection, which is something a lot of traders need for cost-basis reporting and which Alpaca currently doesn’t offer progamgrammatically.

But for me it was mostly just a persistence issue not really sure how I go about catching failed or otherwise delayed transactions just yet.