Hello,
I am trying to construct an algorithm that compares the values of the current bar with those of the previous bars.
Example:
I would like to check
if (low 3 bars ago is > than the current high) and the (current high is greater than the previous low)
How would i code these sorts of criteria in Python?
I appreciate any and all help.
Conor Flynn
Hi Samwel,
I'd recommend utilizing RollingWindows for looking back at prior bars. Here's an example of some code to better show what a RollingWindow is:
# define the RollingWindow in initialize
def Initialize(self):
self.AddEquity("SPY", Resolution.Daily)
# note length is (desired lookback period + 1)
self.lowValues = RollingWindow[float](4)
# in the OnData method, we add the low value to the RollingWindow
# each time the data updates
def OnData(self, data):
# add data
self.lowValues.Add(data["SPY"].Low)
# make sure window is properly warmed up before using
if(not self.lowValues.IsReady):
return
if(self.lowValues[3] > data["SPY"].High and [other conditionals]):
# perform action
Hopefully that helps!
-Conor
Samwel Kibet
Conor Flynn , Thank you for your help. I managed to create algorithm, and it works smoothly. However, i noticed it was only taking 1 trade within a years duration. I don't know if the problem is my strategy, or the conditions i set, to exit the trade. Would you be so kind and take look, at my code and see where i may have made an error, and where i could tweak, to better the algorithm.
i attached a backtest below.
Derek Melchin
Hi Samwel Kibet,
Only 1 trade is placed because the exit logic below makes it so the Liquidate method is never called.
if current_price > self.highestsymbolprice: self.highestsymbolprice = current_price updateFields = UpdateOrderFields() updateFields.StopPrice = self.highestsymbolprice * 0.98 self.trailingstop = updateFields.StopPrice if current_price <= self.trailingstop: self.Liquidate(ticker)
The attached backtest shows that the security price crosses below the trailing stop price, but `current_price <= self.trailingstop` is always False.
For assistance setting trailing stop orders, refer to this bootcamp lesson.
Best,
Derek Melchin
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.
Samwel Kibet
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!