entry_price
and the calculation of Profit and Loss (PnL) are fundamental components of the core system logic, managed directly by the ClearingHouse
. This ensures that all accounting is precise, robust, and based on real-time data.
Entry Price
The entry_price
is a critical field within the Position
data structure for each user. It represents the volume-weighted average price at which the current position was established.
entry_price
is updated by the ClearingHouse
’s report_trade
function every time a trade is executed. This function accurately recalculates the volume-weighted average price whether the trade increases, decreases, or completely flips the direction of the position (e.g., from long to short).u64
) to prevent floating-point inaccuracies, ensuring high precision in all calculations.calculate_position_pnl
function is responsible for computing the unrealized PnL. The formula is:
PnL = (Mark Price - Entry Price) * Size