Repaints Three Bar Reversal Pattern [LuxAlgo] For ThinkOrSwim

Repaints

OGK

New member
VIP
mod note:
This study uses future bars. It waits until the future and then goes back to previous candles and repaints the signals. Probably will not work in watchlists. To use in scanners must look back at least 3 bars because the signals do not present in real time.

The author states:
The three-bar reversal pattern is a technical analysis pattern that signals a potential reversal in the prevailing trend. The pattern consists of three consecutive bar formations:

  • First Bar and Second Bar: 2 consecutive of the same sentiment, representing the prevailing trend in the market.
  • Third Bar: Confirms the reversal by closing beyond the high or low of the first bar, signaling a potential change in market sentiment.

m82sZyi.png
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Three Bar Reversal Pattern [LexAlgo]
Link to the indicator

requesting conversion for the subject LuxAlgo publicly published Three Bar Reversal Pattern .

https://www.tradingview.com/script/ct5E4FSn-Three-Bar-Reversal-Pattern-LuxAlgo/


Code:
// 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=5

indicator("Three Bar Reversal Pattern [LuxAlgo]", 'LuxAlgo - Three Bar Reversal Pattern', true, max_labels_count = 500, max_lines_count = 500, max_boxes_count = 500)

//---------------------------------------------------------------------------------------------------------------------
// Settings
//---------------------------------------------------------------------------------------------------------------------{

display  = display.all - display.status_line
brpType = input.string("All", "Pattern Type", options = ["Normal", "Enhanced", "All"], display = display)
brpSR  = input.string("Level", "Derived Support and Resistance", options = ["Level", "Zone", "None"], display = display)
brpAC  = input.color(#2962ff, 'Bullish Reversal Patterns')
brpSC  = input.color(#ff9800, 'Bearish Reversal Patterns')

trendIndiGroup = 'Trend Filtering'
trendType = input.string("None", "Filtering", options = ["Moving Average Cloud", "Supertrend", "Donchian Channels", "None"], group = trendIndiGroup, inline = 'flt', display = display)
trendFilt = input.string("Aligned", "", options = ["Aligned", "Opposite"], group = trendIndiGroup, inline = 'flt', display = display) // options = ["Aligned", "Opposite", "No detection"]
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() =>
    (close[2] < open[2]) and
     (low[1] < low[2]) and (high[1] < high[2]) and (close[1] < open[1]) and
     (close > open) and (high > high[2]) //(close > high[1])

isBearishReversal() =>
    (close[2] > open[2]) and
     (high[1] > high[2]) and (low[1] > low[2]) and (close[1] > open[1]) and
     (close < open) and (low < low[2]) //(close < low[1])

//---------------------------------------------------------------------------------------------------------------------}
// 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 : os

dcUpper = 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 = true

if 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 := true

if 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 := true

if 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

bullishReversal = isBullishReversal() and C_UpTrend
bearishReversal = isBearishReversal() and C_DownTrend

var 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 = na

if bullishReversal and (brpType == 'All' ? true : brpType == 'Enhanced' ? close > high[2] ? true : false : brpType == 'Normal' ? close < high[2] ? true : false : false)
    bullProcess := true

    lbAT := 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)'))

    lnAT := line.new(bar_index[2], high[2], bar_index, high[2], color = color.new(brpAC, 53))
    lnAB := line.new(bar_index[1], math.min(low[1], low), bar_index[0], math.min(low[1], low), color = color.new(brpAC, 53))
    linefill.new(lnAT, lnAB, color.new(brpAC, 73))

    lnAT2 := line.new(bar_index[2], high[2], bar_index, high[2], color = color.new(brpAC, 53))
    lnAB2 := line.new(bar_index[1], math.min(low[1], low), bar_index[0], math.min(low[1], low), color = color.new(brpAC, 53))

    bullHigh := brpSR == 'Zone' ? math.max(low[1], low) : math.min(low[1], low)

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()) + ' bars 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 := true

    if close[1] < lnAB.get_price(bar_index) or bearishReversal
        lbAT.set_tooltip('pattern failed\nthe low of the pattern breached')
        bullProcess := false

    if not bullProcess
        lnAT2.set_x2(bar_index[1])
        lnAB2.set_x2(bar_index[1])
    else
        lnAT2.set_x2(bar_index)
        lnAB2.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 := false


var 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 = na

if bearishReversal and (brpType == 'All' ? true : brpType == 'Enhanced' ? close < low[2] ? true : false : brpType == 'Normal' ? close > low[2] ? true : false : false)
    bearProcess := true

    lbST := 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)'))

    lnSB := line.new(bar_index[2], low[2], bar_index, low[2], color = color.new(brpSC, 53))
    lnST := line.new(bar_index[1], math.max(high[1], high), bar_index[0], math.max(high[1], high), color = color.new(brpSC, 53))
    linefill.new(lnST, lnSB, color.new(brpSC, 73))

    lnSB2 := line.new(bar_index[2], low[2], bar_index, low[2], color = color.new(brpSC, 53))
    lnST2 := line.new(bar_index[1], math.max(high[1], high), bar_index[0], math.max(high[1], high), color = color.new(brpSC, 53))

    bearLow := brpSR == 'Zone' ? math.min(high[1], high) : math.max(high[1], high)

if bearProcess
    if close[1] > lnST.get_price(bar_index) or bullishReversal
        lbST.set_tooltip('pattern failed\nthe high of the pattern breached')
        bearProcess := false

    if 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()) + ' bars 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' ? 73 : 53), bgcolor = color.new(brpSC, 73))
        bearProcess2 := true

    if not bearProcess
        lnST2.set_x2(bar_index[1])
        lnSB2.set_x2(bar_index[1])
    else
        lnST2.set_x2(bar_index)
        lnSB2.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

//---------------------------------------------------------------------------------------------------------------------}
check the below:

CSS:
#// Indicator for TOS
#// © LuxAlgo
#indicator("Three Bar Reversal Pattern [LuxAlgo]", 'LuxAlgo - Three Bar Reversal Pattern',
# Converted by Sam4Cok@Samer800     - 02/2025

input PatternType = {"Normal", "Enhanced", default "All"}; # "Pattern Type"
input DerivedSupportAndResistance = {default "Level", "Zone", "None"}; # "Derived Support and Resistance"
#trendIndiGroup = 'Trend Filtering'
input FilteringType = {"Moving Average Cloud", "Supertrend", "Donchian Channels", default "None"}; # "Filtering"
input FilteringDirection = {default "Aligned", "Opposite"};
#ma_Group  = 'Moving Average Settings'
input MovingAverageType = AverageType.HULL; #, "Type"
input movAvgFastLength  = 50; #, 'Fast Length', minval = 1, maxval = 100, group = ma_Group, display = display)
input movAvgSlowLength  = 200; # 'Slow Length', minval = 100, group = ma_Group, display = display)
#st_Group  = 'Supertrend Settings'
input supertrendAtrLength = 10; #, 'ATR Length', minval=1, group = st_Group, display = display)
input supertrendFactor = 3.0; # 'Factor', minval = 2, step = 0.1, group = st_Group, display = display)
#dc_Group  = 'Donchian Channel Settings'
input DonchianChannelLength = 13; #, 'Length', minval = 1, group = dc_Group, display = display)

def na = Double.NaN;
def last = IsNaN(close);
def cloud = FilteringType==FilteringType."Moving Average Cloud";
def super = FilteringType==FilteringType."Supertrend";
def dc = FilteringType==FilteringType."Donchian Channels";
def aligned = FilteringDirection==FilteringDirection."Aligned";
def non = DerivedSupportAndResistance==DerivedSupportAndResistance."None";
def zone = DerivedSupportAndResistance==DerivedSupportAndResistance."Zone";

#-- Functions
Script supertrend {
input factor = 3;
input atrPeriod = 10;
    def src = hl2;
    def tr = if isNaN(high[1]) then (high - low) else
             if !high[1] then (high - low) else TrueRange(high, close, low);
    def atr = WildersAverage(tr, atrPeriod);
    def upBand = src + factor * atr;
    def loBand = src - factor * atr;
    def lowerBand; def upperBand;
    def prevLowerBand = if isNaN(lowerBand[1]) then loBand else
                        if !lowerBand[1] then loBand else lowerBand[1];
    def prevUpperBand = if isNaN(upperBand[1]) then upBand else
                        if !upperBand[1] then upBand else upperBand[1];
    lowerBand = if loBand > prevLowerBand or close[1] < prevLowerBand then loBand else prevLowerBand;
    upperBand = if upBand < prevUpperBand or close[1] > prevUpperBand then upBand else prevUpperBand;
    def _direction;
    def superTrend;
    def prevSuperTrend = superTrend[1];
    if isNaN(atr[1]) {
        _direction = 1;
   } else if !atr[1] {
        _direction = 1;
    } else if prevSuperTrend == prevUpperBand {
        _direction = if close > upperBand then -1 else 1;
    } else {
        _direction = if close < lowerBand then 1 else -1;
    }
    superTrend = if _direction == -1 then lowerBand else upperBand;
    plot st = if !superTrend then Double.NaN else superTrend;
    plot dir = if !_direction then Double.NaN else _direction;
    }

Script isBullishReversal {
    def last = !IsNaN(close) and !IsNaN(high) and !IsNaN(open);
    def bull = if last then (close[2] < open[2]) and
     (low[1] < low[2]) and (high[1] < high[2]) and (close[1] < open[1]) and
     (close > open) and (high > high[2]) else no;
    plot out = bull;
}
Script isBearishReversal {
    def last = !IsNaN(close) and !IsNaN(low) and !IsNaN(open);
    def bear = if last then (close[2] > open[2]) and
     (high[1] > high[2]) and (low[1] > low[2]) and (close[1] > open[1]) and
     (close < open) and (low < low[2]) else no;
    plot out = bear;
}

#// Calculations - Trend Indicators - Moving Average Cloud
def maFast = movingAverage(MovingAverageType, close, movAvgFastLength);
def maSlow = movingAverage(MovingAverageType, close, movAvgSlowLength);
AddCloud(if cloud then maFast else na, maSlow, Color.DARK_GREEN, Color.DARK_RED, yes);

#// Calculations - Trend Indicators - Supertrend
def oc2 = (open + close) / 2;
def supertrend = supertrend(supertrendFactor, supertrendAtrLength).st;
def direction  = supertrend(supertrendFactor, supertrendAtrLength).dir;
plot upTrend = if !last and super and direction < 0 then supertrend else na; # "Up Trend"
plot dnTrend = if direction < 0 then na else if !last and super then supertrend else na; # "Down Trend"
upTrend.SetDefaultColor(Color.DARK_GREEN);
dnTrend.SetDefaultColor(Color.DARK_RED);

AddCloud(oc2, upTrend, Color.DARK_GREEN, Color.DARK_GREEN);
AddCloud(dnTrend, oc2, Color.DARK_RED, Color.DARK_RED);

#// Calculations - Trend Indicators - Donchian Channels
def upper = highest(close, DonchianChannelLength);
def lower = lowest(close, DonchianChannelLength);
def os;
def os1 = os[1];
    os = if upper > upper[1] then 1 else if lower < lower[1] then 0 else os1;

plot dcUpper = if !last and dc and !os then upper else na;
plot dcLower = if !last and dc and os  then lower else na;
dcUpper.SetDefaultColor(Color.DARK_RED);
dcLower.SetDefaultColor(Color.DARK_GREEN);

AddCloud(oc2, dcLower, Color.DARK_GREEN, Color.DARK_GREEN);
AddCloud(dcUpper, oc2, Color.DARK_RED, Color.DARK_RED);

#/ Calculations - 3-Bar Reversal Pattern
def C_DownTrend;
def C_UpTrend;
if !last then {
Switch (FilteringType) {
Case "Moving Average Cloud" :
    if Aligned {
        C_DownTrend = close < maFast and maFast < maSlow;
        C_UpTrend = close > maFast and maFast > maSlow;
    } else {
        C_DownTrend = close > maFast and maFast > maSlow;
        C_UpTrend = close < maFast and maFast < maSlow;
    }
Case "Supertrend" :
    if Aligned {
        C_DownTrend = direction > 0;
        C_UpTrend = direction < 0;
    } else {
        C_DownTrend = direction < 0;
        C_UpTrend = direction > 0;
    }
Case "Donchian Channels" :
    if Aligned {
        C_DownTrend = !os;
        C_UpTrend = os;
    } else {
        C_DownTrend = os;
        C_UpTrend = !os;
    }
Default :
        C_DownTrend = yes;
        C_UpTrend = yes;
}
} else {
    C_DownTrend = no;
    C_UpTrend = no;
}
def bullishReversal = isBullishReversal() and C_UpTrend;
def bearishReversal = isBearishReversal() and C_DownTrend;

def brpBull; def brpBear;
if !last then {
Switch (PatternType) {
Case "Normal" :
    brpBull = if close < high[2] then yes else no;
    brpBear = if close > low[2] then yes else no;
Case "Enhanced" :
    brpBull = if close > high[2] then yes else no;
    brpBear = if close < low[2] then yes else no;
Default :
    brpBull = yes;
    brpBear = yes;
}
} else {
    brpBull = no;
    brpBear = no;
}

#-- Bull
def lbAT;
def lbAT1;
def lnAT;
def lnAB;
def lnAT1;
def lnAB1;
def lnAT2;
def lnAB2;
def bullProcess;
def bullProcess1;
def bullProcess2;
def bullProcess3;
def cntInAT;
def cntInAB;
def bullHigh0;
def bullHigh;
def bullHigh1;
def bullLow;
def bullLow1;
def cntLabUp;
def labTxtUp;
if bullishReversal and brpBull {
    cntInAT = 0;
    cntInAB = 0;
    cntLabUp = 0;
    bullProcess = yes;
    lbAT = -3;
    lnAT = if !last then high[2] else lnAT[1];
    lnAB = if !last then min(low[1], low) else lnAB[1];
    lnAT1 = if !last then high[2] else lnAT[1];
    lnAB1 = if !last then min(low[1], low) else lnAB[1];
    bullHigh0 = if !last then if zone then max(low[1], low)else min(low[1], low) else bullHigh0[1];
    } else {
    cntInAT = if cntInAT[1] >= 2 then na else cntInAT[1] + 1;
    cntInAB = if cntInAB[1] >= 1 then na else cntInAB[1] + 1;
    cntLabUp = cntLabUp[1] + 1;
    bullProcess = bullProcess1[1];
    lbAT = no;
    lnAT = lnAT[1];
    lnAB = lnAB[1];
    lnAT1 = lnAT2[1];
    lnAB1 = lnAB2[1];
    bullHigh0 = bullHigh0[1];
}
def closeAT = if !last[-1] and close[-1] then close > lnAT else no;
def closeAB = if !last[-1] and close[-1] then close < lnAB else no;
if bullProcess {
    if closeAT {
        labTxtUp = if bullProcess and bullProcess != bullProcess[1] then -1 else cntLabUp;
        lbAT1 = if bullProcess and bullProcess != bullProcess[1] then no else yes;
        bullProcess1 = no;
        bullProcess2 = yes;
        bullHigh1 = bullHigh0;
        bullLow1 = lnAB;
    } else if closeAB or bearishReversal {
        bullProcess1 = no;
        bullProcess2 = bullProcess3[1];
        bullHigh1 = bullHigh[1];
        bullLow1 = bullLow[1];
        labTxtUp = -2;
        lbAT1 = no;
    } else {
        bullProcess1 = bullProcess;
        bullProcess2 = bullProcess3[1];
        bullHigh1 = bullHigh[1];
        bullLow1 = bullLow[1];
        labTxtUp = labTxtUp[1];
        lbAT1 = no;
    }
    lnAT2 = if !bullProcess1 then na else lnAT1;
    lnAB2 = if !bullProcess1 then na else lnAB1;
} else {
    bullProcess1 = bullProcess;
    bullHigh1 = bullHigh[1];
    bullLow1 = bullLow[1];
    bullProcess2 = bullProcess3[1];
    labTxtUp = labTxtUp[1];
    lbAT1 = no;
    lnAT2 = lnAT1;
    lnAB2 = lnAB1;
}

if bullProcess2 and !non {
    bullLow =  bullLow1;
    bullHigh = bullHigh1;
    bullProcess3 = if close > bullLow then bullProcess2 else no;
} else {
    bullHigh = na;
    bullLow = na;
    bullProcess3 = bullProcess2;
}
def bullLo = if !last and bullLow then bullLow else na;
def bullHi = if !last and bullHigh then bullHigh else na;

#-- Signals
plot sigUp = if lbAT then low else na;
plot sigUp1 = if lbAT1 and !lbAT then low - AbsValue(open - close) / 2 else na;
sigUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
sigUp1.SetPaintingStrategy(PaintingStrategy.POINTS);
sigUp.SetDefaultColor(Color.CYAN);
sigUp1.SetDefaultColor(Color.CYAN);

#-- bull level
plot bullLvl = if zone then na else bullLo;
bullLvl.SetPaintingStrategy(PaintingStrategy.DASHES);
bullLvl.SetDefaultColor(Color.CYAN);

#-- bull extend
plot BullUp = if !last and lnAT2 then lnAT2 else na;
plot BullDn = if !last and lnAB2 then lnAB2 else na;
BullUp.SetDefaultColor(Color.CYAN);
BullDn.SetDefaultColor(Color.CYAN);

#-- bull new
plot newBullUp = if !isNaN(cntInAT[-2]) then lnAT[-2] else na;
plot newBullDn = if !isNaN(cntInAB[-1]) then lnAB[-1] else na;
newBullUp.SetLineWeight(2);
newBullDn.SetLineWeight(2);
newBullUp.SetDefaultColor(Color.CYAN);
newBullDn.SetDefaultColor(Color.CYAN);

AddCloud(newBullUp, if !isNaN(newBullDn) then newBullDn else newBullUp, Color.CYAN);
AddCloud(if zone and bullHi==bullHi[1] then bullHi else na, bullLo, Color.CYAN, Color.CYAN, yes);

#-- Label Up
def infoB = if lbAT then labTxtUp else infoB[1];

AddLabel(1, if infoB == -1 then "enchanced pattern (confirmed at detection)\nprice activity above the pattern high"
             else if infoB > 0 then "pattern confirmed " + infoB + " bars later" else
                  if infoB == - 2 then "pattern failed\nthe low of the pattern breached" else
                "new bullish pattern detected " + (if close > high[2] then "(enchanced)" else "(normal)"), Color.CYAN);
#-- Bear
def lbST;
def lbST2;
def lnSB;
def lnST;
def lnSB1;
def lnST1;
def lnSB2;
def lnST2;
def bearProcess;
def bearProcess1;
def bearProcess2;
def bearProcess3;
def cntInST;
def cntInSB;
def bearLow0;
def bearLow;
def bearLow1;
def bearHigh;
def bearHigh1;
def cntLabDn;
def labTxtDn;
if bearishReversal and brpBear {
    cntInST = 0;
    cntInSB = 0;
    cntLabDn = 0;
    bearProcess = yes;
    lbST = -3;
    lnSB = if !last then low[2] else lnSB[1];
    lnST = if !last then max(high[1], high) else lnST[1];
    lnSB1 = if !last then low[2] else lnSB1[1];
    lnST1 = if !last then max(high[1], high) else lnST1[1];
    bearLow0 = if !last then if zone then min(high[1], high)else max(high[1], high) else bearLow0[1];
    } else {
    cntInST = if cntInST[1] >= 1 then na else cntInST[1] + 1;
    cntInSB = if cntInSB[1] >= 2 then na else cntInSB[1] + 1;
    cntLabDn = cntLabDn[1] + 1;
    bearProcess = bearProcess1[1];
    lbST = no;
    lnSB = lnSB[1];
    lnST = lnST[1];
    lnSB1 = lnSB2[1];
    lnST1 = lnST2[1];
    bearLow0 = bearLow0[1];
}
def closeST = if !last[-1] and close[-1] then close > lnST else closeST[1];
def closeSB = if !last[-1] and close[-1] then close < lnSB else closeSB[1];
if bearProcess {
    if closeST or bullishReversal {
        bearProcess1 = no;
        bearProcess2 = bearProcess3[1];
        bearHigh1 = bearHigh[1];
        bearLow1 = bearLow[1];
        labTxtDn = -1;
        lbST2 = no;
    } else if closeSB {
        labTxtDn = if bearProcess and bearProcess != bearProcess[1] then -2 else cntLabDn;
        lbST2 = if bearProcess and bearProcess != bearProcess[1] then no else yes;
        bearProcess1 = no;
        bearProcess2 = yes;
        bearHigh1 = lnST;
        bearLow1 = bearLow0;
    } else {
        bearProcess1 = bearProcess;
        bearProcess2 = bearProcess3[1];
        bearHigh1 = bearHigh[1];
        bearLow1 = bearLow[1];
        labTxtDn = if !labTxtDn[1] then -3 else labTxtDn[1];
        lbST2 = no;
    }
    lnSB2 = if !bearProcess1 then na else lnSB1;
    lnST2 = if !bearProcess1 then na else lnST1;
} else {
    bearProcess1 = bearProcess;
    bearProcess2 = bearProcess3[1];
    bearHigh1 = bearHigh[1];
    bearLow1 = bearLow[1];
    labTxtDn = if !labTxtDn[1] then -3 else labTxtDn[1];
    lbST2 = no;
    lnSB2 = lnSB1;
    lnST2 = lnST1;
}

if bearProcess2 and !non {
    bearHigh = bearHigh1;
    bearLow =  bearLow1;
    bearProcess3 = if close < bearHigh then bearProcess2 else no;
} else {
    bearHigh = na;
    bearLow = na;
    bearProcess3 = bearProcess2;
}
def bearLo = if !last and bearLow then bearLow else na;
def bearHi = if !last and bearHigh then bearHigh else na;

#-- Signals
plot sigDn = if lbST then high else na;
plot sigDn2 = if lbST2 and !lbST then high + AbsValue(open - close) / 2 else na;
sigDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sigDn2.SetPaintingStrategy(PaintingStrategy.POINTS);
sigDn.SetDefaultColor(Color.MAGENTA);
sigDn2.SetDefaultColor(Color.MAGENTA);

#-- Levels
plot bearLvl = if zone then na else bearHi;
bearLvl.SetPaintingStrategy(PaintingStrategy.DASHES);
bearLvl.SetDefaultColor(Color.MAGENTA);

#-- Extend
plot BearUp = if !last and lnST2 then lnST2 else na;
plot BearDn = if !last and lnSB2 then lnSB2 else na;
BearUp.SetDefaultColor(Color.MAGENTA);
BearDn.SetDefaultColor(Color.MAGENTA);

#-- new bearish
plot newBearUp = if last then na else if !isNaN(cntInST[-1]) then lnST[-1] else na;
plot newBearDn = if last then na else if !isNaN(cntInSB[-2]) then lnSB[-2] else na;
newBearUp.SetLineWeight(2);
newBearDn.SetLineWeight(2);
newBearUp.SetDefaultColor(Color.MAGENTA);
newBearDn.SetDefaultColor(Color.MAGENTA);

#-- Cloud
AddCloud(if !isNaN(newBearUp) then newBearUp else newBearDn, newBearDn, Color.MAGENTA);
AddCloud(if zone and bearHi==bearHi[1] then bearHi else na, bearLo, Color.MAGENTA, Color.MAGENTA, yes);

#-- LAbel Dn
def infoS = if lbST then labTxtDn else infoS[1];

AddLabel(1, if infoS == -1 then "pattern failed\nthe high of the pattern breached" else
            if infoS == -2 then "enchanced pattern (confirmed at detection)\nprice activity below the pattern low"
             else if infoS > 0 then "pattern confirmed " + infoS + " bars later" else
            "new bearish pattern detected " + (if close < low[2] then "(enchanced)" else "(normal)"), Color.MAGENTA);

#-- END of CODE
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
419 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top