/// <summary> /// Calculate Sharpe Ratio, as described here: /// <see href="https://en.wikipedia.org/wiki/Sharpe_ratio"/> /// </summary> /// <param name="series"></param> /// <param name="riskFreeRate"></param> /// <param name="n"></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></returns> public static ITimeSeries <double> SharpeRatio(this ITimeSeries <double> series, ITimeSeries <double> riskFreeRate, int n, CacheId parentId = null, [CallerMemberName] string memberName = "", [CallerLineNumber] int lineNumber = 0) { var cacheId = new CacheId(parentId, memberName, lineNumber, series.GetHashCode(), riskFreeRate.GetHashCode(), n); var excessReturn = series .Return(cacheId) .Subtract(riskFreeRate .Return(cacheId), cacheId); return(excessReturn .EMA(n, cacheId) .Divide(excessReturn .FastStandardDeviation(n, cacheId) .Max(IndicatorsBasic.Const(1e-10, cacheId), cacheId), cacheId)); }
/// <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)); }
/// <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)); }