Back

Unsettled funds

Hmm, today I’m learning about margin calls.

 

I’ve been familiar with the 3 day settlement period for buying/selling for a long time, though I’m terrified that I’m missing something.

I *might* be missing something.

 

Here’s the scenario:

Margin account.

Say you start out with $99k stock of XYZ with settled funds, and you have $1000 of cash in your margin account.

You sell $99k of XYZ, and then immediately turn around and buy $99k of ABC the same day using the unsettled funds.

I just realized that during the 3 day settlement period for XYZ, I am actually borrowing $99k on margin,

and that a margin call could happen during that period.

 

This happened in my algorithm in Oct. 2008 when Halliburton stock dropped from $25 to $15 during a 3 day period.

The algorithm got a margin call to sell HAL at $15 !!!

My first reaction was, "oh, another bug in my code".  Now I realize I was missing something!

2 days later HAL bounced back to as high as $23.50, but the damage was already done.

 

 

I guess the moral of the story is that you shouldn’t ever use unsettled funds?

I wonder, when you get a margin call, do you have any choice as to which stock gets sold to cover,

or does the brokerage always pick the one that was bought using unsettled funds?

 

Crap this kind of scenario is going to take a ton of logic to protect against.

Does anyone have a strategy to protect against this?

 

Also, does anyone here have an algorithm that detects which stocks are getting margin calls, and buys them?  (kind of joking, kind of seriously asking)

 

Update Backtest








I followed your reasoning, but it would be clearer if I could see it in action (code).

If you do not mind sharing your code with the community, could you please attach a backtest? It will make it easy for the community to look into it and help you.

Margin call orders are issued when Portfolio.MarginRemaining is zero or lower. You can add logic to verify whether the remaining margin is reaching a fraction of Portfolio.TotalPortfolioValue (e.g. 20% - Portfolio.MarginRemaining < Portfolio.TotalPortfolio * 0,2) and liquidade securities with the worst performance.

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.


Ok, I added some log messages and now I see whats going wrong.

I've been using Portfolio.Cash instead of Portfolio.MarginRemaining when buying.

However, I don't understand why my MarginRemaining is not always equal to cash, since I'm not buying on margin?

Securities[symbol].SetLeverage(1m); // we are not using leverage

 

This should probably be a separate post:

The attached algorithm only contains my execution logic, with strategy ommitted.  I really don't care to spend a ton of time on my limit order execution logic, but I realize it needs to be much more sophisticated.  Anyone out there in the same boat that wants to partner on the execution phase?  My execution phase is only 500 lines of code and it probably would need to be more like 10000 lines to be viable, 100000 lines to be competitive.  I'm thinking the execution logic needs to have a callback into my strategy code, so that the strategy code can tweak some knobs each second tick to adjust the way the limit order(s) are updated or cancelled.

2008-01-07 09:31:00 statuscash 100000.00 margin 100000.00
2008-01-07 09:31:00 entrycash 67010.53 margin 67010.53 HAL 977 @33.76
2008-01-07 09:31:00 entrycash 34032.00 margin 34032.00 CVX 482 @68.42HAL 977 @33.76
2008-01-07 09:31:00 entrycash 1042.19 margin 1042.19 XOM 450 @73.31CVX 482 @68.42HAL 977 @33.76
2008-02-12 13:41:00 statuscash 1042.19 margin -8417.56 XOM 450 @67.36CVX 482 @59.53HAL 977 @31.20
2008-03-20 11:21:00 statuscash 1042.19 margin -6757.36 XOM 450 @67.21CVX 482 @61.08HAL 977 @32.20
2008-04-25 15:31:00 statuscash 1042.19 margin 8834.91 XOM 450 @73.55CVX 482 @67.81HAL 977 @41.92
2008-06-03 13:11:00 statuscash 1042.19 margin 11129.41 XOM 450 @69.97CVX 482 @73.85HAL 977 @42.94
2008-07-10 10:51:00 statuscash 1042.19 margin 7331.99 XOM 450 @67.76CVX 482 @69.49HAL 977 @42.22
2008-08-14 15:01:00 statuscash 1042.19 margin -165.30 XOM 450 @62.42CVX 482 @63.77HAL 977 @39.83
2008-09-22 12:41:00 statuscash 1042.19 margin -3598.21 XOM 450 @65.00CVX 482 @66.10HAL 977 @33.98
0


Is this also valid?  This sets maintenance model to 0.

            foreach (string symbol in symbols)
            {
                AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
                //Securities[symbol].SlippageModel = new ConstantSlippageModel(0.001m);
                //Securities[symbol].SetLeverage(1m); // we are not using leverage
                SecurityMarginModel model = new SecurityMarginModel(1,0);
                Securities[symbol].MarginModel = model;
            }

0

Portfolio.MarginRemaining takes into account the Equity (Portfolio.TotalPortfolioValue) and the maintenance margin of every security in your portfolio. The maintenance margin of a security depends on the volume of your position when you open it and its leverage, so it is a constant. On the other hand, total portfolio value depends on value of your securities.
In your case, the price of your securities are lower than when you bought it, so your Portfolio.TotalPortfolioValue is lower and consequently Portfolio.MarginRemaining is negative. On the other hand, Portfolio.Cash is a reservoir of P&L that only changes when positions are opened or closed (unless you are trading currencies).

These calculations are made even if your leverage is 1. In this case, when you open a position, you are required to have all the cash (not only a margin) and there won't be margin calls.

That code snipped if valid, but it is redundant, since that is the default. You cannot put a value lower than 1 for leverage.
 

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.


First off, hallelujah that this is open source cuz otherwise I would have been ripping my hair out all weekend trying to figure out what was happening.

This prevents margin calls.

Securities[symbol].SetLeverage(1m); // we are not using leverage

 

However, I cannot use a significant portion of my Portfolio.Cash unless I use this instead:

SecurityMarginModel model = new SecurityMarginModel(1, 0); // _maintenanceMarginRequirement = 0
Securities[symbol].MarginModel = model;

For example, if value of my stocks drop, then TotalMarginRemaining becomes negative. In the project I attached above, my purchases fail to execute even though there is enough Portfolio.Cash to purchase them.

Something near line 319 of SecurityTransactionManager.cs prevents you from using all your cash.  That said, it looks like any change to that code would break something else.

To be safe, I should probably use this instead:

new SecurityMarginModel(1, 0.02); // _maintenanceMarginRequirement 1/50th of 1 percent

 

0

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