I've written an algorithm using SetBrokerageModel(BrokerageName.Alpaca, AccountType.Cash); It runs every market open and uses MACD to decide whether to be in SPY or TLT. I use SetHoldings(...) to accomplish this setting one to 0 and the other to less than 1.0 (I've tried various values). However, when it runs, I routinely get "Insufficient buying power to complete order" for a period of 4 days, then the order is filled. I'm assuming this means the model requires 4 days after a sell for funds to settle before the cash is available for buy? But reading the Alpaca docs, I dont' see where this is the case. My understanding is that the results of a sell should be available as "buying power" immediately after it is filled. What am I missing here? Algorithm below...
https://www.quantconnect.com/terminal/processCache?request=embedded_backtest_fef4081514e053a7fbca784a4133dcc0.html
Halldor Andersen
Hi Daniel.
Implement your trading logic in the method OnData() rather than calling Schedule.On() for an intraday event when using daily data resolution for SPY and TLT. Also, use Liquidate() rather than SetHoldings() to close a position:
// In OnData() // Liquidate TLT and allocate 80% of portfolio value to SPY if (fast > slow){ if(Portfolio["TLT"].Invested) Liquidate("TLT"); if(!Portfolio["SPY"].Invested) SetHoldings("SPY", weight); } // Liquidate SPY and allocate 80% of portfolio value to TLT else if (slow > fast){ if (Portfolio["SPY"].Invested) Liquidate("SPY"); if (!Portfolio["TLT"].Invested) SetHoldings("TLT", weight); }
I've attached backtest where I demonstrate the above.
Daniel Root
Thanks for the reply. I understand how that is a better way to write the algorithm, but this does not resolve the issue. If you run the algo you posted and look at the Trades tab and logging, you find that many of the orders fail with a margin error. My expectation (which I'm sure is wrong somehow) is that as long as I have > 25k to get past Pattern Day Trade Protection, then I should be able to buy immediately after a sell executes. I am basing this on the "Buying Power" section here:
https://docs.alpaca.markets/orders/#buying-powerJack Simonson
Hi Daniel,
Although the sell orders are being filled, the actual settlement for cash accounts takes 2-3 business days (you can read more here). The trades are primarily being placed on Wednesdays or Thursdays and so the settlement won't occur until the next Monday or Tuesday, which is why you are seeing the time gap between order-fill and settlement. Right now, we only support cash accounts for Alpaca, but we plan to add margin account functionality.
Hogan Pope
I'm currently having a very similar issue, have margin accounts been added yet? I ask so I can know if this is my problem or it's something else
Alethea Lin
Hi Hogan,
Sorry to hear that you are having a similar issue. Unfortunately, Alpaca has not added margin support to their brokerage implementation. Please follow this Git Issue for updates.
Thanks for your support!
Daniel Root
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!