Hi Jared Broad !
First i want to say, that you have created an amazing tool and community for not only trading but reprodcible science in
general.
I am looking at changing algorithm parameters to experiment with lower time frames.
I am looking at your pairs trading algorithm attached below.
Obiously i am getting the error -
Backtest Handled Error: Order Error: id: 2, Insufficient buying power to complete order (Value:12386.2475), Reason: Id: 2, Initial Margin: 6194.123759, Free Margin: 5883.4498536
Andrew Czeizler
I assume because i am trading a higher time frames i am running out of money.
Is there anyway to change the algorithm so it only trades a smaller selection of portoflio at one time?
Would this be the best way to fix it?
Many thanks,
Best,
Andrew
Andrew Czeizler
Apologies backtest attached.
Best,
Andrew
Jing Wu
self.Sell(symbol, quantity)
self.Buy(symbol, quantity)
The quantity here determines how many shares to buy/sell. You can calculate the order quantity to achieve target-percent holdings with
self.CalculateOrderQuantity(symbol, weight)
The "weight" controls the target percentage.
Andrew Czeizler
Thank you so much Jing!
I have made some changes and the code is attached :)
if you could give it a breif look, that would be amazing.
I am getting another error -
Backtest Handled Error: Unable to compute order quantity of XOM. Reason: The order quantity is less than the lot size of 1 and has been rounded to zero.Target order value 73.66402874735100000. Order fees 1. Order quantity 0.0000000000000000000000000000.. Returning null
Im not sure why.Many thanks,Best,AndrewJing Wu
In equity trading, lot size refers to the minimum number of shares you can buy in one transaction. The lot size is 1 for stocks. It means you should at least buy one share. You can not buy 0.3 shares or 1.3 shares. When quantity = self.CalculateOrderQuantity(symbol, weight) computes the order quantity, it returns a decimal value. When the self.Sell(symbols, quantity) or self.Buy(symbol, quantity) takes a float quantity, LEAN will round the float number to the multiple of the lot size.
This error message means the available cash is not sufficient to buy even 1 share of the specified stock. You can add a check before placing an order for example
if spread[-1] > mean + self.threshold * std: if not self.Portfolio[self.symbols[0]].Quantity > 0 and not self.Portfolio[self.symbols[0]].Quantity < 0: quantity = float(self.CalculateOrderQuantity(self.symbols[0],0.2)) if quantity > 1: self.Sell(self.symbols[1], quantity) self.Buy(self.symbols[0], ratio * quantity)
Andrew Czeizler
Hi Jing!
Thank you! It works!
amazing!
I am now trying it with forex data but for some reason i am getting errors.
I have spent an hour over it but cant see my error.
Best,
Andrew
Norman Aybar
Jing,
What would be the equivalent solution to the 'The order quantity is less than the lot size of 1' error for the algorithm framework? Playing with the execution model quantity calculation does not seem to fix it :(
Jing Wu
Hi Andrew,
The forex asset has QuoteBar data. data[symbol] in OnData(self, data) is a quote bar. You define the rolling window with QuoteBar type.
i.hist_window = RollingWindow[QuoteBar](self.numdays)
Jing Wu
Hi norman_aybar, the warning message means your buying power is not sufficient to buy even one share. You can reduce the weight if you use SetHoldings(symbol, weight). If you use the MarketOrder(symbol, quantity), you need to make sure your buying power is sufficient to buy or sell the specified quantity.
self.CalculateOrderQuantity(symbol, weight) can help you calculate the order quantity based on your available buying power and your weight allocation.
Andrew Czeizler
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!