https://www.quantconnect.com/terminal/processCache/?request=embedded_backtest_58ca1040e9b3648370fab13fb81edecf.html
Setup/Strategy
This example will sell puts on SPY, once per day.
It will make a total of 12 orders. Order 11 and 12 are denied for insufficient buying power.
This date range just happens to end in failed orders, if you let it continue another month, many more orders will fill (margin is always changing, ofcourse)
Order #1 - Margin Calculation
After the first trade, I already don't understand why margin used is only 77109.8 instead of either 0 (because the account has cash) or 492220(beacuse that's how much it would cost to cover the assignment).
I believe some sort of Brokerage MarginModel is being used when the MarketOrder method is called, but I assumed I should be able to determine available margin/buying power with existing methods and simply not place the trade, but I can't figure it out.
Starting BP: 1000000.0 Margin Remaining: 1000000.0 Margin Used: 0.0
Selling puts: 23@214.0
Ending BP: 922861.45 Margin Remaining: 922861.45 Margin Used: 77109.8
Order #2 - Margin Variance
After the above trade margin used is 77109.8. Before making another trade, the margin used is calculated to be 76539.4(maybe premiums settled? or underlying went up slightly?)
Again, I don't see how the final Margin used figure was computed. I have looked in github for the method but can't seem to narrow it down.
Starting BP: 923512.35 Margin Remaining: 923512.35 Margin Used: 76539.40000000001
Selling puts: 21@214.0
Ending BP: 853675.7999999999 Margin Remaining: 853675.7999999999 Margin Used: 146360.2
Order #11 and #12 - Order Error
Finally even though I have 462k in buying power on 1/23, I cannot sell 10 puts at 214 strike - which should only need 214k to cover!
And again the next day on the 24th, the same order is attempted and denied.
#11 - (1/23) 170217P00214000SellMarket
Starting BP: 462330.10000000003 Margin Remaining: 462330.10000000003 Margin Used: 541116.4
Selling puts: 10@214.0
Backtest Handled Error: Order Error: id: 11
Insufficient buying power to complete order (Value:-355)
Reason: Id: 11
Initial Margin: -532235.90000000000000000000001
Free Margin: 462330.09999999999999999999989
#12 - (1/24) 170217P0021400Sell Market
Starting BP: 461649.7 Margin Remaining: 461649.7 Margin Used: 542282.8
Selling puts: 10@214.0
Backtest Handled Error: Order Error: id: 12
Insufficient buying power to complete order (Value:-325)
Reason: Id: 12
Initial Margin: -533372.29999999999999999999998
Free Margin: 461649.69999999999999999999999
Questions
- What do Value: -355 and -325 represent in the Error returned from MarketOrder?
- What is InitialMargin and why doesn't it match MarginUsed? Even the error suggests I should have available margin to sell this put.
- What MarginModel is being used by default? Where Can I see the implementation?
- Can I completely disable margin for backtesting purposes and treat it like a pure cash level II options account where the put is actually fully secured with the cash instead of margin monies?
I have read about 10 other forum posts talking about Margin with options as well as this exact insufficient buying power, but so far I haven't gotten any of the suggestions to work with my example nor do they perfectly fit my problem. Thank you in advance, let me know if you need more examples or more information! I'm pretty sure I just don't understand some key thing so my assumptions are wrong.
Player two
This doesn't seem to work. Still got margin called on a cash account? I'll post simplified backtest and code somepoint tonight.
self.SetBrokerageModel(BrokerageName.Default, QuantConnect.AccountType.Cash)
Alexandre Catarino
Hi player two ,
We have been working on Options margin modeling in Lean. Please check out the following GitHub issue:
Options margin modeling #4065
Meanwhile, you can implement a custom buying power model (see example at CustomBuyingPowerModelAlgorithm) that consider that there is sufficient margin for every order:
class CustomBuyingPowerModel(BuyingPowerModel): def HasSufficientBuyingPowerForOrder(self, parameters): return HasSufficientBuyingPowerForOrderResult(True)
Player two
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!