Example #1
0
        /// <summary>
        /// Creates a new Stochastics Indicator from the specified periods.
        /// </summary>
        /// <param name="name">The name of this indicator.</param>
        /// <param name="period">The period given to calculate the Fast %K</param>
        /// <param name="kPeriod">The K period given to calculated the Slow %K</param>
        /// <param name="dPeriod">The D period given to calculated the Slow %D</param>
        public Stochastic(string name, int period, int kPeriod, int dPeriod)
            : base(name)
        {
            _maximum  = new Maximum(name + "_Max", period);
            _mininum  = new Minimum(name + "_Min", period);
            _sumFastK = new Sum(name + "_SumFastK", kPeriod);
            _sumSlowK = new Sum(name + "_SumD", dPeriod);

            FastStoch = new FunctionalIndicator <IBaseDataBar>(name + "_FastStoch",
                                                               input => ComputeFastStoch(period, input),
                                                               fastStoch => _maximum.IsReady,
                                                               () => _maximum.Reset()
                                                               );

            StochK = new FunctionalIndicator <IBaseDataBar>(name + "_StochK",
                                                            input => ComputeStochK(period, kPeriod, input),
                                                            stochK => _maximum.IsReady,
                                                            () => _maximum.Reset()
                                                            );

            StochD = new FunctionalIndicator <IBaseDataBar>(name + "_StochD",
                                                            input => ComputeStochD(period, kPeriod, dPeriod),
                                                            stochD => _maximum.IsReady,
                                                            () => _maximum.Reset()
                                                            );
        }
Example #2
0
        /// <summary>
        /// Creates a new IchimokuKinkoHyo indicator from the specific periods
        /// </summary>
        /// <param name="name">The name of this indicator</param>
        /// <param name="tenkanPeriod">The Tenkan-sen period</param>
        /// <param name="kijunPeriod">The Kijun-sen period</param>
        /// <param name="senkouAPeriod">The Senkou A Span period</param>
        /// <param name="senkouBPeriod">The Senkou B Span period</param>
        /// <param name="senkouADelayPeriod">The Senkou A Span delay</param>
        /// <param name="senkouBDelayPeriod">The Senkou B Span delay</param>
        public IchimokuKinkoHyo(string name, int tenkanPeriod = 9, int kijunPeriod = 26, int senkouAPeriod = 26, int senkouBPeriod = 52, int senkouADelayPeriod = 26, int senkouBDelayPeriod = 26)
            : base(name)
        {
            WarmUpPeriod = Math.Max(tenkanPeriod + senkouADelayPeriod, kijunPeriod + senkouADelayPeriod);
            WarmUpPeriod = Math.Max(WarmUpPeriod, senkouBPeriod + senkouBDelayPeriod);

            TenkanMaximum         = new Maximum(name + "_TenkanMax", tenkanPeriod);
            TenkanMinimum         = new Minimum(name + "_TenkanMin", tenkanPeriod);
            KijunMaximum          = new Maximum(name + "_KijunMax", kijunPeriod);
            KijunMinimum          = new Minimum(name + "_KijunMin", kijunPeriod);
            SenkouBMaximum        = new Maximum(name + "_SenkouBMaximum", senkouBPeriod);
            SenkouBMinimum        = new Minimum(name + "_SenkouBMinimum", senkouBPeriod);
            DelayedTenkanSenkouA  = new Delay(name + "DelayedTenkan", senkouADelayPeriod);
            DelayedKijunSenkouA   = new Delay(name + "DelayedKijun", senkouADelayPeriod);
            DelayedMaximumSenkouB = new Delay(name + "DelayedMax", senkouBDelayPeriod);
            DelayedMinimumSenkouB = new Delay(name + "DelayedMin", senkouBDelayPeriod);
            Chikou = new Delay(name + "_Chikou", senkouADelayPeriod);

            SenkouA = new FunctionalIndicator <IndicatorDataPoint>(
                name + "_SenkouA",
                input => SenkouA.IsReady ? (DelayedTenkanSenkouA + DelayedKijunSenkouA) / 2 : decimal.Zero,
                senkouA => DelayedTenkanSenkouA.IsReady && DelayedKijunSenkouA.IsReady,
                () =>
            {
                Tenkan.Reset();
                Kijun.Reset();
            });

            SenkouB = new FunctionalIndicator <IndicatorDataPoint>(
                name + "_SenkouB",
                input => SenkouB.IsReady ? (DelayedMaximumSenkouB + DelayedMinimumSenkouB) / 2 : decimal.Zero,
                senkouA => DelayedMaximumSenkouB.IsReady && DelayedMinimumSenkouB.IsReady,
                () =>
            {
                Tenkan.Reset();
                Kijun.Reset();
            });

            Tenkan = new FunctionalIndicator <IndicatorDataPoint>(
                name + "_Tenkan",
                input => Tenkan.IsReady ? (TenkanMaximum + TenkanMinimum) / 2 : decimal.Zero,
                tenkan => TenkanMaximum.IsReady && TenkanMinimum.IsReady,
                () =>
            {
                TenkanMaximum.Reset();
                TenkanMinimum.Reset();
            });

            Kijun = new FunctionalIndicator <IndicatorDataPoint>(
                name + "_Kijun",
                input => Kijun.IsReady ? (KijunMaximum + KijunMinimum) / 2 : decimal.Zero,
                kijun => KijunMaximum.IsReady && KijunMinimum.IsReady,
                () =>
            {
                KijunMaximum.Reset();
                KijunMinimum.Reset();
            });
        }
Example #3
0
        /// <summary>
        /// Creates a new IchimokuKinkoHyo indicator from the specific periods
        /// </summary>
        /// <param name="name">The name of this indicator</param>
        /// <param name="tenkanPeriod">The Tenkan-sen period</param>
        /// <param name="kijunPeriod">The Kijun-sen period</param>
        /// <param name="senkouAPeriod">The Senkou A Span period</param>
        /// <param name="senkouBPeriod">The Senkou B Span period</param>
        /// <param name="senkouADelayPeriod">The Senkou A Span delay</param>
        /// <param name="senkouBDelayPeriod">The Senkou B Span delay</param>
        public IchimokuKinkoHyo(string name, int tenkanPeriod = 9, int kijunPeriod = 26, int senkouAPeriod = 26, int senkouBPeriod = 52, int senkouADelayPeriod = 26, int senkouBDelayPeriod = 26)
            : base(name)
        {
            TenkanMaximum         = new Maximum(name + "_TenkanMax", tenkanPeriod);
            TenkanMinimum         = new Minimum(name + "_TenkanMin", tenkanPeriod);
            KijunMaximum          = new Maximum(name + "_KijunMax", kijunPeriod);
            KijunMinimum          = new Minimum(name + "_KijunMin", kijunPeriod);
            SenkouBMaximum        = new Maximum(name + "_SenkouBMaximum", senkouBPeriod);
            SenkouBMinimum        = new Minimum(name + "_SenkouBMinimum", senkouBPeriod);
            DelayedTenkanSenkouA  = new Delay(name + "DelayedTenkan", senkouADelayPeriod);
            DelayedKijunSenkouA   = new Delay(name + "DelayedKijun", senkouADelayPeriod);
            DelayedMaximumSenkouB = new Delay(name + "DelayedMax", senkouBDelayPeriod);
            DelayedMinimumSenkouB = new Delay(name + "DelayedMin", senkouBDelayPeriod);


            SenkouA = new FunctionalIndicator <TradeBar>(
                name + "_SenkouA",
                input => computeSenkouA(senkouAPeriod, input),
                senkouA => DelayedTenkanSenkouA.IsReady && DelayedKijunSenkouA.IsReady,
                () =>
            {
                Tenkan.Reset();
                Kijun.Reset();
            });

            SenkouB = new FunctionalIndicator <TradeBar>(
                name + "_SenkouB",
                input => computeSenkouB(senkouBPeriod, input),
                senkouA => DelayedMaximumSenkouB.IsReady && DelayedMinimumSenkouB.IsReady,
                () =>
            {
                Tenkan.Reset();
                Kijun.Reset();
            });


            Tenkan = new FunctionalIndicator <TradeBar>(
                name + "_Tenkan",
                input => ComputeTenkan(tenkanPeriod, input),
                tenkan => TenkanMaximum.IsReady && TenkanMinimum.IsReady,
                () =>
            {
                TenkanMaximum.Reset();
                TenkanMinimum.Reset();
            });

            Kijun = new FunctionalIndicator <TradeBar>(
                name + "_Kijun",
                input => ComputeKijun(kijunPeriod, input),
                kijun => KijunMaximum.IsReady && KijunMinimum.IsReady,
                () =>
            {
                KijunMaximum.Reset();
                KijunMinimum.Reset();
            });
        }
        /// <summary>
        /// Creates a new AverageTrueRange indicator using the specified period and moving average type
        /// </summary>
        /// <param name="name">The name of this indicator</param>
        /// <param name="period">The smoothing period used to smooth the true range values</param>
        /// <param name="movingAverageType">The type of smoothing used to smooth the true range values</param>
        public AverageTrueRange(string name, int period, MovingAverageType movingAverageType = MovingAverageType.Wilders)
            : base(name)
        {
            _smoother = movingAverageType.AsIndicator(string.Format("{0}_{1}", name, movingAverageType), period);

            TradeBar previous = null;

            TrueRange = new FunctionalIndicator <TradeBar>(name + "_TrueRange", currentBar =>
            {
                // in our ComputeNextValue function we'll just call the ComputeTrueRange
                var nextValue = ComputeTrueRange(previous, currentBar);
                previous      = currentBar;
                return(nextValue);
            }   // in our IsReady function we just need at least one sample
                                                           , trueRangeIndicator => trueRangeIndicator.Samples >= 1
                                                           );
        }
Example #5
0
        /// <summary>
        /// Creates a new AverageTrueRange indicator using the specified period and moving average type
        /// </summary>
        /// <param name="name">The name of this indicator</param>
        /// <param name="period">The smoothing period used to smooth the true range values</param>
        /// <param name="movingAverageType">The type of smoothing used to smooth the true range values</param>
        public AverageTrueRange(string name, int period, MovingAverageType movingAverageType = MovingAverageType.Wilders)
            : base(name)
        {
            WarmUpPeriod = period;

            _smoother = movingAverageType.AsIndicator($"{name}_{movingAverageType}", period);

            TrueRange = new FunctionalIndicator <IBaseDataBar>(name + "_TrueRange", currentBar =>
            {
                // in our ComputeNextValue function we'll just call the ComputeTrueRange
                var nextValue = ComputeTrueRange(_previous, currentBar);
                _previous     = currentBar;
                return(nextValue);
            }   // in our IsReady function we just need at least one sample
                                                               , trueRangeIndicator => trueRangeIndicator.Samples >= 1
                                                               );
        }
Example #6
0
        /// <summary>
        /// Creates a new AroonOscillator from the specified up/down periods.
        /// </summary>
        /// <param name="name">The name of this indicator</param>
        /// <param name="upPeriod">The lookback period to determine the highest high for the AroonDown</param>
        /// <param name="downPeriod">The lookback period to determine the lowest low for the AroonUp</param>
        public AroonOscillator(string name, int upPeriod, int downPeriod)
            : base(name)
        {
            var max = new Maximum(name + "_Max", upPeriod);

            AroonUp = new FunctionalIndicator <IndicatorDataPoint>(name + "_AroonUp",
                                                                   input => ComputeAroonUp(upPeriod, max, input),
                                                                   aroonUp => max.IsReady
                                                                   );

            var min = new Minimum(name + "_Min", downPeriod);

            AroonDown = new FunctionalIndicator <IndicatorDataPoint>(name + "_AroonDown",
                                                                     input => ComputeAroonDown(downPeriod, min, input),
                                                                     aroonDown => min.IsReady
                                                                     );
        }
Example #7
0
        /// <summary>
        /// Initializes a new instance of the KeltnerChannels class
        /// </summary>
        /// <param name="name">The name of this indicator</param>
        /// <param name="period">The period of the average true range and moving average (middle band)</param>
        /// <param name="k">The number of multiples specifying the distance between the middle band and upper or lower bands</param>
        /// <param name="movingAverageType">The type of moving average to be used</param>
        public KeltnerChannels(string name, int period, decimal k, MovingAverageType movingAverageType = MovingAverageType.Simple)
            : base(name)
        {
            _k = k;

            //Initialise ATR and SMA
            AverageTrueRange = new AverageTrueRange(name + "_AverageTrueRange", period, MovingAverageType.Simple);
            MiddleBand       = movingAverageType.AsIndicator(name + "_MiddleBand", period);

            //Compute Lower Band
            LowerBand = new FunctionalIndicator <IBaseDataBar>(name + "_LowerBand",
                                                               input => ComputeLowerBand(),
                                                               lowerBand => MiddleBand.IsReady,
                                                               () => MiddleBand.Reset()
                                                               );

            //Compute Upper Band
            UpperBand = new FunctionalIndicator <IBaseDataBar>(name + "_UpperBand",
                                                               input => ComputeUpperBand(),
                                                               upperBand => MiddleBand.IsReady,
                                                               () => MiddleBand.Reset()
                                                               );
        }
Example #8
0
        /// <summary>
        /// Initializes a new instance of the KeltnerChannels class
        /// </summary>
        /// <param name="name">The name of this indicator</param>
        /// <param name="period">The period of the average true range and moving average (middle band)</param>
        /// <param name="k">The number of multiples specifying the distance between the middle band and upper or lower bands</param>
        /// <param name="movingAverageType">The type of moving average to be used</param>
        public KeltnerChannels(string name, int period, decimal k, MovingAverageType movingAverageType = MovingAverageType.Simple)
            : base(name)
        {
            WarmUpPeriod = period;

            //Initialise ATR and SMA
            AverageTrueRange = new AverageTrueRange(name + "_AverageTrueRange", period, movingAverageType);
            MiddleBand       = movingAverageType.AsIndicator(name + "_MiddleBand", period);

            //Compute Lower Band
            LowerBand = new FunctionalIndicator <IBaseDataBar>(name + "_LowerBand",
                                                               input => MiddleBand.IsReady ? MiddleBand.Current.Value - AverageTrueRange.Current.Value * k : decimal.Zero,
                                                               lowerBand => MiddleBand.IsReady,
                                                               () => MiddleBand.Reset()
                                                               );

            //Compute Upper Band
            UpperBand = new FunctionalIndicator <IBaseDataBar>(name + "_UpperBand",
                                                               input => MiddleBand.IsReady ? MiddleBand.Current.Value + AverageTrueRange.Current.Value * k : decimal.Zero,
                                                               upperBand => MiddleBand.IsReady,
                                                               () => MiddleBand.Reset()
                                                               );
        }
Example #9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AverageDirectionalIndex"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="period">The period.</param>
        public AverageDirectionalIndex(string name, int period)
            : base(name)
        {
            _period = period;

            _trueRange = new FunctionalIndicator <IBaseDataBar>(name + "_TrueRange",
                                                                ComputeTrueRange,
                                                                isReady => _previousInput != null
                                                                );

            _directionalMovementPlus = new FunctionalIndicator <IBaseDataBar>(name + "_PositiveDirectionalMovement",
                                                                              ComputePositiveDirectionalMovement,
                                                                              isReady => _previousInput != null
                                                                              );

            _directionalMovementMinus = new FunctionalIndicator <IBaseDataBar>(name + "_NegativeDirectionalMovement",
                                                                               ComputeNegativeDirectionalMovement,
                                                                               isReady => _previousInput != null
                                                                               );

            PositiveDirectionalIndex = new FunctionalIndicator <IndicatorDataPoint>(name + "_PositiveDirectionalIndex",
                                                                                    input =>
            {
                // Computes the Plus Directional Indicator(+DI period).
                if (_smoothedTrueRange != 0 && _smoothedDirectionalMovementPlus.IsReady)
                {
                    return(100m * _smoothedDirectionalMovementPlus.Current.Value / _smoothedTrueRange.Current.Value);
                }
                return(0m);
            },
                                                                                    positiveDirectionalIndex => _smoothedDirectionalMovementPlus.IsReady,
                                                                                    () =>
            {
                _directionalMovementPlus.Reset();
                _trueRange.Reset();
            }
                                                                                    );

            NegativeDirectionalIndex = new FunctionalIndicator <IndicatorDataPoint>(name + "_NegativeDirectionalIndex",
                                                                                    input =>
            {
                // Computes the Minus Directional Indicator(-DI period).
                if (_smoothedTrueRange != 0 && _smoothedDirectionalMovementMinus.IsReady)
                {
                    return(100m * _smoothedDirectionalMovementMinus.Current.Value / _smoothedTrueRange.Current.Value);
                }
                return(0m);
            },
                                                                                    negativeDirectionalIndex => _smoothedDirectionalMovementMinus.IsReady,
                                                                                    () =>
            {
                _directionalMovementMinus.Reset();
                _trueRange.Reset();
            }
                                                                                    );

            _smoothedTrueRange = new FunctionalIndicator <IndicatorDataPoint>(name + "_SmoothedTrueRange",
                                                                              input =>
            {
                // Computes the Smoothed True Range value.
                var value = Samples > _period + 1 ? _smoothedTrueRange.Current.Value / _period : 0m;
                return(_smoothedTrueRange.Current.Value + _trueRange.Current.Value - value);
            },
                                                                              isReady => Samples > period
                                                                              );

            _smoothedDirectionalMovementPlus = new FunctionalIndicator <IndicatorDataPoint>(name + "_SmoothedDirectionalMovementPlus",
                                                                                            input =>
            {
                // Computes the Smoothed Directional Movement Plus value.
                var value = Samples > _period + 1 ? _smoothedDirectionalMovementPlus.Current.Value / _period : 0m;
                return(_smoothedDirectionalMovementPlus.Current.Value + _directionalMovementPlus.Current.Value - value);
            },
                                                                                            isReady => Samples > period
                                                                                            );

            _smoothedDirectionalMovementMinus = new FunctionalIndicator <IndicatorDataPoint>(name + "_SmoothedDirectionalMovementMinus",
                                                                                             input =>
            {
                // Computes the Smoothed Directional Movement Minus value.
                var value = Samples > _period + 1 ? _smoothedDirectionalMovementMinus.Current.Value / _period : 0m;
                return(_smoothedDirectionalMovementMinus.Current.Value + _directionalMovementMinus.Current.Value - value);
            },
                                                                                             isReady => Samples > period
                                                                                             );

            _averageDirectionalIndex = new WilderMovingAverage(period);
        }
Example #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AverageDirectionalIndex"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="period">The period.</param>
        public AverageDirectionalIndex(string name, int period)
            : base(name)
        {
            _period = period;

            TrueRange = new FunctionalIndicator <IBaseDataBar>(name + "_TrueRange",
                                                               currentBar =>
            {
                var value = ComputeTrueRange(currentBar);
                return(value);
            },
                                                               isReady => _previousInput != null
                                                               );

            DirectionalMovementPlus = new FunctionalIndicator <IBaseDataBar>(name + "_PositiveDirectionalMovement",
                                                                             currentBar =>
            {
                var value = ComputePositiveDirectionalMovement(currentBar);
                return(value);
            },
                                                                             isReady => _previousInput != null
                                                                             );


            DirectionalMovementMinus = new FunctionalIndicator <IBaseDataBar>(name + "_NegativeDirectionalMovement",
                                                                              currentBar =>
            {
                var value = ComputeNegativeDirectionalMovement(currentBar);
                return(value);
            },
                                                                              isReady => _previousInput != null
                                                                              );

            PositiveDirectionalIndex = new FunctionalIndicator <IndicatorDataPoint>(name + "_PositiveDirectionalIndex",
                                                                                    input => ComputePositiveDirectionalIndex(),
                                                                                    positiveDirectionalIndex => DirectionalMovementPlus.IsReady && TrueRange.IsReady,
                                                                                    () =>
            {
                DirectionalMovementPlus.Reset();
                TrueRange.Reset();
            }
                                                                                    );

            NegativeDirectionalIndex = new FunctionalIndicator <IndicatorDataPoint>(name + "_NegativeDirectionalIndex",
                                                                                    input => ComputeNegativeDirectionalIndex(),
                                                                                    negativeDirectionalIndex => DirectionalMovementMinus.IsReady && TrueRange.IsReady,
                                                                                    () =>
            {
                DirectionalMovementMinus.Reset();
                TrueRange.Reset();
            }
                                                                                    );

            SmoothedTrueRange = new FunctionalIndicator <IndicatorDataPoint>(name + "_SmoothedTrueRange",
                                                                             currentBar => ComputeSmoothedTrueRange(period),
                                                                             isReady => _previousInput != null
                                                                             );


            SmoothedDirectionalMovementPlus = new FunctionalIndicator <IndicatorDataPoint>(name + "_SmoothedDirectionalMovementPlus",
                                                                                           currentBar => ComputeSmoothedDirectionalMovementPlus(period),
                                                                                           isReady => _previousInput != null
                                                                                           );

            SmoothedDirectionalMovementMinus = new FunctionalIndicator <IndicatorDataPoint>(name + "_SmoothedDirectionalMovementMinus",
                                                                                            currentBar => ComputeSmoothedDirectionalMovementMinus(period),
                                                                                            isReady => _previousInput != null
                                                                                            );
        }