/// <summary> /// Normalize time series over number of bars; 1.0 being the average. /// </summary> /// <param name="series">input time series</param> /// <param name="n">normalizing 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>normalized time series</returns> public static ITimeSeries <double> Normalize(this ITimeSeries <double> series, int n, CacheId parentId = null, [CallerMemberName] string memberName = "", [CallerLineNumber] int lineNumber = 0) { var cacheId = new CacheId(parentId, memberName, lineNumber, series.GetHashCode(), n); return(series.Divide(series.EMA(n, cacheId), cacheId)); }
/// <summary> /// Return current drawdown in percent, as value between 0 and 1. /// </summary> /// <param name="series">input time series</param> /// <param name="n">length of observation window</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>drawdown as time series</returns> public static ITimeSeries <double> Drawdown(this ITimeSeries <double> series, int n, CacheId parentId = null, [CallerMemberName] string memberName = "", [CallerLineNumber] int lineNumber = 0) { var cacheId = new CacheId(parentId, memberName, lineNumber, series.GetHashCode(), n); // TODO: rewrite this, using buffered lambda, see MaxDrawdown return(IndicatorsBasic.Const(1.0, cacheId) .Subtract( series .Divide( series .Highest(n, cacheId), cacheId), cacheId)); }
/// <summary> /// Calculate logarithmic momentum: m = Ln(p[0] / p[n]) /// </summary> /// <param name="series">input time series</param> /// <param name="n">number of bars for regression</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>log momentum, as time series</returns> public static ITimeSeries <double> LogMomentum(this ITimeSeries <double> series, int n = 21, CacheId parentId = null, [CallerMemberName] string memberName = "", [CallerLineNumber] int lineNumber = 0) { var cacheId = new CacheId(parentId, memberName, lineNumber, series.GetHashCode(), n.GetHashCode()); #if true return(IndicatorsBasic.BufferedLambda( prev => Math.Log(series[0] / series[n]), 0.0, cacheId)); #else // retired 04/02/2019 return(series .Divide(series .Delay(n, cacheId) .Max(1e-10, cacheId), cacheId) .Log(cacheId) .Divide(n, cacheId)); #endif }
/// <summary> /// Normalize time series over number of bars; 1.0 being the average. /// </summary> /// <param name="series">input time series</param> /// <param name="n">normalizing period</param> /// <returns>normalized time series</returns> public static ITimeSeries <double> Normalize(this ITimeSeries <double> series, int n) { return(series.Divide(series.EMA(n))); }