Hi,
Newbie here trying to work my way through building first algo on Quantconnect. Using QCAlgorithm and RSIalpha as reference (https://github.com/QuantConnect/Lean/blob/master/Algorithm.Framework/Alphas/RsiAlphaModel.py)
How do I compare an indicator result vs the last close bar?
Example: calculate Bollinger band (bb). If last bar close > 2 times Bollinger upper band, trip high. Have used the GetState function in the RSI example:
def GetState(self, bb, previous):
if last_bar_close > 2*bb.UpperBand.Current.Value:
return State.TrippedHigh
Yuxin Guan
Hello Dat,
With a reference to RsiAlphaModel.py, I attached a backtest which achieves the requirement in the question.
Within
def GetState(self, rsi, previous, bb, close):
, we put
if close > 2*bb.UpperBand.Current.Value: return State.TrippedHigh
In order to get variables
"code"
and"bb"
, we need to add them in"class SymbolData"
:self.BB = bb self.Close = close
To update
"code"
and"bb"
, in"def OnSecuritiesChanged(self, algorithm, changes):",
we add:for symbol in addedSymbols: bb = algorithm.BB(symbol, self.period, 3, MovingAverageType.Wilders, self.resolution) close = algorithm.Securities[symbol].Close
and
for tuple in history.loc[ticker].itertuples(): bb.Update(tuple.Index, tuple.close) self.symbolDataBySymbol[symbol] = SymbolData(symbol, rsi, bb, close)
To generate insights, in
"def Update(self, algorithm, data):"
we addbb = symbolData.BB close = symbolData.Close state = self.GetState(rsi, previous_state, bb, close) if state != previous_state and rsi.IsReady and bb.IsReady:
Hope this could help.
Best,
Yuxin Guan
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.
Dat En
Thank you! I managed to get this to work. I still don't quite understand some parts of the code:
1) What are the following lines for?
for tuple in history.loc[ticker].itertuples():
bb.Update(tuple.Index, tuple.close
2) bb = symbolData.BB is assigned in Update module whenever data is updated. However the symbolData is only updated in OnSecuritiesChanged module when the security is added:
bb = algorithm.BB(symbol, self.period, 3, MovingAverageType.Wilders, self.resolution)
self.symbolDataBySymbol[symbol] = SymbolData(symbol, rsi, bb, close)
How is symbolData being updated when new data comes in then?
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!