Back

Accessing index value in RSI

Wilder defined RSI as 100 - 100/(1+RS) where RS = avg. of N days' up closes / avg. of N days' down closes.
The RelativeStrengthIndex implementation at https://github.com/QuantConnect/Lean/blob/master/Indicators/RelativeStrengthIndex.cs#L77 seems to provide via public getters, an AverageGain and AverageLoss, but no "IndexValue". The index is actually correctly calculated but is inside the protected function computeNextValue that I cannot access from my code.

Do I have to manually do this?
RSI(computed) = 100 - 100 / (1 + rsi.AverageGain.Value/rsi.AverageLoss.Value)
Update Backtest








The AverageGain and AverageLoss properties are considered 'secondary' outputs of the RSI indicator. The 'primary' output can be accessed via the public 'Current' property on the indicator.

I've attached a short example showing the usage of the RSI, in short you should be able to just 'use' your variable as a decimal.
var rsi = RSI("SPY", 14, MovingAverageType.Wilders, Resolution.Daily);
if (rsi > 50m) {// }


Also have a peak at the moving average cross example. It shows direct usage of the indicators.

Let me know if this answers your question!
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.


Yes, it does. Thanks Michael.
0

Anyone know how to get the RSI in Python?

rsi = RSI("SPY", 14, MovingAverageType.Wilders, Resolution.Daily)

Above does not work. I think you have to call RSI like it is a class method within QCAlgorythm class (strange).

rsi = self.RSI("SPY", 14, MovingAverageType.Wilders, Resolution.Daily)

When I run the above code I always get 0. 

0

Doug Wood  - The RSI isn't ready when its first created; you need to wait for it to be "primed". You should only create it once in initialize then reuse the object. The RSI is a helper method which creates the indicator and subscribes it for data.

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.


Thanks  Jared Broad I am not sure what I am missing. my Initialize method looks like this:

def Initialize(self):
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage)
self.SetCash(100000)
self.SetStartDate(2016,1,1)
self.SetEndDate(2017,8,1)

# subscribe to data with 20 day warmup
self.spy = self.AddEquity("SPY", Resolution.Daily)
self.SetWarmUp(20)

# 10 mins before market opens call before_trading_starts
self.Schedule.On(self.DateRules.EveryDay(self.spy.Symbol),
self.TimeRules.AfterMarketOpen(self.spy.Symbol, -10),
Action(self.before_trading_starts))

My goal is to simply log the RSI (for the prior day's close) 10 mins before the market opens - using my scheduled "before_trading_starts" method:

def before_trading_starts(self):
self.rsi = self.RSI("SPY", 14, MovingAverageType.Wilders, Resolution.Daily)
#self.rsi = self.RSI("SPY", 14, MovingAverageType.Wilders, Resolution.Daily).Current
#self.rsi = self.RSI("SPY", 14, MovingAverageType.Wilders, Resolution.Daily).Current.Value
self.Log('RSI: {}'.format(self.rsi))

You can see I am trying a few things different things. I get either 0 or what looks like a datetime object as a response. Most (maybe all) of the forum posts and docs on the subject are written in C# so I am trying to get up to speed...

0

In QuantConnect we can create an indicator with its constructor or with a helper method:

self.rsi = RelativeStrengthIndex(14, MovingAverageType.Wilders)
# or
self.rsi = RSI("SPY", 14, MovingAverageType.Wilders, Resolution.Daily)

Please note that if we use the helper method, we must define the symbol and the resolution (is it optional, since it will consider the security subscription resolution if not defined). In this case, the engine will update the indicator for us. Otherwise, with the contructor method, we would have to update the indicator:

self.rsi.Update(time, value)

Every time you call the helper method, you will create a new indicator and assign it to self.rsi. That is why you are always getting 0: the indicator is not ready yet since it needs 14 data points.

How to fix it?
We just need to create it once in Initialize:

def Initialize(self):
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage)
self.SetCash(100000)
self.SetStartDate(2016,1,1)
self.SetEndDate(2017,8,1)

# subscribe to data with 20 day warmup
self.spy = self.AddEquity("SPY", Resolution.Daily)
self.SetWarmUp(20)

self.rsi = RSI("SPY", 14, MovingAverageType.Wilders, Resolution.Daily)

We can see the values change at before_trading_starts:

def before_trading_starts(self):
# It should be ready, since there is a warm up period was set
if not self.rsi.IsReady: return

self.Log('RSI: {}'.format(self.rsi))
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.


A few add-on questions. How do we pull the value out? I seeem to get an RSI Object when I try to log the value. 

Also how do we set it so that we are performing the RSI calculations on the 'Open' field of data. 

0

When we are working with C#, we can rely on implicit conversion. Our indicators are implicitly converted into a decimal value. In python, we need to explicitly get it:

value = self.rsi.Current.Value

Helper methods of data point indicators, those who take only one value (close, open, high, low, etc), have a parameter that allows us to select the field:

self.rsi = RSI("SPY", 14, MovingAverageType.Wilders, Resolution.Daily, Field.Open)
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.


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