Candlestick Structure [LuxAlgo] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
WE1oM92.png


Author Message:

The Candlestick Structure indicator detects major market trends and displays various candlestick patterns aligning with the detected trend, filtering out potentially unwanted patterns as a result. Multiple trend detection methods are included and can be selected by the users.

CODE:

CSS:
#/ This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
#// © LuxAlgo
#indicator("Candlestick Structure [LuxAlgo]",shorttitle = "LuxAlgo - Candlestick Structure",
# Converted by Sam4Cok@Samer800    - 08/2024
#//Bullish
input showClouds = yes; #(true, title = "Show Gradient",
input colorBarOptions = {"Minor Trend", "Major Trend",Default "Don't Color Bars"};
input labelOptions = {Default "Top Reversals Only", "All Reversals Structure", "Don't Show Labels"};
input BullishHammer = yes;
input BullishInvertedHammer = yes;
input BullishEngulfing = yes;
input BullishRisingThree = yes;
input BullishThreeWhiteSoldiers = yes;
input BullishMorningStar = yes;
input BullishHarami = yes;
input BullishTweezerBottom = yes;
#//Bearish
input BearishHangingMan = yes;
input BearishShootingStar = yes;
input BearishEngulfing = yes;
input BearishFallingThree = yes;
input BearishThreeBlackCrows = yes;
input BearishEveningStar = yes;
input BearishHarami = yes;
input BearishTweezerTop = yes;
#//Trend Options
input minorTrendLength = 10;      # "Minor Trend Length"
input majorTrendType = {"Moving Average", "ChoCh", default "SuperTrend", "Donchian Channel"}; # "Major Trend Method"
input chochLength = 20;           # "Swing Length"
input movAvgType  = AverageType.EXPONENTIAL;
input fastMovAvgLength = 50;      # "Short EMA Length"
input slowMovAvgLength = 200;     # "Long EMA Length"
input SupertrendFactor = 4.0;     # "Super Trend Factor"
input SupertrendLength = 20;      # "Length"
input DonchianChannelLength = 40; # "Donchian Channel Length"
#//Style

def na = Double.NaN;
def last = IsNaN(close);
def choch = majorTrendType == majorTrendType."ChoCh";
script count {
    input condition = yes;
    input filter = yes;
    def bothCond = condition and filter;
    def reversals = reversals[1] + (if bothCond then 1 else 0);
    def total = total[1] + (if condition then 1 else 0);
    def percentChg = reversals / total * 100;
    def rndPer = Round(percentChg, 2);
    plot cond = if IsNaN(bothCond) then no else bothCond;
    plot per = if IsNaN(rndPer) then 0 else rndPer;
    plot cnt = if IsNaN(total) then 0 else total;
}
#/ The same on Pine Script®
script supertrend {
    input factor = 3;
    input atrPeriod = 10;
    def src = hl2;
    def atr = ATR(Length = atrPeriod);
    def up = src + factor * atr;
    def lo = src - factor * atr;
    def lowerBand = CompoundValue(1, if !lowerBand[1] then lo else
                    if lo > lowerBand[1] or close[1] < lowerBand[1] then lo else lowerBand[1], lo);
    def upperBand = CompoundValue(1, if !upperBand[1] then up else
                    if up < upperBand[1] or close[1] > upperBand[1] then up else upperBand[1], up);
    def _direction;
    def superTrend;
    def prevUpperBand = CompoundValue(1, upperBand[1], up);
    def prevSuperTrend = CompoundValue(1, superTrend[1], 0);
    if IsNaN(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;
    }
    def trend = CompoundValue(1, if !_direction then 1 else _direction, 1);
    superTrend = if trend == -1 then lowerBand else upperBand;
    plot st = superTrend;
    plot dir = trend * -1;
}
#//EMA Trend Detection
script get_ema {
    input _len1 = 50;
    input _len2 = 200;
    input avgType = AverageType.EXPONENTIAL;
    def s_ema = MovingAverage(avgType, close, _len1);
    def l_ema = MovingAverage(avgType, close, _len2);
    def s_dir = if s_ema > l_ema then 1 else
                if s_ema < l_ema then -1 else s_dir[1];
    plot ema1 = s_ema;
    plot ema2 = l_ema;
    plot dir  = s_dir;
}
#//Donchian Channel Trend Detection
script get_dc {
    input _length = 40;
    def len = if _length > 0 then _length else 1;
    def upper = Highest(high, len);
    def lower = Lowest(low, len);
    def s_dir = if high == upper and s_dir[1] != 1 then 1 else
                if low == lower and s_dir[1] != -1 then -1 else s_dir[1];
    plot lo = lower;
    plot up = upper;
    plot dir = s_dir;
}
#//Structure ChoCh Trend Detection
#Script get_choch {
def upper = Highest(high, chochLength);
def lower = Lowest(low, chochLength);
def os = if high[chochLength] > upper then 0 else
             if low[chochLength]  < lower then 1 else os[1];
def top = if (os == 0) and (os[1] != 0) then high[chochLength] else na;
def btm = if (os == 1) and (os[1] != 1) then low[chochLength] else na;
def t = !IsNaN(top);
def b = !IsNaN(btm);
def t_val;
def b_val;
def t_bar;
def b_bar;
def t_count; # = 1
def b_count; # = 1
def t_count1;
def b_count1;
def bar_index = BarNumber();
if t {
    t_bar = bar_index[chochLength];
    t_val = top;
    t_count1 = 1;
} else {
    t_bar = t_bar[1];
    t_val = if last then na else If(t_val[1], t_val[1], upper[chochLength]);
    t_count1 = t_count[1];
}
if b {
    b_bar = bar_index[chochLength];
    b_val = btm;
    b_count1 = 1;
} else {
    b_bar = b_bar[1];
    b_val = if last then na else If(b_val[1], b_val[1], lower[chochLength]);
    b_count1 = b_count[1];
}
def crosUp = (close > t_val) and (close[1] <= t_val[1]);
def crosDn = (close < b_val) and (close[1] >= b_val[1]);
def bull_choch = crosUp and t_count1 == 1;
def bear_choch = crosDn and b_count1 == 1;
def s_dir = if bull_choch and s_dir[1] != 1 then 1 else
            if bear_choch and s_dir[1] != -1 then -1 else s_dir[1];
def barEndUp;
def barEndDn;
def lineUp;
def lineDn;
def barStaUp;
def barStaDn;
if bull_choch and s_dir[1] != 1 {
    barStaUp    = t_bar;
    barEndUp = bar_index;
    barStaDn = na;
    barEndDn = na;
    lineUp = t_val;
    lineDn = lineDn[1];
    t_count = 0;
    b_count = b_count1;
} else if bear_choch and s_dir[1] != -1 {
    barStaUp = barStaUp[1];
    barEndUp = barEndUp[1];
    barStaDn = b_bar;
    barEndDn = bar_index;
    lineDn = b_val;
    lineUp = lineUp[1];
    t_count = t_count1;
    b_count = 0;
} else {
    barStaUp = barStaUp[1];
    barEndUp = barEndUp[1];
    barEndDn = na;
    barStaDn = na;
    lineDn = lineDn[1];
    lineUp = lineUp[1];
    t_count = t_count1;
    b_count = b_count1;
}
#//Trends
def minor_v1 = get_dc(minorTrendLength).lo;
def minor_v2 = get_dc(minorTrendLength).up;
def minor_dir = get_dc(minorTrendLength).dir;
def major_v1;
def major_v2;
def major_dir;
switch (majorTrendType) {
case "Moving Average" :
    major_v1 = get_ema(fastMovAvgLength, slowMovAvgLength, movAvgType).ema1;
    major_v2 = get_ema(fastMovAvgLength, slowMovAvgLength, movAvgType).ema2;
    major_dir = get_ema(fastMovAvgLength, slowMovAvgLength, movAvgType).dir;
case "ChoCh" :
    major_v1 = na;
    major_v2 = na;
    major_dir = s_dir;
case "Donchian Channel" :
    major_v1 = get_dc(DonchianChannelLength).lo;
    major_v2 = get_dc(DonchianChannelLength).up;
    major_dir = get_dc(DonchianChannelLength).dir;
default :
    major_v1 = supertrend(SupertrendFactor, SupertrendLength).st;
    major_v2 = supertrend(SupertrendFactor, SupertrendLength).st;
    major_dir = supertrend(SupertrendFactor, SupertrendLength).dir;
}
def emaType = majorTrendType == majorTrendType."Moving Average";
def minor_bear = minor_dir < 0;
def minor_bull = minor_dir > 0;
def major_bear = major_dir < 0;
def major_bull = major_dir > 0;
def major_col = if major_bear then -1 else if major_bull then 1 else 0;
def minor_col = if minor_bear then -1 else if minor_bull then 1 else 0;
def major_up = if emaType then major_v1 else (if major_bull then major_v1 else close);
def major_down = if emaType then major_v2 else (if major_bear then major_v2 else close);
def major_up_col = if emaType then major_col else (if major_bull then major_col else 0);
def major_dn_col = if emaType then major_col else (if major_bear then major_col else 0);

def chUp = if !choch then na else
           if bar_index == HighestAll(barStaUp) then high else
           if bar_index == HighestAll(barEndUp) then lineUp else if (close[1] > chUp[1]) then na else chUp[1];
def chDn = if !choch then na else
           if bar_index == HighestAll(barStaDn) then low else
           if bar_index == HighestAll(barEndDn) then lineDn else if (close[1] < chDn[1]) then na else chDn[1];
def AvgBarUp = Floor((barStaUp + barEndUp) / 2);
def AvgBarDn = Floor((barStaDn + barEndDn) / 2);


#/Plots
def c_top = Max(open, close); # //Top of candle
def c_bot = Min(open, close); # //Bottom of candle

plot ma1 = if major_up_col then major_up else na;     # "Major Uptrend"
plot ma2 = if major_dn_col then major_down else na; # "Major Downtrend"

ma1.AssignValueColor(if major_up_col > 0 then Color.GREEN else
                     if major_up_col < 0 then Color.RED else Color.GRAY);
ma2.AssignValueColor(if major_dn_col > 0 then Color.GREEN else
                     if major_dn_col < 0 then Color.RED else Color.GRAY);

AddCloud(if showClouds and !emaType then c_bot else na, ma1, Color.DARK_GREEN);
AddCloud(if showClouds and !emaType then ma2 else na, c_top, Color.DARK_RED);
AddCloud(if showClouds and emaType then ma1 else na, ma2, Color.DARK_GREEN, Color.DARK_RED);

#-- ChoChDn plot
plot chochUp = if choch and chUp then chUp else na;
plot chochDn = if choch and chDn then chDn else na;
chochUp.SetStyle(Curve.MEDIUM_DASH);
chochDn.SetStyle(Curve.MEDIUM_DASH);
chochUp.SetDefaultColor(Color.UPTICK);
chochDn.SetDefaultColor(Color.DOWNTICK);

AddChartBubble(choch and bar_index == HighestAll(AvgBarUp), chUp, "CHoCH", Color.UPTICK);
AddChartBubble(choch and bar_index == HighestAll(AvgBarDn), chDn, "CHoCH", Color.DOWNTICK, no);

AddCloud(if showClouds and choch then chochUp else na, c_top, Color.DARK_GREEN);
AddCloud(if showClouds and choch then c_bot else na, chochDn, Color.DARK_RED);
#//Color Bars
def barColor;
Switch (colorBarOptions) {
Case "Minor Trend" : barColor = minor_col;
Case "Major Trend" : barColor = major_col;
Default : barColor = 0;
}
AssignPriceColor(if !barColor then Color.CURRENT else
                  if barColor>0 then Color.CYAN else if barColor<0 then Color.MAGENTA else Color.GRAY);

# Patterns
#//Bearish
def dash = labelOptions==labelOptions."All Reversals Structure";
def topRe = labelOptions==labelOptions."Top Reversals Only";

def hammerTog   = BullishHammer;
def ihammerTog  = BullishInvertedHammer;
def bulleTog    = BullishEngulfing;
def r3Tog       = BullishRisingThree;
def twsTog      = BullishThreeWhiteSoldiers;
def mstarTog    = BullishMorningStar;
def bullhTog    = BullishHarami;
def btmTweezTog = BullishTweezerBottom;
def hmanTog     = BearishHangingMan;
def sstarTog    = BearishShootingStar;
def beareTog    = BearishEngulfing;
def f3Tog       = BearishFallingThree;
def tbcTog      = BearishThreeBlackCrows;
def estarTog    = BearishEveningStar;
def bearhTog    = BearishHarami;
def topTweezTog = BearishTweezerTop;

#//Candestick Patterns

def rc = close < open; # // Red Candle
def gc = close > open; # // Green Candle

#//Candle measurements

def hl_width = high - low; # //Total candle width (wick to wick)
def bod_width = (c_top - c_bot); # //Width of candle body (open to close)
def hw_per = ((high - c_top) / hl_width) * 100;
def lw_per = ((c_bot - low) / hl_width) * 100;
def b_per = (bod_width / hl_width) * 100;
def doji = RoundDown(close, 2) == RoundDown(open, 2);

#//Bullish patterns
def hCond = (lw_per > (b_per * 2) and b_per < 50 and hw_per < 2 and !doji);
def ihCond = (hw_per > (b_per * 2) and b_per < 50 and lw_per < 2 and !doji);
def r3Cond = (gc[4] and b_per[4] > 50) and
             (rc[3] and c_top[3] <= high[4] and c_bot[3] >= low[4]) and
             (rc[2] and c_top[2] <= high[4] and c_bot[2] >= low[4]) and
             (rc[1] and c_top[1] <= high[4] and c_bot[1] >= low[4]) and
             (gc and close > high[4] and b_per > 50);
def bullCond = (r3Cond and minor_bull[4]);
def bullEngCond = rc[1] and gc and (bod_width > (bod_width[1] / 2)) and
                 (open < close[1]) and c_top > c_top[1] and !bullCond and !doji[1];
def twsCond = (gc[2] and b_per[2] > 70) and
     (gc[1] and b_per[1] > 70 and c_bot[1] >= c_bot[2] and c_bot[1] <= c_top[2] and close[1] > high[2]) and
     (gc and b_per > 70 and c_bot >= c_bot[1] and c_bot <= c_top[1] and close > high[1]);
def msCond = (rc[2] and b_per[2] > 80) and
             (rc[1] and bod_width[1] < (bod_width[2] / 2) and open[1] < close[2]) and
             (gc and close > hl2[2]);
def bullHaramiCond = gc and (high <= c_top[1] and low >= c_bot[1]) and rc[1];
def tbCond = (RoundDown(low, 2) - RoundDown(low[1], 2)) == 0 and gc and rc[1];

def hammer_ = count(hCond, minor_bear).cond;
def hammer_per = count(hCond, minor_bear).per;
def hammer_cnt = count(hCond, minor_bear).cnt;

def inv_hammer = count(ihCond , minor_bear).cond;
def inv_hammer_per = count(ihCond, minor_bear).per;
def inv_hammer_cnt = count(ihCond, minor_bear).cnt;

def rising_3 = count(r3Cond, minor_bull[4]).cond;
def rising_3_per = count(r3Cond, minor_bull[4]).per;
def rising_3_cnt = count(r3Cond, minor_bull[4]).cnt;

def bull_engulfing = count(bullEngCond, minor_bear).cond;
def bull_engulfing_per = count(bullEngCond, minor_bear).per;
def bull_engulfing_cnt = count(bullEngCond, minor_bear).cnt;

def soldiers = count(twsCond, minor_bear[2]).cond;
def soldiers_per = count(twsCond, minor_bear[2]).per;
def soldiers_cnt = count(twsCond, minor_bear[2]).cnt;

def m_star = count(msCond, minor_bear[2]).cond;
def m_star_per = count(msCond, minor_bear[2]).per;
def m_star_cnt = count(msCond, minor_bear[2]).cnt;

def bull_harami = count(bullHaramiCond, minor_bear[1]).cond;
def bull_harami_per = count(bullHaramiCond, minor_bear[1]).per;
def bull_harami_cnt = count(bullHaramiCond, minor_bear[1]).cnt;

def tweezer_btm = count(tbCond, minor_bear[1]).cond;
def tweezer_btm_per = count(tbCond, minor_bear[1]).per;
def tweezer_btm_cnt = count(tbCond, minor_bear[1]).cnt;

#// Bearish patterns
def ssCond = (hw_per > (b_per * 2) and b_per < 50 and lw_per < 2 and !doji);
def hmCond = (lw_per > (b_per * 2) and b_per < 50 and hw_per < 2 and !doji);
def f3Cond = (rc[4] and b_per[4] > 50) and
             (gc[3] and c_top[3] <= high[4] and c_bot[3] >= low[4]) and
             (gc[2] and c_top[2] <= high[4] and c_bot[2] >= low[4]) and
             (gc[1] and c_top[1] <= high[4] and c_bot[1] >= low[4]) and
             (rc and close < low[4] and b_per > 50);
def bearCond = (f3Cond and minor_bear[4]);
def bearEngCond = gc[1] and rc and (bod_width > (bod_width[1] / 2)) and
                 (open > close[1]) and c_bot < c_bot[1] and !bearCond and !doji[1];
def tbcCond = (rc[2] and b_per[2] > 70) and (rc[1] and b_per[1] > 70 and
            c_top[1] <= c_top[2] and c_top[1] >= c_bot[2] and close[1] < low[2]) and
           (rc and b_per > 70 and c_top <= c_top[1] and c_top >= c_bot[1] and close < low[1]);
def esCond = (gc[2] and b_per[2] > 80) and
             (gc[1] and bod_width[1] < (bod_width[2] / 2) and open[1] > close[2]) and
             (rc and close < hl2[2]);
def bearHaramiCond = rc and (high <= c_top[1] and low >= c_bot[1]) and gc[1];
def ttCond = (RoundDown(high, 2) - RoundDown(high[1], 2))==0 and rc and gc[1];

def s_star = count(ssCond, minor_bull).cond;
def s_star_per = count(ssCond, minor_bull).per;
def s_star_cnt = count(ssCond, minor_bull).cnt;

def h_man = count(hmCond, minor_bull).cond;
def h_man_per = count(hmCond, minor_bull).per;
def h_man_cnt = count(hmCond, minor_bull).cnt;

def falling_3 = count(f3Cond, minor_bear[4]).cond;
def falling_3_per = count(f3Cond, minor_bear[4]).per;
def falling_3_cnt = count(f3Cond, minor_bear[4]).cnt;

def bear_engulfing = count(bearEngCond, minor_bull).cond;
def bear_engulfing_per = count(bearEngCond, minor_bull).per;
def bear_engulfing_cnt = count(bearEngCond, minor_bull).cnt;

def crows = count(tbcCond, minor_bull[2]).cond;
def crows_per = count(tbcCond, minor_bull[2]).per;
def crows_cnt = count(tbcCond, minor_bull[2]).cnt;

def e_star = count(esCond, minor_bull[2]).cond;
def e_star_per = count(esCond, minor_bull[2]).per;
def e_star_cnt = count(esCond, minor_bull[2]).cnt;

def bear_harami = count(bearHaramiCond, minor_bull[1]).cond;
def bear_harami_per = count(bearHaramiCond, minor_bull[1]).per;
def bear_harami_cnt = count(bearHaramiCond, minor_bull[1]).cnt;

def tweezer_top = count(ttCond, minor_bull[1]).cond;
def tweezer_top_per = count(ttCond, minor_bull[1]).cond;
def tweezer_top_cnt = count(ttCond, minor_bull[1]).cond;   

#// Bullish Stats

AddLabel(dash and hammer_per, "Hammer(" + hammer_per + "%)", Color.GREEN);
AddLabel(dash and inv_hammer_per, "Inverted Hammer(" + inv_hammer_per + "%)", Color.GREEN);
AddLabel(dash and bull_engulfing_per, "Bullish Engulfing(" + bull_engulfing_per + "%)", Color.GREEN);
AddLabel(dash and rising_3_per, "Rising 3(" + rising_3_per + "%)", Color.GREEN);
AddLabel(dash and soldiers_per, "3 White Soldiers(" + soldiers_per + "%)", Color.GREEN);
AddLabel(dash and m_star_per, "Morning Star(" + m_star_per + "%)", Color.GREEN);
AddLabel(dash and bull_harami_per, "Bull Harami(" + bull_harami_per + "%)", Color.GREEN);
AddLabel(dash and tweezer_btm_per, "Tweezer Bottom(" + tweezer_btm_per + "%)", Color.GREEN);
# // Bearish Stats
AddLabel(dash and h_man_per, "Hanging Man(" + h_man_per + "%)", Color.RED);
AddLabel(dash and s_star_per, "Shooting Star(" + s_star_per + "%)", Color.RED);
AddLabel(dash and bear_engulfing_per, "Bear Engulfing(" + bear_engulfing_per + "%)", Color.RED);
AddLabel(dash and falling_3_per, "Falling 3(" + falling_3_per + "%)", Color.RED);
AddLabel(dash and crows_per, "3 Black Crows(" + crows_per + "%)", Color.RED);
AddLabel(dash and e_star_per, "Evening Star(" + e_star_per + "%)", Color.RED);
AddLabel(dash and bear_harami_per, "Bear Harami(" + bear_harami_per + "%)", Color.RED);
AddLabel(dash and tweezer_top_per, "Tweezer Top(" + tweezer_top_per + "%)", Color.RED);


script Ranking {
    input v0 = 0;
    input v1 = 0;
    input v2 = 0;
    input v3 = 0;
    input v4 = 0;
    input v5 = 0;
    input v6 = 0;
    input v7 = 0;
    input v8 = 0;
    def r1 = v0 > v1;
    def r2 = v0 > v2;
    def r3 = v0 > v3;
    def r4 = v0 > v4;
    def r5 = v0 > v5;
    def r6 = v0 > v6;
    def r7 = v0 > v7;
    def r8 = v0 > v8;
    plot result = r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8 + 1;
}
#-- Bull
def ham = Ranking(hammer_per, hammer_per, inv_hammer_per, bull_engulfing_per, rising_3_per, soldiers_per, m_star_per, bull_harami_per, tweezer_btm_per);
def inv = Ranking(inv_hammer_per, hammer_per, inv_hammer_per, bull_engulfing_per, rising_3_per, soldiers_per, m_star_per, bull_harami_per, tweezer_btm_per);
def bEng = Ranking(bull_engulfing_per, hammer_per, inv_hammer_per, bull_engulfing_per, rising_3_per, soldiers_per, m_star_per, bull_harami_per, tweezer_btm_per);
def rsi3 = Ranking(rising_3_per, hammer_per, inv_hammer_per, bull_engulfing_per, rising_3_per, soldiers_per, m_star_per, bull_harami_per, tweezer_btm_per);
def sold = Ranking(soldiers_per, hammer_per, inv_hammer_per, bull_engulfing_per, rising_3_per, soldiers_per, m_star_per, bull_harami_per, tweezer_btm_per);
def mstar = Ranking(m_star_per, hammer_per, inv_hammer_per, bull_engulfing_per, rising_3_per, soldiers_per, m_star_per, bull_harami_per, tweezer_btm_per);
def bHar = Ranking(bull_harami_per, hammer_per, inv_hammer_per, bull_engulfing_per, rising_3_per, soldiers_per, m_star_per, bull_harami_per, tweezer_btm_per);
def TwzB = Ranking(tweezer_btm_per, hammer_per, inv_hammer_per, bull_engulfing_per, rising_3_per, soldiers_per, m_star_per, bull_harami_per, tweezer_btm_per);
#-- Bull cnt
def hamcnt = Ranking(hammer_cnt, hammer_cnt, inv_hammer_cnt, bull_engulfing_cnt, rising_3_cnt, soldiers_cnt, m_star_cnt, bull_harami_cnt, tweezer_btm_cnt);
def invcnt = Ranking(inv_hammer_cnt, hammer_cnt, inv_hammer_cnt, bull_engulfing_cnt, rising_3_cnt, soldiers_cnt, m_star_cnt, bull_harami_cnt, tweezer_btm_cnt);
def bEngcnt = Ranking(bull_engulfing_cnt, hammer_cnt, inv_hammer_cnt, bull_engulfing_cnt, rising_3_cnt, soldiers_cnt, m_star_cnt, bull_harami_cnt, tweezer_btm_cnt);
def rsi3cnt = Ranking(rising_3_cnt, hammer_cnt, inv_hammer_cnt, bull_engulfing_cnt, rising_3_cnt, soldiers_cnt, m_star_cnt, bull_harami_cnt, tweezer_btm_cnt);
def soldcnt = Ranking(soldiers_cnt, hammer_cnt, inv_hammer_cnt, bull_engulfing_cnt, rising_3_cnt, soldiers_cnt, m_star_cnt, bull_harami_cnt, tweezer_btm_cnt);
def mstarcnt = Ranking(m_star_cnt, hammer_cnt, inv_hammer_cnt, bull_engulfing_cnt, rising_3_cnt, soldiers_cnt, m_star_cnt, bull_harami_cnt, tweezer_btm_cnt);
def bHarcnt = Ranking(bull_harami_cnt, hammer_cnt, inv_hammer_cnt, bull_engulfing_cnt, rising_3_cnt, soldiers_cnt, m_star_cnt, bull_harami_cnt, tweezer_btm_cnt);
def TwzBcnt = Ranking(tweezer_btm_cnt, hammer_cnt, inv_hammer_cnt, bull_engulfing_cnt, rising_3_cnt, soldiers_cnt, m_star_cnt, bull_harami_cnt, tweezer_btm_cnt);
#-- bear
def hang = Ranking(h_man_per, h_man_per, s_star_per, bear_engulfing_per, falling_3_per, crows_per, e_star_per, bear_harami_per, tweezer_top_per);
def sstar = Ranking(s_star_per, h_man_per, s_star_per, bear_engulfing_per, falling_3_per, crows_per, e_star_per, bear_harami_per, tweezer_top_per);
def sEng = Ranking(bear_engulfing_per, h_man_per, s_star_per, bear_engulfing_per, falling_3_per, crows_per, e_star_per, bear_harami_per, tweezer_top_per);
def fall3 = Ranking(falling_3_per, h_man_per, s_star_per, bear_engulfing_per, falling_3_per, crows_per, e_star_per, bear_harami_per, tweezer_top_per);
def crow = Ranking(crows_per, h_man_per, s_star_per, bear_engulfing_per, falling_3_per, crows_per, e_star_per, bear_harami_per, tweezer_top_per);
def estar = Ranking(e_star_per, h_man_per, s_star_per, bear_engulfing_per, falling_3_per, crows_per, e_star_per, bear_harami_per, tweezer_top_per);
def sHar = Ranking(bear_harami_per, h_man_per, s_star_per, bear_engulfing_per, falling_3_per, crows_per, e_star_per, bear_harami_per, tweezer_top_per);
def TwzT = Ranking(tweezer_top_per, h_man_per, s_star_per, bear_engulfing_per, falling_3_per, crows_per, e_star_per, bear_harami_per, tweezer_top_per);
#-- bear Count
def hangcnt = Ranking(h_man_cnt, h_man_cnt, s_star_cnt, bear_engulfing_cnt, falling_3_cnt, crows_cnt, e_star_cnt, bear_harami_cnt, tweezer_top_cnt);
def sstarcnt = Ranking(s_star_cnt, h_man_cnt, s_star_cnt, bear_engulfing_cnt, falling_3_cnt, crows_cnt, e_star_cnt, bear_harami_cnt, tweezer_top_cnt);
def sEngcnt = Ranking(bear_engulfing_cnt, h_man_cnt, s_star_cnt, bear_engulfing_cnt, falling_3_cnt, crows_cnt, e_star_cnt, bear_harami_cnt, tweezer_top_cnt);
def fall3cnt = Ranking(falling_3_cnt, h_man_cnt, s_star_cnt, bear_engulfing_cnt, falling_3_cnt, crows_cnt, e_star_cnt, bear_harami_cnt, tweezer_top_cnt);
def crowcnt = Ranking(crows_cnt, h_man_cnt, s_star_cnt, bear_engulfing_cnt, falling_3_cnt, crows_cnt, e_star_cnt, bear_harami_cnt, tweezer_top_cnt);
def estarcnt = Ranking(e_star_cnt, h_man_cnt, s_star_cnt, bear_engulfing_cnt, falling_3_cnt, crows_cnt, e_star_cnt, bear_harami_cnt, tweezer_top_cnt);
def sHarcnt = Ranking(bear_harami_cnt, h_man_cnt, s_star_cnt, bear_engulfing_cnt, falling_3_cnt, crows_cnt, e_star_cnt, bear_harami_cnt, tweezer_top_cnt);
def TwzTcnt = Ranking(tweezer_top_cnt, h_man_cnt, s_star_cnt, bear_engulfing_cnt, falling_3_cnt, crows_cnt, e_star_cnt, bear_harami_cnt, tweezer_top_cnt);

def maxBull = Max(ham, Max(inv, Max(bEng, Max(rsi3, Max(sold, Max(mstar, Max(bHar, TwzB)))))));
def maxBear = Max(hang, Max(sstar, Max(sEng, Max(fall3, Max(crow, Max(estar, Max(sHar, TwzT)))))));
def cntBull = Max(hamcnt, Max(invcnt, Max(bEngcnt, Max(rsi3cnt, Max(soldcnt, Max(mstarcnt, Max(bHarcnt,TwzBcnt)))))));
def cntBear = Max(hangcnt,Max(sstarcnt, Max(sEngcnt, Max(fall3cnt, Max(crowcnt,Max(estarcnt, Max(sHarcnt,TwzTcnt)))))));

def hiBull = if maxBull == ham   then 8 else
             if maxBull == inv   then 7 else
             if maxBull == bEng  then 6 else
             if maxBull == rsi3  then 5 else
             if maxBull == sold  then 4 else
             if maxBull == mstar then 3 else
             if maxBull == bHar  then 2 else 1;
def hiBear = if maxBear == hang  then 8 else
             if maxBear == sstar then 7 else
             if maxBear == sEng  then 6 else
             if maxBear == fall3 then 5 else
             if maxBear == crow  then 4 else
             if maxBear == estar then 3 else
             if maxBear == sHar  then 2 else 1;
def cnBull = if cntBull == hamcnt   then 8 else
             if cntBull == invcnt   then 7 else
             if cntBull == bEngcnt  then 6 else
             if cntBull == rsi3cnt  then 5 else
             if cntBull == soldcnt  then 4 else
             if cntBull == mstarcnt then 3 else
             if cntBull == bHarcnt  then 2 else 1;
def cnBear = if cntBear == hangcnt  then 8 else
             if cntBear == sstarcnt then 7 else
             if cntBear == sEngcnt  then 6 else
             if cntBear == fall3cnt then 5 else
             if cntBear == crowcnt  then 4 else
             if cntBear == estarcnt then 3 else
             if cntBear == sHarcnt  then 2 else 1;

AddLabel(topRe and maxBull and hiBull == 8, "Hammer(" + hammer_per + "%)", Color.CYAN);
AddLabel(topRe and maxBull and hiBull == 7, "Inverted Hammer(" + inv_hammer_per + "%)", Color.CYAN);
AddLabel(topRe and maxBull and hiBull == 6, "Bullish Engulfing(" + bull_engulfing_per + "%)", Color.CYAN);
AddLabel(topRe and maxBull and hiBull == 5, "Rising 3(" + rising_3_per + "%)", Color.CYAN);
AddLabel(topRe and maxBull and hiBull == 4, "3 White Soldiers(" + soldiers_per + "%)", Color.CYAN);
AddLabel(topRe and maxBull and hiBull == 3, "Morning Star(" + m_star_per + "%)", Color.CYAN);
AddLabel(topRe and maxBull and hiBull == 2, "Bull Harami(" + bull_harami_per + "%)", Color.CYAN);
AddLabel(topRe and maxBull and hiBull == 1, "Tweezer Bottom(" + tweezer_btm_per + "%)", Color.CYAN);
# bear
AddLabel(topRe and maxBear and hiBear == 8, "Hanging Man(" + h_man_per + "%)", Color.MAGENTA);
AddLabel(topRe and maxBear and hiBear == 7, "Shooting Star(" + s_star_per + "%)", Color.MAGENTA);
AddLabel(topRe and maxBear and hiBear == 6, "Bear Engulfing(" + bear_engulfing_per + "%)", Color.MAGENTA);
AddLabel(topRe and maxBear and hiBear == 5, "Falling 3(" + falling_3_per + "%)", Color.MAGENTA);
AddLabel(topRe and maxBear and hiBear == 4, "3 Black Crows(" + crows_per + "%)", Color.MAGENTA);
AddLabel(topRe and maxBear and hiBear == 3, "Evening Star(" + e_star_per + "%)", Color.MAGENTA);
AddLabel(topRe and maxBear and hiBear == 2, "Bear Harami(" + bear_harami_per + "%)", Color.MAGENTA);
AddLabel(topRe and maxBear and hiBear == 1, "Tweezer Top(" + tweezer_top_per + "%)", Color.MAGENTA);

#//Bullish patterns
AddChartBubble(hammerTog and hammer_ and !hammer_[1] and major_bull, low, "H",
                if cnBull == 8 then Color.CYAN else Color.GREEN, no);
AddChartBubble(ihammerTog and inv_hammer and !inv_hammer[1] and major_bull, low, "IH",
                if cnBull == 7 then Color.CYAN else Color.GREEN, no);
AddChartBubble(bulleTog and bull_engulfing and !bull_engulfing[1] and major_bull, low, "EG",
                if cnBull == 6 then Color.CYAN else Color.GREEN, no);
AddChartBubble(r3Tog and rising_3 and !rising_3[1] and major_bull, low, "R3",
                if cnBull == 5 then Color.CYAN else Color.GREEN, no);
AddChartBubble(twsTog and soldiers and !soldiers[1] and major_bull, low, "3WS",
                if cnBull == 4 then Color.CYAN else Color.GREEN, no);
AddChartBubble(mstarTog and m_star and !m_star[1] and major_bull, low, "MS",
                if cnBull == 3 then Color.CYAN else Color.GREEN, no);
AddChartBubble(bullhTog and bull_harami and !bull_harami[1] and major_bull, low, "H",
                if cnBull == 2 then Color.CYAN else Color.GREEN, no);
AddChartBubble(btmTweezTog and tweezer_btm and !tweezer_btm[1] and major_bull, low, "TB",
                if cnBull == 1 then Color.CYAN else Color.GREEN, no);

#//Bearish patterns
AddChartBubble(hmanTog and h_man and !h_man[1] and major_bear, high, "HM",
               if cnBear == 8 then Color.MAGENTA else Color.RED);
AddChartBubble(sstarTog and s_star and !s_star[1] and major_bear, high, "SS",
               if cnBear == 7 then Color.MAGENTA else Color.RED);
AddChartBubble(beareTog and bear_engulfing and !bear_engulfing[1] and major_bear, high, "EG",
               if cnBear == 6 then Color.MAGENTA else Color.RED);
AddChartBubble(f3Tog and falling_3 and !falling_3[1] and major_bear, high, "F3",
               if cnBear == 5 then Color.MAGENTA else Color.RED);
AddChartBubble(tbcTog and crows and !crows[1] and major_bear, high, "3BC",
               if cnBear == 4 then Color.MAGENTA else Color.RED);
AddChartBubble(estarTog and e_star and !e_star[1] and major_bear, high, "ES",
               if cnBear == 3 then Color.MAGENTA else Color.RED);
AddChartBubble(bearhTog and bear_harami and !bear_harami[1] and major_bear, high, "H",
               if cnBear == 2 then Color.MAGENTA else Color.RED);
AddChartBubble(topTweezTog and tweezer_top and !tweezer_top[1] and major_bear, high, "TT",
               if cnBear == 1 then Color.MAGENTA else Color.RED);
# end of code
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
346 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