Contents
Indicator Reference
Relative Moving Average
Create Manual Indicators
You can manually create a RelativeMovingAverage
indicator, so it doesn’t automatically update. Manual indicators let you update their values with any data you choose. The following reference table describes the RelativeMovingAverage
constructor.
RelativeMovingAverage()1/2
RelativeMovingAverage QuantConnect.Indicators.RelativeMovingAverage (string
name,int
period )
Initializes a new instance of the RelativeMovingAverage class with the specified name and period.
RelativeMovingAverage()2/2
RelativeMovingAverage QuantConnect.Indicators.RelativeMovingAverage (
int
period
)
Initializes a new instance of the SimpleMovingAverage class with the default name and period.
Update Manual Indicators
You can update the indicator automatically or manually.
Automatic Update
To register a manual indicator for automatic updates with the security data, call the RegisterIndicator
method.
private RelativeMovingAverage _rma; // In Initialize() _rma = new RelativeMovingAverage(name, period); _rma.Updated += IndicatorUpdateMethod; RegisterIndicator(symbol, _rma, Resolution.Daily); // In IndicatorUpdateMethod() if (_rma.IsReady) { var indicatorValue = _rma.Current.Value; }
# In Initialize() self.rma = RelativeMovingAverage(name, period) self.rma.Updated += self.IndicatorUpdateMethod self.RegisterIndicator(symbol, self.rma, Resolution.Daily) # In IndicatorUpdateMethod() if self.rma.IsReady: indicator_value = self.rma.Current.Value
To customize the data that automatically updates the indicator, see Custom Indicator Periods and Custom Indicator Values.
Manual Update
Updating your indicator manually enables you to control when the indicator is updated and what data you use to update it. To manually update the indicator, call the Update
method with time/decimal pair. The indicator will only be ready after you prime it with enough data.
private RelativeMovingAverage _rma; private Symbol symbol; // In Initialize() _rma = new RelativeMovingAverage(period); symbol = AddEquity("SPY").Symbol; // In OnData() if (data.ContainsKey(_symbol)) { _rma.Update(data[symbol].EndTime, data[symbol].High); } if (_rma.IsReady) { var indicatorValue = _rma.Current.Value; }
# In Initialize() self.rma = RelativeMovingAverage(period) self.symbol = self.AddEquity("SPY").Symbol # In OnData() if data.ContainsKey(self.symbol): self.rma.Update(data[self.symbol].EndTime, data[self.symbol].High) if self.rma.IsReady: indicator_value = self.rma.Current.Value
Create Automatic Indicators
The RMA method creates an RelativeMovingAverage indicator, sets up a consolidator to update the indicator, and then returns the indicator so you can use it in your algorithm.
The following reference table describes the RMA
method:
RMA()1/1
RelativeMovingAverage QuantConnect.Algorithm.QCAlgorithm.RMA (Symbol
symbol,Int32
period,*Nullable<Resolution>
resolution,*Func<IBaseData, Decimal>
selector )
Creates a new Relative Moving Average indicator for the symbol. The indicator will be automatically updated on the given resolution.
If you don't provide a resolution, it defauls to the security resolution. If you provide a resolution, it must be greater than or equal to the resolution of the security. For instance, if you subscribe to hourly data for a security, you should update its indicator with data that spans 1 hour or longer.
For more information about the selector argument, see Alternative Price Fields.
Get Indicator Values
To get the value of the indicator, use its Current.Value
attribute.
private RelativeMovingAverage _rma; // In Initialize() var symbol = AddEquity("SPY").Symbol; _rma = RMA(symbol, period); // In OnData() if (_rma.IsReady) { var shortAverage = _rma.ShortAverage.Current.Value; var mediumAverage = _rma.MediumAverage.Current.Value; var longAverage = _rma.LongAverage.Current.Value; var current = _rma.Current.Value; }
# In Initialize() symbol = self.AddEquity("SPY").Symbol self.rma = self.RMA(symbol, period) # In OnData() if self.rma.IsReady: short_average = self.rma.ShortAverage.Current.Value medium_average = self.rma.MediumAverage.Current.Value long_average = self.rma.LongAverage.Current.Value current = self.rma.Current.Value
Visualization
To plot indicator values, in the OnData
event handler, call the Plot
method.
private RelativeMovingAverage _rma; // In Initialize() var symbol = AddEquity("SPY").Symbol; _rma = RMA(symbol, period); // In OnData() if (_rma.IsReady) { Plot("My Indicators", "shortaverage", _rma.ShortAverage); Plot("My Indicators", "mediumaverage", _rma.MediumAverage); Plot("My Indicators", "longaverage", _rma.LongAverage); Plot("My Indicators", "relativemovingaverage", _rma.Current); }
# In Initialize() symbol = self.AddEquity("SPY").Symbol self.rma = self.RMA(symbol, period) # In OnData() if self.rma.IsReady: self.Plot("My Indicators", "shortaverage", self.rma.ShortAverage) self.Plot("My Indicators", "mediumaverage", self.rma.MediumAverage) self.Plot("My Indicators", "longaverage", self.rma.LongAverage) self.Plot("My Indicators", "relativemovingaverage", self.rma.Current)
For more information about plotting indicators, see Plotting Indicators.