Back

Indicator help

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

 

Update Backtest







0

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.


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 add

bb = 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

0


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?

 

0

Hi Dat,

1) The first line accesses the data for our ticker through the .loc method, and the .itertuples() converts the DataFrame into an object we can easily iterate through. To learn more about how to use pandas and its objects, check out their tutorials.

2) Since the BB (Bollinger Band) indicator is generated by our algorithm object, this algorithm object automatically takes care of updating our BB indicator. Since the bb variable is passed into the SymbolData constructor, and because Python object parameters operate on pass-by-reference rules (which means changes to an object anywhere is reflected everywhere that object is used), when our BB indicator is updated by the algorithm object, the change will be reflected in the SymbolData BB field as well.

Best,
Shile Wen

2

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.


Update Backtest





0

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.


Loading...

This discussion is closed