/// <summary> /// Creates a new CompositeIndicator such that the result will be average of a first indicator weighted by a second one /// </summary> /// <param name="value">Indicator that will be averaged</param> /// <param name="weight">Indicator that provides the average weights</param> /// <param name="period">Average period</param> /// <returns>Indicator that results of the average of first by weights given by second</returns> public static CompositeIndicator WeightedBy <T, TWeight>(this IndicatorBase <T> value, TWeight weight, int period) where T : IBaseData where TWeight : IndicatorBase <IndicatorDataPoint> { var x = new WindowIdentity(period); var y = new WindowIdentity(period); var numerator = new Sum("Sum_xy", period); var denominator = new Sum("Sum_y", period); value.Updated += (sender, consolidated) => { x.Update(consolidated); if (x.Samples == y.Samples) { numerator.Update(consolidated.Time, consolidated.Value * y.Current.Value); } }; weight.Updated += (sender, consolidated) => { y.Update(consolidated); if (x.Samples == y.Samples) { numerator.Update(consolidated.Time, consolidated.Value * x.Current.Value); } denominator.Update(consolidated); }; return(numerator.Over(denominator)); }
/// <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) { _ema.Update(new IndicatorDataPoint { Time = input.Time, Value = input.High - input.Low }); _ema2.Update(_ema.Current); _sum.Update(new IndicatorDataPoint { Time = input.Time, Value = _ema.Current / _ema2.Current }); if (!_sum.IsReady) { return(_sum.Period); } else { return(_sum); } }
/// <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); if (Samples == 1) { _previousInput = input; return 50m; } var buyingPressure = new IndicatorDataPoint { Value = input.Close - Math.Min(input.Low, _previousInput.Close) }; _sumBuyingPressure1.Update(buyingPressure); _sumBuyingPressure2.Update(buyingPressure); _sumBuyingPressure3.Update(buyingPressure); _sumTrueRange1.Update(_trueRange.Current); _sumTrueRange2.Update(_trueRange.Current); _sumTrueRange3.Update(_trueRange.Current); _previousInput = input; if (!IsReady) return 50m; var average1 = _sumBuyingPressure1.Current.Value / _sumTrueRange1.Current.Value; var average2 = _sumBuyingPressure2.Current.Value / _sumTrueRange2.Current.Value; var average3 = _sumBuyingPressure3.Current.Value / _sumTrueRange3.Current.Value; return 100m * (4 * average1 + 2 * average2 + average3) / 7; }
/// <summary> /// Computes the next value for this indicator from the given state. /// </summary> /// <param name="input">The input value to this indicator on this time step</param> /// <returns>A new value for this indicator</returns> protected override decimal ComputeNextValue(TradeBar input) { var denominator = (input.High - input.Low); var flowRatio = denominator > 0 ? input.Volume * (input.Close - input.Low - (input.High - input.Close)) / denominator : 0m; _flowRatioSum.Update(input.EndTime, flowRatio); _volumeSum.Update(input.EndTime, input.Volume); return(!IsReady || _volumeSum == 0m ? 0m : _flowRatioSum / _volumeSum); }
/// <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) { _ema1.Update(input.Time, input.High - input.Low); if (_ema2.IsReady) { _sum.Update(input.Time, _ema1.Current / _ema2.Current); } if (!_sum.IsReady) { return(_sum.Period); } return(_sum); }