Hello,
I am trying to aggregate my historical trades to calculate my average position price and size. To do this I need to know which trades are buy/sell to open (buy/sell to close can be calculated by tracking when size becomes zero). The only other alternative I can think of would be to request ALL history since the account was created and funded to ensure starting from a flat position. Of course, this is highly impractical when we only want to analyze the last month of historical trades. Below is an example simplification of a ApiTradeHistoryDTO[ ] array filtered by a single symbol.
As an example:
Sell 1 at 99 (closing a previous position)
Buy 1 at 100 (opening new position, we need a bit to know to start here rather than the prior order)
Buy 1 at 101
Buy 1 at 102
Sell 1 at 103
Sell 1 at 104
Sell 1 at 105 (net position closed)
The average position is Long 3 at 101. The only way to calculate this is by knowing that the second order in the array was a buy to open order. Otherwise, we would mistake the first order for an opening order when it is actually a closing order. What is the recommended way to get this result with my historical trades?