Back

Struggling to add an RSI selection factor to this algo can someone help?

Add a momentum factor to this previously published Leveraged ETF algo to shift the tickers arround based on momentum. I am trying to add RSI indactor to sell and buy tickers above 70 or 80 but I don't seem to be able to figure out how to add it in. I can see the data in the libary but can't get past that. Can anyone help?

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.


I tried something like this. I can log the RSI values using self.rsi.get(secuity_hold.Symbol.Value) to get the values from the rsi dictionary using secuity_hold.Symbol.Value as a key but It dose not seem to like that if I use that statement in an if statement. Not sure what to try next. Any ideas?


for kvp in self.Portfolio:
#self.Log(f"\nkvp: {type(kvp)}\nkvp.Key: {type(kvp.Key)}\nkvp.Value: {type(kvp.Value)}")
security_hold = kvp.Value
#self.Log(str(self.rsi.get(security_hold.Symbol.Value) ))
#self.Log(str(kvp.Value))
#self.Log(str(security_hold.Symbol.Value)) #shows all the securities in the libary
#self.Log(str(security_hold.Invested)) #shows all the securities in the libary invested or not invested as True or False
# liquidate the security which is no longer in the top3 momentum list

if security_hold.Invested and (security_hold.Symbol.Value not in top3.index):
self.Liquidate(security_hold.Symbol)

security_hold = kvp.Value
if security_hold.Invested and (int(self.rsi.get(security_hold.Symbol.Value)) >= 80):
self.Liquidate(security_hold.Symbol)

 

0

This seems close, I loaded both dictionaries into a dataframe. I can filter the data frame by MOMP but I can't seem to remove rows with RSI greater than 80. I keep getting the same error. 

it does not like 

RSIlessthan80 = alldata.drop(alldata[alldata['rsivalue']<=80].index)

Gives cannot get managed object

 

def Rebalance(self):
if self.IsWarmingUp: return

#pd = panda data series for mom (not used)
#top3 = pd.Series(self.data).sort_values(ascending = False)[:3]

#self.Log(str(top3.index))
#self.Log(str(pd.Series(self.data)))
#self.Log(str(self.data))
alldata = pd.DataFrame({'momp':pd.Series(self.momp),'rsivalue':pd.Series(self.rsi)})
RSIlessthan80 = alldata.drop(alldata[alldata['rsivalue']<=80].index)
top3 = RSIlessthan80.sort_values(by='momp', ascending = False)[:3]
#self.Log(str(alldata))

 

0


Hi Steven,

To ensure the algorithm is resilient to ticker changes, we should change `self.symbols` to `tickers` and save a list of symbols as a class variable.

self.symbols = []
tickers = ["TQQQ", "UBT", "TECL","UGLD","TYD"]
for ticker in tickers:
symbol = self.AddEquity(ticker, Resolution.Hour).Symbol
self.symbols.append(symbol)
self.mom[symbol] = self.MOM(symbol, period, Resolution.Hour)
self.rsi[symbol] = self.RSI(symbol, period2, Resolution.Daily)
self.momp[symbol] = self.MOMP(symbol, period3, Resolution.Daily)

This change also has the benefit of reducing the number of loops the Initialize method has to go through.

In regards to the error described above, we can resolve this by filling the `alldata` DataFrame with indicator values instead of indicator objects.

alldata = pd.DataFrame({'momp':[self.mom[symbol].Current.Value for symbol in self.symbols],
'rsi': [self.rsi[symbol].Current.Value for symbol in self.symbols]})
highest_rsi = alldata[alldata['rsi'] > 80]
top3 = highest_rsi.sort_values(by='momp', ascending = False)[:3]
top3_symbols = [self.symbols[i] for i in top3.index]

Lastly, it should be pointed out that the indicator values will be stale on each call to the Rebalance method. Notice how the resolution of the indicators is Hour/Daily while the Rebalance method is called at 15:30. When using daily data, it's best to schedule events at midnight. To schedule events at other times, we recommend increasing the data resolution.

See the attached backtest for reference.

Best,
Derek Melchin

1

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.


Derek, 

Thank you so much for your help. You should get a big raise!

0

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