Hello. In order to calculate the number of shares I can buy, I use this formula:
self.equity = self.Portfolio.GetBuyingPower(self.symbol, OrderDirection.Buy)
quantity = math.floor(self.equity / self.Securities[self.symbol].Close)
But when testing on live trading, I keep getting this error:
LiveTradingResultHandler.OrderEvent(): Time: 07/17/2023 19:00:00 OrderID: 9 EventID: 1 Symbol: TSLA Status: Invalid Quantity: 3640 Message: Order Error: ids: [9], Insufficient buying power to complete orders (Value:[1498419.6520]), Reason: Id: 9, Initial Margin: 1498446.2640736949846468781985, Free Margin: 1024936.045111858458839390115514. BrokerId:
This error also occurred during the backtest, but only a few times during the year of trading and the algorithm generally worked fine:
Order Error: ids: [3], Insufficient buying power to complete orders (Value: [106648,96]), Reason: Id: 3, Initial Margin: 106650.72, Free Margin: 105541.48
As I understand it, GetBuyingPower should already be returning the amount to me, given the margin, but it seems that this is not happening. How do I fix this error?
Second:
And the second small question I wanted to know: when I run a live trade, the log.txt file records all the information about it. During the weekend when the exchange is down, most of the entries in the file look like this:
Isolator.ExecuteWithTimeLimit(): Used: 148, Sample: 138, App: 1101, CurrentTimeStepElapsed: 00:00.000. CPU: 5%
InteractiveBrokersBrokerage.HandleError(): RequestId: -1 ErrorCode: 2103 - Market data farm connection is broken:hfarm
InteractiveBrokersBrokerage.StartGatewayRestartTask(): skipped request: restart task already scheduled
InteractiveBrokersBrokerage.HandleError(): RequestId: -1 ErrorCode: 2157 - Sec-def data farm connection is broken:secdefhk
InteractiveBrokersBrokerage.HandleError(): RequestId: -1 ErrorCode: 2105 - HMDS data farm connection is broken:hkhmds
InteractiveBrokersBrokerage.StartGatewayRestartTask(): skipped request: restart task already scheduled
And it's a very long scroll through to find the right information. Is there any way to view the report but without such technical messages?
Mia Alissi
Hello Adam,
Regarding your first question, the
GetBuyingPower
method returns the buying power for a specific security, considering the portfolio's total margin available. However, it doesn't guarantee that you will have enough buying power to complete the order when it's placed, especially in live trading. This is because the market conditions and prices can change rapidly, and the buying power can be affected by other orders that are filled around the same time.One common scenario where this error occurs is when you place a market on open order with daily data. If you place the order with
SetHoldings
or useCalculateOrderQuantity
to determine the order quantity, LEAN calculates the order quantity based on the market close price. If the open price on the following day makes your order more expensive, then you may have insufficient buying power. To avoid the order response error in this case, you can either use intraday data and place trades when the market is open or adjust your buying power buffer. Here is how you can adjust your buying power buffer:(Source: QuantConnect Documentation)
Regarding your second question, unfortunately, the information extracts provided do not contain any details about filtering or customizing the log output for live trading. You may want to check the QuantConnect documentation or forum for more information on this topic.
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.
Adam C Brownlie
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!