Hi guys, got a question. My algo only places orders at 15:00:00 on certain days. I have 2x leverage set. Whenever my leverage is above 1.0, I'm noticing random margin calls for orders placed at midnight that I didn't request. Has anyone had this problem before? Is this a bug?
Thanks!
2015-09-12 00:00:00 TVIX $13.24674USD Market -1,926 Short Filled Margin Call
2015-09-10 15:00:00 TVIX $14.68467USD Market 10,241 Long Filled
2015-09-08 15:00:00 TVIX $16.96302USD Market -8,029 Short Filled
2015-09-08 00:00:00 TVIX $USD Market On Open -8,029 Short Canceled
2015-09-03 15:00:00 TVIX $15.08507USD Market 8,029 Long Filled
2015-09-01 15:00:00 TVIX $14.76522USD Market -6,951 Short Filled
2015-08-27 15:00:00 TVIX $11.95194USD Market 6,951 Long Filled
2015-08-19 15:00:00 XIV $47.83212USD Market -1,761 Short Filled
2015-08-18 15:00:00 XIV $48.49845USD Market 1,761 Long Filled
2015-08-17 15:00:00 XIV $47.79216USD Market -1,760 Short Filled
2015-07-29 15:00:00 XIV $46.99695USD Market 1,760 Long Filled
2015-07-28 15:00:00 XIV $43.70625USD Market -1,898 Short Filled
2015-07-14 15:00:00 XIV $43.81377USD Market 1,898 Long Filled
2015-06-30 15:00:00 XIV $40.42953USD Market -1,606 Short Filled
2015-06-30 00:00:00 XIV $40.42953USD Market -936 Short Filled Margin Call
2015-06-23 15:00:00 XIV $48.50846USD Market 2,542 Long Filled
2015-06-22 15:00:00 XIV $46.48347USD Market -2,683 Short Filled
2015-06-19 15:00:00 XIV $47.01697USD Market 2,683 Long Filled
2015-06-16 15:00:00 XIV $44.67528USD Market -2,306 Short Filled
2015-03-20 15:00:00 XIV $34.12409USD Market 2,306 Long Filled
2015-03-19 15:00:00 XIV $34.08588USD Market -2,209 Short Filled
2015-02-25 15:00:00 XIV $32.45242USD Market 2,209 Long Filled
2015-02-19 15:00:00 XIV $29.52045USD Market -2,395 Short Filled
2015-02-13 15:00:00 XIV $29.06904USD Market 2,395 Long Filled
2014-12-11 15:00:00 XIV $33.99597USD Market -1,838 Short Filled
2014-12-11 00:00:00 XIV $33.99597USD Market -466 Short Filled Margin Call
Stephen Oehler
Ah, my bad. These are apparently "modeled" margin calls for going below 50% of the borrowed margin. Good to know.
If anyone has any question what that means, I'll be happy to explain further.
Stephen Oehler
Actually I take it back, it looks like I'm getting margin calls for no real reason. See the attached logs and you'll note that, with the long purchase of XIV on 2011-12-05, my portfolio is suddenly dinged by a margin call four days later, after taking just a 7% hit.
Shouldn't margin calls only occur if you drop below 50% basis of the levered cash?
Time Symbol Price Quantity Type Status Value Tag 2011-12-01T20:00:00Z XIV 5.71571 1677 Market 3 9585.24567 2011-12-02T20:00:00Z XIV 5.82417 -1677 Market 3 -9767.13309 2011-12-05T20:00:00Z XIV 5.85585 1693 Market 3 9913.95405 2011-12-09T05:00:00Z XIV 5.44455 -254 Market 3 -1382.9157 Margin Call 2011-12-23T20:00:00Z XIV 6.87312 -1439 Market 3 -9890.41968 2012-01-13T20:00:00Z XIV 7.5075 1679 Market 3 12605.0925 2012-01-19T20:00:00Z XIV 7.62237 -1679 Market 3 -12797.95923
Stephen Oehler
This is interesting. I see that in the LEAN engine, margin calls are executed when no margin is remaining.
So what is happening is, when I order something with 2x leverage, I use up all of my remaining margin immediately (or it is very close to zero), which is normal. But then if the security goes down in value by even a little bit, my remaining margin becomes negative, and a margin call is lobbied against me.
Is this the real behavior of the brokerage? I was under the impression that brokerages only lobby a margin call at you if your total unlevered cash is lower than their margin requirement.
See Tradier Brokerage's definition: "The standard margin requirement for equities and ETFs trading above $3/share is 50%. This means that half of the money needed for the purchase of these stocks can be borrowed."
By the current margin call model in LEAN, isn't it impossible to ever use 2x leverage? Or rather, in order to use 2x leverage in LEAN, you'd need to always have a portfolio that is increasing in value.
Stephen Oehler
I've attached a backtest to illustrate this behavior. All the algorithm does is buy XIV with 2x leverage on 2011-12-05, and holds it. Two days later, it gets hit with a margin call after losing only 16%.
Most brokerages that have 2x margin capabilities require a 50% cash basis, meaning you should be able to lose 50% before getting dinged with a margin call.
Stephen Oehler
Sorry, here is the backtest.
Stephen Oehler
After sleeping on it, I realized that I am wrong in my thinking. To clarify, there is no bug; this is correct behavior. I'll explain in case someone has the same difficulty in understanding margins.
Most brokerages have a requirement that, in order to borrow money as leverage, you must have a minimum amount of money in your account. In Tradier's case, if you borrow money for leverage, that minimum requirement is 50% of the total leveraged + unlevered account balance. For example, let's say you have $1000 in your account, and you set your leverage to 1.5x. You are "borrowing" an additional $500 to add to your account to boost your gains (and potentially augment your losses). That $500 isn't yours, and in fact you'll be paying interest on it the longer that you have it, but you can use it as if it was yours (i.e. buy/sell, etc).
So, okay, you have $1500 effectively in your account. You purchase some shares of $AAPL. Suddenly the next day, AAPL plummets by 10%. This amounts to a $150 loss to your portfolio. None of this loss is reflected in the levered money you borrowed: it is all taken out of your contribution. So your portfolio's balance now looks like this: (your contribution: $850) + (borrowed money: $500) = $1350. This is fine and will not result in a margin call (an action where the brokerage will force a sale of some of your securities to prevent you from losing borrowed money). A margin call will only be lobbied against you if your contribution of money reduces to less than the minimum margin requirement. Recall that the minimum requirement for your contribution is 50% of the total of your account when you first made the trade. We had $1500 when we first made the trade, so 50% of that would be $750. If our contribution of money ever gets below $750, we get served a margin call.
Let's take our example further. We have $1350 in our account after having taken a 10% hit. Let's assume we lose another $200 (bad day!). Suddenly we're down to the following account balance: (your contribution: $650) + (borrowed money: $500) = $1150. Our contribution of $650 is now below the 50% mark ($750), and so we're hit with a margin call. Nasty business.
So what was confusing me before was 2x leverage; it didn't make sense to me that I would get hit with a margin call almost immediately after applying 2x leverage to a trade. But if you think about it, you'd need your 2x leveraged trade to immediately increase in value, or at least remain flat, in order to not get a margin call. Example, we apply a 2x leverage to a $1000 buy on AAPL. This means (our contribution: $1000) + (borrowed money: $1000) = $2000. Remember that our minimum requirement is 50%, so we better darn well ensure we never go below our $1000 contribution. Unfortunately this means that, in this scenario, we cannot possible take a single loss without getting a margin call.
So, in short, don't go full 2x leverage unless you can ensure that your losses can be quickly covered. A good rule of thumb that I came up with this morning is: look at your maximum drawdown and pretend that that drawdown magnitude is a common occurrence. Make sure your leverage isn't so high that you'll be triggering margin calls at that drawdown.
Jared Broad
Thank you for the detailed explaination Stephen :) I couldn't have said it better myself!
Before we decided on this setup we did empirical testing with Interactive Brokers -- submitting $20k of AAPL orders with only $10k cash, and monitoring how long it lasted before a margin call was hit. We found it would result in a IB margin call within 2-3 minutes of a price drop below our starting position.
My only addition is that its like this because the brokerages don't take any risk. The accuracy and scanning frequency of each brokerage will be different - some will be intraday (like IB) but others will be recalculated daily. We model our default from IB which gets scanned every 5 minutes and has a small buffer to avoid too many calls being placed on intraday noise.
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.
Stephen Oehler
Thanks for elaborating on that Jared! Good to know about the time-to-margin estimates.
While we're on the topic, I was wondering how fees for transactions and interest-on-margin is paid? Is it something we're supposed to pay on the side within Tradier (like on a credit card or something) or is it automatically extracted from your account? If the latter, how does it extract fees for interest-on-margin if all of your money is tied up in securities at the time? And how often does it pull those out?
I suppose these would be questions that are better asked to Tradier itself; but I was wondering how QuantConnect fits into all of this. Does QC track changes in portfolio balanced due to fees and commissions?
Jared Broad
At the moment interest is free, it does depend on each brokerage. We have a feature in progress to model the interest of margin here: https://github.com/QuantConnect/Lean/pull/449
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.
Stephen Oehler
Hi Jared,
Could you perhaps clarify "at the moment interest is free"? Is margin at Tradier brokerage free (part of that $1 per trade promotional setup)? Or do you mean that the models in LEAN currently assume interest on margin to be zero?
Stephen Oehler
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!