1-2-3 Pattern Expo For ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
GDse5Tk.png

Try this. NOT EXACT Conversion but can give good 1-2-3 patterns. More over, I included unbroken 1-2-3 patterns as well in dark yellow.
You can use the indicator to show swing highs and lows also.

CODE
CSS:
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
#// © Zeiierman
#indicator("1-2-3 Pattern (Expo)",overlay=true,max_bars_back=5000,max_labels_count=500,max_lines_count=500)
# Converted by Sam4Cok@Samer800 - 12/2022 - Not Typlical

input Period = 10;
input BreakSignalStyle = {Default Bubbles, Arrows, None}; # "Show Break"
input ShowBreakLines = yes;
input BrokenPattern = yes;        #,"Show Pattern"
input UnbrokenPattern = yes;      #,"Show Unconfirmed Pattern"
input showPivots  = no;           #,"Show Pivots"
input ZigzagLine  = no;
input wicks = yes;

def na = Double.NaN;
def Style = if BreakSignalStyle==BreakSignalStyle.Bubbles then 1 else
            if BreakSignalStyle==BreakSignalStyle.Arrows then -1 else 0;
script fixnan{
    input source = close;
    def fix = if !isnan(source) then source else fix[1];
    plot result = fix;
}
script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL = 5;    # default Pivot Lookback Left
    input lbR = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
def src1;
def src2;
if wicks {
    src1 = high;
    src2 = low;
 } else {
    if !wicks {
    src1 = close;
    src2 = close;
    } else {
    src1 = src1[1];
    src2 = src2[1];
}}
def bar   = if BarNumber() < 3 then na else BarNumber();
def pvtHi = FindPivots(src1, 1, Period, Period);
def pvtLo = FindPivots(src2,-1, Period, Period);
def hh; def ll; def pos;
def Pvt0; def Pvt1; def Pvt2; def idx0;def idx1; def idx2;
if !IsNaN(pvtHi) and pos[1] <= 0 {
    hh = src1;
    ll = ll[1];
    Pvt0 = src1;
    Pvt1 = Pvt0[1];
    pvt2 = Pvt1[1];
    idx0 = bar;
    idx1 = idx0[1];
    idx2 = idx1[1];
    pos = 1;
} else {
    if !IsNaN(pvtLo) and pos[1] >= 0 {
        hh = hh[1];
        ll = src2;
        Pvt0 = src2;
        Pvt1 = Pvt0[1];
        pvt2 = Pvt1[1];
    idx0 = Bar;
    idx1 = idx0[1];
    idx2 = idx1[1];
        pos = -1;
    } else {
        hh = na;
        ll = na;
        Pvt0 = Pvt0[1];
        Pvt1 = Pvt1[1];
        pvt2 = pvt2[1];
    idx0 = idx0[1];
    idx1 = idx1[1];
    idx2 = idx2[1];
        pos = pos[1];
    }
}
def Pvts0 = Pvt0;
def Pvts1 = Pvt1;
def Pvts2 = Pvt2;
def Preidx0; def Preidx1;def Preidx2;
if (pvts0>pvts2) and (pvts0<pvts1) {
    Preidx0 = idx0;
    Preidx1 = idx1;
    Preidx2 = idx2;
       } else {
if (pvts0<pvts2) and (pvts0>pvts1) {
    Preidx0 = idx0;
    Preidx1 = idx1;
    Preidx2 = idx2;
       } else {
    Preidx0 = Preidx0[1];
    Preidx1 = Preidx1[1];
    Preidx2 = Preidx2[1];
}}
def topPvt  = !isNaN(hh);
def BotPvt  = !isNaN(ll);
def highPvt = if !isNaN(hh) then hh else highPvt[1];
def lowPvt  = if !isNaN(ll) then ll else lowPvt[1];
def pivot   = if topPvt then highPvt else if BotPvt then lowPvt else na;
def Pvtfix  = fixnan(pivot);
def lastbar = IsNaN(close[-1]) and !IsNaN(close);

def crosUp = src1 > pvts1;
def crosDn = src2 < pvts1;
def UpCount = if crosUp then UpCount[1] + 1 else 0;
def DnCount = if crosDn then DnCount[1] + 1 else 0;

def pattern; def labelH; def labelL;def idxs0; def idxs1;def idxs2;
if crosUp==1 and pattern[1] {
    if (pvts0>pvts2) and (pvts0<pvts1) {
           labelH = pvts1;
           labelL = labelL[1];
    idxs0 = idx0;
    idxs1 = idx1;
    idxs2 = idx2;
           pattern = no;
       } else {
           labelH = labelH[1];
           labelL = labelL[1];
    idxs0 = idxs0[1];
    idxs1 = idxs1[1];
    idxs2 = idxs2[1];
           pattern = pattern[1];}
       } else {
if crosDn==1 and pattern[1] {
    if (pvts0<pvts2) and (pvts0>pvts1) {
           labelH = labelH[1];
           labelL = pvts1;
    idxs0 = idx0;
    idxs1 = idx1;
    idxs2 = idx2;
           pattern = no;
       } else {
           labelH = labelH[1];
           labelL = labelL[1];
    idxs0 = idxs0[1];
    idxs1 = idxs1[1];
    idxs2 = idxs2[1];
           pattern = pattern[1];}
       } else {
           labelH = na;
           labelL = na;
    idxs0 = idxs0[1];
    idxs1 = idxs1[1];
    idxs2 = idxs2[1];
           pattern = if pvts1!=pvts1[1] then yes else pattern[1];
}}
def PreBreak = CompoundValue(1,if(highestAll(Preidx1)==bar,if(pos>0,src1,src2),PreBreak[1]),if(pos>0,src1,src2));

plot preBreakLine = if !UnbrokenPattern or isNaN(close) then na else if PreBreak==PreBreak[1] then PreBreak else na;
preBreakLine.SetDefaultColor(CreateColor(181,139,0));
preBreakLine.SetStyle(Curve.SHORT_DASH);

def UnPatLine = if !UnbrokenPattern then na else if highestAll(Preidx2)==bar or highestAll(Preidx1)==bar or
                   highestAll(Preidx0)==bar then if pos>0 then src1 else src2 else na;
plot UnConfPatLine = UnPatLine;
UnConfPatLine.SetDefaultColor(CreateColor(181,139,0));
UnConfPatLine.EnableApproximation();

def PatLine = if !BrokenPattern then na else if highestAll(idxs2)==bar or highestAll(idxs1)==bar or highestAll(idxs0)==bar
               then if pos>0 then src1 else src2 else na;
plot ConfPatLine = PatLine;
ConfPatLine.SetDefaultColor(Color.GRAY);
ConfPatLine.EnableApproximation();

def condition = highestAll(idxs1)==bar;
def offset = fold j = 0 to 50  with p while p < 2 do
              p + (if p == 1 then j - 1 else if GetValue(condition, j) then 1 else 0);
def Hiprice = GetValue(if pos>0 then src1 else src2, offset-1);

plot Line = if !BrokenPattern or isNaN(close) or Hiprice!=Hiprice[1] then na else Hiprice;
Line.SetDefaultColor(Color.GRAY);
Line.SetStyle(Curve.LONG_DASH);

def LineH; def LineL;
LineH = CompoundValue(3, if !isNaN(labelH) then labelH else LineH[1], labelH);
def HlineCount = if LineH==LineH[1] then HlineCount[1] + 1 else 0;
plot HiLine = if isNaN(close) or !ShowBreakLines or src1<LineH or HlineCount>50 then na else LineH;
HiLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HiLine.SetDefaultColor(Color.CYAN);

LineL = CompoundValue(3, if !isNaN(labelL) then labelL else LineL[1], labelL);
def LlineCount = if LineL==LineL[1] then LlineCount[1] + 1 else 0;
plot LoLine = if isNaN(close) or !ShowBreakLines or src2>LineL or LlineCount>50 then na else LineL;
LoLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LoLine.SetDefaultColor(Color.MAGENTA);

#-- ZigZagHighLow Line
def colcond = if IsNaN(pivot) then colcond[1] else if pivot < Pvtfix[1] then 0 else 1;
plot pivot_zigzag = if !ZigzagLine then na else if lastbar then close else pivot;
pivot_zigzag.EnableApproximation();
pivot_zigzag.AssignValueColor(if colcond[1] then Color.LIGHT_RED else Color.LIGHT_GREEN);

#--- Arrows
plot ArrowHi = if Style<0 and crosUp and !isNaN(labelH) then low else na;
ArrowHi.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
ArrowHi.SetDefaultColor(Color.WHITE);
plot ArrowLo = if Style<0 and crosDn and !isNaN(labelL) then high else na;
ArrowLo.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ArrowLo.SetDefaultColor(Color.YELLOW);

#-- Chart Bubbles - Confirmed
AddChartBubble(BrokenPattern and highestAll(idxs2)==bar,if(pos>0,src1,src2),1,Color.GRAY,if(pos>0,yes,no));
AddChartBubble(BrokenPattern and highestAll(idxs1)==bar,if(pos>0,src1,src2),2,Color.GRAY,if(pos>0,yes,no));
AddChartBubble(BrokenPattern and highestAll(idxs0)==bar,if(pos>0,src1,src2),3,Color.GRAY,if(pos>0,yes,no));
#-- Chart Bubbles - UnConfirmed
AddChartBubble(UnbrokenPattern and highestAll(Preidx2)==bar,if(pos>0,src1,src2),1,CreateColor(181,139,0),if(pos>0,yes,no));
AddChartBubble(UnbrokenPattern and highestAll(Preidx1)==bar,if(pos>0,src1,src2),2,CreateColor(181,139,0),if(pos>0,yes,no));
AddChartBubble(UnbrokenPattern and highestAll(Preidx0)==bar,if(pos>0,src1,src2),3,CreateColor(181,139,0),if(pos>0,yes,no));
#--- Breaks
AddChartBubble(Style>0 and crosUp and labelH, labelH, "Break", Color.CYAN, no);
AddChartBubble(Style>0 and crosDn and labelL, labelL, "Break", Color.MAGENTA, yes);
#-- Highs and lows
AddChartBubble(showPivots and hh and hh> pvts1[1],hh,"HH", Color.GREEN, yes);
AddChartBubble(showPivots and hh and hh<=pvts1[1],hh,"LH", Color.DARK_GREEN, yes);
AddChartBubble(showPivots and ll and ll> pvts1[1],ll,"HL", Color.DARK_RED, no);
AddChartBubble(showPivots and ll and ll<=pvts1[1],ll,"LL", Color.RED, no);


#--- END 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
173 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