Need Help: Simple W/M Pattern & FVG Indicator (NinjaScript / NT8)

Hi everyone,

I am looking for some help creating a simple custom indicator for NinjaTrader 8. I have been trying to piece the logic together but am hitting a wall with the NinjaScript syntax.

The logic is straightforward: I want to automatically map Double Tops (M) and Double Bottoms (W) on my chart, mark the Fair Value Gap (FVG) / Order Block at the head or the neckline breakout for an entry point, and plot horizontal target lines for a Stop Loss and three Take Profit levels.

Since these are standard institutional concepts, does anyone have an existing script they could share, or a template I could use to get this working? I would deeply appreciate any guidance the community can offer. Thank you so much.

Can you provide some sort of image or picture as to what you are describing?

#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Windows.Media;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion

namespace NinjaTrader.NinjaScript.Indicators
{
public class WTDoubleTopBottomFVG : Indicator
{
private class SwingPoint
{
public int Bar;
public double Price;

        public SwingPoint(int bar, double price)
        {
            Bar = bar;
            Price = price;
        }
    }

    private class PatternSetup
    {
        public bool IsLong;

        public int FirstPivotBar;
        public int NecklineBar;
        public int SecondPivotBar;

        public double FirstPivot;
        public double Neckline;
        public double SecondPivot;

        public double Entry;
        public double Stop;

        public double TP1;
        public double TP2;
        public double TP3;

        public bool HasZone;
        public bool IsFVG;

        public double ZoneTop;
        public double ZoneBottom;

        public bool Activated;
        public bool Invalidated;

        public int BreakoutBar;
        public int EndBar;

        public double SecondPivotVolumeRatio;
        public double BreakoutVolumeRatio;

        public string Tag;
    }

    private List<SwingPoint> swingHighs;
    private List<SwingPoint> swingLows;
    private List<PatternSetup> setups;

    private HashSet<int> usedTopSecondPivots;
    private HashSet<int> usedBottomSecondPivots;

    private PatternSetup activeSetup;

    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            Description =
                "Double Top / Double Bottom detector with volume validation, directional FVG colors, FVG/Order Block entry, directional stop rectangle and 3 targets.";

            Name = "WTDoubleTopBottomFVG";

            Calculate = Calculate.OnBarClose;

            IsOverlay = true;
            DisplayInDataBox = false;
            DrawOnPricePanel = true;
            PaintPriceMarkers = false;
            IsSuspendedWhileInactive = true;

            //=================================================
            // PATTERN
            //=================================================

            SwingStrength = 5;

            TopBottomToleranceTicks = 8;

            MinimumPatternBars = 8;
            MaximumPatternBars = 100;

            MinimumPatternHeightTicks = 12;

            //=================================================
            // VOLUME
            //=================================================

            VolumeAveragePeriod = 20;

            HighVolumeMultiplier = 1.50;

            RequireSecondPivotHighVolume = true;
            RequireBreakoutHighVolume = true;

            ShowVolumeValidationLabels = false;

            //=================================================
            // ENTRY
            //=================================================

            // 0 = Auto
            // 1 = Neckline
            // 2 = FVG
            // 3 = Order Block

            EntryMode = 0;

            FVGSearchBars = 15;
            OrderBlockSearchBars = 12;

            //=================================================
            // RISK
            //=================================================

            StopBufferTicks = 4;

            Target1R = 1.0;
            Target2R = 2.0;
            Target3R = 3.0;

            //=================================================
            // STOP RECTANGLE
            //=================================================

            StopRectangleHeightTicks = 6;
            StopRectangleOpacity = 70;

            //=================================================
            // FVG
            //=================================================

            FVGOpacity = 35;

            //=================================================
            // DISPLAY
            //=================================================

            ShowPatternLines = true;
            ShowNeckline = true;

            ShowFVG = true;
            ShowOrderBlock = true;

            ShowEntry = true;

            ShowStopRectangle = true;

            ShowTargets = true;

            ShowBreakoutMarker = true;
        }

        else if (State == State.DataLoaded)
        {
            swingHighs =
                new List<SwingPoint>();

            swingLows =
                new List<SwingPoint>();

            setups =
                new List<PatternSetup>();

            usedTopSecondPivots =
                new HashSet<int>();

            usedBottomSecondPivots =
                new HashSet<int>();

            activeSetup = null;
        }
    }

    //=========================================================
    // MAIN
    //=========================================================

    protected override void OnBarUpdate()
    {
        int requiredBars =
            Math.Max(
                (SwingStrength * 2) + 10,
                VolumeAveragePeriod +
                SwingStrength + 5);

        if (CurrentBar < requiredBars)
            return;

        DetectNewSwing();

        CheckPendingBreakouts();

        UpdateActiveSetup();
    }

    //=========================================================
    // SWING DETECTION
    //=========================================================

    private void DetectNewSwing()
    {
        int barsAgo =
            SwingStrength;

        bool isSwingHigh = true;
        bool isSwingLow = true;

        double candidateHigh =
            High[barsAgo];

        double candidateLow =
            Low[barsAgo];

        for (int i = 1;
             i <= SwingStrength;
             i++)
        {
            if (High[barsAgo - i] >=
                candidateHigh)
            {
                isSwingHigh = false;
            }

            if (High[barsAgo + i] >
                candidateHigh)
            {
                isSwingHigh = false;
            }

            if (Low[barsAgo - i] <=
                candidateLow)
            {
                isSwingLow = false;
            }

            if (Low[barsAgo + i] <
                candidateLow)
            {
                isSwingLow = false;
            }
        }

        int absoluteBar =
            CurrentBar -
            barsAgo;

        if (isSwingHigh)
        {
            AddSwingHigh(
                absoluteBar,
                candidateHigh);

            TryCreateDoubleTop();
        }

        if (isSwingLow)
        {
            AddSwingLow(
                absoluteBar,
                candidateLow);

            TryCreateDoubleBottom();
        }
    }

    private void AddSwingHigh(
        int bar,
        double price)
    {
        if (swingHighs.Count > 0)
        {
            if (swingHighs[
                swingHighs.Count - 1].Bar == bar)
            {
                return;
            }
        }

        swingHighs.Add(
            new SwingPoint(
                bar,
                price));

        if (swingHighs.Count > 300)
            swingHighs.RemoveAt(0);
    }

    private void AddSwingLow(
        int bar,
        double price)
    {
        if (swingLows.Count > 0)
        {
            if (swingLows[
                swingLows.Count - 1].Bar == bar)
            {
                return;
            }
        }

        swingLows.Add(
            new SwingPoint(
                bar,
                price));

        if (swingLows.Count > 300)
            swingLows.RemoveAt(0);
    }

    //=========================================================
    // DOUBLE TOP
    //=========================================================

    private void TryCreateDoubleTop()
    {
        if (swingHighs.Count < 2)
            return;

        SwingPoint secondTop =
            swingHighs[
                swingHighs.Count - 1];

        if (usedTopSecondPivots.Contains(
            secondTop.Bar))
        {
            return;
        }

        for (int i =
                swingHighs.Count - 2;
             i >= 0;
             i--)
        {
            SwingPoint firstTop =
                swingHighs[i];

            int separation =
                secondTop.Bar -
                firstTop.Bar;

            if (separation <
                MinimumPatternBars)
            {
                continue;
            }

            if (separation >
                MaximumPatternBars)
            {
                break;
            }

            double differenceTicks =
                Math.Abs(
                    secondTop.Price -
                    firstTop.Price)
                / TickSize;

            if (differenceTicks >
                TopBottomToleranceTicks)
            {
                continue;
            }

            SwingPoint neckline =
                FindLowestSwingBetween(
                    firstTop.Bar,
                    secondTop.Bar);

            if (neckline == null)
                continue;

            double lowerTop =
                Math.Min(
                    firstTop.Price,
                    secondTop.Price);

            double heightTicks =
                (lowerTop -
                 neckline.Price)
                / TickSize;

            if (heightTicks <
                MinimumPatternHeightTicks)
            {
                continue;
            }

            CreateDoubleTop(
                firstTop,
                neckline,
                secondTop);

            usedTopSecondPivots.Add(
                secondTop.Bar);

            break;
        }
    }

    private void CreateDoubleTop(
        SwingPoint firstTop,
        SwingPoint neckline,
        SwingPoint secondTop)
    {
        double pivotRatio =
            GetVolumeRatio(
                secondTop.Bar);

        if (RequireSecondPivotHighVolume &&
            pivotRatio <
            HighVolumeMultiplier)
        {
            return;
        }

        PatternSetup setup =
            new PatternSetup();

        setup.IsLong = false;

        setup.FirstPivotBar =
            firstTop.Bar;

        setup.NecklineBar =
            neckline.Bar;

        setup.SecondPivotBar =
            secondTop.Bar;

        setup.FirstPivot =
            firstTop.Price;

        setup.Neckline =
            neckline.Price;

        setup.SecondPivot =
            secondTop.Price;

        setup.SecondPivotVolumeRatio =
            pivotRatio;

        setup.EndBar = -1;

        setup.Activated = false;
        setup.Invalidated = false;

        setup.Tag =
            "WT_M_" +
            secondTop.Bar;

        FindEntryZone(setup);

        CalculateTradeLevels(setup);

        setups.Add(setup);

        CheckExistingBreakout(setup);
    }

    //=========================================================
    // DOUBLE BOTTOM
    //=========================================================

    private void TryCreateDoubleBottom()
    {
        if (swingLows.Count < 2)
            return;

        SwingPoint secondBottom =
            swingLows[
                swingLows.Count - 1];

        if (usedBottomSecondPivots.Contains(
            secondBottom.Bar))
        {
            return;
        }

        for (int i =
                swingLows.Count - 2;
             i >= 0;
             i--)
        {
            SwingPoint firstBottom =
                swingLows[i];

            int separation =
                secondBottom.Bar -
                firstBottom.Bar;

            if (separation <
                MinimumPatternBars)
            {
                continue;
            }

            if (separation >
                MaximumPatternBars)
            {
                break;
            }

            double differenceTicks =
                Math.Abs(
                    secondBottom.Price -
                    firstBottom.Price)
                / TickSize;

            if (differenceTicks >
                TopBottomToleranceTicks)
            {
                continue;
            }

            SwingPoint neckline =
                FindHighestSwingBetween(
                    firstBottom.Bar,
                    secondBottom.Bar);

            if (neckline == null)
                continue;

            double upperBottom =
                Math.Max(
                    firstBottom.Price,
                    secondBottom.Price);

            double heightTicks =
                (neckline.Price -
                 upperBottom)
                / TickSize;

            if (heightTicks <
                MinimumPatternHeightTicks)
            {
                continue;
            }

            CreateDoubleBottom(
                firstBottom,
                neckline,
                secondBottom);

            usedBottomSecondPivots.Add(
                secondBottom.Bar);

            break;
        }
    }

    private void CreateDoubleBottom(
        SwingPoint firstBottom,
        SwingPoint neckline,
        SwingPoint secondBottom)
    {
        double pivotRatio =
            GetVolumeRatio(
                secondBottom.Bar);

        if (RequireSecondPivotHighVolume &&
            pivotRatio <
            HighVolumeMultiplier)
        {
            return;
        }

        PatternSetup setup =
            new PatternSetup();

        setup.IsLong = true;

        setup.FirstPivotBar =
            firstBottom.Bar;

        setup.NecklineBar =
            neckline.Bar;

        setup.SecondPivotBar =
            secondBottom.Bar;

        setup.FirstPivot =
            firstBottom.Price;

        setup.Neckline =
            neckline.Price;

        setup.SecondPivot =
            secondBottom.Price;

        setup.SecondPivotVolumeRatio =
            pivotRatio;

        setup.EndBar = -1;

        setup.Activated = false;
        setup.Invalidated = false;

        setup.Tag =
            "WT_W_" +
            secondBottom.Bar;

        FindEntryZone(setup);

        CalculateTradeLevels(setup);

        setups.Add(setup);

        CheckExistingBreakout(setup);
    }

    //=========================================================
    // NECKLINE SEARCH
    //=========================================================

    private SwingPoint FindLowestSwingBetween(
        int startBar,
        int endBar)
    {
        SwingPoint result = null;

        foreach (SwingPoint point
            in swingLows)
        {
            if (point.Bar <= startBar)
                continue;

            if (point.Bar >= endBar)
                continue;

            if (result == null ||
                point.Price <
                result.Price)
            {
                result = point;
            }
        }

        return result;
    }

    private SwingPoint FindHighestSwingBetween(
        int startBar,
        int endBar)
    {
        SwingPoint result = null;

        foreach (SwingPoint point
            in swingHighs)
        {
            if (point.Bar <= startBar)
                continue;

            if (point.Bar >= endBar)
                continue;

            if (result == null ||
                point.Price >
                result.Price)
            {
                result = point;
            }
        }

        return result;
    }

    //=========================================================
    // VOLUME VALIDATION
    //=========================================================

    private double GetVolumeRatio(
        int absoluteBar)
    {
        if (!ValidAbsoluteBar(
            absoluteBar))
        {
            return 0;
        }

        if (absoluteBar <
            VolumeAveragePeriod)
        {
            return 0;
        }

        double totalVolume = 0;
        int count = 0;

        for (int bar =
                absoluteBar -
                VolumeAveragePeriod;
             bar <
                absoluteBar;
             bar++)
        {
            if (!ValidAbsoluteBar(bar))
                continue;

            totalVolume +=
                GetVolume(bar);

            count++;
        }

        if (count <= 0)
            return 0;

        double averageVolume =
            totalVolume /
            count;

        if (averageVolume <= 0)
            return 0;

        double currentVolume =
            GetVolume(
                absoluteBar);

        return
            currentVolume /
            averageVolume;
    }

    //=========================================================
    // EXISTING BREAKOUT
    //=========================================================

    private void CheckExistingBreakout(
        PatternSetup setup)
    {
        if (setup == null)
            return;

        if (setup.Activated ||
            setup.Invalidated)
        {
            return;
        }

        int firstBar =
            setup.SecondPivotBar + 1;

        for (int bar =
                firstBar;
             bar <= CurrentBar;
             bar++)
        {
            if (!ValidAbsoluteBar(bar))
                continue;

            double close =
                GetClose(bar);

            bool breakout;

            if (setup.IsLong)
            {
                breakout =
                    close >
                    setup.Neckline;
            }
            else
            {
                breakout =
                    close <
                    setup.Neckline;
            }

            if (!breakout)
                continue;

            ValidateBreakoutVolume(
                setup,
                bar);

            return;
        }
    }

    //=========================================================
    // LIVE BREAKOUT
    //=========================================================

    private void CheckPendingBreakouts()
    {
        for (int i = 0;
             i < setups.Count;
             i++)
        {
            PatternSetup setup =
                setups[i];

            if (setup.Activated)
                continue;

            if (setup.Invalidated)
                continue;

            if (CurrentBar <=
                setup.SecondPivotBar)
            {
                continue;
            }

            bool breakout = false;

            if (setup.IsLong)
            {
                if (Close[0] >
                    setup.Neckline)
                {
                    breakout = true;
                }
            }
            else
            {
                if (Close[0] <
                    setup.Neckline)
                {
                    breakout = true;
                }
            }

            if (!breakout)
                continue;

            ValidateBreakoutVolume(
                setup,
                CurrentBar);
        }
    }

    //=========================================================
    // BREAKOUT VOLUME
    //=========================================================

    private void ValidateBreakoutVolume(
        PatternSetup setup,
        int breakoutBar)
    {
        double breakoutRatio =
            GetVolumeRatio(
                breakoutBar);

        setup.BreakoutVolumeRatio =
            breakoutRatio;

        if (RequireBreakoutHighVolume &&
            breakoutRatio <
            HighVolumeMultiplier)
        {
            setup.Invalidated = true;

            return;
        }

        ActivateSetup(
            setup,
            breakoutBar);
    }

    //=========================================================
    // ACTIVATE SETUP
    //=========================================================

    private void ActivateSetup(
        PatternSetup setup,
        int breakoutBar)
    {
        if (setup.Activated)
            return;

        setup.Activated = true;

        setup.BreakoutBar =
            breakoutBar;

        FinalizePreviousSetup(
            setup.FirstPivotBar);

        activeSetup =
            setup;

        if (setup.IsLong)
        {
            DrawDoubleBottom(
                setup);
        }
        else
        {
            DrawDoubleTop(
                setup);
        }

        DrawSetupLevels(
            setup,
            CurrentBar);

        MarkBreakout(
            setup,
            breakoutBar);

        if (ShowVolumeValidationLabels)
        {
            DrawVolumeLabels(
                setup);
        }
    }

    //=========================================================
    // FINALIZE PREVIOUS SETUP
    //=========================================================

    private void FinalizePreviousSetup(
        int newSetupStartBar)
    {
        if (activeSetup == null)
            return;

        if (!activeSetup.Activated)
            return;

        if (activeSetup.EndBar >= 0)
            return;

        int endBar =
            newSetupStartBar;

        if (endBar <
            activeSetup.SecondPivotBar)
        {
            endBar =
                activeSetup.SecondPivotBar;
        }

        if (endBar >
            CurrentBar)
        {
            endBar =
                CurrentBar;
        }

        activeSetup.EndBar =
            endBar;

        DrawSetupLevels(
            activeSetup,
            endBar);

        activeSetup = null;
    }

    //=========================================================
    // UPDATE ACTIVE SETUP
    //=========================================================

    private void UpdateActiveSetup()
    {
        if (activeSetup == null)
            return;

        if (!activeSetup.Activated)
            return;

        if (activeSetup.EndBar >= 0)
            return;

        DrawSetupLevels(
            activeSetup,
            CurrentBar);
    }

    //=========================================================
    // ENTRY ZONE
    //=========================================================

    private void FindEntryZone(
        PatternSetup setup)
    {
        setup.HasZone = false;

        //=====================================================
        // NECKLINE ONLY
        //=====================================================

        if (EntryMode == 1)
        {
            setup.Entry =
                RoundPrice(
                    setup.Neckline);

            return;
        }

        double zoneTop;
        double zoneBottom;

        //=====================================================
        // FVG
        //=====================================================

        if (EntryMode == 0 ||
            EntryMode == 2)
        {
            if (FindFVG(
                setup.IsLong,
                setup.SecondPivotBar,
                out zoneTop,
                out zoneBottom))
            {
                setup.HasZone = true;

                setup.IsFVG = true;

                setup.ZoneTop =
                    zoneTop;

                setup.ZoneBottom =
                    zoneBottom;

                setup.Entry =
                    RoundPrice(
                        (zoneTop +
                         zoneBottom)
                        / 2.0);

                return;
            }
        }

        //=====================================================
        // ORDER BLOCK
        //=====================================================

        if (EntryMode == 0 ||
            EntryMode == 3)
        {
            if (FindOrderBlock(
                setup.IsLong,
                setup.SecondPivotBar,
                out zoneTop,
                out zoneBottom))
            {
                setup.HasZone = true;

                setup.IsFVG = false;

                setup.ZoneTop =
                    zoneTop;

                setup.ZoneBottom =
                    zoneBottom;

                setup.Entry =
                    RoundPrice(
                        (zoneTop +
                         zoneBottom)
                        / 2.0);

                return;
            }
        }

        setup.Entry =
            RoundPrice(
                setup.Neckline);
    }

    //=========================================================
    // FVG DETECTION
    //=========================================================

    private bool FindFVG(
        bool bullishSetup,
        int pivotBar,
        out double zoneTop,
        out double zoneBottom)
    {
        zoneTop = 0;
        zoneBottom = 0;

        int minimumBar =
            Math.Max(
                2,
                pivotBar -
                FVGSearchBars);

        for (int thirdBar =
                pivotBar;
             thirdBar >= minimumBar;
             thirdBar--)
        {
            int firstBar =
                thirdBar - 2;

            if (!ValidAbsoluteBar(
                firstBar))
            {
                continue;
            }

            if (!ValidAbsoluteBar(
                thirdBar))
            {
                continue;
            }

            double firstHigh =
                GetHigh(
                    firstBar);

            double firstLow =
                GetLow(
                    firstBar);

            double thirdHigh =
                GetHigh(
                    thirdBar);

            double thirdLow =
                GetLow(
                    thirdBar);

            //=================================================
            // BULLISH / LONG FVG
            //=================================================

            if (bullishSetup)
            {
                if (thirdLow >
                    firstHigh)
                {
                    zoneTop =
                        thirdLow;

                    zoneBottom =
                        firstHigh;

                    return true;
                }
            }

            //=================================================
            // BEARISH / SHORT FVG
            //=================================================

            else
            {
                if (thirdHigh <
                    firstLow)
                {
                    zoneTop =
                        firstLow;

                    zoneBottom =
                        thirdHigh;

                    return true;
                }
            }
        }

        return false;
    }

    //=========================================================
    // ORDER BLOCK
    //=========================================================

    private bool FindOrderBlock(
        bool bullishSetup,
        int pivotBar,
        out double zoneTop,
        out double zoneBottom)
    {
        zoneTop = 0;
        zoneBottom = 0;

        int minimumBar =
            Math.Max(
                0,
                pivotBar -
                OrderBlockSearchBars);

        for (int bar =
                pivotBar;
             bar >= minimumBar;
             bar--)
        {
            if (!ValidAbsoluteBar(bar))
                continue;

            double open =
                GetOpen(bar);

            double close =
                GetClose(bar);

            double high =
                GetHigh(bar);

            double low =
                GetLow(bar);

            //=================================================
            // BULLISH ORDER BLOCK
            //=================================================

            if (bullishSetup &&
                close < open)
            {
                zoneTop = high;
                zoneBottom = low;

                return true;
            }
            //=================================================
            // BEARISH ORDER BLOCK
            //=================================================

            if (!bullishSetup &&
                close > open)
            {
                zoneTop = high;
                zoneBottom = low;

                return true;
            }
        }

        return false;
    }

    //=========================================================
    // TRADE LEVELS
    //=========================================================

    private void CalculateTradeLevels(
        PatternSetup setup)
    {
        //=====================================================
        // LONG / W
        //=====================================================

        if (setup.IsLong)
        {
            double lowestBottom =
                Math.Min(
                    setup.FirstPivot,
                    setup.SecondPivot);

            setup.Stop =
                RoundPrice(
                    lowestBottom -
                    (StopBufferTicks *
                     TickSize));

            double risk =
                setup.Entry -
                setup.Stop;

            if (risk < TickSize)
                risk = TickSize;

            setup.TP1 =
                RoundPrice(
                    setup.Entry +
                    (risk *
                     Target1R));

            setup.TP2 =
                RoundPrice(
                    setup.Entry +
                    (risk *
                     Target2R));

            setup.TP3 =
                RoundPrice(
                    setup.Entry +
                    (risk *
                     Target3R));
        }

        //=====================================================
        // SHORT / M
        //=====================================================

        else
        {
            double highestTop =
                Math.Max(
                    setup.FirstPivot,
                    setup.SecondPivot);

            setup.Stop =
                RoundPrice(
                    highestTop +
                    (StopBufferTicks *
                     TickSize));

            double risk =
                setup.Stop -
                setup.Entry;

            if (risk < TickSize)
                risk = TickSize;

            setup.TP1 =
                RoundPrice(
                    setup.Entry -
                    (risk *
                     Target1R));

            setup.TP2 =
                RoundPrice(
                    setup.Entry -
                    (risk *
                     Target2R));

            setup.TP3 =
                RoundPrice(
                    setup.Entry -
                    (risk *
                     Target3R));
        }
    }

    //=========================================================
    // DRAW M
    //=========================================================

    private void DrawDoubleTop(
        PatternSetup setup)
    {
        if (!ShowPatternLines)
            return;

        int firstAgo =
            CurrentBar -
            setup.FirstPivotBar;

        int neckAgo =
            CurrentBar -
            setup.NecklineBar;

        int secondAgo =
            CurrentBar -
            setup.SecondPivotBar;

        Draw.Line(
            this,
            setup.Tag + "_M1",
            firstAgo,
            setup.FirstPivot,
            neckAgo,
            setup.Neckline,
            Brushes.OrangeRed);

        Draw.Line(
            this,
            setup.Tag + "_M2",
            neckAgo,
            setup.Neckline,
            secondAgo,
            setup.SecondPivot,
            Brushes.OrangeRed);

        Draw.Text(
            this,
            setup.Tag + "_M",
            "M",
            secondAgo,
            setup.SecondPivot +
            (8 * TickSize),
            Brushes.OrangeRed);
    }

    //=========================================================
    // DRAW W
    //=========================================================

    private void DrawDoubleBottom(
        PatternSetup setup)
    {
        if (!ShowPatternLines)
            return;

        int firstAgo =
            CurrentBar -
            setup.FirstPivotBar;

        int neckAgo =
            CurrentBar -
            setup.NecklineBar;

        int secondAgo =
            CurrentBar -
            setup.SecondPivotBar;

        Draw.Line(
            this,
            setup.Tag + "_W1",
            firstAgo,
            setup.FirstPivot,
            neckAgo,
            setup.Neckline,
            Brushes.LimeGreen);

        Draw.Line(
            this,
            setup.Tag + "_W2",
            neckAgo,
            setup.Neckline,
            secondAgo,
            setup.SecondPivot,
            Brushes.LimeGreen);

        Draw.Text(
            this,
            setup.Tag + "_W",
            "W",
            secondAgo,
            setup.SecondPivot -
            (8 * TickSize),
            Brushes.LimeGreen);
    }

    //=========================================================
    // DRAW SETUP
    //=========================================================

    private void DrawSetupLevels(
        PatternSetup setup,
        int endAbsoluteBar)
    {
        if (!setup.Activated)
            return;

        if (endAbsoluteBar <
            setup.SecondPivotBar)
        {
            endAbsoluteBar =
                setup.SecondPivotBar;
        }

        if (endAbsoluteBar >
            CurrentBar)
        {
            endAbsoluteBar =
                CurrentBar;
        }

        int startAgo =
            CurrentBar -
            setup.SecondPivotBar;

        int endAgo =
            CurrentBar -
            endAbsoluteBar;

        //=====================================================
        // NECKLINE
        //=====================================================

        if (ShowNeckline)
        {
            Draw.Line(
                this,
                setup.Tag + "_NECK",
                startAgo,
                setup.Neckline,
                endAgo,
                setup.Neckline,
                Brushes.Gold);

            Draw.Text(
                this,
                setup.Tag + "_NECK_TEXT",
                "NECK",
                endAgo,
                setup.Neckline,
                Brushes.Gold);
        }

        //=====================================================
        // FVG / ORDER BLOCK
        //=====================================================

        if (setup.HasZone)
        {
            bool showZone =
                (setup.IsFVG &&
                 ShowFVG)
                ||
                (!setup.IsFVG &&
                 ShowOrderBlock);

            if (showZone)
            {
                Brush zoneBrush;
                int zoneOpacity;

                //=============================================
                // FVG COLORS
                //
                // LONG FVG  = GREEN
                // SHORT FVG = RED
                //=============================================

                if (setup.IsFVG)
                {
                    if (setup.IsLong)
                    {
                        zoneBrush =
                            Brushes.LimeGreen;
                    }
                    else
                    {
                        zoneBrush =
                            Brushes.Red;
                    }

                    zoneOpacity =
                        FVGOpacity;
                }

                //=============================================
                // ORDER BLOCK
                // KEEP EXISTING BLUE COLOR
                //=============================================

                else
                {
                    zoneBrush =
                        Brushes.DodgerBlue;

                    zoneOpacity =
                        15;
                }

                Draw.Rectangle(
                    this,
                    setup.Tag + "_ZONE",
                    false,
                    startAgo,
                    setup.ZoneTop,
                    endAgo,
                    setup.ZoneBottom,
                    zoneBrush,
                    zoneBrush,
                    zoneOpacity);

                Draw.Text(
                    this,
                    setup.Tag + "_ZONE_TEXT",
                    setup.IsFVG
                        ? (setup.IsLong
                            ? "LONG FVG"
                            : "SHORT FVG")
                        : "OB",
                    endAgo,
                    setup.ZoneTop,
                    zoneBrush);
            }
        }

        //=====================================================
        // ENTRY
        //=====================================================

        if (ShowEntry)
        {
            Draw.Line(
                this,
                setup.Tag + "_ENTRY",
                startAgo,
                setup.Entry,
                endAgo,
                setup.Entry,
                Brushes.White);

            Draw.Text(
                this,
                setup.Tag + "_ENTRY_TEXT",
                setup.IsLong
                    ? "LONG"
                    : "SHORT",
                endAgo,
                setup.Entry,
                Brushes.White);
        }

        //=====================================================
        // STOP RECTANGLE
        //=====================================================

        if (ShowStopRectangle)
        {
            DrawStopRectangle(
                setup,
                startAgo,
                endAgo);
        }

        //=====================================================
        // TARGETS
        //=====================================================

        if (ShowTargets)
        {
            DrawTarget(
                setup,
                startAgo,
                endAgo,
                setup.TP1,
                "1");

            DrawTarget(
                setup,
                startAgo,
                endAgo,
                setup.TP2,
                "2");

            DrawTarget(
                setup,
                startAgo,
                endAgo,
                setup.TP3,
                "3");
        }
    }

    //=========================================================
    // STOP RECTANGLE
    //
    // LONG TEXT = GREEN
    // SHORT TEXT = RED
    //=========================================================

    private void DrawStopRectangle(
        PatternSetup setup,
        int startAgo,
        int endAgo)
    {
        string stopText =
            setup.IsLong
            ? "SHORT"
            : "LONG";

        Brush stopBrush;

        if (stopText == "LONG")
        {
            stopBrush =
                Brushes.LimeGreen;
        }
        else
        {
            stopBrush =
                Brushes.Red;
        }

        double halfHeight =
            (StopRectangleHeightTicks *
             TickSize) / 2.0;

        if (halfHeight <
            TickSize)
        {
            halfHeight =
                TickSize;
        }

        double boxTop =
            setup.Stop +
            halfHeight;

        double boxBottom =
            setup.Stop -
            halfHeight;

        Draw.Rectangle(
            this,
            setup.Tag +
            "_STOP_RECTANGLE",
            false,
            startAgo,
            boxTop,
            endAgo,
            boxBottom,
            stopBrush,
            stopBrush,
            StopRectangleOpacity);

        int textAgo =
            endAgo;

        if (startAgo >
            endAgo)
        {
            textAgo =
                endAgo +
                ((startAgo -
                  endAgo) / 2);
        }

        Draw.Text(
            this,
            setup.Tag +
            "_STOP_RECTANGLE_TEXT",
            stopText,
            textAgo,
            setup.Stop,
            Brushes.White);
    }