I’m trying to use half of my remaining margin to buy securities with a 2x leverage margin setup. My live account also provides 2x leverage, but both the simulation and live brokerage are returning incorrect values for remaining margin and buying power. I’m using an Alpaca margin account.
For example, with an initial cash balance of $100K, the remaining margin and buying power should be $200K. However, both self.portfolio.get_buying_power(s.symbol, OrderDirection.BUY) and self.portfolio.margin_remaining return only $100K.
Am I missing something here? I’ve attached sample code to verify the margin calculation. Thank you for your help.
Mia Alissi
Here is a sample algorithm for Marc to verify correct 2x margin/buying power setup in an Alpaca margin account. This template logs the relevant buying power and margin values before and after an order, with comments to clarify expected output:
Instructions:
GetBuyingPower()andMarginRemainingshould show $200,000 after initialization (for $100k cash with 2x leverage).Let me know if you spot a discrepancy when running this, and I can help investigate further!
Sources: Buying Power - QuantConnect.com, Custom Buying Power Model Algorithm, Custom Margin Interest Rate Model Algorithm, Margin Call Events Algorithm, Bybit Crypto Regression Algorithm
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.
Marc Woo
The test code still shows incorrect values. Here are the logs:
2021-01-04 09:31:00 : INITIAL ─ Expected BP: 200,000.00 | GetBuyingPower(): 100,000.00 | MarginRemaining: 100,000.00
2021-01-04 09:31:00 : POST-ORDER ─ Bought 283 sh @ ≈352.20. New GetBuyingPower(): 50,158.16 | MarginRemaining: 50,158.16
Marc Woo
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!