Example #1
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            // calculate Linear Regression
            double sumX    = (double)Period * (Period - 1) * 0.5;
            double divisor = sumX * sumX - (double)Period * Period * (Period - 1) * (2 * Period - 1) / 6;
            double sumXY   = 0;

            for (int count = 0; count < Period && CurrentBar - count >= 0; count++)
            {
                sumXY += count * Input[count];
            }

            y.Set(Input[0]);
            double slope     = ((double)Period * sumXY - sumX * SUM(y, Period)[0]) / divisor;
            double intercept = (SUM(y, Period)[0] - slope * sumX) / Period;
            double linReg    = intercept + slope * (Period - 1);

            // Calculate Standard Error
            double sumSquares = 0;

            for (int count = 0; count < Period && CurrentBar - count >= 0; count++)
            {
                double linRegX = intercept + slope * (Period - 1 - count);
                double valueX  = Input[count];
                double diff    = Math.Abs(valueX - linRegX);

                sumSquares += diff * diff;
            }
            double stdErr = Math.Sqrt(sumSquares / Period);

            Middle.Set(linReg);
            Upper.Set(linReg + stdErr);
            Lower.Set(linReg - stdErr);
        }
Example #2
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            double smaValue    = AhrensMA(period).AhrensHL[0];
            double stdDevValue = StdDev(Period)[0];

            Upper.Set(smaValue + NumStdDev * stdDevValue);
            Middle.Set(smaValue);
            Lower.Set(smaValue - NumStdDev * stdDevValue);
        }
Example #3
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            double smaValue    = SMA(Period)[0];
            double stdDevValue = StdDev(SMA(Range(), 20), period)[0];

            Print("stdDevValue = " + stdDevValue);
            Upper.Set(smaValue + NumStdDev * stdDevValue);
            Middle.Set(smaValue);
            Lower.Set(smaValue - NumStdDev * stdDevValue);
        }
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            double maValue = 0;

            switch (matype)
            {
            case 1:
            {
                Middle.Set(maValue = EMA(Inputs[0], Period)[0]);
                break;
            }

            case 2:
            {
                Middle.Set(maValue = HMA(Inputs[0], Period)[0]);
                break;
            }

            case 3:
            {
                Middle.Set(maValue = SMA(Inputs[0], Period)[0]);
                break;
            }

            case 4:
            {
                Middle.Set(maValue = TMA(Inputs[0], Period)[0]);
                break;
            }

            case 5:
            {
                Middle.Set(maValue = TEMA(Inputs[0], Period)[0]);
                break;
            }

            case 6:
            {
                Middle.Set(maValue = WMA(Inputs[0], Period)[0]);
                break;
            }
            }

            Upper.Set(maValue + (maValue * EnvelopePercentage / 100));
            Lower.Set(maValue - (maValue * EnvelopePercentage / 100));
        }
Example #5
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            double averageValue = 0;

            if (Smoothed)
            {
                averageValue = smoothedAverage[0];
            }
            else
            {
                averageValue = average[0];
            }
            double offset = 0;

            if (Smoothed)
            {
                offset = NumStdDev * smoothedVolatility[0];
            }
            else
            {
                offset = NumStdDev * volatility[0];
            }

            if (showMidband)
            {
                Middle.Set(averageValue);
            }
            else
            {
                Middle.Reset();
            }
            if (showChannels)
            {
                Upper.Set(averageValue + offset);
                Lower.Set(averageValue - offset);
            }
            else
            {
                Upper.Reset();
                Lower.Reset();
            }

            if (CurrentBar < Math.Max(Period, StdDevPeriod))
            {
                return;
            }
            if (FirstTickOfBar)
            {
                neutralSlope = threshold * averageTrueRange[1] / 1000;
                priorBull    = bullish;
                middle       = Middle[1] + Middle[2];
                upper        = Upper[1] + Upper[2];
                lower        = Lower[1] + Lower[2];
            }

            if (2 * averageValue > middle + 3 * neutralSlope)
            {
                PlotColors[1][0] = upColor;
                if (useCentralSlope)
                {
                    PlotColors[0][0] = UpColor;
                    PlotColors[2][0] = UpColor;
                }
                if (priorBull <= 0)
                {
                    index = CurrentBar;
                }
                if (Opacity != 0)
                {
                    DrawRegion("bollinger" + index, CurrentBar - index + 1, 0, Upper, Lower, Color.Transparent, UpColor, Opacity);
                }
                bullish = 1;
            }
            else if (2 * averageValue < middle - 3 * neutralSlope)
            {
                PlotColors[1][0] = DownColor;
                if (useCentralSlope)
                {
                    PlotColors[0][0] = DownColor;
                    PlotColors[2][0] = DownColor;
                }
                if (priorBull >= 0)
                {
                    index = CurrentBar;
                }
                if (Opacity != 0)
                {
                    DrawRegion("bollinger" + index, CurrentBar - index + 1, 0, Upper, Lower, Color.Transparent, DownColor, Opacity);
                }
                bullish = -1;
            }
            else
            {
                PlotColors[1][0] = NeutralColor;
                if (useCentralSlope)
                {
                    PlotColors[0][0] = NeutralColor;
                    PlotColors[2][0] = NeutralColor;
                }
                if (priorBull != 0)
                {
                    index = CurrentBar;
                }
                if (Opacity != 0)
                {
                    DrawRegion("bollinger" + index, CurrentBar - index + 1, 0, Upper, Lower, Color.Transparent, NeutralColor, Opacity);
                }
                bullish = 0;
            }

            if (!useCentralSlope)
            {
                if (2 * averageValue + 2 * offset >= upper + 3 * neutralSlope)
                {
                    PlotColors[0][0] = UpColor;
                }
                else if (2 * averageValue + 2 * offset <= upper - 3 * neutralSlope)
                {
                    PlotColors[0][0] = DownColor;
                }
                else
                {
                    PlotColors[0][0] = NeutralColor;
                }

                if (2 * averageValue - 2 * offset >= lower + 3 * neutralSlope)
                {
                    PlotColors[2][0] = UpColor;
                }
                else if (2 * averageValue - 2 * offset <= lower - 3 * neutralSlope)
                {
                    PlotColors[2][0] = DownColor;
                }
                else
                {
                    PlotColors[2][0] = NeutralColor;
                }
            }
        }
Example #6
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            if (CurrentBar == 0)
            {
                return;
            }

            // First we calculate the linear regression parameters

            double sumX    = (double)Period * (Period - 1) * 0.5;
            double divisor = sumX * sumX -
                             (double)Period * Period * (Period - 1) * (2 * Period - 1) / 6;
            double sumXY = 0;
            double sumY  = 0;

            int barCount = Math.Min(Period, CurrentBar);

            for (int count = 0; count < barCount; count++)
            {
                sumXY += count * Input[count];
                sumY  += Input[count];
            }

            if (divisor == 0 || Period == 0)
            {
                return;
            }

            double slope     = ((double)Period * sumXY - sumX * sumY) / divisor;
            double intercept = (sumY - slope * sumX) / Period;

            slopeSeries.Set(slope);
            interceptSeries.Set(intercept);

            // Next we calculate the standard deviation of the
            // residuals (vertical distances to the regression line).

            double sumResiduals = 0;

            for (int count = 0; count < barCount; count++)
            {
                double regressionValue = intercept + slope * (Period - 1 - count);
                double residual        = Math.Abs(Input[count] - regressionValue);

                sumResiduals += residual;
            }

            double avgResiduals = sumResiduals / Math.Min(CurrentBar - 1, Period);

            sumResiduals = 0;
            for (int count = 0; count < barCount; count++)
            {
                double regressionValue = intercept + slope * (Period - 1 - count);
                double residual        = Math.Abs(Input[count] - regressionValue);

                sumResiduals += (residual - avgResiduals) * (residual - avgResiduals);
            }

            double stdDeviation = Math.Sqrt(sumResiduals / Math.Min(CurrentBar + 1, Period));

            stdDeviationSeries.Set(stdDeviation);

            double middle = intercept + slope * (Period - 1);

            Middle.Set(middle);
            Upper.Set(middle + stdDeviation * Width);
            Lower.Set(middle - stdDeviation * Width);
        }