Please Convert N Bar Reversal Detector to Prorealtime
Forums › ProRealTime English forum › ProBuilder support › Please Convert N Bar Reversal Detector to Prorealtime
- This topic has 11 replies, 5 voices, and was last updated 2 months ago by SnorreDK.
-
-
10/02/2024 at 7:43 AM #238387
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo
//@version=5indicator(“N Bar Reversal Detector [LuxAlgo]”, ‘LuxAlgo – N Bar Reversal Detector’, true, max_labels_count = 500, max_lines_count = 500, max_boxes_count = 500)
//———————————————————————————————————————
// Settings
//———————————————————————————————————————{display = display.all – display.status_line
brpGroup = ‘Pattern Detection’
brpTypeTT = ‘Selects the type of reversal pattern to detect:\n\n’ +
‘ *Normal: Detects standard reversal patterns based on the high and low prices of the sequence.\n\n’ +
‘ *Enhanced: Detects advanced reversal patterns where the most recent closing price must exceed or fall below the high or low of the first candle.\n\n’ +
‘ *All: Includes both normal and enhanced patterns.’
brpType = input.string(“All”, “Pattern Type”, options = [“Normal”, “Enhanced”, “All”], group = brpGroup, display = display, tooltip = brpTypeTT)
numBarsTT = ‘Specifies the number of candles in the sequence used to identify a reversal pattern. The length N includes the initial N-1 candles plus the most recent candle (Nth) for pattern detection.’
numBars = input.int(7, ‘Reversal Pattern Sequence Length’, minval = 2, group = brpGroup, display = display, tooltip = numBarsTT)
minBrasTT = ‘Sets the minimum percentage of the first N-1 candles that must be bullish (for a bearish reversal) or bearish (for a bullish reversal) to qualify as a valid reversal pattern. This setting helps adjust the sensitivity of the pattern detection.’
minBars = input.int(50, ‘Min Percentage of Required Candles’, minval = 0, maxval = 100, group = brpGroup, display = display, tooltip = minBrasTT) / 100brpSRTT = ‘Shows horizontal support and resistance lines based on the highest high or lowest low of the pattern’
brpSR = input.string(“Level”, “Derived Support and Resistance”, options = [“Level”, “None”], group = brpGroup, display = display, tooltip = brpSRTT)
brpAC = input.color(#089981, ‘Bullish Reversal Patterns’, group = brpGroup)
brpSC = input.color(#f23645, ‘Bearish Reversal Patterns’, group = brpGroup)trendIndiGroup = ‘Trend Filtering’
trendTT = ‘Selects the trend filtering method for detecting reversal patterns and specifies the alignment with the trend.’
trendType = input.string(“None”, “Filtering”, options = [“Moving Average Cloud”, “Supertrend”, “Donchian Channels”, “None”], group = trendIndiGroup, inline = ‘flt’, display = display, tooltip = trendTT)
trendFilt = input.string(“Aligned”, “”, options = [“Aligned”, “Opposite”], group = trendIndiGroup, inline = ‘flt’, display = display)
trendAC = input.color(#089981, ‘Bullish Trend’, inline = ‘trnd’)
trendSC = input.color(#f23645, ‘ Bearish Trend’, inline = ‘trnd’)ma_Group = ‘Moving Average Settings’
maType = input.string(“HMA”, “Type”, options = [“SMA”, “EMA”, “HMA”, “RMA”, “WMA”, “VWMA”], group = ma_Group, display = display)
maFLength = input.int(50, ‘Fast Length’, minval = 1, maxval = 100, group = ma_Group, display = display)
maSLength = input.int(200, ‘Slow Length’, minval = 100, group = ma_Group, display = display)st_Group = ‘Supertrend Settings’
atrPeriod = input.int(10, ‘ATR Length’, minval=1, group = st_Group, display = display)
factor = input.float(3, ‘Factor’, minval = 2, step = 0.1, group = st_Group, display = display)dc_Group = ‘Donchian Channel Settings’
length = input.int(13, ‘Length’, minval = 1, group = dc_Group, display = display)//———————————————————————————————————————}
// Functions / Methods
//———————————————————————————————————————{movingAverage(source, length, maType) =>
switch maType
“SMA” => ta.sma (source, length)
“EMA” => ta.ema (source, length)
“HMA” => ta.hma (source, length)
“RMA” => ta.rma (source, length)
“WMA” => ta.wma (source, length)
“VWMA” => ta.vwma(source, length)donchian(len) => math.avg(ta.lowest(len), ta.highest(len))
isBullishReversal() =>
var bool bullReversal = na
float bullLow = low[numBars]
int bearCount = 0for i = 1 to numBars – 1
if high[i] > high[numBars]
bullReversal := false
break
else
bullReversal := true
bullLow := math.min(bullLow, low[i])
if open[i] > close[i]
bearCount += 1if bearCount / (numBars-1) >= minBars
bullReversal := true
else
bullReversal := false[math.min(bullLow, low), bullReversal and high > high[numBars]]
isBearishReversal() =>
var bool bearReversal = na
float bearHigh = high[numBars]
int bullCount = 0for i = 1 to numBars – 1
if low[i] < low[numBars]
bearReversal := false
break
else
bearReversal := true
bearHigh := math.max(bearHigh, high[i])
if open[i] < close[i]
bullCount += 1if bullCount / (numBars-1) >= minBars
bearReversal := true
else
bearReversal := false[math.max(bearHigh, high), bearReversal and low < low[numBars]]
//———————————————————————————————————————}
// Calculations – Trend Indicators – Moving Average Cloud
//———————————————————————————————————————{maFast = movingAverage(close, maFLength, maType)
maSlow = movingAverage(close, maSLength, maType)maColor = maFast > maSlow ? trendAC : trendSC
ma1 = plot(trendType == ‘Moving Average Cloud’ ? maFast : na, “ma fast”, color.new(maColor, 81), 1, plot.style_linebr, display = display, editable = false)
ma2 = plot(trendType == ‘Moving Average Cloud’ ? maSlow : na, “ma slow”, color.new(maColor, 73), 1, plot.style_linebr, display = display, editable = false)fill(ma1, ma2, math.max(maFast, maSlow), math.min(maFast, maSlow), color.new(maColor, maFast > maSlow ? 99 : 81), color.new(maColor, maFast > maSlow ? 81 : 99))
//———————————————————————————————————————}
// Calculations – Trend Indicators – Supertrend
//———————————————————————————————————————{[supertrend, direction] = ta.supertrend(factor, atrPeriod)
supertrend := barstate.isfirst ? na : supertrend
upTrend = plot(direction < 0 ? trendType == ‘Supertrend’ ? supertrend : na : na, “Up Trend”, color.new(trendAC, 73), style = plot.style_linebr, display = display, editable = false)
downTrend = plot(direction < 0 ? na : trendType == ‘Supertrend’ ? supertrend : na, “Down Trend”, color.new(trendSC, 73), style = plot.style_linebr, display = display, editable = false)
bodyMiddle = plot(barstate.isfirst ? na : trendType == ‘Supertrend’ ? (open + close) / 2 : na, “Body Middle”, display = display.none, editable = false)fill(bodyMiddle, upTrend , supertrend, (open + close) / 2, color.new(trendAC, 81), color.new(chart.bg_color, 100), fillgaps = false)
fill(bodyMiddle, downTrend, (open + close) / 2, supertrend, color.new(chart.bg_color, 100), color.new(trendSC, 81), fillgaps = false)//———————————————————————————————————————}
// Calculations – Trend Indicators – Donchian Channels
//———————————————————————————————————————{var os = 0
upper = ta.highest(close, length)
lower = ta.lowest(close, length)
os := upper > upper[1] ? 1 : lower < lower[1] ? 0 : osdcUpper = plot(trendType == ‘Donchian Channels’ ? upper : na, color = os == 1 ? color.new(trendAC, 99) : color.new(trendSC, 73), display = display, editable = false)
dcLower = plot(trendType == ‘Donchian Channels’ ? lower : na, color = os == 1 ? color.new(trendAC, 73) : color.new(trendSC, 99), display = display, editable = false)fill(dcUpper, dcLower, upper, lower, os == 1 ? color.new(chart.bg_color, 100) : color.new(trendSC, 81) , os == 0 ? color.new(chart.bg_color, 100) : color.new(trendAC, 81))
//———————————————————————————————————————}
// Calculations – 3-Bar Reversal Pattern
//———————————————————————————————————————{C_DownTrend = true
C_UpTrend = trueif trendType == ‘Moving Average Cloud’
if trendFilt == ‘Aligned’
C_DownTrend := close < maFast and maFast < maSlow
C_UpTrend := close > maFast and maFast > maSlow
else if trendFilt == ‘Opposite’
C_DownTrend := close > maFast and maFast > maSlow
C_UpTrend := close < maFast and maFast < maSlow
else
C_DownTrend := true
C_UpTrend := trueif trendType == ‘Supertrend’
if trendFilt == ‘Aligned’
C_DownTrend := direction > 0
C_UpTrend := direction < 0
else if trendFilt == ‘Opposite’
C_DownTrend := direction < 0
C_UpTrend := direction > 0
else
C_DownTrend := true
C_UpTrend := trueif trendType == ‘Donchian Channels’
if trendFilt == ‘Aligned’
C_DownTrend := os == 0
C_UpTrend := os == 1
else if trendFilt == ‘Opposite’
C_DownTrend := os == 1
C_UpTrend := os == 0
else
C_DownTrend := true
C_UpTrend := true[bullLow, isBullish] = isBullishReversal()
bullishReversal = isBullish and C_UpTrend[bearHigh, isBearish] = isBearishReversal()
bearishReversal = isBearish and C_DownTrendvar line lnAT = na
var line lnAB = na
var line lnAT2 = na
var line lnAB2 = na
var label lbAT = na
var box bxA = na
var bool bullProcess = false
var bool bullProcess2 = false
var float bullHigh = naif bullishReversal and not bullishReversal[1] and (brpType == ‘All’ ? true : brpType == ‘Enhanced’ ? close > high[numBars] ? true : false : brpType == ‘Normal’ ? close < high[numBars] ? true : false : false)
bullProcess := truelbAT := label.new(bar_index, low, ‘▲’, color = color(na), textcolor = color.new(brpAC, 07), style = label.style_label_up, size = size.small, tooltip = ‘new bullish pattern detected’ + (close > high[2] ? ‘ (enchanced)’ : ‘ (normal)’))
lnAT2 := line.new(bar_index[numBars], high[numBars], bar_index, high[numBars], color = color.new(brpAC, 53))
lnAB2 := line.new(bar_index[numBars], bullLow, bar_index, bullLow, color = color.new(brpAC, 53))
linefill.new(lnAT2, lnAB2, color.new(brpAC, 73))lnAT := line.new(bar_index[numBars], high[numBars], bar_index, high[numBars], color = color.new(brpAC, 53))
lnAB := line.new(bar_index[numBars], bullLow, bar_index, bullLow, color = color.new(brpAC, 53))bullHigh := brpSR == ‘Zone’ ? math.max(low[1], low) : bullLow
if bullProcess
if close[1] > lnAT.get_price(bar_index)
if bullProcess[1] and bullProcess[1] != bullProcess[2]
lbAT.set_tooltip(‘enchanced pattern (confirmed at detection)\nprice activity above the pattern high’)
else
lbAT.set_tooltip(‘pattern confirmed ‘ + str.tostring(bar_index[1] – lbAT.get_x()) + ‘ bar(s) later’)
label.new(bar_index[1], low[1], ‘⦁’, color = color(na), textcolor = color.new(brpAC, 07), style = label.style_label_up, size = size.small, tooltip = ‘confirmation bar\nprice activity above the pattern high’)bullProcess := false
bxA := box.new(bar_index, bullHigh, bar_index, lnAB.get_price(bar_index), color.new(brpAC, brpSR == ‘Zone’ ? 73 : 53), bgcolor = color.new(brpAC, 73))
bullProcess2 := trueif close[1] < lnAB.get_price(bar_index) or bearishReversal
lbAT.set_tooltip(‘pattern failed\nthe low of the pattern breached’)
bullProcess := falseif not bullProcess
lnAT.set_x2(bar_index[1])
lnAB.set_x2(bar_index[1])
else
lnAT.set_x2(bar_index)
lnAB.set_x2(bar_index)if bullProcess2 and brpSR != ‘None’
if close > bxA.get_bottom()
bxA.set_right(bar_index)
else
bxA.set_right(bar_index)
bullProcess2 := falsevar line lnST = na
var line lnSB = na
var line lnST2 = na
var line lnSB2 = na
var label lbST = na
var box bxS = na
var bool bearProcess = false
var bool bearProcess2 = false
var float bearLow = naif bearishReversal and not bearishReversal[1] and (brpType == ‘All’ ? true : brpType == ‘Enhanced’ ? close < low[numBars] ? true : false : brpType == ‘Normal’ ? close > low[numBars] ? true : false : false)
bearProcess := truelbST := label.new(bar_index, high, ‘▼’, color = color(na), textcolor = color.new(brpSC, 07), style = label.style_label_down, size = size.small, tooltip = ‘new bearish pattern detected’ + (close < low[2] ? ‘ (enchanced)’ : ‘ (normal)’))
lnSB2 := line.new(bar_index[numBars], low[numBars], bar_index, low[numBars], color = color.new(brpSC, 53))
lnST2 := line.new(bar_index[numBars], bearHigh, bar_index, bearHigh, color = color.new(brpSC, 53))
linefill.new(lnST2, lnSB2, color.new(brpSC, 89))lnSB := line.new(bar_index[numBars], low[numBars], bar_index, low[numBars], color = color.new(brpSC, 53))
lnST := line.new(bar_index[numBars], bearHigh, bar_index, bearHigh, color = color.new(brpSC, 53))bearLow := brpSR == ‘Zone’ ? math.min(high[1], high) : bearHigh
if bearProcess
if close[1] > lnST.get_price(bar_index) or bullishReversal
lbST.set_tooltip(‘pattern failed\nthe high of the pattern breached’)
bearProcess := falseif close[1] < lnSB.get_price(bar_index)
if bearProcess[1] and bearProcess[1] != bearProcess[2]
lbST.set_tooltip(‘enchanced pattern (confirmed at detection)\nprice activity below the pattern low’)
else
lbST.set_tooltip(‘pattern confirmed ‘ + str.tostring(bar_index[1] – lbST.get_x()) + ‘ bar(s) later’)
label.new(bar_index[1], high[1], ‘⦁’, color = color(na), textcolor = color.new(brpSC, 07), style = label.style_label_down, size = size.small, tooltip = ‘confirmation bar\nprice activity blow the pattern low’)bearProcess := false
bxS := box.new(bar_index, lnST.get_price(bar_index), bar_index, bearLow, color.new(brpSC, brpSR == ‘Zone’ ? 89 : 53), bgcolor = color.new(brpSC, 89))
bearProcess2 := trueif not bearProcess
lnST.set_x2(bar_index[1])
lnSB.set_x2(bar_index[1])
else
lnST.set_x2(bar_index)
lnSB.set_x2(bar_index)if bearProcess2 and brpSR != ‘None’
if close < bxS.get_top()
bxS.set_right(bar_index)
else
bxS.set_right(bar_index)
bearProcess2 := false//———————————————————————————————————————}
10/02/2024 at 3:29 PM #238412- Only post in the language of the forum that you are posting in. For example English only in the English speaking forums and French only in the French speaking forums.
- Do not double post. Ask your question only once and only in one forum. All double posts will be deleted anyway so posting the same question multiple times will just be wasting your own time and will not get you an answer any quicker. Double posting just creates confusion in the forums
- Do not append posts to unrelated tropics.
The basic rules are highlighted in yellow below.
Thanks 🙂
10/04/2024 at 4:14 PM #238499Here you have the indicator: https://www.prorealcode.com/prorealtime-indicators/n-reversal-detector/
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278//---------------------------------------------------////PRC_N reversal Detector//version = 0//04.10.2024//Iván González @ www.prorealcode.com//Sharing ProRealTime knowledge//---------------------------------------------------////------------------Inputs---------------------------////---------------------------------------------------////Trend fileteringtrendType=0 //0=no trend / 1=MA trend / 2=ST trend / 3=Donchian trendTrendFilter=1 // True=Aligned / False=Opposite//Moving average settingsmaType=7maFlength=50maSLength=200//Supertrend settingsatrPeriod=10factor=3//Donchian Channels settingslength=13//Pattern DetectionnumBars=7minBars=50//---------------------------------------------------////-----Trend indicators - Moving average Cloud-------////---------------------------------------------------//maFast=average[maFlength,maType](close)maSlow=average[maSLength,maType](close)if trendType=1 thenif maFast>maSlow thenr=8g=153b=129elser=242g=54b=69endifcolorbetween(maFast,maSlow,r,g,b,20)endif//---------------------------------------------------////---------Trend indicators - Supertrend-------------////---------------------------------------------------//atr = averagetruerange[atrPeriod](close)up = high - factor*atrup1 = up[1]if close[1] > up1 thenup = max(up,up1)elseup = upendifdn = low + factor*atrdn1 = dn[1]if close[1] < dn1 thendn = min(dn,dn1)elsedn = dnendifonce trend = 1if trend = -1 and close > dn1 thentrend = 1elsif trend = 1 and close < up1 thentrend = -1elsetrend = trendendifif trendType=2 thenif trend = 1 thenmysupertrend = upr=8g=153b=129elsemysupertrend = dnr=242g=54b=69endifbodyMiddle=(open+close)/2colorbetween(mysupertrend,bodyMiddle,r,g,b,25)endif//---------------------------------------------------////------Trend indicators - Donchian Channels---------////---------------------------------------------------//upper=highest[length](close)lower=lowest[length](close)if trendType=3 thenonce os=0if upper>upper[1] thenos=1r=8g=153b=129elsif lower<lower[1] thenos=0r=242g=54b=69elseos=osendifcolorbetween(upper,lower,r,g,b,20)endif//---------------------------------------------------////--------------3-Bar Reversal Pattern---------------////---------------------------------------------------//if trendType=1 thenif TrendFilter=1 thenCDownTrend= close<maFast and maFast<maSlowCUpTrend= close>maFast and maFast>maSlowelsif TrendFilter=0 thenCDownTrend=close>maFast and maFast>maSlowCUpTrend=close<maFast and maFast<maSlowelseCDownTrend=1CUpTrend=1endifelsif trendType=2 thenif TrendFilter=1 thenCDownTrend= trend=-1CUpTrend= trend=1elsif TrendFilter=0 thenCDownTrend=trend=1CUpTrend=trend=-1elseCDownTrend=1CUpTrend=1endifelsif TrendType=3 thenif TrendFilter=1 thenCDownTrend=os=0CUpTrend=os=1elsif TrendFilter=0 thenCDownTrend=os=1CUpTrend=os=0elseCDownTrend=1CUpTrend=1endifelseCDownTrend=1CUpTrend=1endif//---------------------------------------------------////----------Bullish Reversal Detection---------------////---------------------------------------------------//bullReversal=0bullLow=low[numBars]bearCount=0for i=1 to numBars-1 doif high[i]>high[numBars] thenbullReversal=0breakelsebullReversal=1bullLow=min(bullLow,low[i])if open[i]>close[i] thenbearCount=1+bearCountendifendifnextif bearCount/(numBars-1)>= minBars/100 thenbullReversal=1elsebullReversal=0endifbullLow=min(bullLow,low)isBullish=bullReversal and high>high[numBars]bullishReversal=isBullish and CUpTrend//---------------------------------------------------////----------Bearish Reversal Detection---------------////---------------------------------------------------//bearReversal=0bearHigh=high[numBars]bullCount=0for i=1 to numBars-1 doif low[i]<low[numBars] thenbearReversal=0breakelsebearReversal=1bearHigh=max(bearHigh,high[i])if open[i]<close[i] thenbullCount=1+bullCountendifendifnextif bullCount/(numBars-1)>= minBars/100 thenbearReversal=1elsebearReversal=0endifbearHigh=max(bearHigh,high)isBearish=bearReversal and low<low[numBars]bearishReversal=isBearish and CDownTrend//---------------------------------------------------////-----------------Plot Signals----------------------////---------------------------------------------------////Bullish conditionsif bullishReversal and bullishReversal[1]=0 thenprevlnABx=lnABxprevlnAB=lnABlnAT=high[numbars]lnATx=barindex[numbars]lnAB=bullLowlnABx=barindexbullProcess=1drawtext("▲",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 thendrawsegment(prevlnABx+1,prevlnAB,barindex[1],prevlnAB)style(line,2)coloured(8,153,129)endiflowLevelbreaken=0endifif bullProcess=1 thenif close[1]<lnAB and lowLevelbreaken=0 thenlowLevelbreaken=1drawsegment(lnABx+1,lnAB,barindex[1],lnAB)style(line,2)coloured(8,153,129)bullprocess=0endifendif//---------------------------------------------------////Bearish conditionsif bearishReversal and bearishReversal[1]=0 thenprevlnSTx=lnSTxprevlnST=lnSTlnSB=low[numbars]lnSBx=barindex[numbars]lnST=bearHighlnSTx=barindexbearProcess=1drawtext("▼",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 thendrawsegment(prevlnSTx+1,prevlnST,barindex[1],prevlnST)style(line,2)coloured(242,54,69)endifHighLevelbreaken=0endifif bearProcess=1 thenif close[1]>lnST and HighLevelbreaken=0 thenHighLevelbreaken=1drawsegment(lnSTx+1,lnST,barindex[1],lnST)style(line,2)coloured(242,54,69)bearProcess=0endifendif//---------------------------------------------------//return10/04/2024 at 4:52 PM #23850110/08/2024 at 7:28 AM #238643Bonjour IvanJe voudrais me servir du niveau des segments pour entrer ou sortir d’une stratégie (je dis bien des niveaux et pas des croisements)peux tu m’indiquer comment opérer avec exempleMerciHello Ivan I would like to use the segment level to enter or exit a strategy (I mean levels and not crossings) can you tell me how to operate with an example Thank you
10/08/2024 at 8:24 AM #238647Only post in the language of the forum that you are posting in. For example English only in the English speaking forums and French only in the French speaking forums. Thanks 🙂
10/08/2024 at 9:01 AM #23865710/08/2024 at 1:44 PM #238718Roberto excuse moi mais c’était bien indiqué en haut forum françaiscomment veux tu savoir la langue si c’est sans arrêt traduit .Forum ProRealTime en françaisMerci YvanRoberto excuse me but it was clearly indicated at the top French forum
how do you want to know the language if it is constantly translated. ProRealTime Forum in French
Thanks Yvan
10/08/2024 at 4:07 PM #238730This forum is Forums › ProRealTime English forum › ProBuilder support ›, as you can see from the English flag below.
Please use English only!
10/08/2024 at 5:48 PM #23874110/09/2024 at 9:36 AM #238754Just at the bottom of the yellow rectangle.
10/09/2024 at 12:17 PM #238769 -
AuthorPosts
Find exclusive trading pro-tools on