/// <summary> /// Calculate range over the specified number of past bars. /// </summary> /// <param name="series">input time series</param> /// <param name="n">number of bars to search</param> /// <returns>range between highest and lowest value of past n bars</returns> public static ITimeSeries <double> Range(this ITimeSeries <double> series, int n) { return(series .Highest(n) .Subtract(series .Lowest(n))); }
/// <summary> /// Calculate Stochastic Oscillator, as described here: /// <see href="https://en.wikipedia.org/wiki/Stochastic_oscillator"/> /// </summary> /// <param name="series">input time series</param> /// <param name="n">oscillator 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>Stochastic Oscillator as time series</returns> public static _StochasticOscillator StochasticOscillator(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); var container = Cache <_StochasticOscillator> .GetData( cacheId, () => new _StochasticOscillator()); double hh = series .Highest(n, cacheId)[0]; double ll = series .Lowest(n, cacheId)[0]; double price = series[0]; container.PercentK = IndicatorsBasic.BufferedLambda( v => 100.0 * (price - ll) / Math.Max(1e-10, hh - ll), 50.0, cacheId); container.PercentD = container.PercentK .SMA(3, cacheId); return(container); }
/// <summary> /// Calculate range over the specified number of past bars. /// </summary> /// <param name="series">input time series</param> /// <param name="n">number of bars to search</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>range between highest and lowest value of past n bars</returns> public static ITimeSeries <double> Range(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 .Highest(n, cacheId) .Subtract(series .Lowest(n, cacheId), cacheId)); }
/// <summary> /// Calculate volatility estimate from recent trading range. /// </summary> /// <param name="series">input time series</param> /// <param name="n">length of calculation 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>volatility as time series</returns> public static ITimeSeries <double> VolatilityFromRange(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(IndicatorsBasic.BufferedLambda( (v) => { double hi = series.Highest(n)[0]; double lo = series.Lowest(n)[0]; return 0.80 * Math.Sqrt(1.0 / n) * Math.Log(hi / lo); }, 0.0, cacheId)); }
/// <summary> /// Calculate Williams %R, as described here: /// <see href="https://en.wikipedia.org/wiki/Williams_%25R"/> /// </summary> /// <param name="series">input time series</param> /// <param name="n">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>Williams %R as time series</returns> public static ITimeSeries <double> WilliamsPercentR(this ITimeSeries <double> series, int n = 10, CacheId parentId = null, [CallerMemberName] string memberName = "", [CallerLineNumber] int lineNumber = 0) { var cacheId = new CacheId(parentId, memberName, lineNumber, series.GetHashCode(), n); return(IndicatorsBasic.BufferedLambda( (v) => { double hh = series.Highest(n)[0]; double ll = series.Lowest(n)[0]; double price = series[0]; return -100.0 * (hh - price) / Math.Max(1e-10, hh - ll); }, -50.0, cacheId)); }