示例#1
0
 /// <summary>
 /// Resets this indicator to its initial state
 /// </summary>
 public override void Reset()
 {
     base.Reset();
     MiddleBand.Reset();
     LowerBand.Reset();
     UpperBand.Reset();
 }
示例#2
0
 /// <summary>
 /// Resets this indicator to its initial state
 /// </summary>
 public override void Reset()
 {
     AverageTrueRange.Reset();
     MiddleBand.Reset();
     UpperBand.Reset();
     LowerBand.Reset();
     base.Reset();
 }
示例#3
0
 /// <summary>
 /// Computes the next value of the following sub-indicators from the given state:
 /// StandardDeviation, MiddleBand, UpperBand, LowerBand
 /// </summary>
 /// <param name="input">The input given to the indicator</param>
 /// <returns>The input is returned unmodified.</returns>
 protected override decimal ComputeNextValue(IndicatorDataPoint input)
 {
     StandardDeviation.Update(input);
     MiddleBand.Update(input);
     UpperBand.Update(input);
     LowerBand.Update(input);
     return(input);
 }
示例#4
0
 /// <summary>
 /// Resets this indicator and all sub-indicators (StandardDeviation, LowerBand, MiddleBand, UpperBand)
 /// </summary>
 public override void Reset()
 {
     StandardDeviation.Reset();
     MiddleBand.Reset();
     UpperBand.Reset();
     LowerBand.Reset();
     base.Reset();
 }
示例#5
0
 /// <summary>
 /// Initializes a new instance of the BollingerBands class
 /// </summary>
 /// <param name="name">The name of this indicator</param>
 /// <param name="period">The period of the standard deviation and moving average (middle band)</param>
 /// <param name="k">The number of standard deviations 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 BollingerBands(String name, int period, decimal k, MovingAverageType movingAverageType = MovingAverageType.Simple)
     : base(name)
 {
     MovingAverageType = movingAverageType;
     StandardDeviation = new StandardDeviation(name + "_StandardDeviation", period);
     MiddleBand        = movingAverageType.AsIndicator(name + "_MiddleBand", period);
     LowerBand         = MiddleBand.Minus(StandardDeviation.Times(k), name + "_LowerBand");
     UpperBand         = MiddleBand.Plus(StandardDeviation.Times(k), name + "_UpperBand");
 }
示例#6
0
        /// <summary>
        ///      Computes the next value of this indicator from the given state
        /// </summary>
        /// <param name="time"></param>
        /// <param name="input">The input given to the indicator</param>
        /// <returns>
        ///      A new value for this indicator
        /// </returns>
        protected override DoubleArray Forward(long time, DoubleArray input)
        {
            var coeff = _width * (input[HighIdx] - input[LowIdx]) / (input[HighIdx] + input[LowIdx]);

            LowerBand.Update(time, input[LowIdx] * (1 - coeff));
            UpperBand.Update(time, input[HighIdx] * (1 + coeff));
            MiddleBand.Update(time, input[CloseIdx]);

            return(MiddleBand);
        }
示例#7
0
 /// <summary>
 /// Resets this indicator and all sub-indicators (StandardDeviation, LowerBand, MiddleBand, UpperBand, BandWidth, %B)
 /// </summary>
 public override void Reset()
 {
     StandardDeviation.Reset();
     MiddleBand.Reset();
     UpperBand.Reset();
     LowerBand.Reset();
     BandWidth.Reset();
     PercentB.Reset();
     base.Reset();
 }
示例#8
0
        /// <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 coeff = _width * (input.High - input.Low) / (input.High + input.Low);

            LowerBand.Update(input.Time, input.Low * (1 - coeff));
            UpperBand.Update(input.Time, input.High * (1 + coeff));
            MiddleBand.Update(input.Time, input.Close);

            return(MiddleBand);
        }
示例#9
0
        /// <summary>
        /// Computes the next value for this indicator from the given state.
        /// </summary>
        /// <param name="input">The TradeBar to this indicator on this time step</param>
        /// <returns>A new value for this indicator</returns>
        protected override decimal ComputeNextValue(TradeBar input)
        {
            AverageTrueRange.Update(input);

            var typicalPrice = (input.High + input.Low + input.Close) / 3m;

            MiddleBand.Update(input.Time, typicalPrice);
            Console.WriteLine(input.Time.ToString("yyyymmdd") + "\t" + typicalPrice.SmartRounding() + "\t" + MiddleBand.Current.Value.SmartRounding());
            // poke the upper/lower bands, they actually don't use the input, they compute
            // based on the ATR and the middle band
            LowerBand.Update(input);
            UpperBand.Update(input);
            return(MiddleBand);
        }
示例#10
0
        /// <summary>
        ///      Computes the next value for this indicator from the given state.
        /// </summary>
        /// <param name="time"></param>
        /// <param name="input">The TradeBar to this indicator on this time step</param>
        /// <returns>A new value for this indicator</returns>
        protected override DoubleArray Forward(long time, DoubleArray input)
        {
            AverageTrueRange.Update(time, input);

            var typicalPrice = (input[HighIdx] + input[LowIdx] + input[CloseIdx]) / 3d;

            MiddleBand.Update(time, typicalPrice);

            // poke the upper/lower bands, they actually don't use the input, they compute
            // based on the ATR and the middle band
            LowerBand.Update(time, input);
            UpperBand.Update(time, input);
            return(MiddleBand);
        }
示例#11
0
        /// <summary>
        /// Computes the next value for this indicator from the given state.
        /// </summary>
        /// <param name="input">The TradeBar to this indicator on this time step</param>
        /// <returns>A new value for this indicator</returns>
        protected override decimal ComputeNextValue(IBaseDataBar input)
        {
            AverageTrueRange.Update(input);

            var typicalPrice = (input.High + input.Low + input.Close) / 3m;

            MiddleBand.Update(input.Time, typicalPrice);

            // poke the upper/lower bands, they actually don't use the input, they compute
            // based on the ATR and the middle band
            LowerBand.Update(input);
            UpperBand.Update(input);
            return(MiddleBand);
        }
示例#12
0
        /// <summary>
        /// Initializes a new instance of the BollingerBands class
        /// </summary>
        /// <param name="name">The name of this indicator</param>
        /// <param name="period">The period of the standard deviation and moving average (middle band)</param>
        /// <param name="k">The number of standard deviations 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 BollingerBands(string name, int period, decimal k, MovingAverageType movingAverageType = MovingAverageType.Simple)
            : base(name)
        {
            WarmUpPeriod      = period;
            MovingAverageType = movingAverageType;
            StandardDeviation = new StandardDeviation(name + "_StandardDeviation", period);
            MiddleBand        = movingAverageType.AsIndicator(name + "_MiddleBand", period);
            LowerBand         = MiddleBand.Minus(StandardDeviation.Times(k), name + "_LowerBand");
            UpperBand         = MiddleBand.Plus(StandardDeviation.Times(k), name + "_UpperBand");

            var UpperMinusLower = UpperBand.Minus(LowerBand);

            BandWidth = UpperMinusLower
                        .Over(MiddleBand)
                        .Times(new ConstantIndicator <IndicatorDataPoint>("ct", 100m), name + "_BandWidth");

            Price    = new Identity(name + "_Close");
            PercentB = IndicatorExtensions.Over(
                Price.Minus(LowerBand),
                UpperMinusLower,
                name + "_%B");
        }
示例#13
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()
                                                               );
        }
示例#14
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()
                                                               );
        }
示例#15
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, double k, MovingAverageType movingAverageType = MovingAverageType.Simple)
            : base(name)
        {
            _k           = k;
            WarmUpPeriod = period;

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

            //Compute Lower Band
            LowerBand = new FunctionalIndicator(name + "_LowerBand",
                                                (time, input) => MiddleBand.IsReady ? MiddleBand - AverageTrueRange * _k : Constants.Zero,
                                                lowerBand => MiddleBand.IsReady,
                                                () => MiddleBand.Reset()
                                                );

            //Compute Upper Band
            UpperBand = new FunctionalIndicator(name + "_UpperBand",
                                                (time, input) => MiddleBand.IsReady ? MiddleBand + AverageTrueRange * _k : Constants.Zero,
                                                upperBand => MiddleBand.IsReady,
                                                () => MiddleBand.Reset()
                                                );
        }
示例#16
0
        /// <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)
        {
            MiddleBand.Update(new IndicatorDataPoint
            {
                Time  = input.Time,
                Value = input.Close
            });

            var coeff = _width * (input.High - input.Low) / (input.High + input.Low);

            LowerBand.Update(new IndicatorDataPoint
            {
                Time  = input.Time,
                Value = input.Low * (1 - coeff)
            });

            UpperBand.Update(new IndicatorDataPoint
            {
                Time  = input.Time,
                Value = input.High * (1 + coeff)
            });

            return(MiddleBand);
        }
示例#17
0
 /// <summary>
 ///      Computes the next value of the following sub-indicators from the given state:
 ///      StandardDeviation, MiddleBand, UpperBand, LowerBand
 /// </summary>
 /// <param name="time"></param>
 /// <param name="input">The input given to the indicator</param>
 /// <returns>The input is returned unmodified.</returns>
 protected override DoubleArray Forward(long time, DoubleArray input)
 {
     StandardDeviation.Update(time, input);
     MiddleBand.Update(time, input);
     return(input);
 }
示例#18
0
        public BollingerBands(CandleStickCollection candleSticks, int period = 20,
                              double multiplier = 2, PriceSource priceSource = PriceSource.Close)
        {
            int length = candleSticks.Count - period;

            double[]          prices = priceSource.GetArrayFromCandleStickCollection(candleSticks);
            StandardDeviation standardDeviation;

            LowerBand  = UpperBand = new double[length];
            MiddleBand = MovingAverages.SMA(candleSticks, period, priceSource).MA;

            for (int i = 0; i < length; i++)
            {
                standardDeviation = new StandardDeviation(prices.GetSegment(i, i + period), MiddleBand.GetSegment(i, i + period));
                UpperBand[i]      = MiddleBand[i] + multiplier * standardDeviation.Value;
                LowerBand[i]      = MiddleBand[i] - multiplier * standardDeviation.Value;
            }
        }