Hello, I've been trying really hard to understand the buying power calculation and SetHoldings(), I've searched this forum as well as the GitHub issues and still couldn't find what I want to know, I really hope you guys can help me.
As shown in the attached backtest, I call SetHoldings() and log various attributes, the logging result is shown below:
These are my questions:
1. Why is the initial buying power $100000 instead of $200000? I expected my buying power to be $200000 because my leverage is 2.
2. Why did my buying power become ~$50000 after buying the stock?
3. Why did the SetHoldings("WMT", 1) only buy 1556 shares of the stock instead of 1557? The cash was sufficient to buy 1557 shares and pay for the fees. The strange thing is that it bought one single share the next day.
Thanks a lot in advance!!
Michael Manus
very good question!
but very very bad approach!
1 & 2) read the documentation my friend:
SetHoldings sets a fraction of unlevered equity. e.g. If you have 2x available leverage,
and SetHoldings to 1.0 the algorithm will use 1.0 of your available buying power.
To maximize buying power in this case you would make the SetHoldings fractions total 2.0.
Michael Manus
in the algorithm lab click on the left API TAB and check the portfolio section following methods:
GetBuyingPower
GetMarginRemaining
MarginRemaining
..
HanByul P
Mochunhei, As Michael said, 'SetHoldings('WMT', 1.0)' keeps maintaining the weight of the stock holding to the fixed 100 pecrent of portfolio (1.0x). Your example algo will not use up entire leverage (2.0x) because of 'SetHoldings('WMT', 1.0). If you want to use all of margin you put, you should change it to 'SetHoldings('WMT', 2.0). And also, you need to change 'Resolution.Daily' to 'Minute' or use 'Schedule' function if you want to trade more frequently.
Or you can approach different way: Change your order type to a quantity based one (e.g. MarketOrder, and so on). I attached a sample algo where it uses up all the margin available in two trades (See 'Total Cost' in the 'Logs'.). It will buy 1.0x of total portfoio value each time until it consumes all the margin you set. Don't be bothered by 'GetBuyingPower'. Once you set leverage, you don't have to worry about the 'BuyingPower'.
One thing you should be careful of is that you should watch the leverage because you might get a margin call if your portfolio value drops significantly.
BTW, I think 'SetHoldings' method is a very good and convenient way to control your leverage level to a certain level. Hope this helps. Thank you.
Seersquant
For me this is not working anymore, has the name/args of the function changed?
symb_bp = algorithm.Portfolio.GetMarginRemaining("SPY", OrderDirection.Buy) TypeError : No method matches given arguments for GetMarginRemaining (Open Stacktrace)
Â
Rahul Chowdhury
Hi Seersquant,
There's been recent changes in the way buying power and margin are calculated. You can check out this thread to learn more about that.
You can access the remaining margin with
algorithm.Portfolio.MarginRemaining
Â
Seersquant
Thanks, that's helpful. So this is total MarginRemaing, or BuyingPower, based on 2X or 4X leverage. If we start with 25K cash would we expect this number to be 50K or 100K?
Rahul Chowdhury
Hi Seersquant,
It would be $25k; Assuming 2x leverage; a new AAPL position worth $25k would reduce your margin to 12.5k. The margin impact = cost/leverage.
Mochunhei
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!