Calculating Futures Position Cost, Open Price, and Floating P&L in CTP
Summary
This implementation note explains how to extend a CTP trading gateway so position query responses include an average opening price and floating profit or loss. It proposes subclassing the trading API and overriding its investor-position response handler, then using a gateway subclass that initializes the replacement API. During each response, the handler looks up contract metadata, aggregates position quantity and cost, and derives the average price using contract size.
The example also calculates prior-day holdings differently for SHFE and INE positions than for other exchanges, accumulates frozen quantity and reported profit, and estimates floating P&L from settlement price and open cost with a direction-dependent sign. It emits accumulated positions when the query’s final response arrives, then clears the cache. This is integration guidance rather than a tested performance study; it depends on the framework’s data structures and exchange-specific position fields, and the excerpt does not discuss validation against live account statements or edge cases such as missing contract metadata.
Key ideas
- Override the investor-position query handler to derive fields absent from the default CTP response handling.
- Aggregate position quantity and cost, using contract size to compute an average opening price.
- Calculate prior-day volume with separate handling for SHFE and INE positions.
- Estimate floating P&L from settlement price and open cost, reversing the sign for short positions.
- Emit positions after the final query response and then clear the aggregation cache.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.