I am trying to backtest my double calendar spread strategy, which includes both long call calendar spread and long put calendar spread. I found how to place a single calendar spread order from the documentation center. However, when I tried to place a double calendar spread, which has four legs, I got the “Insufficient Buying Power” error. All four legs could not be placed.
If I placed the two calendar spreads separately, only the first order, either put or call, can be placed successfully; the second one also got the “Insufficient Buying Power” error.
Here is the code
def on_data(self, slice: Slice) -> None:
if slice.time.hour == 09 and slice.time.minute == 45:
self.Debug(f'MarginRemaining={self.Portfolio.MarginRemaining}')
self.Debug(self.option_symbol)
self.Debug(slice.option_chains)
chains = slice.option_chains.get(self.option_symbol)
short_expire_date = slice.time.date() + timedelta(days=14)
long_expire_date = slice.time.date() + timedelta(days=15)
legs = []
if self._put_delta != 0:
puts = [i for i in chains if i.right == OptionRight.PUT]
puts_short = [i for i in puts if i.expiry.date() == short_expire_date]
puts_long = [i for i in puts if i.expiry.date() == long_expire_date]
deltas = [i.greeks.delta for i in puts_short]
if len(puts_short) and len(puts_long):
short_put = min(puts_short, key=lambda obj: abs(abs(obj.greeks.delta) - self._put_delta))
long_put = next((obj for obj in puts_long if obj.strike == short_put.strike), None)
if short_put and long_put:
self.Debug(f'Sell put at {short_put.strike}, {np.mean([short_put.ask_price, short_put.bid_price])}')
self.Debug(f'Buy put at {long_put.strike}, {np.mean([long_put.ask_price, long_put.bid_price])}')
# self.MarketOrder(short_put.Symbol, -1)
# self.MarketOrder(long_put.Symbol, 1)
legs = [Leg.create(short_put.symbol, -1), Leg.create(long_put.symbol, 1)]
## If I do self.combo_market_order(legs) here, it can go through but the call side will fail due to insufficient buying power.
if self._call_delta != 0:
calls = [i for i in chains if i.right == OptionRight.CALL]
calls_short = [i for i in calls if i.expiry.date() == short_expire_date]
calls_long = [i for i in calls if i.expiry.date() == long_expire_date]
if len(calls_short) and len(calls_long):
short_call = min(calls_short, key=lambda obj: abs(abs(obj.greeks.delta) - self._call_delta))
long_call = next((obj for obj in calls_long if obj.strike == short_call.strike), None)
if short_call and long_call:
self.Debug(f'Sell call at {short_call.strike}, {np.mean([short_call.ask_price, short_call.bid_price])}')
self.Debug(f'Buy call {long_call.strike}, {np.mean([long_call.ask_price, long_call.bid_price])}')
# self.MarketOrder(short_call.Symbol, -1)
# self.MarketOrder(long_call.Symbol, 1)
legs = legs + [Leg.create(short_call.symbol, -1), Leg.create(long_call.symbol, 1)]
if len(legs) > 0:
self.combo_market_order(legs, 1) ### Here the legs can include four legs.
Mia Alissi
To address the "Insufficient Buying Power" error when placing a double calendar spread, you can consider the following steps based on the documentation and API details:
Review Buying Power Requirements: Ensure you understand the margin requirements for options trading. The buying power required for a combo order can be significant, especially with multiple legs.
Check Portfolio Margin: Verify that your portfolio has enough margin available to cover the combined margin requirements of all four legs of the double calendar spread. You can use
self.Portfolio.MarginRemaining
to check the available margin.Separate Orders: If placing both calendar spreads simultaneously results in insufficient buying power, consider placing them separately. Ensure that the first spread does not consume all available margin.
Adjust Order Quantity: Reduce the quantity of the orders to fit within the available buying power, or increase the cash in your account to meet the margin requirements.
Use Custom Buying Power Model: Implement a custom buying power model to simulate different margin requirements or to ensure that the algorithm can handle the orders as expected during backtesting.
Debug Margin Usage: Use debugging statements to monitor the margin usage and available buying power before and after placing each order to identify where the constraints are being exceeded.
Consult Documentation: Refer to the QuantConnect documentation on buying power models and combo market orders to ensure compliance with the platform's requirements and limitations.
Here's how you might adjust your code to include debugging for margin usage:
By adding these debug statements, you can track how much margin is being used and adjust your strategy accordingly. If the margin is insufficient, consider reducing the order size or increasing your account's cash balance.
Sources: Buying Power - QuantConnect.com, Combo Market Orders - QuantConnect.com, Combo Leg Limit Orders - QuantConnect.com, Combo Limit Orders - QuantConnect.com, Index Option Call Calendar Spread Algorithm
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Jie Liu
Hi Mia, Thank you very much for your quick response, but I don't think it's because of the margin problem. As you can see from my code, I also dumped the price of each leg, and here is the log.
You can see there is enough margin to place this order. For the put, the price will be 29.8-25.35=4.45. For the call, the price will be 24.65-20.5=4.15. So totally, it should only cost me (4.45 + 4.15) x 100 = 860.
I guess it could be because your backtest system didn't handle the double calendar strategy well. I see IRON Condo, which is another 4-leg strategy, is supported. I assume it should work, although I haven't tried it. I see single calendar spread is also supported. But this is double calendar.
Louis Szeto
Hi Jie
The initial margin requirement is not the same as the maintenance margin. In this case, although the maintenance margin is not a problem, the initial margin requirement is still a limit.
If you are sure your broker permits this trade, you can just abandon the initial margin limitation by using the NullBuyingPowerModel:
Best
Louis
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Jie Liu
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!