Hello fellows, I´m new in LEAN and I have the next question:
How can I use the RelativeStrenghtIndex indicator in order to filter the universe selection, In the Bootcamp, there is a similarity
but whit EMA conditions, but I have no clue how to replicate it but just with RSI or another indicator such as MACD for example that have many variables to fill, no just the numbers of bar to take in consideration.
class EMAMomentumUniverse(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019, 1, 7)
self.SetEndDate(2019, 4, 1)
self.SetCash(100000)
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.CoarseSelectionFunction)
self.averages = { }
def CoarseSelectionFunction(self, universe):
selected = []
universe = sorted(universe, key=lambda c: c.DollarVolume, reverse=True)
universe = [c for c in universe if c.Price > 10][:100]
for coarse in universe:
symbol = coarse.Symbol
if symbol not in self.averages:
# 1. Call history to get an array of 200 days of history data
history = self.History(symbol, 200, Resolution.Daily)
#2. Adjust SelectionData to pass in the history result
self.averages[symbol] = SelectionData(history)
self.averages[symbol].update(self.Time, coarse.AdjustedPrice)
if self.averages[symbol].is_ready() and self.averages[symbol].fast > self.averages[symbol].slow:
selected.append(symbol)
return selected[:10]
def OnSecuritiesChanged(self, changes):
for security in changes.RemovedSecurities:
self.Liquidate(security.Symbol)
for security in changes.AddedSecurities:
self.SetHoldings(security.Symbol, 0.10)
class SelectionData():
#3. Update the constructor to accept a history array
def __init__(self, history):
self.slow = ExponentialMovingAverage(200)
self.fast = ExponentialMovingAverage(50)
#4. Loop over the history data and update the indicators
for data in history.itertuples():
self.fast.Update(data.Index[1], data.close)
self.slow.Update(data.Index[1], data.close)
def is_ready(self):
return self.slow.IsReady and self.fast.IsReady
def update(self, time, price):
self.fast.Update(time, price)
self.slow.Update(time, price)
In the SelectionData class, the Bootcamp example places the ExponentialMovingAverage with just one parameter, the bar number to be considered for calculating it, but how can I set up this with other indicators such as RSI, or MACD that have many others variables to fill.
Thanks a lot!
Ryan Riordon
Hello Rene,
You might find these pages helpful:
https://www.quantconnect.com/docs/algorithm-reference/indicators#Indicators-Initializing-IndicatorsSearch for RSI on that page.
https://www.quantconnect.com/forum/discussion/8045/universe-coarse-fine-selection-with-past-indicators-values-indexed-in-rollingwindow/p1https://www.quantconnect.com/forum/discussion/4545/rolling-window-of-consolidated-indicator-data/p1https://www.quantconnect.com/forum/discussion/8340/moving-average-with-rolling-window/p1https://www.quantconnect.com/forum/discussion/6307/how-to-update-several-rolling-windows-with-multiple-tradebarconsolidators/p1https://www.quantconnect.com/forum/discussion/2743/macd-history-rolling-window/p1https://www.quantconnect.com/forum/discussion/9551/putting-sub-indicator-properties-into-rolling-window/p1Rene Ordosgoitia
Thank you Ryan I appreciate your reply! I will check it out in order to learn more!
Derek Melchin
Hi Rene,
To replace the indicators to this algorithm, we should:
See the attached backtest for reference. Note that the conditional in the CoarseSelectionFunction has not been set yet.
Going forward, consider reviewing our Introduction to Financial Python tutorial series.
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.
Rene Ordosgoitia
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!