Entry Price & PNL
Overview
On Bulk Exchange, the 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.
Core Logic: Unlike systems where entry price might be a frontend calculation, on Bulk Exchange, it is a core, backend-managed value.
Updates: The
entry_price
is updated by theClearingHouse
'sreport_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).Data Integrity: The entry price is stored as a scaled integer (
u64
) to prevent floating-point inaccuracies, ensuring high precision in all calculations.
Unrealized PnL
Unrealized PnL reflects the current profit or loss of an open position. It is a vital metric for risk management and is used to determine account equity for margin calculations.
Calculation: The
calculate_position_pnl
function is responsible for computing the unrealized PnL. The formula is:PnL = (Mark Price - Entry Price) * Size
Mark Price: The calculation is always performed using the current Mark Price. This ensures that PnL reflects the fair value of the contract and is not susceptible to temporary wicks or manipulation of the last traded price.
Numerical Stability: To ensure accuracy and prevent errors, the PnL calculation is performed using a 128-bit signed integer (
i128
). This allows the system to handle potential negative PnL values safely and avoids overflow issues that could arise with smaller integer types.
Last updated