/// <summary> /// Calculate Relative Strength Index, as described here: /// <see href="https://en.wikipedia.org/wiki/Relative_strength_index"/> /// </summary> /// <param name="series">input time series</param> /// <param name="n">smoothing period</param> /// <param name="parentId">caller cache id, optional</param> /// <param name="memberName">caller's member name, optional</param> /// <param name="lineNumber">caller line number, optional</param> /// <returns>RSI time series</returns> public static ITimeSeries <double> RSI(this ITimeSeries <double> series, int n = 14, CacheId parentId = null, [CallerMemberName] string memberName = "", [CallerLineNumber] int lineNumber = 0) { var cacheId = new CacheId(parentId, memberName, lineNumber, series.GetHashCode(), n); ITimeSeries <double> returns = series.Return(cacheId); double avgUp = returns .Max(0.0, cacheId) .EMA(n, cacheId)[0]; double avgDown = -returns .Min(0.0, cacheId) .EMA(n, cacheId)[0]; double rs = avgUp / Math.Max(1e-10, avgDown); return(IndicatorsBasic.BufferedLambda( v => 100.0 - 100.0 / (1 + rs), 50.0, cacheId)); }