Lean icon indicating copy to clipboard operation
Lean copied to clipboard

Can't Maximize Position Size of OptionStrategies

Open DerekMelchin opened this issue 3 years ago • 4 comments

Expected Behavior

We can place an order with OptionStrategies to maximize the position size (minimize the amount of margin remaining).

Actual Behavior

When we try to maximize the position size of OptionStrategies, we receive the error

Order Error: id: 1, Insufficient buying power to complete order

Related issue: #2709 Related thread: Examples of how to use OptionStrategyPositionGroupBuyingPowerModel?

Potential Solution

Fix the bug in the OptionStrategyPositionGroupBuyingPowerModel. None of the examples in the repo using OptionStrategies maximize the position size. They all use a small order size of <= 10. We need more unit tests to cover the case of attaining the maximum position size.

Reproducing the Problem

Run this backtest.

When placing a single order for the BullPutSpread, with self.Order(strategy, 54) # Works in interval [1, 54] the orders go through without error but we have $7,111 of $10K margin remaining. Increasing the order size to 55 throws the error.

Additionally, when placing the orders incrementally with

for i in range(64): # Works in interval [1, 64]
    self.Order(strategy, 1)

we can achieve a larger position size but we have $6,480 of $10K margin remaining. Increasing the range to range(65) throws the error.

System Information

Checklist

  • [x] I have completely filled out this template
  • [x] I have confirmed that this issue exists on the current master branch
  • [x] I have confirmed that this is not a duplicate issue by searching issues
  • [x] I have provided detailed steps to reproduce the issue

DerekMelchin avatar Jun 22 '21 18:06 DerekMelchin

I think this is because the orders are being placed individually internally by Lean, and later if finds strategy matches from the current portfolio, reducing margin requirements. The combo orders are not there yet which is what should probably be used instead. In the provided example backtest splitting the Order(strategy, x) in multiple operations should do the trick

Martin-Molinero avatar Jun 22 '21 19:06 Martin-Molinero

Hey, @AlexCatarino asked me to add my case to the issue

I backtest a futures spread trading strategy

It seems that QuantConnect broker simulation is not correct about margin amount for future spreads QuantConnect just summarise margins of two contracts. In fact QuantConnect requires 4 times bigger margin than Interactive Broker does

I expected QuantConnect to reproduce futures spread margin correctly as broker does

QuantConnect requires 18k$ margin. The strategy short CL Jul 21 and long CL Sep 21 contracts. qc code

image

Interactive Broker required 4k$ margin for that spread image

wheredyoufindthis avatar Jun 27 '21 17:06 wheredyoufindthis

Any ETA or expected timeline for this issue?

ikamanu avatar Jul 25 '21 06:07 ikamanu

Is there any workaround for this issue for the time being?

rafael-trevisan avatar Jun 09 '22 03:06 rafael-trevisan

It would be nice to have something like

self.SetHoldings(option_strategy, 1)

so we can maximize the position size.

DerekMelchin avatar Jan 12 '23 22:01 DerekMelchin

It would be nice to have something like

self.SetHoldings(option_strategy, 1)

so we can maximize the position size.

And please also consider adding Insight(option_strategy, ...) and PortfolioTarget(option_stratetgy, quantity) so that we can use it in the context of the Algorithm Framework as well. See also #6661.

ArthurAsenheimer avatar Jan 13 '23 08:01 ArthurAsenheimer

The underlying bug here was solved, opening new feature request to add missing sugar syntax for option strategies https://github.com/QuantConnect/Lean/issues/7261.

Martin-Molinero avatar May 22 '23 18:05 Martin-Molinero