Bar-to-Bar Divergence Strategy: A New Approach

 

by: Dexter M. Garcia (code: %H&S Divergence)

An Overview of Divergence 

Divergence serves as a vital tool in technical analysis, aiding traders in identifying and responding to shifts in price action. It occurs when a technical indicator or oscillator moves counter to the price trend or overall price movement. Divergence patterns often indicate potential price reversals, whether negative or positive. Negative divergence manifests when an asset's price climbs while the indicator descends or exhibits bearish signals. Conversely, positive divergence arises when the asset's price falls, yet the indicator ascends or shows bullish signals. 

Conventionally, traders confirm divergence by connecting highs or lows on the price chart and corresponding points on the indicator, ensuring alignment. True divergence manifests when these connected lines exhibit clearly opposing slopes. Divergence signals hold greater significance in higher time frames but may prove less reliable in lower time frames. Moreover, once a divergence is identified and the price has already shifted significantly, the signal's validity may diminish. 

Despite its utility, implementing a divergence strategy can pose challenges. Divergence signals may occasionally yield false alarms and may not manifest in all instances of price reversals. Additionally, divergence patterns can persist for extended periods before a price reversal occurs. Charting and interpreting slope directions can also prove challenging, with each trader applying subjective methods prone to errors, potentially leading to inaccurate trade decisions.

 Bar-to-Bar Divergence: A New Approach 

The Bar-to-Bar Divergence Strategy introduces an innovative approach to detecting and recognizing divergences, aiming to address challenges commonly encountered in traditional divergence trading methods. By striving to minimize subjectivity and charting errors, this strategy offers a more objective means of analysis. While firmly grounded in divergence theory, it diverges from conventional techniques by connecting the closing prices of candles with their corresponding RSI values to identify divergence patterns.

 To identify a bearish divergence, begin by locating the "green anchor," which denotes a green candle with the highest RSI value over a specified period. Next, draw a line from the closing price of the green anchor to the closing price of the subsequent green candle, known as the "green reference candle." Ensure that the RSI slope aligns with the green candles under examination. Both the anchor and reference green candles must be succeeded by a red candle to confirm the bearish divergence.

Figure 1. Bar-to-Bar Bearish Divergence


Conversely, to identify a bullish divergence, pinpoint the "red anchor," representing a red candle with the lowest RSI value over the designated period. Connect the closing price of the red anchor to the closing price of the subsequent red reference candle, while ensuring alignment with the RSI slope of the red candles in question. Like bearish divergence, both the anchor and reference red candles must be followed by a green candle to validate the bullish divergence pattern. 

Figure 2. Bar-to-Bar Bullish Divergence


It is important to use other indicators to confirm the signal such as support and resistance levels, pivot points or price action patterns.

 A Pine Script for Bar-to-Bar Divergence 

This Pine script is designed to pinpoint bar-to-bar divergence and candle price action within specified start and end dates, where the start date denotes the anchor candle, and the end date represents the reference candle. 

The script commences by establishing an input parameter for the RSI calculation length, with a default setting of period 3. Users can adjust the start and end dates to their preferred candle date and time, with date markers appearing on the chart for manual adjustments. 

By analyzing the price action and RSI values of candles on the selected dates, the script determines whether a short or long entry should be made. This evaluation is based on a comparison of the closing prices and RSI values between the start and end candles, with the objective of identifying market sentiment divergences.

 

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

// © DexterMGarcia 

//@version=5

strategy("Microdivergence ver4", overlay=true) 

// Input parameters

rsi_length = input.int(3, "RSI Length", minval=1) 

// Calculate RSI

rsi = ta.rsi(close, rsi_length)

 

// Define the specific start and end dates for the markers

startDate = input.time(defval=timestamp("31 Jan 2022 02:20 +0000"), title="Start Date")

endDate = input.time(defval=timestamp("31 Jan 2022 02:20 +0000"), title="End Date")

 

// Find the index of the candle corresponding to the start date

startIndex = ta.barssince(time == startDate)

 

// Find the index of the candle corresponding to the end date

endIndex = ta.barssince(time == endDate)

 

// Plot a dot marker at the specific start date with yellow color

plotshape(startIndex >= 0, style=shape.circle, location=location.abovebar, color=color.yellow, size=size.small, title="Start Marker")

 

// Plot a dot marker at the specific end date with red color

plotshape(endIndex >= 0, style=shape.circle, location=location.abovebar, color=color.red, size=size.small, title="End Marker")

 

// Check conditions for green candles

if (startIndex >= 0 and endIndex >= 0)

    // Check if both candles at start and end dates are bullish

    if (close[startIndex] > open[startIndex] and close[endIndex] > open[endIndex])

        // Condition 1: if closing price of candle at start date is higher than closing price of candle at end date but RSI value of start date is lower than RSI value of end date, then generate a short signal

        if (close[startIndex] > close[endIndex] and rsi[startIndex] < rsi[endIndex])

            strategy.entry("Short", strategy.short)

        // Condition 2: if closing price of candle at start date is lower than closing price of candle at end date but RSI value of start date is higher than RSI value of end date, then generate a short signal

        else if (close[startIndex] < close[endIndex] and rsi[startIndex] > rsi[endIndex])

            strategy.entry("Short", strategy.short)

        // Condition 3: if closing price and RSI value of candle at end date are both lower than closing price and RSI value of candle at start date, then generate a short signal

        else if (close[endIndex] < close[startIndex] and rsi[endIndex] < rsi[startIndex])

            strategy.entry("Short", strategy.short)

        // Condition 4: if closing price and RSI value of candle at end date are both higher than closing price and RSI value of candle at start date, then generate a long signal

        else if (close[endIndex] > close[startIndex] and rsi[endIndex] > rsi[startIndex])

            strategy.entry("Long", strategy.long)

    // Check if both candles at start and end dates are bearish

    else if (close[startIndex] < open[startIndex] and close[endIndex] < open[endIndex])

        // Condition 1: if closing price of red candle at start date is higher than closing price of red candle at end date but RSI value of start date is lower than end date then generate a long signal

        if (close[startIndex] > close[endIndex] and rsi[startIndex] < rsi[endIndex])

            strategy.entry("Long", strategy.long)

        // Condition 2: if closing price of red candle at start date is lower than closing price of red candle at end date but RSI value at start date is higher than RSI at end date then generate a long signal

        else if (close[startIndex] < close[endIndex] and rsi[startIndex] > rsi[endIndex])

            strategy.entry("Long", strategy.long)

        // Condition 3: if both closing price of red candle and RSI value at start date are lower than closing price of red candle and RSI value at end date then generate long signal

        else if (close[startIndex] < close[endIndex] and rsi[startIndex] < rsi[endIndex])

            strategy.entry("Long", strategy.long)

        // Condition 4: if both closing price of red candle and RSI value at start date are higher than closing price of red candle and RSI value at end date then generate short signal

        else if (close[startIndex] > close[endIndex] and rsi[startIndex] > rsi[endIndex])

            strategy.entry("Short", strategy.short)

 

 References:

What is Divergence in Trading? | Definition & Examples | Finbold

Trading Strategy: How to trade divergence with technical indicators (flowbank.com)

 

 

 

 

 

 


Comments

Popular posts from this blog

RSI-Based Dynamic Grid