Recomputing Average Cost and Floating P&L for Existing Futures Positions
Summary
This note addresses a position-accounting problem reported for Shanghai-listed futures: after opening new contracts and closing older ones, the remaining same-day position’s average price can become zero. It presents a recalculation approach that starts with the existing position’s price, volume, and contract size, then incorporates the incoming position cost and volume to derive a revised average price. The method also updates open cost, open price, realized profit and loss, and frozen quantity using the position direction.
For floating profit, it compares settlement value with the stored open cost and assigns the sign according to whether the position is long or short. The example is a code fragment rather than a complete explanation or tested fix. It does not specify the broker data conventions, contract-specific accounting rules, or how to handle edge cases such as zero volume or inconsistent cost fields, so those details need verification in the target system.
Key ideas
- The note recalculates an existing position’s average price by combining its prior cost with the reported position cost.
- Position volume and profit and loss are updated from the incoming position data.
- Frozen quantity is accumulated using the field corresponding to the position direction.
- Floating profit is based on settlement value relative to open cost, with the sign adjusted for long or short positions.
- The fragment’s field conventions and edge cases require validation in the intended trading system.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.