The N Reversal Detector is a technical indicator developed to detect market trend reversal points. This indicator combines multiple approaches to identify both trends and potential market reversals, integrating the use of moving averages, the Supertrend indicator, and Donchian Channels. Its primary goal is to alert traders of potential changes in market direction, both in bullish and bearish trends.
Unlike other indicators that focus on a single method to identify market direction, the N Reversal Detector uses a multi-filter system. Depending on the selected configuration, the indicator can identify aligned or opposing trends, providing clear signals for traders looking for entries based on trend reversals.
In addition to filtering trends, the indicator also implements a reversal pattern detection system for both bullish and bearish reversals, making it highly versatile for use in multiple trading styles, whether on daily, intraday, or long-term charts.
The N Reversal Detector consists of several key elements that work together to identify trends and potential reversal points. Below are the main components of the indicator:
The indicator offers three types of trend filters that can be selected through the trendType variable. These filters allow you to adjust how the indicator detects trends and how it interprets reversal signals:
maFlength (default 50): Length of the fast moving average.maSLength (default 200): Length of the slow moving average.maType (default 7): Type of moving average (e.g., simple, exponential, etc.).atrPeriod (default 10): Period for the ATR.factor (default 3): Multiplier for the ATR to determine trend bands.length (default 13): Period for the Donchian Channels.The indicator includes a reversal pattern detection module based on the last three bars of the chart. This module identifies conditions where the price has reached an extreme (high or low) and has started to reverse:
numBars (default 7): Number of bars used to detect the pattern.minBars (default 50): Minimum percentage of bearish bars required to validate the reversal.numBars and minBars are also used for bearish reversals.The TrendFilter parameter is used to further refine the signals generated by the indicator. This filter allows the user to choose whether reversal signals should align with the overall trend or if they can be contrary:
True): Reversal signals are only generated when they align with the prevailing trend (bullish for bullish signals and bearish for bearish signals).False): Reversal signals are allowed even if they are contrary to the main trend, which may generate more signals but also increases the risk of false alarms.The N Reversal Detector offers a range of adjustable parameters that allow users to tailor the indicator’s behavior to their preferences and trading strategies. Below are the main configuration variables and how they affect the indicator’s performance.
trendType (default 0): Defines the type of trend filter to use.
TrendFilter (default 1): Controls whether signals should align with the detected trend.
maFlength (default 50): Defines the period for the fast moving average.maSLength (default 200): Defines the period for the slow moving average.maType (default 7): Determines the type of moving average (simple, exponential, etc.).atrPeriod (default 10): Number of periods used to calculate the Average True Range (ATR), which is key for determining the Supertrend bands.factor (default 3): Multiplier applied to the ATR to set the Supertrend bands.length (default 13): Number of bars used to calculate the highs and lows for the Donchian channel.numBars (default 7): Number of bars analyzed to detect a bullish or bearish reversal. A higher number of bars provides more conservative signals.minBars (default 50): Defines the minimum percentage of bars that must be bullish or bearish for the reversal pattern to be considered valid.The N Reversal Detector is highly customizable, meaning traders can adjust the parameters according to market conditions or their risk preferences. Below are some common adjustment examples:
trendType = 1 and adjust maFlength and maSLength according to the timeframe they are analyzing. For intraday charts, it may be more effective to reduce the moving averages periods (e.g., maFlength = 20 and maSLength = 50).atrPeriod = 14 and factor = 2 can help produce tighter bands that react to price fluctuations.trendType = 3) may be more appropriate. Adjusting the length to 20 or more can help capture significant breakout movements.trendType = 0 and TrendFilter = 0. This will generate more signals but may increase the number of false signals.TrendFilter = 1. This will reduce the number of signals generated but increase the likelihood that they are reliable.The N Reversal Detector works by combining different technical analysis techniques to detect both trends and potential reversals. In this section, we will break down the code to explain how buy and sell signals are generated and how the different components of the indicator interact.
When trendType = 1 is selected, the indicator uses two moving averages to identify the trend:
maFast): Calculated with the maFlength parameter, which defines the number of periods.maSlow): Calculated with the maSLength parameter.If the fast moving average (maFast) is above the slow moving average (maSlow), it is considered a bullish trend, and an area between the two moving averages is colored in green (RGB: 8, 153, 129). Otherwise, it is considered a bearish trend, and the area is colored in red (RGB: 242, 54, 69).
if trendType=1 then
if maFast>maSlow then
r=8
g=153
b=129
else
r=242
g=54
b=69
endif
colorbetween(maFast,maSlow,r,g,b,20)
endif
This code draws the area between the two moving averages using colors to indicate whether the trend is bullish or bearish, enhancing trend visualization on the chart.
With trendType = 2, the Supertrend indicator is activated, which uses the ATR (Average True Range) to calculate trend bands:
up): Calculated by subtracting the ATR multiplied by the adjustment factor (factor) from the bar’s high.dn): Calculated by adding the ATR multiplied by the factor to the bar’s low.atr = averagetruerange[atrPeriod](close)
up = high - factor*atr
dn = low + factor*atr
The Supertrend follows the price action, adjusting dynamically according to volatility. If the current price crosses above the upper band, it is considered a bullish trend, while crossing below the lower band indicates a bearish trend.
if trendType=2 then
if trend = 1 then
mysupertrend = up
r=8
g=153
b=129
else
mysupertrend = dn
r=242
g=54
b=69
endif
bodyMiddle=(open+close)/2
colorbetween(mysupertrend,bodyMiddle,r,g,b,25)
endif
Here, the indicator colors the area between the Supertrend line and the close price to visualize the prevailing trend.
When trendType = 3 is selected, the indicator uses Donchian Channels, which plot the highest and lowest values over a period defined by the length parameter to determine whether there is a bullish or bearish trend.
upper = highest[length](close)
lower = lowest[length](close)
If the price is reaching new highs, the indicator detects a bullish trend; if it is reaching new lows, it detects a bearish trend. Like the other trend filters, the area between the Donchian channel lines is colored using green for bullish trends and red for bearish trends.
if trendType=3 then
once os=0
if upper>upper[1] then
os=1
r=8
g=153
b=129
elsif lower<lower[1] then
os=0
r=242
g=54
b=69
else
os=os
endif
colorbetween(upper,lower,r,g,b,20)
endif
The indicator looks for a bullish reversal pattern based on the last numBars bars. It evaluates whether the price has consistently dropped and has started to reverse. A minimum percentage of bearish bars (defined by minBars) is required to validate the pattern.
The process for detecting a bearish reversal is similar, but this time the indicator looks for a series of bullish bars followed by a reversal to the downside.
When a bullish or bearish reversal is confirmed, the indicator plots the corresponding signals on the chart, along with lines and rectangles highlighting key support and resistance levels.
if bullishReversal and bullishReversal[1]=0 then
drawtext("▲",barindex,low)coloured("green")
drawsegment(lnATx,lnAT,barindex,lnAT)coloured(8,153,129)style(line,1)
drawsegment(lnATx,bullLow,barindex,bullLow)coloured(8,153,129)style(line,1)
drawrectangle(lnATx,bullLow,barindex,lnAT)coloured(8,153,129,0)fillcolor(8,153,129,25)
endif
if bearishReversal and bearishReversal[1]=0 then
drawtext("▼",barindex,high)coloured("red")
drawsegment(lnSBx,lnSB,barindex,lnSB)coloured(242,54,69)
drawsegment(lnSBx,bearHigh,barindex,bearHigh)coloured(242,54,69)
drawrectangle(lnSBx,bearHigh,barindex,lnSB)coloured(242,54,69,0)fillcolor(242,54,69,25)
endif
The N Reversal Detector is a versatile and comprehensive indicator designed to detect both bullish and bearish reversals using a combination of trend filters and price patterns. This indicator allows traders to adapt their analysis to different market conditions, whether the market is trending or range-bound.
Key features of the indicator include:
In summary, the N Reversal Detector is a powerful tool for traders seeking to identify entry and exit points based on potential trend reversals, and who require flexibility to tailor the indicator to various market conditions.
//---------------------------------------------------//
//PRC_N reversal Detector
//version = 0
//04.10.2024
//Iván González @ www.prorealcode.com
//Sharing ProRealTime knowledge
//---------------------------------------------------//
//------------------Inputs---------------------------//
//---------------------------------------------------//
//Trend filetering
trendType=0 //0=no trend / 1=MA trend / 2=ST trend / 3=Donchian trend
TrendFilter=1 // True=Aligned / False=Opposite
//Moving average settings
maType=7
maFlength=50
maSLength=200
//Supertrend settings
atrPeriod=10
factor=3
//Donchian Channels settings
length=13
//Pattern Detection
numBars=7
minBars=50
//---------------------------------------------------//
//-----Trend indicators - Moving average Cloud-------//
//---------------------------------------------------//
maFast=average[maFlength,maType](close)
maSlow=average[maSLength,maType](close)
if trendType=1 then
if maFast>maSlow then
r=8
g=153
b=129
else
r=242
g=54
b=69
endif
colorbetween(maFast,maSlow,r,g,b,20)
endif
//---------------------------------------------------//
//---------Trend indicators - Supertrend-------------//
//---------------------------------------------------//
atr = averagetruerange[atrPeriod](close)
up = high - factor*atr
up1 = up[1]
if close[1] > up1 then
up = max(up,up1)
else
up = up
endif
dn = low + factor*atr
dn1 = dn[1]
if close[1] < dn1 then
dn = min(dn,dn1)
else
dn = dn
endif
once trend = 1
if trend = -1 and close > dn1 then
trend = 1
elsif trend = 1 and close < up1 then
trend = -1
else
trend = trend
endif
if trendType=2 then
if trend = 1 then
mysupertrend = up
r=8
g=153
b=129
else
mysupertrend = dn
r=242
g=54
b=69
endif
bodyMiddle=(open+close)/2
colorbetween(mysupertrend,bodyMiddle,r,g,b,25)
endif
//---------------------------------------------------//
//------Trend indicators - Donchian Channels---------//
//---------------------------------------------------//
upper=highest[length](close)
lower=lowest[length](close)
if trendType=3 then
once os=0
if upper>upper[1] then
os=1
r=8
g=153
b=129
elsif lower<lower[1] then
os=0
r=242
g=54
b=69
else
os=os
endif
colorbetween(upper,lower,r,g,b,20)
endif
//---------------------------------------------------//
//--------------3-Bar Reversal Pattern---------------//
//---------------------------------------------------//
if trendType=1 then
if TrendFilter=1 then
CDownTrend= close<maFast and maFast<maSlow
CUpTrend= close>maFast and maFast>maSlow
elsif TrendFilter=0 then
CDownTrend=close>maFast and maFast>maSlow
CUpTrend=close<maFast and maFast<maSlow
else
CDownTrend=1
CUpTrend=1
endif
elsif trendType=2 then
if TrendFilter=1 then
CDownTrend= trend=-1
CUpTrend= trend=1
elsif TrendFilter=0 then
CDownTrend=trend=1
CUpTrend=trend=-1
else
CDownTrend=1
CUpTrend=1
endif
elsif TrendType=3 then
if TrendFilter=1 then
CDownTrend=os=0
CUpTrend=os=1
elsif TrendFilter=0 then
CDownTrend=os=1
CUpTrend=os=0
else
CDownTrend=1
CUpTrend=1
endif
else
CDownTrend=1
CUpTrend=1
endif
//---------------------------------------------------//
//----------Bullish Reversal Detection---------------//
//---------------------------------------------------//
bullReversal=0
bullLow=low[numBars]
bearCount=0
for i=1 to numBars-1 do
if high[i]>high[numBars] then
bullReversal=0
break
else
bullReversal=1
bullLow=min(bullLow,low[i])
if open[i]>close[i] then
bearCount=1+bearCount
endif
endif
next
if bearCount/(numBars-1)>= minBars/100 then
bullReversal=1
else
bullReversal=0
endif
bullLow=min(bullLow,low)
isBullish=bullReversal and high>high[numBars]
bullishReversal=isBullish and CUpTrend
//---------------------------------------------------//
//----------Bearish Reversal Detection---------------//
//---------------------------------------------------//
bearReversal=0
bearHigh=high[numBars]
bullCount=0
for i=1 to numBars-1 do
if low[i]<low[numBars] then
bearReversal=0
break
else
bearReversal=1
bearHigh=max(bearHigh,high[i])
if open[i]<close[i] then
bullCount=1+bullCount
endif
endif
next
if bullCount/(numBars-1)>= minBars/100 then
bearReversal=1
else
bearReversal=0
endif
bearHigh=max(bearHigh,high)
isBearish=bearReversal and low<low[numBars]
bearishReversal=isBearish and CDownTrend
//---------------------------------------------------//
//-----------------Plot Signals----------------------//
//---------------------------------------------------//
//Bullish conditions
if bullishReversal and bullishReversal[1]=0 then
prevlnABx=lnABx
prevlnAB=lnAB
lnAT=high[numbars]
lnATx=barindex[numbars]
lnAB=bullLow
lnABx=barindex
bullProcess=1
drawtext("▲",barindex,low)coloured("green")
drawsegment(lnATx,lnAT,barindex,lnAT)coloured(8,153,129)style(line,1)
drawsegment(lnATx,bullLow,barindex,bullLow)coloured(8,153,129)style(line,1)
drawrectangle(lnATx,bullLow,barindex,lnAT)coloured(8,153,129,0)fillcolor(8,153,129,25)
if lowLevelbreaken=0 then
drawsegment(prevlnABx+1,prevlnAB,barindex[1],prevlnAB)style(line,2)coloured(8,153,129)
endif
lowLevelbreaken=0
endif
if bullProcess=1 then
if close[1]<lnAB and lowLevelbreaken=0 then
lowLevelbreaken=1
drawsegment(lnABx+1,lnAB,barindex[1],lnAB)style(line,2)coloured(8,153,129)
bullprocess=0
endif
endif
//---------------------------------------------------//
//Bearish conditions
if bearishReversal and bearishReversal[1]=0 then
prevlnSTx=lnSTx
prevlnST=lnST
lnSB=low[numbars]
lnSBx=barindex[numbars]
lnST=bearHigh
lnSTx=barindex
bearProcess=1
drawtext("▼",barindex,high)coloured("red")
drawsegment(lnSBx,lnSB,barindex,lnSB)coloured(242,54,69)
drawsegment(lnSBx,bearHigh,barindex,bearHigh)coloured(242,54,69)
drawrectangle(lnSBx,bearHigh,barindex,lnSB)coloured(242,54,69,0)fillcolor(242,54,69,25)
if HighLevelbreaken=0 then
drawsegment(prevlnSTx+1,prevlnST,barindex[1],prevlnST)style(line,2)coloured(242,54,69)
endif
HighLevelbreaken=0
endif
if bearProcess=1 then
if close[1]>lnST and HighLevelbreaken=0 then
HighLevelbreaken=1
drawsegment(lnSTx+1,lnST,barindex[1],lnST)style(line,2)coloured(242,54,69)
bearProcess=0
endif
endif
//---------------------------------------------------//
return