tradingstrategy-ai / trade-executor

A Python framework for managing positions and trades in DeFi
https://tradingstrategy.ai
Other
101 stars 28 forks source link

Position data discrepancy #964

Closed kenkunz closed 3 months ago

kenkunz commented 3 months ago

Issue

There appears to be a discrepancy in the position statistics related to quantity, price and value.

Details

See discord thread for full background.

While getting feedback on a new table-based design for position details, @tsorro noted that the change in value for the position didn't seem to accurately reflect the expected value based on the quantity and price change:

image Is this math correct? How can the position value delta (-20.63) be smaller than the underlying asset price delta (-78.77) when multiplier (quantity) is 1.28? Maybe my own math is wrong šŸ˜„

The position in question is: https://tradingstrategy.ai/strategies/enzyme-polygon-eth-breakout/closed-positions/6

The source of data for each of the metrics in the above table is identified below (from the state endpoint for this strategy):

Type Open Close
Executed portfolio.closed_positions.6.opened_at portfolio.closed_positions.6.closed_at
Quantity stats.position.6[0].quantity
(from first stats entry for position)
stats.position.6.at(-2).quantity
(from second-to-last stats entry for position)
Price portfolio.closed_positions.6.trades.11.executed_price
(from position's first trade)
portfolio.closed_positions.6.trades.12.executed_price
(from position's last trade)
Value stats.position.6[0].value
(from first stats entry for position)
stats.position.6.at(-2).value
(from second-to-last stats entry for position)

Questions

1. What is the source of the apparent data discrepancy?

2. Is frontend using the correct properties from state payload for each of these metrics?

AlexTheLion123 commented 3 months ago

After further inspection, it appears that TradingPosition.get_value(), which is used to calculate the metrics in the Value row, can return incorrect results since it is a planned value

Therefore, @kenkunz, we can switch from stats.position.6[0].value, since it is a planned and not executed value, to using the value from the trades list i.e. the value circled in red here is correct.

Same thing for the closing value

tradingstrategy ai_strategies_enzyme-polygon-eth-breakout_closed-positions_6