Back

Set Custom Margin Model For Short Options

I am looking at the documentation (OptionMarginModel and ISecurityMarginModel) and am having trouble understanding how I can set a custom margin model for short options. Right now if you short an option it appears as if you need 80% of Underlying's Market Price. However, the IB Formula is only approximately 20% plus the option price. I set the brokerage model to InteractiveBrokersBrokerage but the margin for short options is still ~80%. The problem here is that my backtest's orders are being rejected due to insufficient funds even though in real trading it would still be permitted.

How can I create a custom margin model for short options?

Update Backtest








To create a custom margin model, we need to create a class that implements the ISecurityMarginModel interface. 
In your particular case, you can copy the OptionsMarginModel, rename it as, for example, MyOptionsMarginModel and edit the GetMarginRequirement method. 
Finally after you subscribe to the options, you must assign your new custom model:

// In Initialize
var option = AddOption("GOOG");
option.MarginModel = new MyOptionMarginModel();

 

1

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.


Thanks for the reply. So I tried adding that class but am now getting an extensive error about a zero index despite not modifying anything about the existing OptionsMarginModel class. I shared the error with you (Alexandre) under the project name Option Selling Example.

0

Hi Sofyan, please share the project for free code assistance or for private support submit a ticket through the left tab in the IDE.

0

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.


Attached is the backtest + code. The error was resolved, but changing around GetMarginRequirement doesn't seem to do anything. The same amount of margin seems to apply regardless of what is in that function. I'm also having trouble printing out the actual margin requirements of a particular contract once I get it (after line 90).

0


I compared your IBOptionMarginModel against OptionMarginModel and there is not difference in the code.

0

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.


I left it as is because it didn't matter what I put in GetMargin. For example, in this backtest I just return 0.2m and it didn't make any difference to the margin calculation. I can put 0, 1, a hundred, a million, etc and it makes no impact. 

0


Sorry for taking too long to get back to you.
If we set the margin model to the option object:

var option = AddOption(UnderlyingTicker);
option.MarginModel = new IBOptionMarginModel();

we are just setting it for the "canonical symbol" and this will not propagate to the any options.
Therefore we need to use SetSecurityInitializer method. It sets the security initializer function, used to initialize/configure securities after creation:

// Must come before AddOption
SetSecurityInitializer(new MySecurityInitializer());
var option = AddOption(_symbol);

where MySecurityInitializer is defined as:

public class MySecurityInitializer : ISecurityInitializer
{
public void Initialize(Security security, bool seedSecurity)
{
if (security is Option)
{
security.MarginModel = new IBOptionMarginModel();
}
}
}

 

1

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.


Alternatively, we can use the following SetSecurityInitializer method with Func overload:

SetSecurityInitializer(x => {
if (x is Option)
{
x.MarginModel = new IBOptionMarginModel();
}});

and we don't need to create the extra "MySecurityInitializer" class from the solution above.

0

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.


Thanks, this did the trick. However, the price of VXX being determined in the margin model is different than the one in the algo. It isn't happening with SPY options or other symbols, but the Margin Model Class thinks that the price of VXX is $176.68 at 3/10/2014. Meanwhile, the main algorithm thinks the price of VXX is $44.38. I assume this is because $176.88 is the smoothed out price with VXX's reverse splits, while $44.38 was the actual price in March 2014. 

How do we get the actual March 2014 price of $44.38 in the margin model rather than the $176.88?

https://www.quantconnect.com/backtests/11319/585132/8ad11b45573373cda935c24f74bcd9c0-log.txt

0


For Options you must use DataNormalizationMode.Raw.

Securities[_symbol].SetDataNormalizationMode(DataNormalizationMode.TotalReturn);

 

0

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.


So I already had that in my code (in main.cs)....Am I supposed to put some version of this in the IBOptionMarginModel? In IBOptionMarginModel GetMarginRequirement function I tried setting the below, but the IBOptionModel still thinks VXX's price is ~$177. How do you normalize inside an OptionMarginModel?

security.SetDataNormalizationMode(DataNormalizationMode.TotalReturn);

 

0

In you main.cs, you have: 

security.SetDataNormalizationMode(DataNormalizationMode.TotalReturn);

but, like Jared said, you should:

security.SetDataNormalizationMode(DataNormalizationMode.Raw);

Under this settings, I could verify that contract.UnderlyingLastPrice in OptionSellAlgo.OrderCallOption method and underlying.Close in IBOptionMarginModel.GetMarginRequirement present the same value.

 

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed