Exemplo n.º 1
2
        /// <summary>
        /// Take a swing on the course, with the specified angle and velocity.
        ///		Exceptions:
        ///			AngleInvalidException
        /// </summary>
        /// <param name="angle">angle of the launch</param>
        /// <param name="velocity">speed of the ball</param>
        public void Swing(double angle, double velocity)
        {
            // Create the swing object, with the angle and velocity provided
            Swing swing = new Swing(angle, velocity, this);

            // Add the swing to the list
            Swings.Add(swing);

            // Increment the Swing Counter
            this.SwingCounter++;

            // If the distance to the hole is less than the tolerance,
            // the player has won, so end the course.
            if (this.DistanceFromHole            // distance
                <= Course.Tolerance /* tolerance */)
            {
                this.HasEnded = true;
                this.HasWon   = true;

                // If the distance is greater than the total distance plus the rough,
                // the player is out of bounds, so end the course in failure
            }
            else if (this.DistanceFromHole > (CourseLength + CourseRough))
            {
                this.IsOutOfBounds = true;
                this.HasEnded      = true;
                this.HasLost       = true;
            }
        }
Exemplo n.º 2
0
        public void Swing(double angle, double velocity)
        {
            // Skapa svingen med hjälp av vinkeln och styrkan
            Swing swing = new Swing(angle, velocity, this);

            // Lägger till svingen till listan
            Swings.Add(swing);

            // Ökar slagräknaren
            this.SwingCounter++;

            // Ifall vi är inom tolleransen av vad som är att bollen är i hålet
            if (this.DistanceFromHole
                <= Course.Tolerance)
            {
                this.HasEnded = true;
                this.HasWon   = true;

                // Ifall bollen har flugit utanför gränsen
            }
            else if (this.DistanceFromHole > (CourseLength + CourseRough))
            {
                this.IsOutOfBounds = true;
                this.HasEnded      = true;
                this.HasLost       = true;
            }
        }
Exemplo n.º 3
0
 public void DisplayEndingStausToUser()
 {
     foreach (var swing in Swings)
     {
         Console.WriteLine("Swing {0}", Swings.IndexOf(swing) + 1);
         Console.WriteLine("Distance travelled: {0:0.00}", swing.Distance);
         Console.WriteLine("Angle: {0}", swing.Angle);
         Console.WriteLine("Velocity: {0}", swing.Velocity);
     }
 }
Exemplo n.º 4
0
        public void PitchOS()
        {
            Swings p = OppSwing();
            int    n = random.Next(100);

            if (p == Swings.Take)
            {
                if (n < 80)
                {
                    Strike();
                }
                else
                {
                    Ball();
                }
            }
            else if (p == Swings.Fastball)
            {
                if (n < 75)
                {
                    Strike();
                }
                else
                {
                    int hit = random.Next(25);
                    Hit(hit);
                }
            }
            else if (p == Swings.BreakingBall)
            {
                if (n < 75)
                {
                    Strike();
                }
                else
                {
                    int hit = random.Next(25);
                    Hit(hit);
                }
            }
            else if (p == Swings.OffSpeed)
            {
                if (n < 35)
                {
                    Strike();
                }
                else
                {
                    int hit = random.Next(25);
                    Hit(hit);
                }
            }
        }
Exemplo n.º 5
0
        public void SimulateSwing()
        {
            double distance       = 0;
            double angleInRadians = (Math.PI / 180) * Angle;


            distance = Convert.ToDouble(Math.Pow(Velocity, 2) / _gravity * Math.Sin(2 * angleInRadians));
            Swing swing = new Swing(Angle, Velocity, distance);

            Swings.Add(swing);
            TotalDistanceTravelled = TotalDistanceTravelled + distance;
            StopCondition          = DistanceToCup - TotalDistanceTravelled;
            NoOfSwings++;
            SendSwingInfoToUser(distance);
        }
Exemplo n.º 6
0
        public void Swing(double angle, double velocity)
        {
            Swing swing = new Swing(angle, velocity, this);

            Swings.Add(swing);
            this.SwingCounter++;
            if (this.DistanceFromHole <= Ball.Tolerance)
            {
                this.Ended = true;
                this.Won   = true;
            }
            else if (this.DistanceFromHole > Rough)
            {
                this.IsOutOfBounds = true;
                this.Ended         = true;
                this.Lost          = true;
            }
        }
Exemplo n.º 7
0
        private void CalcUpSwing(int bar, double high, bool updateHigh)
        {
            if (!updateHigh)
            {
                lastHigh         = curHigh;
                lastHighBar      = curHighBar;
                lastHighDuration = curHighDuration;
                lastHighRelation = curHighRelation;
                swingCounterUp++;
                swingSlope     = 1;
                trendChangeBar = bar;

                TrendChangeBarStats upStats = new TrendChangeBarStats(CurrentBar, Open[0], High[0], Low[0], Close[0], Trend.Up);
                upHistory.Add(upHisIndex, upStats);
                bothHistory.Add(upHisIndex + dnHisIndex, upStats);
                upHisIndex++;
            }
            else
            {
                swingHighs.RemoveAt(swingHighs.Count - 1);
            }

            curHighBar      = bar;
            curHigh         = Math.Round(high, decimalPlaces, MidpointRounding.AwayFromZero);
            curHighLength   = Convert.ToInt32(Math.Round((curHigh - curLow) / TickSize, 0, MidpointRounding.AwayFromZero));
            curHighDuration = curHighBar - curLowBar;

            double dtbOffset = ATR(14)[CurrentBar - curHighBar] * dtbStrength / 100;

            if (curHigh > lastHigh - dtbOffset && curHigh < lastHigh + dtbOffset)
            {
                curHighRelation = Relation.Double;
                BackColor       = Color.Yellow;
                doubleBottomBar = CurrentBar;
                TrendChangeBarStats dtStats = new TrendChangeBarStats(CurrentBar, Open[0], High[0], Low[0], Close[0], Trend.Up);
                dT.Add(dtIndex, dtStats);
                //Print(Environment.NewLine + String.Format("{0}   [{1}]   {2}", Time[0].ToShortDateString(), Bars.BarsSinceSession, bothHistory[bothHistory.Count - 2].ToString()));
                //Print(String.Format("{0}   [{1}]   {2}   DOUBLETOP ALERT", Time[0].ToShortDateString(), Bars.BarsSinceSession, bothHistory[bothHistory.Count - 1].ToString()));
                dtIndex++;
                enableDTOrderEntry = true;
            }
            else if (curHigh < lastHigh)
            {
                curHighRelation = Relation.Lower;
            }
            else
            {
                curHighRelation = Relation.Higher;
            }

            string swingLabel = null;

            switch (curHighRelation)
            {
            case Relation.Higher:
                swingLabel = "HH";
                break;

            case Relation.Lower:
                swingLabel = "LH";
                break;

            case Relation.Double:
                swingLabel = "DT";
                break;
            }

            if (lastHigh != 0.0 || lastLow != 0.0)
            {
                DrawLine("ZigZagUp" + swingCounterUp, true, CurrentBar - curLowBar, curLow, CurrentBar - curHighBar, curHigh, zigZagColourUp, zigZagStyle, zigZagWidth);
                string txtOutput = string.Format("{0}: {1} b.{2}", swingLabel, curHighLength.ToString(), curHighDuration.ToString());
                DrawText("UpLabel" + swingCounterUp, true, txtOutput, CurrentBar - curHighBar, curHigh, offsetText, Color.Black, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
            }


            Swings up = new Swings(curHighBar, curHigh, curHighLength, curHighDuration, curHighRelation);

            swingHighs.Add(up);
            upCount = swingHighs.Count - 1;
        }
Exemplo n.º 8
0
        private void CalcDnSwing(int bar, double low, bool updateLow)
        {
            if (!updateLow)
            {
                lastLow         = curLow;
                lastLowBar      = curLowBar;
                lastLowDuration = curLowDuration;
                lastLowLength   = curLowLength;
                lastLowRelation = curLowRelation;
                swingCounterDn++;
                swingSlope     = -1;
                trendChangeBar = bar;

                TrendChangeBarStats dnStats = new TrendChangeBarStats(CurrentBar, Open[0], High[0], Low[0], Close[0], Trend.Down);
                dnHistory.Add(dnHisIndex, dnStats);
                bothHistory.Add(dnHisIndex + upHisIndex, dnStats);

                //Print(String.Format("{0}   [{1}]   {2}",Time[0].ToShortDateString(),Bars.BarsSinceSession,bothHistory[bothHistory.Count - 1].ToString()));
                dnHisIndex++;

                //BackColor = Color.Coral;
            }
            else
            {
                swingLows.RemoveAt(swingLows.Count - 1);
            }
            curLowBar      = bar;
            curLow         = Math.Round(low, decimalPlaces, MidpointRounding.AwayFromZero);
            curLowLength   = Convert.ToInt32(Math.Round((curLow - curHigh) / TickSize, 0, MidpointRounding.AwayFromZero));
            curLowDuration = curLowBar - curHighBar;

            double dtbOffset = ATR(14)[CurrentBar - curLowBar] * dtbStrength / 100;

            if (curLow > lastLow - dtbOffset && curLow < lastLow + dtbOffset)
            {
                curLowRelation = Relation.Double;
            }
            else if (curLow < lastLow)
            {
                curLowRelation = Relation.Lower;
            }
            else
            {
                curLowRelation = Relation.Higher;
            }

            string swingLabel = null;

            switch (curLowRelation)
            {
            case Relation.Higher:
                swingLabel = "HL";
                break;

            case Relation.Lower:
                swingLabel = "LL";
                break;

            case Relation.Double:
                swingLabel = "DB";
                break;
            }
            if (lastHigh != 0.0 || lastLow != 0.0)
            {
                DrawLine("ZigZagDown" + swingCounterDn, true, CurrentBar - curHighBar, curHigh, CurrentBar - curLowBar, curLow, zigZagColourDn, zigZagStyle, zigZagWidth);
                string txtOutput = string.Format("{0}: {1} b.{2}", swingLabel, curLowLength.ToString(), curLowDuration.ToString());
                DrawText("DnLabel" + swingCounterDn, true, txtOutput, CurrentBar - curLowBar, curLow, -offsetText, Color.Black, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
            }

            // On new low adjust the breakeven stop to higher low.

            if (swingStopLoss != null)
            {
                //Print (String.Format(" Current StopPrice {0}      lastHigh {1}",swingStopLoss.StopPrice,lastHigh));
                if (swingStopLoss.StopPrice > lastHigh && scalpProfitTarget == null)
                {
                    // Print(Bars.BarsSinceSession+"MOVING THE STOPLOSS");
                    try
                    {
                        if (Close[0] < lastHigh)
                        {
                            swingStopLoss = ExitShortStop(0, true, swingStopLoss.Quantity, lastHigh, "SW.HL", "SW.DTEN");
                        }
                        else if (Close[0] >= lastHigh)
                        {
                            ExitShortStop(0, true, swingStopLoss.Quantity, High[0] + 1 * TickSize, "SW.HL" + " MKT", "SW.DTEN");
                        }
                    }
                    catch (Exception ex)
                    {
                        Print("Exception caused by moving swing stop loss position" + Environment.NewLine + ex.ToString());
                    }
                }
            }
            // Print(String.Format("[{0}]    {1}", Bars.BarsSinceSession, lastHigh));

            Swings dn = new Swings(curLowBar, curLow, curLowLength, curLowDuration, curLowRelation);

            swingLows.Add(dn);
            downCount = swingLows.Count - 1;
        }
Exemplo n.º 9
0
        //#####################################################################
        private void CalcDnSwing(int bar, double low, bool updateLow)
        {
            #region New and update Swing values
            if (!updateLow)
            {
                if (VisualizationTypes.GannStyle == visualizationType)
                {
                    for (int i = CurrentBar - trendChangeBar; i >= 0; i--)
                        GannSwing.Set(i, curHigh);
                    GannSwing.Set(low);
                }
                lastLow = curLow;
                lastLowBar = curLowBar;
                lastLowDateTime = curLowDateTime;
                lastLowDuration = curLowDuration;
                lastLowLength = curLowLength;
                lastLowPercent = curLowPercent;
                lastLowRelation = curLowRelation;
                lastLowTime = curLowTime;
                lastLowVolume = curLowVolume;
                swingCounterDn++;
                swingSlope = -1;
                trendChangeBar = bar;

                if (showNakedSwings == true)
                {
                    nakedSwingHighsList.Add(curHigh, curHighBar);
                    DrawRay("NakedSwingHigh" + curHigh.ToString(), false,
                        CurrentBar - curHighBar, curHigh, CurrentBar - curHighBar - 1, curHigh,
                        nakedSwingHighColor, nakedSwingDashStyle, nakedSwingLineWidth);
                }
            }
            else
            {
                if (VisualizationTypes.Dots == visualizationType
                    || VisualizationTypes.DotsAndLines == visualizationType)
                {
                    DoubleBottom.Reset(CurrentBar - curLowBar);
                    LowerLow.Reset(CurrentBar - curLowBar);
                    HigherLow.Reset(CurrentBar - curLowBar);
                }
                swingLows.RemoveAt(swingLows.Count - 1);
            }
            curLowBar = bar;
            curLow = Math.Round(low, decimalPlaces, MidpointRounding.AwayFromZero);
            curLowTime = ToTime(Time[CurrentBar - curLowBar]);
            curLowDateTime = Time[CurrentBar - curLowBar];
            
            curLowLength = Convert.ToInt32(Math.Round((curLow - curHigh) / TickSize,
                0, MidpointRounding.AwayFromZero));
            if (curHighLength != 0)
                curLowPercent = Math.Round(100.0 / curHighLength * Math.Abs(curLowLength), 1);
            curLowDuration = curLowBar - curHighBar;
            double dtbOffset = ATR(14)[CurrentBar - curLowBar] * dtbStrength / 100;
            if (curLow > lastLow - dtbOffset && curLow < lastLow + dtbOffset)
                curLowRelation = Relation.Double;
            else if (curLow < lastLow)
                curLowRelation = Relation.Lower;
            else
                curLowRelation = Relation.Higher;
            if (!CalculateOnBarClose)
                signalBarVolumeUp = Volume[0];
            double swingVolume = 0.0;
            for (int i = CurrentBar - curLowBar; i < CurrentBar - curLowBar + curLowDuration; i++)
                swingVolume = swingVolume + Volume[i];
            if (!CalculateOnBarClose)
                swingVolume = swingVolume + (Volume[CurrentBar - curHighBar]
                    - signalBarVolumeDn);
            if (SwingVolumeTypes.Relativ == swingVolumeType)
                swingVolume = Math.Round(swingVolume / curLowDuration, 0,
                    MidpointRounding.AwayFromZero);
            curLowVolume = Convert.ToInt64(swingVolume);
            #endregion

            #region Visualize swing
            switch (visualizationType)
            {
                case VisualizationTypes.Dots:
                    switch (curLowRelation)
                    {
                        case Relation.Higher:
                            HigherLow.Set(CurrentBar - curLowBar, curLow);
                            break;
                        case Relation.Lower:
                            LowerLow.Set(CurrentBar - curLowBar, curLow);
                            break;
                        case Relation.Double:
                            DoubleBottom.Set(CurrentBar - curLowBar, curLow);
                            break;
                    }
                    break;
                case VisualizationTypes.ZigZagLines:
                    DrawLine("ZigZagDown" + swingCounterDn, AutoScale, CurrentBar - curHighBar,
                        curHigh, CurrentBar - curLowBar, curLow, zigZagColourDn, zigZagStyle, zigZagWidth);
                    break;
                case VisualizationTypes.DotsAndLines:
                    switch (curLowRelation)
                    {
                        case Relation.Higher:
                            HigherLow.Set(CurrentBar - curLowBar, curLow);
                            break;
                        case Relation.Lower:
                            LowerLow.Set(CurrentBar - curLowBar, curLow);
                            break;
                        case Relation.Double:
                            DoubleBottom.Set(CurrentBar - curLowBar, curLow);
                            break;
                    }
                    DrawLine("ZigZagDown" + swingCounterDn, AutoScale, CurrentBar - curHighBar,
                        curHigh, CurrentBar - curLowBar, curLow, zigZagColourDn, zigZagStyle, zigZagWidth);
                    break;
                case VisualizationTypes.GannStyle:
                    for (int i = CurrentBar - trendChangeBar; i >= 0; i--)
                        GannSwing.Set(i, curLow);
                    break;
            }

            if (showNakedSwings == true && nakedSwingLowsList.Count > 0)
            {
                while (nakedSwingLowsList.Count > 0 
                    && nakedSwingLowsList.Keys[nakedSwingLowsList.Count - 1] >= curLow)
                {
                    int counter = nakedSwingLowsList.Count - 1;
                    double nakedSwingLowPrice = nakedSwingLowsList.Keys[counter];
                    RemoveDrawObject("NakedSwingLow" + nakedSwingLowPrice.ToString());
                    if (showHistoricalNakedSwings == true)
                    {
                        DrawLine("NakedSwingLow" + nakedSwingCounter++, false,
                            CurrentBar - nakedSwingLowsList.Values[counter], nakedSwingLowPrice,
                            CurrentBar - curLowBar, nakedSwingLowPrice, nakedSwingLowColor,
                            nakedSwingDashStyle, nakedSwingLineWidth);
                    }
                    nakedSwingLowsList.RemoveAt(counter);
                }
            }
            #endregion

            #region Swing value output
            string output = null;
            string swingLabel = null;
            Color textColor = Color.Transparent;
            if (SwingLengthTypes.False != swingLengthType)
            {
                switch (swingLengthType)
                {
                    case SwingLengthTypes.Ticks:
                        output = curLowLength.ToString();
                        break;
                    case SwingLengthTypes.Ticks_And_Price:
                        output = curLowLength.ToString() + " / " + curLow.ToString();
                        break;
                    case SwingLengthTypes.Points:
                        output = (curLowLength * TickSize).ToString();
                        break;
                    case SwingLengthTypes.Price:
                        output = curLow.ToString();
                        break;
                    case SwingLengthTypes.Price_And_Points:
                        output = curLow.ToString() + " / " + (curLowLength * TickSize).ToString();
                        break;
                    case SwingLengthTypes.Price_And_Ticks:
                        output = curLow.ToString() + " / " + curLowLength.ToString();
                        break;
                    case SwingLengthTypes.Percent:
                        output = (Math.Round((100.0 / curHigh * (curLowLength * TickSize)),
                            2, MidpointRounding.AwayFromZero)).ToString();
                        break;
                }
            }
            if (showSwingDuration != SwingDurationTypes.False)
            {
                string outputDuration = "";
                TimeSpan timeSpan;
                int hours, minutes, seconds = 0;
                switch (showSwingDuration)
                {
                    case SwingDurationTypes.Bars:
                        outputDuration = curLowDuration.ToString();
                        break;
                    case SwingDurationTypes.HoursMinutes:
                        timeSpan = curLowDateTime.Subtract(curHighDateTime);
                        hours = timeSpan.Hours;
                        minutes = timeSpan.Minutes;
                        if (hours == 0)
                            outputDuration = "0:" + minutes.ToString();
                        else if (minutes == 0)
                            outputDuration = hours + ":00";
                        else
                            outputDuration = hours + ":" + minutes;
                        break;
                    case SwingDurationTypes.MinutesSeconds:
                        timeSpan = curLowDateTime.Subtract(curHighDateTime);
                        minutes = timeSpan.Minutes;
                        seconds = timeSpan.Seconds;
                        if (minutes == 0)
                            outputDuration = "0:" + seconds.ToString();
                        else if (seconds == 0)
                            outputDuration = minutes + ":00";
                        else
                            outputDuration = minutes + ":" + seconds;
                        break;
                    case SwingDurationTypes.HoursTotal:
                        timeSpan = curLowDateTime.Subtract(curHighDateTime);
                        outputDuration = Math.Round(timeSpan.TotalHours, 1,
                            MidpointRounding.AwayFromZero).ToString();
                        break;
                    case SwingDurationTypes.MinutesTotal:
                        timeSpan = curLowDateTime.Subtract(curHighDateTime);
                        outputDuration = Math.Round(timeSpan.TotalMinutes, 1,
                            MidpointRounding.AwayFromZero).ToString();
                        break;
                    case SwingDurationTypes.SecondsTotal:
                        timeSpan = curLowDateTime.Subtract(curHighDateTime);
                        outputDuration = Math.Round(timeSpan.TotalSeconds, 1,
                            MidpointRounding.AwayFromZero).ToString();
                        break;
                    case SwingDurationTypes.Days:
                        timeSpan = curLowDateTime.Subtract(curHighDateTime);
                        outputDuration = Math.Round(timeSpan.TotalDays, 1,
                            MidpointRounding.AwayFromZero).ToString();
                        break;
                }
                if (SwingLengthTypes.False != swingLengthType)
                    output = output + " / " + outputDuration;
                else
                    output = outputDuration;
            }
            switch (curLowRelation)
            {
                case Relation.Higher:
                    swingLabel = "HL";
                    textColor = textColourHigher;
                    break;
                case Relation.Lower:
                    swingLabel = "LL";
                    textColor = textColorLower;
                    break;
                case Relation.Double:
                    swingLabel = "DB";
                    textColor = textColourDtb;
                    break;
            }
            if (output != null)
                DrawText("DnLength" + swingCounterDn, AutoScale, output.ToString(),
                    CurrentBar - curLowBar, curLow, -textOffsetLength, textColor, textFont,
                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
            if (showSwingLabel)
                DrawText("DnLabel" + swingCounterDn, AutoScale, swingLabel,
                    CurrentBar - curLowBar, curLow, -textOffsetLabel, textColor, textFont,
                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
            if (showSwingPercent && curLowPercent != 0)
            {
                DrawText("DnPerc" + swingCounterDn, AutoScale, curLowPercent.ToString() + "%",
                    CurrentBar - curLowBar, curLow, -textOffsetPercent, textColor, textFont,
                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
            }
            if (SwingTimeTypes.False != showSwingTime)
            {
                string timeOutput = "";
                switch (showSwingTime)
                {
                    case SwingTimeTypes.Integer:
                        timeOutput = curLowTime.ToString();
                        break;
                    case SwingTimeTypes.HourMinute:
                        timeOutput = string.Format("{0:t}", Time[CurrentBar - curLowBar]);
                        break;
                    case SwingTimeTypes.HourMinuteSecond:
                        timeOutput = string.Format("{0:T}", Time[CurrentBar - curLowBar]);
                        break;
                    case SwingTimeTypes.DayMonth:
                        timeOutput = string.Format("{0:dd.MM}", Time[CurrentBar - curHighBar]);
                        break;
                }
                DrawText("DnTime" + swingCounterDn, AutoScale, timeOutput,
                    CurrentBar - curLowBar, curLow, -textOffsetTime, textColor, textFont,
                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
            }
            if (SwingVolumeTypes.False != swingVolumeType)
            {
                DrawText("DnVolume" + swingCounterDn, AutoScale,
                    TruncIntToStr(Convert.ToInt32(swingVolume)),
                    CurrentBar - curLowBar, curLow, -textOffsetVolume, textColor, textFont,
                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
            }
            #endregion

            Swings dn = new Swings(curLowBar, curLow, curLowLength, curLowDuration, curLowRelation);
            swingLows.Add(dn);
            dnCount = swingLows.Count - 1;
        }
Exemplo n.º 10
0
        //#####################################################################
        #endregion

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            #region Initialize varibles
            //=================================================================
            if (FirstTickOfBar)
            {
                stopOutsideBarCalc = false;

                if (CurrentBar == 1)
                {
                    curHighBar = curLowBar = CurrentBar;
                    curHigh = High[1];
                    curLow = Low[1];
                    curHighDateTime = curLowDateTime = lastHighDateTime = lastLowDateTime = Time[CurrentBar];
                    Swings up = new Swings(curHighBar, curHigh, 0, 0, curHighRelation);
                    Swings dn = new Swings(curLowBar, curLow, 0, 0, curLowRelation);
                    swingHighs.Add(up);
                    swingLows.Add(dn);
                    upCount = swingHighs.Count;
                    dnCount = swingLows.Count;
                }
            }
            // Set new/update high/low back to false, to avoid function 
            // calls which depends on them
            newHigh = newLow = updateHigh = updateLow = false;

            // Checks to make sure we have enough bars to calculate the indicator   
            if (CurrentBar < swingSize)
                return;
            //=================================================================
            #endregion

            #region Swing calculation
            // Swing calculation ==============================================
            switch (swingType)
            {
                case SwingTypes.Standard:
                    #region Standard Swing
                    if (swingSlope == 1 && High[0] <= curHigh)
                        newHigh = false;
                    else
                        newHigh = true;

                    if (swingSlope == -1 && Low[0] >= curLow)
                        newLow = false;
                    else
                        newLow = true;

                    if (FirstTickOfBar && VisualizationTypes.GannStyle == visualizationType)
                    {
                        if (swingSlope == 1)
                            GannSwing.Set(curHigh);
                        else
                            GannSwing.Set(curLow);
                    }

                    // CalculatOnBarClose == true =================================================
                    if (CalculateOnBarClose)
                    {
                        if (newHigh)
                        {
                            for (int i = 1; i < swingSize + 1; i++)
                            {
                                if (High[0] <= High[i])
                                {
                                    newHigh = false;
                                    break;
                                }
                            }
                        }
                        if (newLow)
                        {
                            for (int i = 1; i < swingSize + 1; i++)
                            {
                                if (Low[0] >= Low[i])
                                {
                                    newLow = false;
                                    break;
                                }
                            }
                        }

                        if (newHigh && newLow)
                        {
                            if (swingSlope == -1)
                                newHigh = false;
                            else
                                newLow = false;
                        }
                    }
                    // CalculatOnBarClose == false ================================================
                    else
                    {
                        if (FirstTickOfBar)
                            newSwing = 0;

                        if (newSwing != -1)
                        {
                            if (newHigh)
                            {
                                for (int i = 1; i < swingSize + 1; i++)
                                {
                                    if (High[0] <= High[i])
                                    {
                                        newHigh = false;
                                        break;
                                    }
                                }
                                if (newHigh)
                                    newSwing = 1;
                            }
                        }

                        if (newSwing != 1)
                        {
                            if (newLow)
                            {
                                for (int i = 1; i < swingSize + 1; i++)
                                {
                                    if (Low[0] >= Low[i])
                                    {
                                        newLow = false;
                                        break;
                                    }
                                }
                                if (newLow)
                                    newSwing = -1;
                            }
                        }

                        if (newSwing == 1)
                            newLow = false;
                        if (newSwing == -1)
                            newHigh = false;
                    }

                    // Set new high or low ========================================================
                    if (newHigh)
                    {
                        if (swingSlope != 1)
                        {
                            bar = CurrentBar - HighestBar(High, CurrentBar - curLowBar);
                            price = High[HighestBar(High, CurrentBar - curLowBar)];
                            updateHigh = false;
                        }
                        else
                        {
                            bar = CurrentBar;
                            price = High[0];
                            updateHigh = true;
                        }
                        CalcUpSwing(bar, price, updateHigh);
                    }
                    else if (newLow)
                    {
                        if (swingSlope != -1)
                        {
                            bar = CurrentBar - LowestBar(Low, CurrentBar - curHighBar);
                            price = Low[LowestBar(Low, CurrentBar - curHighBar)];
                            updateLow = false;
                        }
                        else
                        {
                            bar = CurrentBar;
                            price = Low[0];
                            updateLow = true;
                        }
                        CalcDnSwing(bar, price, updateLow);
                    }
                    // ============================================================================
                    #endregion
                    break;
                case SwingTypes.Gann:
                    #region Set bar property
                    //=================================================================
                    if (paintBars)
                    {
                        if (High[0] > High[1])
                        {
                            if (Low[0] < Low[1])
                            {
                                barType = BarType.OutsideBar;
                                BarColor = outsideBarColour;
                            }
                            else
                            {
                                barType = BarType.UpBar;
                                BarColor = upBarColour;
                            }
                        }
                        else
                        {
                            if (Low[0] < Low[1])
                            {
                                barType = BarType.DnBar;
                                BarColor = dnBarColour;
                            }
                            else
                            {
                                barType = BarType.InsideBar;
                                BarColor = insideBarColour;
                            }
                        }
                    }
                    else
                    {
                        if (High[0] > High[1])
                        {
                            if (Low[0] < Low[1])
                                barType = BarType.OutsideBar;
                            else
                                barType = BarType.UpBar;
                        }
                        else
                        {
                            if (Low[0] < Low[1])
                                barType = BarType.DnBar;
                            else
                                barType = BarType.InsideBar;
                        }
                    }
                    //===================================================================
                    #endregion

                    #region Gann Swing
                    // Gann swing =================================================================
                    #region Up swing
                    // Up Swing ===================================================================
                    if (swingSlope == 1)
                    {
                        if (FirstTickOfBar && VisualizationTypes.GannStyle == visualizationType)
                            GannSwing.Set(curHigh);

                        switch (barType)
                        {
                            // Up bar =============================================================
                            case BarType.UpBar:
                                consecutiveBars = 0;
                                consecutiveBarValue = 0.0;
                                if (High[0] > curHigh)
                                {
                                    updateHigh = true;
                                    CalcUpSwing(CurrentBar, High[0], true);
                                    if ((consecutiveBars + 1) == swingSize)
                                        stopOutsideBarCalc = true;
                                }
                                break;
                            // Down bar ===========================================================
                            case BarType.DnBar:
                                if (consecutiveBarNumber != CurrentBar)
                                {
                                    if (consecutiveBarValue == 0.0)
                                    {
                                        consecutiveBars++;
                                        consecutiveBarNumber = CurrentBar;
                                        consecutiveBarValue = Low[0];
                                    }
                                    else if (Low[0] < consecutiveBarValue)
                                    {
                                        consecutiveBars++;
                                        consecutiveBarNumber = CurrentBar;
                                        consecutiveBarValue = Low[0];
                                    }
                                }
                                else if (Low[0] < consecutiveBarValue)
                                    consecutiveBarValue = Low[0];
                                if (consecutiveBars == swingSize || (useBreakouts && Low[0] < curLow))
                                {
                                    consecutiveBars = 0;
                                    consecutiveBarValue = 0.0;
                                    newLow = true;
                                    bar = CurrentBar - LowestBar(Low, CurrentBar - curHighBar);
                                    price = Low[LowestBar(Low, CurrentBar - curHighBar)];
                                    CalcDnSwing(bar, price, false);
                                }
                                break;
                            // Inside bar =========================================================
                            case BarType.InsideBar:
                                if (!ignoreInsideBars)
                                {
                                    consecutiveBars = 0;
                                    consecutiveBarValue = 0.0;
                                }
                                break;
                            // Outside bar ========================================================
                            case BarType.OutsideBar:
                                if (High[0] > curHigh)
                                {
                                    updateHigh = true;
                                    CalcUpSwing(CurrentBar, High[0], true);
                                }
                                else if (!stopOutsideBarCalc)
                                {
                                    if (consecutiveBarNumber != CurrentBar)
                                    {
                                        if (consecutiveBarValue == 0.0)
                                        {
                                            consecutiveBars++;
                                            consecutiveBarNumber = CurrentBar;
                                            consecutiveBarValue = Low[0];
                                        }
                                        else if (Low[0] < consecutiveBarValue)
                                        {
                                            consecutiveBars++;
                                            consecutiveBarNumber = CurrentBar;
                                            consecutiveBarValue = Low[0];
                                        }
                                    }
                                    else if (Low[0] < consecutiveBarValue)
                                        consecutiveBarValue = Low[0];
                                    if (consecutiveBars == swingSize || (useBreakouts && Low[0] < curLow))
                                    {
                                        consecutiveBars = 0;
                                        consecutiveBarValue = 0.0;
                                        newLow = true;
                                        bar = CurrentBar - LowestBar(Low, CurrentBar - curHighBar);
                                        price = Low[LowestBar(Low, CurrentBar - curHighBar)];
                                        CalcDnSwing(bar, price, false);
                                    }
                                }
                                break;
                        }
                    }
                    // End up swing ===============================================================
                    #endregion

                    #region Down swing
                    // Down swing =================================================================
                    else
                    {
                        if (FirstTickOfBar && VisualizationTypes.GannStyle == visualizationType)
                            GannSwing.Set(curLow);

                        switch (barType)
                        {
                            // Dwon bar ===========================================================
                            case BarType.DnBar:
                                consecutiveBars = 0;
                                consecutiveBarValue = 0.0;
                                if (Low[0] < curLow)
                                {
                                    updateLow = true;
                                    CalcDnSwing(CurrentBar, Low[0], true);
                                    if ((consecutiveBars + 1) == swingSize)
                                        stopOutsideBarCalc = true;
                                }
                                break;
                            // Up bar =============================================================
                            case BarType.UpBar:
                                if (consecutiveBarNumber != CurrentBar)
                                {
                                    if (consecutiveBarValue == 0.0)
                                    {
                                        consecutiveBars++;
                                        consecutiveBarNumber = CurrentBar;
                                        consecutiveBarValue = High[0];
                                    }
                                    else if (High[0] > consecutiveBarValue)
                                    {
                                        consecutiveBars++;
                                        consecutiveBarNumber = CurrentBar;
                                        consecutiveBarValue = High[0];
                                    }
                                }
                                else if (High[0] > consecutiveBarValue)
                                    consecutiveBarValue = High[0];
                                if (consecutiveBars == swingSize || (useBreakouts && High[0] > curHigh))
                                {
                                    consecutiveBars = 0;
                                    consecutiveBarValue = 0.0;
                                    newHigh = true;
                                    bar = CurrentBar - HighestBar(High, CurrentBar - curLowBar);
                                    price = High[HighestBar(High, CurrentBar - curLowBar)];
                                    CalcUpSwing(bar, price, false);
                                }
                                break;
                            // Inside bar =========================================================
                            case BarType.InsideBar:
                                if (!ignoreInsideBars)
                                {
                                    consecutiveBars = 0;
                                    consecutiveBarValue = 0.0;
                                }
                                break;
                            // Outside bar ========================================================
                            case BarType.OutsideBar:
                                if (Low[0] < curLow)
                                {
                                    updateLow = true;
                                    CalcDnSwing(CurrentBar, Low[0], true);
                                }
                                else if (!stopOutsideBarCalc)
                                {
                                    if (consecutiveBarNumber != CurrentBar)
                                    {
                                        if (consecutiveBarValue == 0.0)
                                        {
                                            consecutiveBars++;
                                            consecutiveBarNumber = CurrentBar;
                                            consecutiveBarValue = High[0];
                                        }
                                        else if (High[0] > consecutiveBarValue)
                                        {
                                            consecutiveBars++;
                                            consecutiveBarNumber = CurrentBar;
                                            consecutiveBarValue = High[0];
                                        }
                                    }
                                    else if (High[0] > consecutiveBarValue)
                                        consecutiveBarValue = High[0];
                                    if (consecutiveBars == swingSize || (useBreakouts && High[0] > curHigh))
                                    {
                                        consecutiveBars = 0;
                                        consecutiveBarValue = 0.0;
                                        newHigh = true;
                                        bar = CurrentBar - HighestBar(High, CurrentBar - curLowBar);
                                        price = High[HighestBar(High, CurrentBar - curLowBar)];
                                        CalcUpSwing(bar, price, false);
                                    }
                                }
                                break;
                        }
                    }
                    // End down swing =============================================================
                    #endregion
                    // End Gann swing =============================================================
                    #endregion
                    break;
            }
            // End swing calculation ==========================================
            #endregion
        }
Exemplo n.º 11
0
        //#####################################################################
        #endregion

        #region CalcUpSwing(int bar, double high, bool updateHigh)
        //#####################################################################
        private void CalcUpSwing(int bar, double high, bool updateHigh)
        {
            #region New and update swing values
            if (!updateHigh)
            {
                if (VisualizationTypes.GannStyle == visualizationType)
                {
                    for (int i = CurrentBar - trendChangeBar; i >= 0; i--)
                        GannSwing.Set(i, curLow);
                    GannSwing.Set(high);
                }
                lastHigh = curHigh;
                lastHighBar = curHighBar;
                lastHighDateTime = curHighDateTime;
                lastHighDuration = curHighDuration;
                lastHighLength = curHighLength;
                lastHighTime = curHighTime;
                lastHighPercent = curHighPercent;
                lastHighRelation = curHighRelation;
                lastHighVolume = curHighVolume;
                swingCounterUp++;
                swingSlope = 1;
                trendChangeBar = bar;
                upFlip.Set(true);

                if (showNakedSwings == true)
                {
                    nakedSwingLowsList.Add(curLow, curLowBar);
                    DrawRay("NakedSwingLow" + curLow.ToString(), false,
                        CurrentBar - curLowBar, curLow, CurrentBar - curLowBar - 1, curLow,
                        nakedSwingLowColor, nakedSwingDashStyle, nakedSwingLineWidth);
                }
#if NT7
                if (statisticPosition != StatisticPositions.False) dnStatistic();
#endif
            }
            else
            {
                if (VisualizationTypes.Dots == visualizationType
                    || VisualizationTypes.DotsAndLines == visualizationType)
                {
                    DoubleTop.Reset(CurrentBar - curHighBar);
                    HigherHigh.Reset(CurrentBar - curHighBar);
                    LowerHigh.Reset(CurrentBar - curHighBar);
                }
                swingHighs.RemoveAt(swingHighs.Count - 1);
            }
            curHighBar = bar;
            curHigh = Math.Round(high, decimalPlaces, MidpointRounding.AwayFromZero);
            curHighTime = ToTime(Time[CurrentBar - curHighBar]);
            curHighDateTime = Time[CurrentBar - curHighBar];

            curHighLength = Convert.ToInt32(Math.Round((curHigh - curLow) / TickSize,
                0, MidpointRounding.AwayFromZero));
            if (curLowLength != 0)
                curHighPercent = Math.Round(100.0 / Math.Abs(curLowLength) * curHighLength, 1);
            curHighDuration = curHighBar - curLowBar;
            double dtbOffset = ATR(14)[CurrentBar - curHighBar] * dtbStrength / 100;
            if (curHigh > lastHigh - dtbOffset && curHigh < lastHigh + dtbOffset)
                curHighRelation = Relation.Double;
            else if (curHigh < lastHigh)
                curHighRelation = Relation.Lower;
            else
                curHighRelation = Relation.Higher;
            if (!CalculateOnBarClose)
                signalBarVolumeDn = Volume[0];
            double swingVolume = 0.0;
            for (int i = 0; i < curHighDuration; i++)
                swingVolume = swingVolume + Volume[i];
            if (!CalculateOnBarClose)
                swingVolume = swingVolume + (Volume[CurrentBar - curLowBar]
                    - signalBarVolumeUp);
            if (SwingVolumeTypes.Relativ == swingVolumeType)
                swingVolume = Math.Round(swingVolume / curHighDuration, 0, 
                    MidpointRounding.AwayFromZero);
            curHighVolume = Convert.ToInt64(swingVolume);
            #endregion

            #region Visualize swing
            switch (visualizationType)
            {
                case VisualizationTypes.Dots:
                    switch (curHighRelation)
                    {
                        case Relation.Higher:
                            HigherHigh.Set(CurrentBar - curHighBar, curHigh);
                            break;
                        case Relation.Lower:
                            LowerHigh.Set(CurrentBar - curHighBar, curHigh);
                            break;
                        case Relation.Double:
                            DoubleTop.Set(CurrentBar - curHighBar, curHigh);
                            break;
                    }
                    break;
                case VisualizationTypes.ZigZagLines:
                    DrawLine("ZigZagUp" + swingCounterUp, AutoScale, CurrentBar - curLowBar,
                        curLow, CurrentBar - curHighBar, curHigh, zigZagColourUp, zigZagStyle, 
                        zigZagWidth);
                    break;
                case VisualizationTypes.DotsAndLines:
                    switch (curHighRelation)
                    {
                        case Relation.Higher:
                            HigherHigh.Set(CurrentBar - curHighBar, curHigh);
                            break;
                        case Relation.Lower:
                            LowerHigh.Set(CurrentBar - curHighBar, curHigh);
                            break;
                        case Relation.Double:
                            DoubleTop.Set(CurrentBar - curHighBar, curHigh);
                            break;
                    }
                    DrawLine("ZigZagUp" + swingCounterUp, AutoScale, CurrentBar - curLowBar,
                        curLow, CurrentBar - curHighBar, curHigh, zigZagColourUp, zigZagStyle, 
                        zigZagWidth);
                    break;
                case VisualizationTypes.GannStyle:
                    for (int i = CurrentBar - trendChangeBar; i >= 0; i--)
                        GannSwing.Set(i, high);
                    break;
                case VisualizationTypes.ZigZagLinesVolume:
                    if (curHighVolume > curLowVolume)
                        DrawLine("ZigZagUp" + swingCounterUp, AutoScale, CurrentBar - curLowBar,
                            curLow, CurrentBar - curHighBar, curHigh, zigZagColourUp, zigZagStyle,
                            zigZagWidth);
                    else
                        DrawLine("ZigZagUp" + swingCounterUp, AutoScale, CurrentBar - curLowBar,
                            curLow, CurrentBar - curHighBar, curHigh, zigZagColourDn, zigZagStyle,
                            zigZagWidth);
                    break;
            }

            if (showNakedSwings == true && nakedSwingHighsList.Count > 0)
            {
                while (nakedSwingHighsList.Count > 0 && nakedSwingHighsList.Keys[0] <= curHigh)
                {
                    double nakedSwingHighPrice = nakedSwingHighsList.Keys[0];
                    RemoveDrawObject("NakedSwingHigh" + nakedSwingHighPrice.ToString());
                    if (showHistoricalNakedSwings == true)
                    {
                        DrawLine("NakedSwingHigh" + nakedSwingCounter++, false,
                            CurrentBar - nakedSwingHighsList.Values[0], nakedSwingHighPrice,
                            CurrentBar - curHighBar, nakedSwingHighPrice, nakedSwingHighColor,
                            nakedSwingDashStyle, nakedSwingLineWidth);
                    }
                    nakedSwingHighsList.RemoveAt(0);
                }
            }
            #endregion

            #region Swing value output
            string output = null;
            string swingLabel = null;
            Color textColor = Color.Transparent;
            if (SwingLengthTypes.False != swingLengthType)
            {
                switch (swingLengthType)
                {
                    case SwingLengthTypes.Ticks:
                        output = curHighLength.ToString();
                        break;
                    case SwingLengthTypes.Ticks_And_Price:
                        output = curHighLength.ToString() + " / " + curHigh.ToString();
                        break;
                    case SwingLengthTypes.Points:
                        output = (curHighLength * TickSize).ToString();
                        break;
                    case SwingLengthTypes.Price:
                        output = curHigh.ToString();
                        break;
                    case SwingLengthTypes.Price_And_Points:
                        output = curHigh.ToString() + " / " + (curHighLength * TickSize).ToString();
                        break;
                    case SwingLengthTypes.Price_And_Ticks:
                        output = curHigh.ToString() + " / " + curHighLength.ToString();
                        break;
                    case SwingLengthTypes.Percent:
                        output = (Math.Round((100.0 / curLow * (curHighLength * TickSize)),
                            2, MidpointRounding.AwayFromZero)).ToString();
                        break;
                }
            }
            if (showSwingDuration != SwingDurationTypes.False)
            {
                string outputDuration = "";
                TimeSpan timeSpan;
                int hours, minutes, seconds = 0;
                switch (showSwingDuration)
                {
                    case SwingDurationTypes.Bars:
                        outputDuration = curHighDuration.ToString();
                        break;
                    case SwingDurationTypes.HoursMinutes:
                        timeSpan = curHighDateTime.Subtract(curLowDateTime);
                        hours = timeSpan.Hours;
                        minutes = timeSpan.Minutes;
                        if (hours == 0)
                            outputDuration = "0:" + minutes.ToString();
                        else if (minutes == 0)
                            outputDuration = hours + ":00";
                        else
                            outputDuration = hours + ":" + minutes;
                        break;
                    case SwingDurationTypes.MinutesSeconds:
                        timeSpan = curHighDateTime.Subtract(curLowDateTime);
                        minutes = timeSpan.Minutes;
                        seconds = timeSpan.Seconds;
                        if (minutes == 0)
                            outputDuration = "0:" + seconds.ToString();
                        else if (seconds == 0)
                            outputDuration = minutes + ":00";
                        else
                            outputDuration = minutes + ":" + seconds;
                        break;
                    case SwingDurationTypes.HoursTotal:
                        timeSpan = curHighDateTime.Subtract(curLowDateTime);
                        outputDuration = Math.Round(timeSpan.TotalHours, 1,
                            MidpointRounding.AwayFromZero).ToString();
                        break;
                    case SwingDurationTypes.MinutesTotal:
                        timeSpan = curHighDateTime.Subtract(curLowDateTime);
                        outputDuration = Math.Round(timeSpan.TotalMinutes, 1,
                            MidpointRounding.AwayFromZero).ToString();
                        break;
                    case SwingDurationTypes.SecondsTotal:
                        timeSpan = curHighDateTime.Subtract(curLowDateTime);
                        outputDuration = Math.Round(timeSpan.TotalSeconds, 1,
                            MidpointRounding.AwayFromZero).ToString();
                        break;
                    case SwingDurationTypes.Days:
                        timeSpan = curHighDateTime.Subtract(curLowDateTime);
                        outputDuration = Math.Round(timeSpan.TotalDays, 1,
                            MidpointRounding.AwayFromZero).ToString();
                        break;
                }
                if (SwingLengthTypes.False != swingLengthType)
                    output = output + " / " + outputDuration;
                else
                    output = outputDuration;
            }
            switch (curHighRelation)
            {
                case Relation.Higher:
                    swingLabel = "HH";
                    textColor = textColourHigher;
                    break;
                case Relation.Lower:
                    swingLabel = "LH";
                    textColor = textColorLower;
                    break;
                case Relation.Double:
                    swingLabel = "DT";
                    textColor = textColourDtb;
                    break;
            }
            if (output != null)
                DrawText("High" + swingCounterUp, AutoScale, output.ToString(),
                    CurrentBar - curHighBar, curHigh, textOffsetLength, textColor, textFont,
                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
            if (showSwingLabel)
                DrawText("UpLabel" + swingCounterUp, AutoScale, swingLabel,
                    CurrentBar - curHighBar, curHigh, textOffsetLabel, textColor, textFont,
                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
            if (showSwingPercent && curHighPercent != 0)
            {
                DrawText("UpPerc" + swingCounterUp, AutoScale, curHighPercent.ToString() + "%",
                    CurrentBar - curHighBar, curHigh, textOffsetPercent, textColor, textFont,
                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
            }
            if (SwingTimeTypes.False != showSwingTime)
            {
                string timeOutput = "";
                switch (showSwingTime)
                {
                    case SwingTimeTypes.Integer:
                        timeOutput = curHighTime.ToString();
                        break;
                    case SwingTimeTypes.HourMinute:
                        timeOutput = string.Format("{0:t}", Time[CurrentBar - curHighBar]);
                        break;
                    case SwingTimeTypes.HourMinuteSecond:
                        timeOutput = string.Format("{0:T}", Time[CurrentBar - curHighBar]);
                        break;
                    case SwingTimeTypes.DayMonth:
                        timeOutput = string.Format("{0:dd.MM}", Time[CurrentBar - curHighBar]);
                        break;
                }
                DrawText("UpTime" + swingCounterUp, AutoScale, timeOutput,
                    CurrentBar - curHighBar, curHigh, textOffsetTime, textColor, textFont,
                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
            }
            if (SwingVolumeTypes.False != swingVolumeType)
            {
                DrawText("UpVolume" + swingCounterUp, AutoScale, 
                    TruncIntToStr(curHighVolume),
                    CurrentBar - curHighBar, curHigh, textOffsetVolume, textColor, textFont,
                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
            }
            #endregion

            #region GomCD
            // Uncomment the next lines to use Gomi's Cumulative Delta ====================
            if (showGomCD != ShowGomCD.False)
            {
                lastHighGomCdAll = curHighGomCdAll;
                gomCdAllSwingHighValue = Convert.ToInt32(gomCdAllDeltaClose[0]);
                curHighGomCdAll = gomCdAllSwingHighValue - gomCdAllSwingLowValue;
                string gomCdOutput = "";
                switch (showGomCD)
                {
                    case ShowGomCD.AbsoluteCD:
                        gomCdOutput = TruncIntToStr(gomCdAllSwingHighValue);
                        break;
                    case ShowGomCD.AbsoluteCD_And_SwingCD:
                        gomCdOutput = TruncIntToStr(gomCdAllSwingHighValue) + " / " +
                            TruncIntToStr(curHighGomCdAll);
                        break;
                    case ShowGomCD.SwingCD:
                        gomCdOutput = TruncIntToStr(curHighGomCdAll);
                        break;
                    case ShowGomCD.SwingCD_And_AbsoluteCD:
                        gomCdOutput = TruncIntToStr(curHighGomCdAll) + " / " +
                            TruncIntToStr(gomCdAllSwingHighValue);
                        break;
                }
                DrawText("UpGomCd" + swingCounterUp, AutoScale, gomCdOutput,
                    CurrentBar - curHighBar, curHigh, textOffsetGomCd, textColor, textFont,
                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
            }
            // Uncomment till here ================================================================
            #endregion

            Swings up = new Swings(curHighBar, curHigh, curHighLength, curHighDuration, curHighRelation);
            swingHighs.Add(up);
            upCount = swingHighs.Count - 1;
        }
Exemplo n.º 12
0
        //#####################################################################
        #endregion
        //#####################################################################
#endif
        #endregion

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            #region Initialize varibles
            //=================================================================
            if (FirstTickOfBar)
            {
                stopOutsideBarCalc = false;
                entryLevelLine.Set(0);
                abcSignals.Set(0);

                if (divHiddenShortActive == true)
                    divSignal.Set(-2);
                else if (divRegularShortActive == true)
                    divSignal.Set(-1);
                else if (divHiddenLongActive == true)
                    divSignal.Set(2);
                else if (divRegularLongActive == true)
                    divSignal.Set(1);
                else
                    divSignal.Set(0);

                if (curLow == 0.0 || curHigh == 0.0)
                {
                    entryLong.Set(0);
                    entryShort.Set(0);
                }
                else
                {
                    entryLong.Set(entryLong[1]);
                    entryShort.Set(entryShort[1]);
                }

                if (CurrentBar == 1)
                {
                    curHighBar = curLowBar = CurrentBar;
                    curHigh = High[1];
                    curLow = Low[1];
                    curHighDateTime = curLowDateTime = lastHighDateTime = lastLowDateTime = Time[CurrentBar];
                    Swings up = new Swings(curHighBar, curHigh, 0, 0, curHighRelation);
                    Swings dn = new Swings(curLowBar, curLow, 0, 0, curLowRelation);
                    swingHighs.Add(up);
                    swingLows.Add(dn);
                    upCount = swingHighs.Count;
                    dnCount = swingLows.Count;
                    #region GomCD
                    // Uncomment the next lines to use Gomi's Cumulative Delta ====================
                    gomCdAllDeltaClose = gomCdAll.DeltaClose;
                    // Uncomment till here ========================================================
                    #endregion

                    #region Divergence
                    switch (divergenceMode)
	                {
                        case DivergenceMode.Custom:
                            // Add custom divergence indicator here
                            break;
                        case DivergenceMode.GomCD:
                            #region GomCD
                            // Uncomment the next lines to use Gomi's Cumulative Delta ====================
                            divergenceDataHigh = gomCdAll.DeltaHigh;
                            divergenceDataLow = gomCdAll.DeltaHigh;
                            // Uncomment till here ========================================================
                            #endregion
                            break;
                        case DivergenceMode.MACD:
                            divergenceDataHigh = macd.Diff;
                            divergenceDataLow = macd.Diff;
                            break;
                        case DivergenceMode.Stochastics:
                            divergenceDataHigh = stochastics.K;
                            divergenceDataLow = stochastics.K;
                            break;
                        default:
                            break;
	                }
                    #endregion
                }
            }
            // Set new/update high/low back to false, to avoid function 
            // calls which depends on them
            dnFlip.Set(false);
            upFlip.Set(false);
            newHigh = newLow = updateHigh = updateLow = false;

            // Checks to make sure we have enough bars to calculate the indicator   
            if (CurrentBar < swingSize)
                return;
            //=================================================================
            #endregion

            #region Swing calculation
            // Swing calculation ==============================================
            switch (swingType)
            {
                case SwingTypes.Standard:
                    #region Standard Swing
                    if (swingSlope == 1 && High[0] <= curHigh)
                        newHigh = false;
                    else
                        newHigh = true;

                    if (swingSlope == -1 && Low[0] >= curLow)
                        newLow = false;
                    else
                        newLow = true;

                    if (FirstTickOfBar && VisualizationTypes.GannStyle == visualizationType)
                    {
                        if (swingSlope == 1)
                            GannSwing.Set(curHigh);
                        else
                            GannSwing.Set(curLow);
                    }

                    // CalculatOnBarClose == true =================================================
                    if (CalculateOnBarClose)
                    {
                        if (newHigh)
                        {
                            for (int i = 1; i < swingSize + 1; i++)
                            {
                                if (High[0] <= High[i])
                                {
                                    newHigh = false;
                                    break;
                                }
                            }
                        }
                        if (newLow)
                        {
                            for (int i = 1; i < swingSize + 1; i++)
                            {
                                if (Low[0] >= Low[i])
                                {
                                    newLow = false;
                                    break;
                                }
                            }
                        }

                        if (newHigh && newLow)
                        {
                            if (swingSlope == -1)
                                newHigh = false;
                            else
                                newLow = false;
                        }
                    }
                    // CalculatOnBarClose == false ================================================
                    else
                    {
                        if (FirstTickOfBar)
                            newSwing = 0;

                        if (newSwing != -1)
                        {
                            if (newHigh)
                            {
                                for (int i = 1; i < swingSize + 1; i++)
                                {
                                    if (High[0] <= High[i])
                                    {
                                        newHigh = false;
                                        break;
                                    }
                                }
                                if (newHigh)
                                    newSwing = 1;
                            }
                        }

                        if (newSwing != 1)
                        {
                            if (newLow)
                            {
                                for (int i = 1; i < swingSize + 1; i++)
                                {
                                    if (Low[0] >= Low[i])
                                    {
                                        newLow = false;
                                        break;
                                    }
                                }
                                if (newLow)
                                    newSwing = -1;
                            }
                        }

                        if (newSwing == 1)
                            newLow = false;
                        if (newSwing == -1)
                            newHigh = false;
                    }

                    // Set new high or low ========================================================
                    if (newHigh)
                    {
                        if (swingSlope != 1)
                        {
                            bar = CurrentBar - HighestBar(High, CurrentBar - curLowBar);
                            price = High[HighestBar(High, CurrentBar - curLowBar)];
                            updateHigh = false;
                        }
                        else
                        {
                            bar = CurrentBar;
                            price = High[0];
                            updateHigh = true;
                        }
                        CalcUpSwing(bar, price, updateHigh);
                    }
                    else if (newLow)
                    {
                        if (swingSlope != -1)
                        {
                            bar = CurrentBar - LowestBar(Low, CurrentBar - curHighBar);
                            price = Low[LowestBar(Low, CurrentBar - curHighBar)];
                            updateLow = false;
                        }
                        else
                        {
                            bar = CurrentBar;
                            price = Low[0];
                            updateLow = true;
                        }
                        CalcDnSwing(bar, price, updateLow);
                    }
                    // ============================================================================
                    #endregion
                    break;
                case SwingTypes.Gann:
                    #region Set bar property
                    //=================================================================
                    if (paintBars)
                    {
                        if (High[0] > High[1])
                        {
                            if (Low[0] < Low[1])
                            {
                                barType = BarType.OutsideBar;
                                BarColor = outsideBarColour;
                            }
                            else
                            {
                                barType = BarType.UpBar;
                                BarColor = upBarColour;
                            }
                        }
                        else
                        {
                            if (Low[0] < Low[1])
                            {
                                barType = BarType.DnBar;
                                BarColor = dnBarColour;
                            }
                            else
                            {
                                barType = BarType.InsideBar;
                                BarColor = insideBarColour;
                            }
                        }
                    }
                    else
                    {
                        if (High[0] > High[1])
                        {
                            if (Low[0] < Low[1])
                                barType = BarType.OutsideBar;
                            else
                                barType = BarType.UpBar;
                        }
                        else
                        {
                            if (Low[0] < Low[1])
                                barType = BarType.DnBar;
                            else
                                barType = BarType.InsideBar;
                        }
                    }
                    //===================================================================
                    #endregion

                    #region Gann Swing
                    // Gann swing =================================================================
                    #region Up swing
                    // Up Swing ===================================================================
                    if (swingSlope == 1)
                    {
                        if (FirstTickOfBar && VisualizationTypes.GannStyle == visualizationType)
                            GannSwing.Set(curHigh);

                        switch (barType)
                        {
                            // Up bar =============================================================
                            case BarType.UpBar:
                                consecutiveBars = 0;
                                consecutiveBarValue = 0.0;
                                if (High[0] > curHigh)
                                {
                                    updateHigh = true;
                                    CalcUpSwing(CurrentBar, High[0], true);
                                    if ((consecutiveBars + 1) == swingSize)
                                        stopOutsideBarCalc = true;
                                }
                                break;
                            // Down bar ===========================================================
                            case BarType.DnBar:
                                if (consecutiveBarNumber != CurrentBar)
                                {
                                    if (consecutiveBarValue == 0.0)
                                    {
                                        consecutiveBars++;
                                        consecutiveBarNumber = CurrentBar;
                                        consecutiveBarValue = Low[0];
                                    }
                                    else if (Low[0] < consecutiveBarValue)
                                    {
                                        consecutiveBars++;
                                        consecutiveBarNumber = CurrentBar;
                                        consecutiveBarValue = Low[0];
                                    }
                                }
                                else if (Low[0] < consecutiveBarValue)
                                    consecutiveBarValue = Low[0];
                                if (consecutiveBars == swingSize || (useBreakouts && Low[0] < curLow))
                                {
                                    consecutiveBars = 0;
                                    consecutiveBarValue = 0.0;
                                    newLow = true;
                                    bar = CurrentBar - LowestBar(Low, CurrentBar - curHighBar);
                                    price = Low[LowestBar(Low, CurrentBar - curHighBar)];
                                    CalcDnSwing(bar, price, false);
                                }
                                break;
                            // Inside bar =========================================================
                            case BarType.InsideBar:
                                if (!ignoreInsideBars)
                                {
                                    consecutiveBars = 0;
                                    consecutiveBarValue = 0.0;
                                }
                                break;
                            // Outside bar ========================================================
                            case BarType.OutsideBar:
                                if (High[0] > curHigh)
                                {
                                    updateHigh = true;
                                    CalcUpSwing(CurrentBar, High[0], true);
                                }
                                else if (!stopOutsideBarCalc)
                                {
                                    if (consecutiveBarNumber != CurrentBar)
                                    {
                                        if (consecutiveBarValue == 0.0)
                                        {
                                            consecutiveBars++;
                                            consecutiveBarNumber = CurrentBar;
                                            consecutiveBarValue = Low[0];
                                        }
                                        else if (Low[0] < consecutiveBarValue)
                                        {
                                            consecutiveBars++;
                                            consecutiveBarNumber = CurrentBar;
                                            consecutiveBarValue = Low[0];
                                        }
                                    }
                                    else if (Low[0] < consecutiveBarValue)
                                        consecutiveBarValue = Low[0];
                                    if (consecutiveBars == swingSize || (useBreakouts && Low[0] < curLow))
                                    {
                                        consecutiveBars = 0;
                                        consecutiveBarValue = 0.0;
                                        newLow = true;
                                        bar = CurrentBar - LowestBar(Low, CurrentBar - curHighBar);
                                        price = Low[LowestBar(Low, CurrentBar - curHighBar)];
                                        CalcDnSwing(bar, price, false);
                                    }
                                }
                                break;
                        }
                    }
                    // End up swing ===============================================================
                    #endregion

                    #region Down swing
                    // Down swing =================================================================
                    else
                    {
                        if (FirstTickOfBar && VisualizationTypes.GannStyle == visualizationType)
                            GannSwing.Set(curLow);

                        switch (barType)
                        {
                            // Dwon bar ===========================================================
                            case BarType.DnBar:
                                consecutiveBars = 0;
                                consecutiveBarValue = 0.0;
                                if (Low[0] < curLow)
                                {
                                    updateLow = true;
                                    CalcDnSwing(CurrentBar, Low[0], true);
                                    if ((consecutiveBars + 1) == swingSize)
                                        stopOutsideBarCalc = true;
                                }
                                break;
                            // Up bar =============================================================
                            case BarType.UpBar:
                                if (consecutiveBarNumber != CurrentBar)
                                {
                                    if (consecutiveBarValue == 0.0)
                                    {
                                        consecutiveBars++;
                                        consecutiveBarNumber = CurrentBar;
                                        consecutiveBarValue = High[0];
                                    }
                                    else if (High[0] > consecutiveBarValue)
                                    {
                                        consecutiveBars++;
                                        consecutiveBarNumber = CurrentBar;
                                        consecutiveBarValue = High[0];
                                    }
                                }
                                else if (High[0] > consecutiveBarValue)
                                    consecutiveBarValue = High[0];
                                if (consecutiveBars == swingSize || (useBreakouts && High[0] > curHigh))
                                {
                                    consecutiveBars = 0;
                                    consecutiveBarValue = 0.0;
                                    newHigh = true;
                                    bar = CurrentBar - HighestBar(High, CurrentBar - curLowBar);
                                    price = High[HighestBar(High, CurrentBar - curLowBar)];
                                    CalcUpSwing(bar, price, false);
                                }
                                break;
                            // Inside bar =========================================================
                            case BarType.InsideBar:
                                if (!ignoreInsideBars)
                                {
                                    consecutiveBars = 0;
                                    consecutiveBarValue = 0.0;
                                }
                                break;
                            // Outside bar ========================================================
                            case BarType.OutsideBar:
                                if (Low[0] < curLow)
                                {
                                    updateLow = true;
                                    CalcDnSwing(CurrentBar, Low[0], true);
                                }
                                else if (!stopOutsideBarCalc)
                                {
                                    if (consecutiveBarNumber != CurrentBar)
                                    {
                                        if (consecutiveBarValue == 0.0)
                                        {
                                            consecutiveBars++;
                                            consecutiveBarNumber = CurrentBar;
                                            consecutiveBarValue = High[0];
                                        }
                                        else if (High[0] > consecutiveBarValue)
                                        {
                                            consecutiveBars++;
                                            consecutiveBarNumber = CurrentBar;
                                            consecutiveBarValue = High[0];
                                        }
                                    }
                                    else if (High[0] > consecutiveBarValue)
                                        consecutiveBarValue = High[0];
                                    if (consecutiveBars == swingSize || (useBreakouts && High[0] > curHigh))
                                    {
                                        consecutiveBars = 0;
                                        consecutiveBarValue = 0.0;
                                        newHigh = true;
                                        bar = CurrentBar - HighestBar(High, CurrentBar - curLowBar);
                                        price = High[HighestBar(High, CurrentBar - curLowBar)];
                                        CalcUpSwing(bar, price, false);
                                    }
                                }
                                break;
                        }
                    }
                    // End down swing =============================================================
                    #endregion
                    // End Gann swing =============================================================
                    #endregion
                    break;
            }
            // End swing calculation ==========================================
            #endregion

            #region Fibonacci tools
            //=================================================================
            if (newHigh || newLow || updateHigh || updateLow)
            {
                #region Fibonacci extensions
                // Fibonacci extensions ===========================================================
                if (addFibExt)
                {
                    if (lastHigh == 0.0 || lastLow == 0.0)
                        return;

                    if (swingLows[swingLows.Count - 1].relation == Relation.Higher && swingSlope == -1)
                    {
                        int anchor1BarsAgo = CurrentBar - lastLowBar;
                        int anchor2BarsAgo = CurrentBar - curHighBar;
                        int anchor3BarsAgo = CurrentBar - curLowBar;
                        double anchor1Y = lastLow;
                        double anchor2Y = curHigh;
                        double anchor3Y = curLow;
                        DrawFibonacciExtensions("FibExtUp", false, anchor1BarsAgo, anchor1Y,
                            anchor2BarsAgo, anchor2Y, anchor3BarsAgo, anchor3Y);
                    }
                    else if (swingLows[swingLows.Count - 1].relation == Relation.Higher && swingSlope == 1)
                    {
                        int anchor1BarsAgo = CurrentBar - lastLowBar;
                        int anchor2BarsAgo = CurrentBar - lastHighBar;
                        int anchor3BarsAgo = CurrentBar - curLowBar;
                        double anchor1Y = lastLow;
                        double anchor2Y = lastHigh;
                        double anchor3Y = curLow;
                        DrawFibonacciExtensions("FibExtUp", false, anchor1BarsAgo, anchor1Y,
                            anchor2BarsAgo, anchor2Y, anchor3BarsAgo, anchor3Y);
                    }
                    else
                        RemoveDrawObject("FibExtUp");

                    if (swingHighs[swingHighs.Count - 1].relation == Relation.Lower && swingSlope == 1)
                    {
                        int anchor1BarsAgo = CurrentBar - lastHighBar;
                        int anchor2BarsAgo = CurrentBar - curLowBar;
                        int anchor3BarsAgo = CurrentBar - curHighBar;
                        double anchor1Y = lastHigh;
                        double anchor2Y = curLow;
                        double anchor3Y = curHigh;
                        DrawFibonacciExtensions("FibExtDn", false, anchor1BarsAgo, anchor1Y,
                            anchor2BarsAgo, anchor2Y, anchor3BarsAgo, anchor3Y);
                    }
                    else if (swingHighs[swingHighs.Count - 1].relation == Relation.Lower && swingSlope == -1)
                    {
                        int anchor1BarsAgo = CurrentBar - lastHighBar;
                        int anchor2BarsAgo = CurrentBar - lastLowBar;
                        int anchor3BarsAgo = CurrentBar - curHighBar;
                        double anchor1Y = lastHigh;
                        double anchor2Y = lastLow;
                        double anchor3Y = curHigh;
                        DrawFibonacciExtensions("FibExtDn", false, anchor1BarsAgo, anchor1Y,
                            anchor2BarsAgo, anchor2Y, anchor3BarsAgo, anchor3Y);
                    }
                    else
                        RemoveDrawObject("FibExtDn");
                }
                // ================================================================================
                #endregion

                #region Fibonacci retracements
                // Fibonacci retracements =========================================================
                if (addFastFibRet)
                {
                    int anchor1BarsAgo = 0;
                    int anchor2BarsAgo = 0;
                    double anchor1Y = 0.0;
                    double anchor2Y = 0.0;

                    if (swingSlope == 1)
                    {
                        anchor1BarsAgo = CurrentBar - curLowBar;
                        anchor1Y = curLow;
                        anchor2BarsAgo = CurrentBar - curHighBar;
                        anchor2Y = curHigh;
                    }
                    else
                    {
                        anchor1BarsAgo = CurrentBar - curHighBar;
                        anchor1Y = curHigh;
                        anchor2BarsAgo = CurrentBar - curLowBar;
                        anchor2Y = curLow;
                    }
                    DrawFibonacciRetracements("FastFibRet", AutoScale,
                        anchor1BarsAgo, anchor1Y, anchor2BarsAgo, anchor2Y);
                }

                if (addSlowFibRet)
                {
                    if (lastHigh == 0.0 || lastLow == 0.0) return;

                    int anchor1BarsAgo = 0;
                    int anchor2BarsAgo = 0;
                    double anchor1Y = 0.0;
                    double anchor2Y = 0.0;

                    if (swingSlope == 1)
                    {
                        anchor1BarsAgo = CurrentBar - lastHighBar;
                        anchor1Y = lastHigh;
                        anchor2BarsAgo = CurrentBar - curLowBar;
                        anchor2Y = curLow;
                    }
                    else
                    {
                        anchor1BarsAgo = CurrentBar - lastLowBar;
                        anchor1Y = lastLow;
                        anchor2BarsAgo = CurrentBar - curHighBar;
                        anchor2Y = curHigh;
                    }

                    if ((swingSlope == 1 && curHigh < lastHigh) || (swingSlope == -1 && curLow > lastLow))
                        DrawFibonacciRetracements("SlowFibRet", AutoScale, anchor1BarsAgo, anchor1Y,
                            anchor2BarsAgo, anchor2Y);
                    else
                        RemoveDrawObject("SlowFibRet");
                }
                #endregion
            }
            //=================================================================
            #endregion

            #region ABC pattern
            // ABC pattern ====================================================
            #region ABC long pattern
            // ABC long pattern ===================================================================
            if (abcPattern == AbcPatternMode.Long_And_Short || abcPattern == AbcPatternMode.Long)
            {
                if ((updateLow || newLow) && !abcLongChanceInProgress
                        && lastLowRelation == Relation.Lower && curLowRelation == Relation.Higher
                        && curLowPercent > abcMinRetracement && curLowPercent < abcMaxRetracement)
                {
                    drawTag = swingCounterUp;
                    abcLongChanceInProgress = true;
                    entryLineStartBar = CurrentBar;
                    patternCounter++;
                    tmpCounter = swingCounterDn;
                    abcSignals.Set(1);
                    aBar = CurrentBar - lastLowBar;
                    bBar = CurrentBar - curHighBar;
                    cBar = CurrentBar - curLowBar;

                    DrawLine("ABLineUp" + drawTag, AutoScale, aBar, lastLow,
                        bBar, curHigh, abcZigZagColourUp, abcLineStyle, abcLineWidth);
                    DrawLine("BCLineUp" + drawTag, AutoScale, bBar, curHigh,
                        cBar, curLow, abcZigZagColourUp, abcLineStyle, abcLineWidth);
                    DrawLine("ACLineUp" + drawTag, AutoScale, aBar, lastLow,
                        cBar, curLow, abcZigZagColourUp, abcLineStyleRatio, abcLineWidthRatio);
                    if (abcLabel)
                    {
                        DrawText("AUp" + drawTag, AutoScale, "A", aBar, lastLow,
                            -abcTextOffsetLabel, abcTextColourUp, abcTextFont,
                            StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
                        DrawText("BUp" + drawTag, AutoScale, "B", bBar, curHigh,
                            abcTextOffsetLabel, abcTextColourUp, abcTextFont,
                            StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
                        DrawText("CUp" + drawTag, AutoScale, "C", cBar, curLow,
                            -abcTextOffsetLabel, abcTextColourUp, abcTextFont,
                            StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
                    }

                    entryLevel = curLow + Instrument.MasterInstrument.Round2TickSize((Math.Abs(curLowLength) * TickSize)
                        / 100 * retracementEntryValue);
                    entryLevelLine.Set(entryLevel);
                    if (showEntryLine)
                        DrawLine("EntryLine" + CurrentBar.ToString(), AutoScale, 1, entryLevelLine[0], 0, entryLevelLine[0],
                            entryLineColourUp, entryLineStyle, entryLineWidth);
                    if (useAbcAlerts)
                        Alert("Alert_Abc_Long" + alertTag++.ToString(), priorityAbc, "ABC Long" + " ("
                            + Bars.Period.ToString() + ")", fileNameAbcLong, 0, Color.White, Color.Blue);
                    entryLong.Set(entryLevel);
                    if (showRiskManagement)
                    {
                        double entryRM = entryLong[0];
                        double stopRM = curLow - 1 * TickSize;
                        double target100RM = 0.0;
                        if (swingSlope == 1)
                            target100RM = curLow + lastHighLength * TickSize * abcTarget / 100;
                        else
                            target100RM = curLow + curHighLength * TickSize * abcTarget / 100;
                        CalculateLongRisk(entryRM, stopRM, target100RM);
                    }
                }

                if (abcLongChanceInProgress)
                {
                    if (curLowPercent > abcMaxRetracement && tmpCounter == swingCounterDn)
                    {
                        abcLongChanceInProgress = false;
                        RemoveDrawObject("ABLineUp" + drawTag.ToString());
                        RemoveDrawObject("BCLineUp" + drawTag.ToString());
                        RemoveDrawObject("ACLineUp" + drawTag.ToString());
                        RemoveDrawObject("AUp" + drawTag.ToString());
                        RemoveDrawObject("BUp" + drawTag.ToString());
                        RemoveDrawObject("CUp" + drawTag.ToString());
                        // Remove entryLevelLine (maybe remove more objects as drawn (COBC))
                        if (!showHistoricalEntryLine)
                        {
                            for (int index = 0; index < CurrentBar - entryLineStartBar + 1; index++)
                            {
                                RemoveDrawObject("EntryLine" + (CurrentBar - index).ToString());
                            }
                            entryLineStartBar = 0;
                        }
                    }
                    else if (dnFlip[0] && tmpCounter != swingCounterDn)
                    {
                        abcLongChanceInProgress = false;
                        entryLineStartBar = 0;
                    }
                    else
                    {
                        if (updateLow && tmpCounter == swingCounterDn)
                        {
                            aBar = CurrentBar - lastLowBar;
                            bBar = CurrentBar - curHighBar;
                            cBar = CurrentBar - curLowBar;

                            DrawLine("BCLineUp" + drawTag, AutoScale, bBar, curHigh,
                                cBar, curLow, abcZigZagColourUp, abcLineStyle, abcLineWidth);
                            DrawLine("ACLineUp" + drawTag, AutoScale, aBar, lastLow,
                                cBar, curLow, abcZigZagColourUp, abcLineStyleRatio, abcLineWidthRatio);
                            if (abcLabel)
                            {
                                DrawText("CUp" + drawTag, AutoScale, "C", cBar, curLow,
                                    -abcTextOffsetLabel, abcTextColourUp, abcTextFont,
                                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
                            }

                            entryLevel = curLow + Instrument.MasterInstrument.Round2TickSize((Math.Abs(curLowLength) * TickSize)
                                / 100 * retracementEntryValue);
                            entryLevelLine.Set(entryLevel);

                            if (showEntryLine)
                            {
                                if (entryLevelLine[1] == 0)
                                    DrawLine("EntryLine" + CurrentBar.ToString(), AutoScale, 1, entryLevelLine[0], 0, entryLevelLine[0],
                                        entryLineColourUp, entryLineStyle, entryLineWidth);
                                else
                                    DrawLine("EntryLine" + CurrentBar.ToString(), AutoScale, 1, entryLevelLine[1], 0, entryLevelLine[0],
                                        entryLineColourUp, entryLineStyle, entryLineWidth);
                            }
                            entryLong.Set(entryLevel);
                            if (showRiskManagement)
                            {
                                double entryRM = entryLong[0];
                                double stopRM = curLow - 1 * TickSize;
                                double target100RM = 0.0;
                                if (swingSlope == 1)
                                    target100RM = curLow + lastHighLength * TickSize * abcTarget / 100;
                                else
                                    target100RM = curLow + curHighLength * TickSize * abcTarget / 100;
                                CalculateLongRisk(entryRM, stopRM, target100RM);
                            }
                        }
                        else if (FirstTickOfBar)
                        {
                            entryLevelLine.Set(entryLevel);

                            if (showEntryLine)
                            {
                                if (entryLevelLine[1] == 0)
                                    DrawLine("EntryLine" + CurrentBar.ToString(), AutoScale, 1, entryLevelLine[0], 0, entryLevelLine[0],
                                        entryLineColourUp, entryLineStyle, entryLineWidth);
                                else
                                    DrawLine("EntryLine" + CurrentBar.ToString(), AutoScale, 1, entryLevelLine[1], 0, entryLevelLine[0],
                                        entryLineColourUp, entryLineStyle, entryLineWidth);
                            }
                        }
                        abcSignals.Set(1);

                        bool abcLong = false;
                        if (CalculateOnBarClose || Historical)
                        {
                            if (Close[0] > entryLevel)
                            {
                                entryLong.Set(Close[0]);
                                abcLong = true;
                            }
                        }
                        else
                        {
                            if (FirstTickOfBar && Open[0] > entryLevel)
                            {
                                entryLong.Set(Open[0]);
                                abcLong = true;
                            }
                        }

                        if (abcLong)
                        {
                            if (showEntryArrows)
                                DrawArrowUp("AbcUp" + abcEntryTag++.ToString(), AutoScale, 0,
                                    Low[0] - yTickOffset * TickSize, abcTextColourUp);
                            abcLongChanceInProgress = false;
                            entryLineStartBar = 0;
                            entryBar = CurrentBar;
                            abcSignals.Set(2);
                            if (showRiskManagement)
                            {
                                double entryRM = entryLong[0];
                                double stopRM = curLow - 1 * TickSize;
                                double target100RM = 0.0;
                                if (swingSlope == 1)
                                    target100RM = curLow + lastHighLength * TickSize * abcTarget / 100;
                                else
                                    target100RM = curLow + curHighLength * TickSize * abcTarget / 100;
                                CalculateLongRisk(entryRM, stopRM, target100RM);
                            }
                            if (useAbcEntryAlerts)
                                Alert("Alert_Abc_Long_Entry" + alertTag++.ToString(), priorityAbcEntry, "ABC Long Entry" + " ("
                                    + Bars.Period.ToString() + ")", fileNameAbcLongEntry, 0, Color.Blue, Color.White);
                        }
                    }
                }
            }
            // End ABC long pattern ===============================================================
            #endregion

            #region ABC short pattern
            // ABC short pattern ==================================================================
            if (abcPattern == AbcPatternMode.Long_And_Short || abcPattern == AbcPatternMode.Short)
            {
                if ((updateHigh || newHigh) && !abcShortChanceInProgress
                        && lastHighRelation == Relation.Higher && curHighRelation == Relation.Lower
                        && curHighPercent > abcMinRetracement && curHighPercent < abcMaxRetracement)
                {
                    drawTag = swingCounterDn;
                    abcShortChanceInProgress = true;
                    entryLineStartBar = CurrentBar;
                    patternCounter++;
                    tmpCounter = swingCounterUp;
                    abcSignals.Set(-1);

                    aBar = CurrentBar - lastHighBar;
                    bBar = CurrentBar - curLowBar;
                    cBar = CurrentBar - curHighBar;

                    DrawLine("ABLineDn" + drawTag, AutoScale, aBar, lastHigh,
                        bBar, curLow, abcZigZagColourDn, abcLineStyle, abcLineWidth);
                    DrawLine("BCLineDn" + drawTag, AutoScale, bBar, curLow,
                        cBar, curHigh, abcZigZagColourDn, abcLineStyle, abcLineWidth);
                    DrawLine("ACLineDn" + drawTag, AutoScale, aBar, lastHigh,
                        cBar, curHigh, abcZigZagColourDn, abcLineStyleRatio, abcLineWidthRatio);
                    if (abcLabel)
                    {
                        DrawText("ADn" + drawTag, AutoScale, "A", aBar, lastHigh,
                            abcTextOffsetLabel, abcTextColourDn, abcTextFont,
                            StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
                        DrawText("BDn" + drawTag, AutoScale, "B", bBar, curLow,
                            -abcTextOffsetLabel, abcTextColourDn, abcTextFont,
                            StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
                        DrawText("CDn" + drawTag, AutoScale, "C", cBar, curHigh,
                            abcTextOffsetLabel, abcTextColourDn, abcTextFont,
                            StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
                    }

                    entryLevel = curHigh - Instrument.MasterInstrument.Round2TickSize((curHighLength * TickSize)
                        / 100 * retracementEntryValue);
                    entryLevelLine.Set(entryLevel);
                    if (showEntryLine)
                        DrawLine("EntryLine" + CurrentBar.ToString(), AutoScale, 1, entryLevelLine[0], 0, entryLevelLine[0],
                            entryLineColourDn, entryLineStyle, entryLineWidth);
                    if (useAbcAlerts)
                        Alert("Alert_Abc_Short" + alertTag++.ToString(), priorityAbc, "ABC Short" + " ("
                            + Bars.Period.ToString() + ")", fileNameAbcShort, 0, Color.White, Color.Red);
                    entryShort.Set(entryLevel);
                    if (showRiskManagement)
                    {
                        double entryRM = entryShort[0];
                        double stopRM = curHigh + 1 * TickSize;
                        double target100RM = 0.0;
                        if (swingSlope == -1)
                            target100RM = curHigh + lastLowLength * TickSize * abcTarget / 100;
                        else
                            target100RM = curHigh + curLowLength * TickSize * abcTarget / 100;
                        CalculateShortRisk(entryRM, stopRM, target100RM);
                    }
                }

                if (abcShortChanceInProgress)
                {
                    if (curHighPercent > abcMaxRetracement && tmpCounter == swingCounterUp)
                    {
                        abcShortChanceInProgress = false;
                        RemoveDrawObject("ABLineDn" + drawTag.ToString());
                        RemoveDrawObject("BCLineDn" + drawTag.ToString());
                        RemoveDrawObject("ACLineDn" + drawTag.ToString());
                        RemoveDrawObject("ADn" + drawTag.ToString());
                        RemoveDrawObject("BDn" + drawTag.ToString());
                        RemoveDrawObject("CDn" + drawTag.ToString());
                        // Remove entryLevelLine (maybe remove more objects as drawn (COBC))
                        if (!showHistoricalEntryLine)
                        {
                            for (int index = 0; index < CurrentBar - entryLineStartBar + 1; index++)
                            {
                                RemoveDrawObject("EntryLine" + (CurrentBar - index).ToString());
                            }
                            entryLineStartBar = 0;
                        }
                    }
                    else if (upFlip[0] && tmpCounter != swingCounterUp)
                    {
                        abcShortChanceInProgress = false;
                        entryLineStartBar = 0;
                    }
                    else
                    {
                        if (updateHigh && tmpCounter == swingCounterUp)
                        {
                            aBar = CurrentBar - lastHighBar;
                            bBar = CurrentBar - curLowBar;
                            cBar = CurrentBar - curHighBar;

                            DrawLine("BCLineDn" + drawTag, AutoScale, bBar, curLow,
                                cBar, curHigh, abcZigZagColourDn, abcLineStyle, abcLineWidth);
                            DrawLine("ACLineDn" + drawTag, AutoScale, aBar, lastHigh,
                                cBar, curHigh, abcZigZagColourDn, abcLineStyleRatio, abcLineWidthRatio);
                            if (abcLabel)
                            {
                                DrawText("CDn" + drawTag, AutoScale, "C", cBar, curHigh,
                                    abcTextOffsetLabel, abcTextColourDn, abcTextFont,
                                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
                            }

                            entryLevel = curHigh - Instrument.MasterInstrument.Round2TickSize((curHighLength * TickSize)
                                / 100 * retracementEntryValue);
                            entryLevelLine.Set(entryLevel);

                            if (showEntryLine)
                            {
                                if (entryLevelLine[1] == 0)
                                    DrawLine("EntryLine" + CurrentBar.ToString(), AutoScale, 1, entryLevelLine[0], 0, entryLevelLine[0],
                                        entryLineColourDn, entryLineStyle, entryLineWidth);
                                else
                                    DrawLine("EntryLine" + CurrentBar.ToString(), AutoScale, 1, entryLevelLine[1], 0, entryLevelLine[0],
                                        entryLineColourDn, entryLineStyle, entryLineWidth);
                            }
                            entryShort.Set(entryLevel);
                            if (showRiskManagement)
                            {
                                double entryRM = entryShort[0];
                                double stopRM = curHigh + 1 * TickSize;
                                double target100RM = 0.0;
                                if (swingSlope == -1)
                                    target100RM = curHigh + lastLowLength * TickSize * abcTarget / 100;
                                else
                                    target100RM = curHigh + curLowLength * TickSize * abcTarget / 100;
                                CalculateShortRisk(entryRM, stopRM, target100RM);
                            }
                        }
                        else if (FirstTickOfBar)
                        {
                            entryLevelLine.Set(entryLevel);

                            if (showEntryLine)
                            {
                                if (entryLevelLine[1] == 0)
                                    DrawLine("EntryLine" + CurrentBar.ToString(), AutoScale, 1, entryLevelLine[0], 0, entryLevelLine[0],
                                        entryLineColourDn, entryLineStyle, entryLineWidth);
                                else
                                    DrawLine("EntryLine" + CurrentBar.ToString(), AutoScale, 1, entryLevelLine[1], 0, entryLevelLine[0],
                                        entryLineColourDn, entryLineStyle, entryLineWidth);
                            }
                        }
                        abcSignals.Set(-1);

                        bool abcShort = false;
                        if (CalculateOnBarClose || Historical)
                        {
                            if (Close[0] < entryLevel)
                            {
                                entryShort.Set(Close[0]);
                                abcShort = true;
                            }
                        }
                        else
                        {
                            if (FirstTickOfBar && Open[0] < entryLevel)
                            {
                                entryShort.Set(Open[0]);
                                abcShort = true;
                            }
                        }

                        if (abcShort)
                        {
                            if (showEntryArrows)
                                DrawArrowDown("AbcDn" + abcEntryTag++.ToString(), AutoScale, 0,
                                    High[0] + yTickOffset * TickSize, abcTextColourDn);
                            abcShortChanceInProgress = false;
                            entryLineStartBar = 0;
                            entryBar = CurrentBar;
                            abcSignals.Set(-2);
                            if (showRiskManagement)
                            {
                                double entryRM = entryShort[0];
                                double stopRM = curHigh + 1 * TickSize;
                                double target100RM = 0.0;
                                if (swingSlope == -1)
                                    target100RM = curHigh + lastLowLength * TickSize * abcTarget / 100;
                                else
                                    target100RM = curHigh + curLowLength * TickSize * abcTarget / 100;
                                CalculateShortRisk(entryRM, stopRM, target100RM);
                            }
                            if (useAbcEntryAlerts)
                                Alert("Alert_Abc_Short_Entry" + alertTag++.ToString(), priorityAbcEntry, "ABC Short Entry" + " ("
                                    + Bars.Period.ToString() + ")", fileNameAbcShortEntry, 0, Color.Red, Color.Black);
                        }
                    }
                }
            }
            // End ABC short pattern ==============================================================
            #endregion
            // End ABC pattern ================================================
            #endregion

            #region Divergence
            if (divergenceMode != DivergenceMode.False)
            {
                if (divergenceDirection != DivergenceDirection.Short)
                {
                    if (newHigh == true && updateHigh == false)
                    {
                        drawTagDivDn++;
                        divLastSwing = lastHigh;
                        divLastOscValue = Math.Max(divergenceDataHigh[CurrentBar - lastHighBar + 1],
                            Math.Max(divergenceDataHigh[CurrentBar - lastHighBar],
                            divergenceDataHigh[CurrentBar - lastHighBar - 1]));
                    }

                    if (newHigh == true || updateHigh == true)
                    {
                        divCurSwing = curHigh;
                        divCurOscValue = Math.Max(divergenceDataHigh[CurrentBar - curHighBar],
                            divergenceDataHigh[CurrentBar - curHighBar + 1]);

                        if (showDivergenceHidden == true)
                        {
                            if (divLastSwing > divCurSwing && divLastOscValue < divCurOscValue)
                            {
                                DrawLine("DivHidSignalDn" + drawTagDivDn, AutoScale,
                                    CurrentBar - lastHighBar, lastHigh, CurrentBar - curHighBar,
                                    curHigh, divDnLineColour, divDnLineStyle, divDnLineWidth);
                                int textDivBarAgo = Convert.ToInt32(CurrentBar -
                                    (lastHighBar + curHighBar) / 2);
                                double textDivPrice = Instrument.MasterInstrument.Round2TickSize(
                                    (lastHigh + curHigh) / 2);
                                DrawText("DivHidTextDn" + drawTagDivDn, AutoScale, "hDiv",
                                    textDivBarAgo, textDivPrice, 10, divDnLineColour, textFont,
                                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
                                divHiddenShortActive = true;
                                divSignal.Set(-2);
                            }
                            else
                            {
                                RemoveDrawObject("DivHidSignalDn" + drawTagDivDn);
                                RemoveDrawObject("DivHidTextDn" + drawTagDivDn);
                                divHiddenShortActive = false;
                            }
                        }

                        if (showDivergenceRegular == true)
                        {
                            if (divLastSwing < divCurSwing && divLastOscValue > divCurOscValue)
                            {
                                DrawLine("DivSignalDn" + drawTagDivDn, AutoScale,
                                    CurrentBar - lastHighBar, lastHigh, CurrentBar - curHighBar,
                                    curHigh, divDnLineColour, divDnLineStyle, divDnLineWidth);
                                int textDivBarAgo = Convert.ToInt32(CurrentBar -
                                    (lastHighBar + curHighBar) / 2);
                                double textDivPrice = Instrument.MasterInstrument.Round2TickSize(
                                    (lastHigh + curHigh) / 2);
                                DrawText("DivRegTextDn" + drawTagDivDn, AutoScale, "rDiv",
                                    textDivBarAgo, textDivPrice, 10, divDnLineColour, textFont,
                                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
                                divRegularShortActive = true;
                                divSignal.Set(-1);
                            }
                            else
                            {
                                RemoveDrawObject("DivSignalDn" + drawTagDivDn);
                                RemoveDrawObject("DivRegTextDn" + drawTagDivDn);
                                divRegularShortActive = false;
                            }
                        }
                    }
                }

                if (divergenceDirection != DivergenceDirection.Long)
                {
                    if (newLow == true && updateLow == false)
                    {
                        drawTagDivUp++;
                        divLastSwing = lastLow;
                        divLastOscValue = Math.Min(divergenceDataLow[CurrentBar - lastLowBar + 1],
                            Math.Min(divergenceDataLow[CurrentBar - lastLowBar],
                            divergenceDataLow[CurrentBar - lastLowBar - 1]));
                    }

                    if (newLow == true || updateLow == true)
                    {
                        divCurSwing = curLow;
                        divCurOscValue = Math.Min(divergenceDataLow[CurrentBar - curLowBar],
                            divergenceDataLow[CurrentBar - curLowBar + 1]);

                        if (showDivergenceHidden == true)
                        {
                            if (divLastSwing < divCurSwing && divLastOscValue > divCurOscValue)
                            {
                                DrawLine("DivHidSignalup" + drawTagDivUp, AutoScale,
                                    CurrentBar - lastLowBar, lastLow, CurrentBar - curLowBar,
                                    curLow, divUpLineColour, divUpLineStyle, divUpLineWidth);
                                int textDivBarAgo = Convert.ToInt32(CurrentBar -
                                    (lastLowBar + curLowBar) / 2);
                                double textDivPrice = Instrument.MasterInstrument.Round2TickSize(
                                    (lastLow + curLow) / 2);
                                DrawText("DivHidTextUp" + drawTagDivUp, AutoScale, "hDiv",
                                    textDivBarAgo, textDivPrice, -10, divUpLineColour, textFont,
                                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
                                divHiddenLongActive = true;
                                divSignal.Set(2);
                            }
                            else
                            {
                                RemoveDrawObject("DivHidSignalup" + drawTagDivUp);
                                RemoveDrawObject("DivHidTextUp" + drawTagDivUp);
                                divHiddenLongActive = false;
                            }
                        }

                        if (showDivergenceRegular == true)
                        {
                            if (divLastSwing > divCurSwing && divLastOscValue < divCurOscValue)
                            {
                                DrawLine("DivSignalUp" + drawTagDivUp, AutoScale,
                                    CurrentBar - lastLowBar, lastLow, CurrentBar - curLowBar,
                                    curLow, divUpLineColour, divUpLineStyle, divUpLineWidth);
                                int textDivBarAgo = Convert.ToInt32(CurrentBar -
                                    (lastLowBar + curLowBar) / 2);
                                double textDivPrice = Instrument.MasterInstrument.Round2TickSize(
                                    (lastLow + curLow) / 2);
                                DrawText("DivRegTextUp" + drawTagDivUp, AutoScale, "rDiv",
                                    textDivBarAgo, textDivPrice, -10, divUpLineColour, textFont,
                                    StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
                                divRegularLongActive = true;
                                divSignal.Set(1);
                            }
                            else
                            {
                                RemoveDrawObject("DivSignalUp" + drawTagDivUp);
                                RemoveDrawObject("DivRegTextUp" + drawTagDivUp);
                                divRegularLongActive = false;
                            }
                        }
                    }
                }

                if (dnFlip[0] == true)
                {
                    if (divRegularShortActive == true)
                    {
                        divRegularShortActive = false;
                        divSignal.Set(-3);
                    }
                    else if (divHiddenShortActive == true)
                    {
                        divHiddenShortActive = false;
                        divSignal.Set(-4);
                    }
                }
                else if (upFlip[0] == true)
                {
                    if (divRegularLongActive == true)
                    {
                        divRegularLongActive = false;
                        divSignal.Set(3);
                    }
                    else if (divHiddenLongActive == true)
                    {
                        divHiddenLongActive = false;
                        divSignal.Set(4);
                    }
                }
            }
            #endregion
        }