𝗦𝗰𝗮𝗿𝗮𝗯 𝗗𝗶𝗮𝗴𝗻𝗼𝘀𝘁𝗶𝗰 𝗙𝗶𝗲𝗹𝗱 𝗧𝗲𝘀𝘁 #𝟬𝟯𝟮: 𝗢𝗽𝘁𝗶𝗼𝗻 𝗧𝗮𝗿𝗴𝗲𝘁 𝗣𝗿𝗶𝗰𝗶𝗻𝗴 𝗕𝗼𝘂𝗻𝗱𝗮𝗿𝘆
QuantConnect Lean had a serious bug in option target sizing.
If you requested a 10% target for an option position, the engine could deliver more than you asked for.
This happened because the system used the wrong price to calculate quantity. It used a mid or last price instead of the actual price you must pay to buy the option.
When the bid/ask spread is wide, this error grows.
The engine was using security.Price for margin calculations. For a long option, you must buy at the ask. If the engine calculates sizing based on a lower mid-price, your final position will exceed your target weight once the order fills.
We identified the exact repair boundary.
We did not rewrite the whole portfolio construction engine. We did not change how every security type works. We did not change the execution layer.
Instead, we fixed the option-specific margin pricing path.
The fix follows these rules:
- For long option targets: Use the ask price when available.
- For short option targets: Use the bid price when available.
- If no quote is available: Fall back to the existing last/mark price.
This keeps the repair local and safe. It uses better data when it exists but preserves old behavior when it does not.
Validation results:
- Release build passed.
- Focused option-margin tests: 41/41 passed.
- Added regression coverage for both long-side ask pricing and short-side bid pricing.
This fix ensures the engine respects your risk contract by using executable prices for sizing.
Optional learning community: https://t.me/GyaanSetuAi