// Override OnBarUpdate method
        protected override void OnBarUpdate()
        {
            // Protect against too few bars
            if (CurrentBar < period)
            {
                Upper.Reset();
                Lower.Reset();
                return;
            }

            // Solve for the price given a probability
            if (distribution == Distribution.Gaussian)
            {
                double side = Math.Sqrt(-2 * Math.Pow(StdDev(period)[0] / TickSize, 2) * Math.Log(probability * StdDev(Period)[0] / TickSize * Math.Sqrt(2 * Math.PI)));
                Upper.Set((SMA(period)[0] / TickSize + side) * TickSize);
                Lower.Set((SMA(period)[0] / TickSize - side) * TickSize);
            }
            else if (distribution == Distribution.Laplace)
            {
                double side = -HawkmatixAvgDiff(period)[0] / TickSize * Math.Log(probability * 2 * HawkmatixAvgDiff(period)[0] / TickSize);
                Upper.Set((SMA(period)[0] / TickSize + side) * TickSize);
                Lower.Set((SMA(period)[0] / TickSize - side) * TickSize);
            }
            else if (distribution == Distribution.Logistic)
            {
                double s      = Math.Sqrt(3) / Math.PI * StdDev(period)[0] / TickSize;
                double left   = 1 - 2 * probability * s;
                double side   = Math.Sqrt((2 * probability * s - 1) * (2 * probability * s - 1) - 4 * probability * probability * s * s);
                double bottom = 2 * probability * s;
                Upper.Set((SMA(period)[0] / TickSize - s * Math.Log((left - side) / bottom)) * TickSize);
                Lower.Set((SMA(period)[0] / TickSize - s * Math.Log((left + side) / bottom)) * TickSize);
            }
        }
Beispiel #2
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;
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            if (CurrentBar < 20)
            {
                return;
            }
            double truerange;
            double updotplot;
            double lowdotplot;

            if (CurrentBar == 0)
            {
                TrueRange[0] = ((High[0] - Low[0]));
            }
            else
            {
                truerange    = (High[0] - Low[0]);
                truerange    = Math.Max(Math.Abs((Low[0] - Close[1])), Math.Max(truerange, Math.Abs((High[0] - Close[1]))));
                TrueRange[0] = truerange;
            }
            if (CurrentBar < 2)
            {
                return;
            }
            double AtrValue   = SMA(TrueRange, period)[1];                 //
            double Acelfactor = ((atrtimes + 0.1 * risk) * AtrValue);
            double Sigclose;

            if (sideside == 1)
            {
                Sigclose = High[1];
                //Sigclose = Math.Max(High[0],High[1]);
                updotplot = (Sigclose - Acelfactor);

                if (double.IsNaN(dotplot) || (updotplot >= dotplot))
                {
                    dotplot = Bars.Instrument.MasterInstrument.RoundToTickSize(updotplot);
                }
            }
            else if (sideside == -1)
            {
                //Sigclose = Math.Min(Low[0],Low[1]);
                Sigclose   = Low[1];
                lowdotplot = (Sigclose + Acelfactor);

                if (double.IsNaN(dotplot) || (lowdotplot <= dotplot))
                {
                    dotplot = Bars.Instrument.MasterInstrument.RoundToTickSize(lowdotplot);
                }
            }
            counter1++;
            counter2++;


            if (!double.IsNaN(dotplot))
            {
                if (sideside != 1)
                {
                    if ((sideside == -1) && (Close[0] > dotplot))                                               // was high
                    {
                        sideside = 1;
                        counter2 = 0;
                        dotplot  = Bars.Instrument.MasterInstrument.RoundToTickSize((High[0] - Acelfactor));
                    }
                }
                else if (Close[0] < dotplot)                                                                                    // was low
                {
                    sideside = -1;
                    counter2 = 0;
                    dotplot  = Bars.Instrument.MasterInstrument.RoundToTickSize((Low[0] + Acelfactor));
                }
            }
            Sideside[0] = sideside;
            if (Sideside[0] != Sideside[1])
            {
                once = 0;
            }
            if (sideside == 1)
            {
                if (once == 1)
                {
                    Upper[0] = dotplot;
                    Lower.Reset();
                }
                if (once == 0)
                {
                    //DrawArrowUp("arrow" + CurrentBar, true, 0,  dotplot, Color.Blue);
                }
                once = 1;
            }

            if (sideside == -1)
            {
                if (once == 1)
                {
                    Lower[0] = dotplot;
                    Upper.Reset();
                }
                if (once == 0)
                {
                    //DrawArrowDown("arrow" + CurrentBar, true, 0,  dotplot, Color.Red);
                }
                once = 1;
            }
        }