/// <summary> /// Calculate True Strength Index of input time series, as described here: /// <see href="https://en.wikipedia.org/wiki/True_strength_index"/> /// </summary> /// <param name="series">input time series</param> /// <param name="r">smoothing period for momentum</param> /// <param name="s">smoothing period for smoothed momentum</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>TSI time series</returns> public static ITimeSeries <double> TSI(this ITimeSeries <double> series, int r = 25, int s = 13, CacheId parentId = null, [CallerMemberName] string memberName = "", [CallerLineNumber] int lineNumber = 0) { var cacheId = new CacheId(parentId, memberName, lineNumber, series.GetHashCode(), r, s); ITimeSeries <double> momentum = series .Return(cacheId); double numerator = momentum .EMA(r, cacheId) .EMA(s, cacheId)[0]; double denominator = momentum .AbsValue(cacheId) .EMA(r, cacheId) .EMA(s, cacheId)[0]; return(IndicatorsBasic.BufferedLambda( v => 100.0 * numerator / Math.Max(1e-10, denominator), 0.5, cacheId)); }
/// <summary> /// Calculate Commodity Channel Index of input time series, as described here: /// <see href="https://en.wikipedia.org/wiki/Commodity_channel_index"/> /// </summary> /// <param name="series">input time series</param> /// <param name="n">averaging length</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>CCI time series</returns> public static ITimeSeries <double> CCI(this ITimeSeries <double> series, int n = 20, CacheId parentId = null, [CallerMemberName] string memberName = "", [CallerLineNumber] int lineNumber = 0) { var cacheId = new CacheId(parentId, memberName, lineNumber, series.GetHashCode(), n); return(IndicatorsBasic.BufferedLambda( (v) => { ITimeSeries <double> delta = series .Subtract( series .SMA(n, cacheId), cacheId); ITimeSeries <double> meanDeviation = delta .AbsValue(cacheId) .SMA(n, cacheId); return delta[0] / Math.Max(1e-10, 0.015 * meanDeviation[0]); }, 0.5, cacheId)); }