Example #1
0
        protected override IndicatorDataPoint Calculate(T input)
        {
            Inputs.Add(input);
            if (Inputs.Count < Period)
            {
                return(IndicatorDataPoint.Zero);
            }

            double min = double.MaxValue, max = double.MinValue;

            for (int i = 0; i < Period; i++)
            {
                var val = Inputs[i].Value;
                if (val > max)
                {
                    max = val;
                }
                if (val < min)
                {
                    min = val;
                }
            }

            if (max > min)
            {
                return(new IndicatorDataPoint(input.Time, 2d * (input.Value - min) / (max - min) - 1));
            }
            else
            {
                throw new Exception("Unexpected error in Normalize indicator");
            }
        }
Example #2
0
 protected override IndicatorDataPoint Calculate(ITradeBar input)
 {
     Inputs.Add(input);
     if (Inputs.Count >= Period)
     {
         var m = 0d;
         int nh = 0, nl = 0;
         for (int i = 1; i < Period; i++)
         {
             var vi  = Inputs[i].Value;
             var vi1 = Inputs[i - 1].Value;
             if (vi > m && vi > vi1)
             {
                 nl++;
             }
             else if (vi < m && vi < vi1)
             {
                 nh++;
             }
         }
         var value = 100d * (nl + nh) / (Period - 1);
         return(new IndicatorDataPoint(input.Time, value));
     }
     return(IndicatorDataPoint.Zero);
 }
Example #3
0
 protected override IndicatorDataPoint Calculate(ITradeBar input)
 {
     TrueRange.Update(input);
     if (TrueRange.IsReady)
     {
         TrueRanges.Add(TrueRange.Current);
         int stepsCnt = Math.Min(TrueRanges.Count, Period);
         RollingSum += TrueRange.Current.Value;
         if (TrueRanges.Count >= Period)
         {
             RollingSum -= TrueRanges[Period].Value;
         }
         var res = new IndicatorDataPoint(input.Time, RollingSum / stepsCnt);
         return(res);
     }
     return(null);
 }
        protected override MeanAndVarianceRecord Calculate(T input)
        {
            //remove the sample that's exiting the window from the rolling sum
            if (Inputs.Count >= Period)
            {
                var valueToRemove = Inputs[Period - 1].Value;
                _rollingSum          -= valueToRemove;
                _rollingSumOfSquares -= valueToRemove * valueToRemove;
            }

            Inputs.Add(input);
            _rollingSum          += input.Value;
            _rollingSumOfSquares += input.Value * input.Value;

            //inputs has been set as Period + 1 so max size is number of sampes + 1
            var mean          = _rollingSum / Inputs.Count;
            var meanOfSquares = _rollingSumOfSquares / Inputs.Count;

            var variance = meanOfSquares - mean * mean;

            return(new MeanAndVarianceRecord(input.Time, mean, variance));
        }
Example #5
0
        protected override IBaseData Calculate(IBaseData input)
        {
            Inputs.Add(input);
            IBaseData output;
            var       oneRemoved = Inputs.Samples > Inputs.Size;

            if (Inputs.Count < 2)
            {
                output = input;
            }
            else if (!oneRemoved || Inputs.MostRecentlyRemoved.Low > LastOutput.Low)
            {
                //if the sample that's going out of range is NOT the current min then we only need to check if the new sample is lower than current min
                output = input.Low < LastOutput.Low ? input : LastOutput;
            }
            else if (input.Low <= Inputs.MostRecentlyRemoved.Low)
            {
                //the current minimum is going out of range, but signalIn is lower than min then signalIn IS the new min
                output = input;
            }
            else
            {
                //min is going out of window so we need to search again
                var inputs = Inputs.GetRawSamples();
                output = inputs[0];
                foreach (var rec in inputs)
                {
                    if (rec.Low < output.Low)
                    {
                        output = rec;
                    }
                }
            }

            output     = new IndicatorDataPoint(output.Time, output.Low);
            LastOutput = output;
            return(output);
        }