Step Two: Coding in TradingView

I set out to develop a TradingView script that could automatically detect double top and double bottom patterns while incorporating filters to refine the signals. Over the course of about four hours, I iteratively developed and debugged the script—tweaking parameters, adjusting logic, and using TradingView’s visual feedback to better understand how each component behaved.

Working with Copilot to draft and refine the script proved to be a practical approach. The ability to see the pivots, neckline formations, and filter effects directly on the charts helped me quickly identify and correct any issues. This process, from the initial idea through iterative improvements to the final version, highlights a measured and systematic approach to developing a trading tool that is both visually intuitive and logically robust. The results are shown in Figure 1.

Figure 1. A double bottom is formed, the neck line is broken, and a long trade is opened. The trade is closed when the candle's low drops below the Donchian low. The volume filter was disabled.

Explanation of the logic

Below is a detailed description of how the logic works. Only the Long side is given as the short side is exactly the opposite.

Overall Workflow

  1. Detection: The strategy monitors the HMA for three-bar pivot formations. Once a first pivot is identified, it waits for a second matching pivot within a specified range of bars and with a similar HMA value (filtered using the ATR-based tolerance).

  2. Neckline Setting: When a pair of matching pivots is confirmed, the neckline is calculated as either the highest high (for a double bottom) or the lowest low (for a double top) between the two pivot points.

  3. Trade Entry: A trade is triggered if the price breaks through the neckline (plus an ATR buffer) during a valid time window—no earlier than the minimum waiting period and no later than the expiry period. Additionally, the volume filter (if enabled) must confirm that market participation is sufficient, and trade toggles decide if a long or short position is allowed.

  4. Trade Exit: Open positions are monitored using a lagged Donchian channel, and exits occur when price action breaches these dynamic support or resistance levels.

Pattern Detection Using the Hull Moving Average

Pivot Low (for a potential double bottom): A pivot low is identified when the middle bar (indexed as hma[1]) is lower than its neighbors:

isPivotLow if hma[0] > hma[1] and hma[1] < hma[2]

Double Bottom (Bullish) Pattern:

  • First Pivot: When the first pivot low is found, its value and bar index (let’s denote these as PL1 and BL1) are stored.

  • Second Pivot: A later pivot low (with value PL2 appearing at bar BL2) is considered only if the number of bars between these pivots meets the following condition:

minBarsBetween ≤ (BL2−BL1) ≤ pivotCompareLookback

ATR-based Tolerance Check: To ensure the similarity of the two pivots, the difference between their HMA values must be less than or equal to:

∣PL2−PL1∣ ≤ (atrTolerance × ATR)

Neckline setting

Determining the Neckline for Long (Bullish) Entry: Between the first and second pivot, the strategy tracks the highest high. This running maximum (denoted as Hmax) is determined for all bars between BL1 and BL2. The neckline is then set as:

necklineLong=Hmax

Trade Entry Conditions

A. Breakout Entry

For a long trade (double bottom), the entry is made if the following condition is met:

close > necklineLong + (necklineBuffer × ATR)

where the necklineBuffer is a multiplication factor to scale the ATR. These conditions ensure that the price has not only moved past the raw neckline but has done so with a cushion proportional to market volatility.

B. Time Window for Valid Entries

To ensure that trades occur in a timely manner, the strategy only accepts entries if they occur within a dedicated window after the neckline is established. Two parameters control this:

  • Minimum Waiting Period (minWaitBars): The strategy does not allow any trade entry before:

bar index ≥ (necklineBar + minWaitBars)

This delay prevents premature entries that might still be part of the pattern’s formation.

  • Neckline Expiry Period: Once the neckline is set, the opportunity is considered valid only until:

bar index ≤ (necklineBar + expiryBars)

If the price fails to break out within this period, the pattern is regarded as expired, and no trade is initiated on that signal.

C. Volume Filter

To avoid taking trades on weak or unsupported moves, a volume filter condition is optionally applied. This filter works as follows:

  • A moving average of volume is calculated over a period defined by volumeLength\text{volumeLength}:

volSMA = SMA(volume , volumeLength)

  • The current bar’s volume must exceed this moving average scaled by a multiplier volMultiplier\text{volMultiplier}:

volume > volMultiplier × volSMA

This requirement confirms that any breakout signal is supported by above-average trading volume, thus increasing the reliability of the signal.

Trade Exit Conditions

Once a trade is entered, the strategy uses a lagging Donchian channel to determine exit points. The Donchian channel is calculated using the previous bar’s data over a specified lookback period (donchianLength). The exit is triggered if the price’s low falls below the Donchian low:

donchianLow=min⁡{low[1],low[2],…,low[donchianLength]}

if

low < donchianLow

then the long trade is closed.

Does it work? Some initial results (show USDJPY and TSLA)

Next up: transferring it to Metatrader 5.

Previous
Previous

Step One: The Idea

Next
Next

Step Three: Developing the Hull Moving Average Indicator