I have been having issues filling orders using SetHoldings with PortfolioTargets. I am frequently getting the error:
Backtest Handled Error: Order Error: id: 2042, Insufficient buying power to complete order (Value:5383.8783), Reason: Id: 2042, Initial Margin: 2694.784163303, Free Margin: 1658.964742388870114.
The strangest thing to me is sometimes this error comes in groups larger than ten, maybe around 30, even thought my list of portfolio targets is of size 10.
I am wondering why this error is occuring so often - even when I set self.Settings.FreePortfolioValuePercentage = 0.1.
I am also wondering how I can see significantly more than 10 of these errors in less than one month when in theory I should only be making at most 10 trades a month.
Lastly, as a bit of an unrelated question, by choosing stocks with the lowers DollarVolume, I noticed I get equities with DollarVolume less than 10 or 20. Does this mean only $10-$20 of that stock were traded the most recent day? If so, is it unreasonable to expect to be able to trade $1000 worth of that stock in a given day? Does the backtesting engine allow me to make unreasonable trades like this?
Rahul Chowdhury
Hi Michael,
It isn't best practice to use SetHoldings with PortfolioTargets. PortfolioTargets are typically reserved for the algorithms using the framework, specifically the portfolio construction model creates portfolio targets based on insights, then the execution model creates orders based on those portfolio targets. Instead, use SetHoldings(symbol, percentage), which enters you into a position in that symbol of a given percentage of your free portfolio value.
For low liquidity stocks, such as those with less than $20 in volume, the orders placed may not have realistic fills. Lean's default fill and slippage models assume that all orders are immediately and completely filled. In reality, for such low liquidity stocks, your orders may only be partially filled and may have large amounts of slippage if a market order is placed. You can create and implement your own slippage and fill models which can take into consideration liquidity. Learn more in the documentation on reality modeling.
Best
Rahul
Michael Boguslaw
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!