/// <summary> /// Computes the next value of this indicator from the given state /// </summary> /// <param name="input">The input given to the indicator</param> /// <returns>A new value for this indicator</returns> protected override decimal ComputeNextValue(IBaseDataBar input) { _trueRange.Update(input); _directionalMovementPlus.Update(input); _directionalMovementMinus.Update(input); _smoothedTrueRange.Update(Current); _smoothedDirectionalMovementPlus.Update(Current); _smoothedDirectionalMovementMinus.Update(Current); _previousInput = input; PositiveDirectionalIndex.Update(Current); NegativeDirectionalIndex.Update(Current); var diff = Math.Abs(PositiveDirectionalIndex.Current.Value - NegativeDirectionalIndex.Current.Value); var sum = PositiveDirectionalIndex.Current.Value + NegativeDirectionalIndex.Current.Value; if (sum == 0) { return(50m); } _averageDirectionalIndex.Update(input.EndTime, 100m * diff / sum); return(_averageDirectionalIndex.Current.Value); }
/// <summary> /// Computes the Slow Stochastic %K. /// </summary> /// <param name="period">The period.</param> /// <param name="constantK">The constant k.</param> /// <param name="input">The input.</param> /// <returns>The Slow Stochastics %K value.</returns> private decimal ComputeStochK(int period, int constantK, IBaseDataBar input) { var stochK = _maximum.Samples >= (period + constantK - 1) ? _sumFastK / constantK : new decimal(0.0); _sumSlowK.Update(input.Time, stochK); return(stochK * 100); }
/// <summary> /// Computes the Slow Stochastic %K. /// </summary> /// <param name="period">The period.</param> /// <param name="constantK">The constant k.</param> /// <param name="input">The input.</param> /// <returns>The Slow Stochastics %K value.</returns> private decimal ComputeStochK(int period, int constantK, IBaseData input) { var stochK = _maximum.Samples >= (period + constantK - 1) ? _sumFastK.Current.Value / constantK : decimal.Zero; _sumSlowK.Update(input.Time, stochK); return(stochK * 100); }
/// <summary> /// Computes the next value of this indicator from the given state /// </summary> /// <param name="input">The input given to the indicator</param> /// <returns>A new value for this indicator</returns> protected override decimal ComputeNextValue(IBaseDataBar input) { var deMax = 0m; var deMin = 0m; if (Samples > 1) { // By default, DeMin and DeMax must be 0m initially deMax = Math.Max(input.High - _lastHigh, 0); deMin = Math.Max(_lastLow - input.Low, 0); } _maxMA.Update(input.Time, deMax); _minMA.Update(input.Time, deMin); _lastHigh = input.High; _lastLow = input.Low; if (!IsReady) { return(0m); } var currentValue = _maxMA + _minMA; return(currentValue > 0m ? _maxMA / currentValue : 0m); }
/// <summary> /// Computes the Fast Stochastic %K. /// </summary> /// <param name="period">The period.</param> /// <param name="input">The input.</param> /// <returns>The Fast Stochastics %K value.</returns> private decimal ComputeFastStoch(int period, TradeBar input) { var fastStoch = _maximum.Samples >= period ? (input.Close - _mininum) / (_maximum - _mininum) : new decimal(0.0); _sumFastK.Update(input.Time, fastStoch); return(fastStoch * 100); }
/// <summary> /// Computes the next value of this indicator from the given state /// </summary> /// <param name="input">The input given to the indicator</param> /// <returns>A new value for this indicator</returns> protected override decimal ComputeNextValue(IBaseDataBar input) { // compute the true range and then send it to our smoother TrueRange.Update(input); _smoother.Update(input.Time, TrueRange); return(_smoother.Current.Value); }
/// <summary> /// Computes the next value of this indicator from the given state /// </summary> /// <param name="input">The input given to the indicator</param> protected override decimal ComputeNextValue(IBaseDataBar input) { _maximum.Update(input.Time, input.High); _mininum.Update(input.Time, input.Low); FastStoch.Update(input); StochK.Update(input); StochD.Update(input); return(FastStoch); }
/// <summary> /// Computes the next value of this indicator from the given state /// </summary> /// <param name="input">The input given to the indicator</param> /// <returns>A new value for this indicator</returns> protected override decimal ComputeNextValue(TradeBar input) { // compute the true range and then send it to our smoother var trueRange = ComputeTrueRange(_previousInput, input); _smoother.Update(input.Time, trueRange); _previousInput = input; return(_smoother.Current.Value); }
/// <summary> /// Configures the second indicator to receive automatic updates from the first by attaching an event handler /// to first.DataConsolidated /// </summary> /// <param name="second">The indicator that receives data from the first</param> /// <param name="first">The indicator that sends data via DataConsolidated even to the second</param> /// <param name="waitForFirstToReady">True to only send updates to the second if first.IsReady returns true, false to alway send updates to second</param> /// <returns>The reference to the second indicator to allow for method chaining</returns> public static IndicatorBase <IndicatorDataPoint> Of <T>(this IndicatorBase <IndicatorDataPoint> second, IndicatorBase <T> first, bool waitForFirstToReady = true) where T : BaseData { first.Updated += (sender, consolidated) => { // only send the data along if we're ready if (!waitForFirstToReady || first.IsReady) { second.Update(consolidated); } }; return(second); }
/// <summary> /// Computes the Fast Stochastic %K. /// </summary> /// <param name="period">The period.</param> /// <param name="input">The input.</param> /// <returns>The Fast Stochastics %K value.</returns> private decimal ComputeFastStoch(int period, IBaseDataBar input) { var denominator = _maximum - _minimum; // if there's no range, just return constant zero if (denominator == 0m) { return(0m); } var numerator = input.Close - _minimum; var fastStoch = _maximum.Samples >= period ? numerator / denominator : decimal.Zero; _sumFastK.Update(input.Time, fastStoch); return(fastStoch * 100); }
/// <summary> /// Computes the next value of this indicator from the given state /// </summary> /// <param name="input">The input given to the indicator</param> /// <returns>A new value for this indicator</returns> protected override decimal ComputeNextValue(IndicatorDataPoint input) { // Update internal indicator, automatically updates _maximum and _minimum _MACD.Update(input); // Update our Stochastics K, automatically updates our Stochastics D variable which is a smoothed version of K var MACD_K = new IndicatorDataPoint(input.Time, ComputeStoch(_MACD.Current.Value, _maximum.Current.Value, _minimum.Current.Value)); _K.Update(MACD_K); // With our Stochastic D values calculate PF var PF = new IndicatorDataPoint(input.Time, ComputeStoch(_D.Current.Value, _maximumD.Current.Value, _minimumD.Current.Value)); _PF.Update(PF); return(_PFF.Current.Value); }
/// <summary> /// Computes the Fast Stochastic %K. /// </summary> /// <param name="period">The period.</param> /// <param name="input">The input.</param> /// <returns>The Fast Stochastics %K value.</returns> private decimal ComputeFastStoch(int period, IBaseDataBar input) { var denominator = (_maximum - _mininum); var numerator = (input.Close - _mininum); decimal fastStoch; if (denominator == 0m) { // if there's no range, just return constant zero fastStoch = 0m; } else { fastStoch = _maximum.Samples >= period ? numerator / denominator : new decimal(0.0); } _sumFastK.Update(input.Time, fastStoch); return(fastStoch * 100); }
/// <summary> /// Updates the state of this indicator with the given value and returns true /// if this indicator is ready, false otherwise /// </summary> /// <param name="indicator">The indicator to be updated</param> /// <param name="time">The time associated with the value</param> /// <param name="value">The value to use to update this indicator</param> /// <returns>True if this indicator is ready, false otherwise</returns> public static bool Update(this IndicatorBase <IndicatorDataPoint> indicator, DateTime time, decimal value) { return(indicator.Update(new IndicatorDataPoint(time, value))); }
/// <summary> /// Updates the state of this indicator with the given value and returns true /// if this indicator is ready, false otherwise /// </summary> /// <param name="indicator">The indicator to be updated</param> /// <param name="time">The time associated with the value</param> /// <param name="value">The value to use to update this indicator</param> /// <returns>True if this indicator is ready, false otherwise</returns> public static void Update(this IndicatorBase <IndicatorDataPoint> indicator, DateTime time, decimal value) { indicator.Update(new IndicatorDataPoint(time, value)); }