Repaints Harmonic Patterns Indicator for ThinkorSwim (ZigZag, Bat, Butterfly, ABCD)

Repaints
HOP is not in my harmonic indicator so you must have a different one than me
Mine is filled the way I think you are talking about
 

Volatility Trading Range

VTR is a momentum indicator that shows if a stock is overbought or oversold based on its Weekly and Monthly average volatility trading range.

Download the indicator

I tried the same study as you've done with the same WMT(though a different time frame), but it didn't show the nice chart as you have.
Any reason for that?!

5P5Scqv.png
 
There seems to be alot interests with this so I hope my excel sheet helps. https://docs.google.com/spreadsheets/d/1A47lwKou2XPC4evVKVe5IIs3dJYDjaV4NCXTQItGF4Q/edit?usp=sharing

There are multiple calculator, fibs, harmonic pattern, which is a bit sloppy as i was in a hurry, Futures and Equities Risk, and a Harmonic pattern cheat sheet. Except for the harmonic pattern calculator, they are open source from hours of internet searches. I have only found the risk piece of extremely helpful as it doesn't require further research to understand. Other coders could easily program labels using a zigzag or other pivots which rotate into a zz. I posted on such script in another thread. Hope it's of use.

-mcdon030
 
Hello everyone, I found this very interesting study that combines Harmonic Patterns and Projects Fibs depending on Time Frame on the thinkscript cloud. However, something has to be wrong with it because it does not always work. It is not consistent and It comes and goes as you scroll the screen to the right or left and even when changing time frames.

Here is the link I found:

http://tos.mx/XP685Y

It will be great if someone can take a stab at it and iron out its kinks
 
@Ninja Bull Please post the Thinkscript code itself, using the </> icon, not just a shared link... Members should not be required to install the code in their Thinkorswim platforms in order to review the code and provide support... You can just edit your existing post... I'll check it once posted...
 
Code:
###  Resources:
## 1). interpretation of "Scientific Guide to Price Action and Pattern Trading" Author: Young Ho Seo
## 2). https://www.mql5.com/en/articles/1470
## 3). Semafor_3Level_ZZ
### TOS Script resources:
## 1). initial pivot from Mobius Pivot Array
## 2). folds from Dmonkey Examples && JQ's one note
## 3). Mobius Trend Pivots shared 2/4/2019
## 4). ZZtop JQ onenote Author unidentified
## 5). #StudyName: Dilbert_1st2nd3rdOrderPivotFibs_V4
## script guide:
##  1). Inputs:
##    a.  first logic cycles pivots to ZZ:  lines 140-144
##    b.  HHLL script cycles higher highs until a lower low occurs and vice versa  lines 88-130
##    c.  show 1rst,2nd and 3rd bubble show Double bottom, Double Top, Resistance, support and rocketRSI color  : lines 264-281
##    d.  countbetween counts pivots between higher pivots : lines 380-388
## 2). fib relationships  with higher to lower pivots  : lines 299-379
## 3).  : lines 389-374
##    a. fib projection initiates when 3rd order pivot equals 2nd order pivot
##    b. projection order input is similiar to harmonic XABCD trading
##       - twothree equals BC trend with fib levels CD
##       - threefour equals AB trend with fib levels BC
##       - fourfive equals XA trend with fib levels AB
### issues and needed additions:
## 1). fibrelationships to higher pivots need work
## 2). projections may need extended lines when price fails to breach High and low
## 3). ***** need scan for active projection bar  ...  help
## 4). needs over bought and over sold trend lines
## 5). needs linear lines for ascending and descending trends
## 6).  i have a condensed version of a patternfinder(trangles, doubletops, doublebottoms, head and shoulders, wedge,etc) which paints required trendlines if needed, request..
input n = 4;
input tol = .04; ## ratio tolerance
input show1rstzz = yes;
input show2ndzz = yes;
input show3rdzz = yes;
input show1rstbubble = no;
input show2ndbubble = yes;
input show3rdbubble =yes;
input showFibpivotbubble = no;
input showFibrelationbubble = no;
input showprojection = no;
input showHHLLcount = no;
input countbetween = yes;
def bn = BarNumber();
def lbar = HighestAll( if IsNaN(close) then bn else 0);
script fib {
    input v1 = 0;
    input v2 = 0;
    input v3 = 0;
    input tol = 0;
    def ratio = Round((AbsValue(v1 - v2) / AbsValue(v2 - v3)), 2);
## Standard numbers: 0-0.146-0.236-0.382-0.5-0.618-0.764-0.854-1.0-1.236-1.618-2.618-4. 236-6.854
### Pesavento numbers: 0-0.382-0.5-0.618-0.707-0.786-0.854-0.886-1.0-1.128-1.272-1. 414-1.618-2.0-2.414-2. 618-4.0
### Bryce gilmore:  0.25 - 0.382 - 0.5 - 0.618 - 0.667 - 0.786 - 1.0 - 1.272 - 1.618 - 1.732 - 1.75 - 2.0 - 2.236 - 2.5 - 2.618 - 3.0 - 3.33 - 4.236 - 6.854
    def retrace = Between(ratio, (0.146 - tol), (0.146 + tol)) or Between(ratio, (.236 - tol), (.236 + tol)) or Between(ratio, (.382 - tol), (.382 + tol)) or
    Between(ratio, (.50 - tol), (.50 + tol)) or Between(ratio, (.618 - tol), (.618 + tol)) or  Between(ratio, (.667 - tol), (.667 + tol)) or Between(ratio, (.707 - tol), (.707 + tol)) or
    Between(ratio, (.764 - tol), (.764 + tol)) or Between(ratio, (.786 - tol), (.786 + tol)) or Between(ratio, (.854 - tol), (.854 + tol)) or Between(ratio, (.886 - tol), (.886 + tol)) or
    Between(ratio, (1 - tol), (1 + tol)) or Between(ratio, (1.236 - tol), (1.236 + tol)) or Between(ratio, (1.41 - tol), (1.414 + tol)) or Between(ratio, (1.618 - tol), (1.618 + tol)) or
    Between(ratio, (1.732 - tol), (1.732 + tol)) or Between(ratio, (1.75 - tol), (1.75 + tol)) or Between(ratio, (2.00 - tol), (2.000 + tol)) or Between(ratio, (2.236 - tol), (2.236 + tol)) or
    Between(ratio, (2.414 - tol), (2.414 + tol)) or Between(ratio, (2.50 - tol), (2.50 + tol)) or Between(ratio, (2.618 - tol), (2.618 + tol))  or Between(ratio, (3 - tol), (3 + tol)) or
    Between(ratio, (3.33 - tol), (3.33 + tol)) or Between(ratio, (4 - tol), (4 + tol)) or Between(ratio, (4.236 - tol), (4.236 + tol))  or Between(ratio, (6.85 - tol), (6.85 + tol));
    plot one = if IsNaN(0) then  0 else retrace ;
    def getratio = if one then ratio else Double.NaN;
}
script F {
    input value = close ;
    def bn = BarNumber();
    def lbar = HighestAll(if IsNaN(close) then 0 else bn);
    plot xd = fold di = 1 to lbar with da = Double.NaN while IsNaN(da) do if GetValue(value, -di) then di else Double.NaN;
}
script paint {
    input v1 = 0.0 ;
    input v2 = 0.0;
    def bn = BarNumber();
    def lv1bar = HighestAll(if v1 then bn else 0);
    def lv2bar = HighestAll(if v2 then bn else 0);
    def lbar = HighestAll(if IsNaN(close) then 0 else bn);
    plot va1 = if bn >= lv1bar then 0 else fold id = 1 to lbar with t = 0 while t == 0 do if GetValue(v1, -id) then id else 0;
    plot va2 = if bn >= lv2bar  then 0 else fold sd = 1 to lbar with x = 0 while x == 0 do if GetValue(v2, -sd) then sd else 0;
    def for =  GetValue(v2, -va1);
    def for2 =  GetValue(for, - va1);
    plot for3 =  GetValue(for2, - va1);
    plot fva1 =  GetValue(high, -va1);
    plot fva2 = GetValue(low, -va2);
}
#### plot highest high trend until low trend and vice versa
script HHLL {
    input fractall= 0.0;
    input fractalh = 0.0;
def Fractallv = if fractall then low else Fractallv[1];
def Fractalhv = if fractalh then high else Fractalhv[1];
def bn = BarNumber();
def lhighestbar = highestall( if FractalL then bn else 0);
def hhighestbar= highestall( if Fractalh then bn else 0);
def lbar = HighestAll(if IsNaN(close) then 0 else bn);
                def fr1lowc1 = compoundValue(1,if Fractall && Fractallv<=Fractallv[1] then fr1lowc1 [1]+1 else if Fractall && Fractallv>Fractallv[1] then 0 else fr1lowc1 [1],0);
                def fr1highc1= compoundValue(1,if Fractalh && FractalHv>=FractalHv[1] then fr1highc1[1]+1 else if Fractalh && FractalHv<FractalHv[1] then 0 else fr1highc1[1],0);
                def maxl = if Fractall && fr1lowc1>=1  && lhighestbar  then 1 else Double.NaN;
                def maxh = if Fractalh && fr1highc1>=1&& hhighestbar then 1 else Double.NaN;
                def maxlv = if  !isnan(maxl) then low else  maxlv[1];
                def maxhv=  if  !isnan(maxh) then high  else maxhv[1];
                def maxlhighestbar = highestall( if !isnan(maxl) then bn else 0);
                def maxhhighestbar= highestall( if !isnan(maxh)then bn else 0);
                def maxlct =  compoundValue(1,if   !isnan(maxh[1]) then 0 else if (Fractall && !isnan(maxl)) then  maxlct[1]+1 else maxlct[1],0);
                def maxhct = compoundValue(1, if  !isnan(maxl[1]) then 0 else if (Fractalh && !isnan(maxh)) then  maxhct[1]+1 else maxhct[1],0);
                def maxld = fold dig = 1 to lbar with dag = Double.NaN while IsNaN(dag) do if GetValue(maxl, -dig) then dig else Double.NaN;
                def maxhd = fold die = 1 to lbar with dae = Double.NaN while IsNaN(dae) do if GetValue(maxh, -die) then die else Double.NaN;
def LLja  = if bn>=maxlhighestbar &&maxl then 1 else 
                (if !isnan(maxl) && getvalue(maxlct,(-maxld))>1 && getvalue(low,(-maxld))< maxlv or
                !isnan(maxl) && getvalue(maxlct,(-maxld))>maxlct && maxlv < maxlv[1] or
                !isnan(maxl) &&  maxlct>1 && maxlv> maxlv[1]or
                !isnan(maxh) then  double.nan else if
                !isnan(maxl) &&  maxlct<=1  && getvalue(maxlct,(-maxld))==1 or
                !isnan(maxl) && getvalue(maxlct,(-maxld))>maxlct && getvalue(low,(-maxld))> maxlv or
                !isnan(maxl) &&  maxlct>=1  && getvalue(maxhct,(-fractalh))>=1 then 1 else 0 ) ;
def hhja  = if bn>=maxhhighestbar  then 1 else         
              (if !isnan(maxh) && getvalue(maxhct,(-maxhd))>maxhct &&  getvalue(high,(-maxhd))> maxhv or
               !isnan(maxh) && getvalue(maxhct,(-maxhd))>maxhct && maxhv > maxhv[1] or
              !isnan(maxh) &&  maxhct>1 &&  maxhv< maxhv[1]or
               !isnan(maxl) then  double.nan else if
               !isnan(maxh) &&  maxhct<=1 && getvalue(maxhct,(-maxhd))==1 or
               !isnan(maxh) && getvalue(maxhct,(-maxhd))>maxhct && getvalue(high,(-maxhd))< maxhv or
                 !isnan(maxh) &&  maxhct>=1 && getvalue(maxlct,(-maxld))>=1 then 1 else 0 ) ; 
def LLJb =  (!isnan(maxl) && Fractall && !isnan(LLja)) ;
def HHJb =  (!isnan(maxh) && Fractalh && !isnan(HHja )) ;
def LLJc= if isnan(LLJb) then 0 else  (LLJb ) ;
def  HHJc= if isnan(HHJb) then 0 else  (HHJb ) ;
plot hhj =   if IsNaN(close) then 0 else( HHjc ) ;
plot llj  =  if IsNaN(close) then 0 else ( LLjc ) ;
}
## mobius pivots
def hh = fold i = 1 to n with p = 1 while p do high > GetValue(high, -i);
def PivotH = if (bn > n and   high == Highest(high, n) and     hh)   then high   else Double.NaN;
def ll = fold jk = 1 to n with q = 1 while q  do low < GetValue(low, -jk);
def PivotL = if (bn > n and   low == Lowest(low, n) and  ll) then low  else Double.NaN;
def fractalhigh1 = if IsNaN(PivotH ) then 0 else PivotH ;
def fractallow1 = if IsNaN(PivotL ) then 0 else PivotL ;
def fractc = CompoundValue(1, if IsNaN(close) then Double.NaN  else if fractalhigh1 then fractc[1] + 1 else if fractallow1  then fractc[1] + 1 else fractc[1], 1);
#### offset rotation to ZZ
def frh2v2noffset8 = if fractallow1[1] then high else if high > frh2v2noffset8[1] then high else if  fractalhigh1 then Double.NaN else if high >= frh2v2noffset8[1] then high else  frh2v2noffset8[1];
def frl2v2noffset8 = if  fractalhigh1[1] then low else if low < frl2v2noffset8[1] then low else if  fractallow1  then Double.NaN else if low <= frl2v2noffset8[1]  then low else  frl2v2noffset8[1];
def frh2v2noffsetv = if high==getvalue(frh2v2noffset8[1],-f(fractallow1)) then 1 else double.nan;
def frl2v2noffsetv = if low==getvalue(frl2v2noffset8[1],-f(fractalhigh1)) then 1 else double.nan;
#### 1rst order pivots
def Fractalh = if IsNaN(close)  then 0 else (fractalhigh1 or !isnan(frh2v2noffsetv));
def FractalL =  if  IsNaN(close) then 0 else (fractallow1  or  !isnan(frl2v2noffsetv));
def pivotcal = CompoundValue(1, if Fractalh then pivotcal [1] + 1 else if FractalL then pivotcal [1] + 1 else pivotcal [1], 1);
def FractalHc = CompoundValue(1, if Fractalh[1] then 1 else FractalHc[1] + 1, 1);
def FractalLc = CompoundValue(1, if FractalL[1] then 1 else FractalLc[1] + 1, 1);
#### 2nd order pivots
def FractalHv  = if Fractalh  then high else FractalHv[1];
def Fractallv = if FractalL  then low else Fractallv[1];
def prvFrH1 = if FractalHv <> FractalHv[1] then FractalHv[1] else prvFrH1[1];
def prvFrH2 = if FractalHv <> FractalHv[1] then prvFrH1 [1] else prvFrH2[1];
def prvFrH3 = if FractalHv <> FractalHv[1] then prvFrH2 [1] else prvFrH3[1];
def prvFrH4 = if FractalHv <> FractalHv[1] then prvFrH3 [1] else prvFrH4[1];
def FrHbn  = CompoundValue(1,  if Fractalh then bn else FrHbn[1], bn);
def FrHbn1 =  if  FrHbn > FrHbn[1] then FrHbn[1] else FrHbn1[1];
def FrHbn2 = GetValue(FrHbn1, FractalHc);
def FrHbn3 = GetValue(FrHbn2, FractalHc);
def FrHbn4 = GetValue(FrHbn3, FractalHc);
def FrHbn5 = GetValue(FrHbn4, FractalHc);
def FrHbn6 = GetValue(FrHbn5, FractalHc);
def prvFrL1 =  if Fractallv <> Fractallv[1] then Fractallv[1] else prvFrL1[1];
def prvFrL2 =  if Fractallv <> Fractallv[1] then prvFrL1[1] else prvFrL2[1];
def prvFrL3 =   if Fractallv <> Fractallv[1] then prvFrL2[1] else prvFrL3[1];
def prvFrL4 =   if Fractallv <> Fractallv[1] then prvFrL3[1] else prvFrL4[1];
def prvFrL5 =  if Fractallv <> Fractallv[1] then prvFrL4[1] else prvFrL5[1];
def Frlbn = CompoundValue(1,  if FractalL then bn else Frlbn[1], bn);
def FrLbn1 =  if  Frlbn > Frlbn[1] then Frlbn[1] else FrLbn1[1];
def FrLbn2 =  GetValue(FrLbn1, FractalLc);
def FrLbn3 = GetValue(FrLbn2, FractalLc);
def FrLbn4 = GetValue(FrLbn3, FractalLc);
def FrLbn5 = GetValue(FrLbn4, FractalLc);
#### 3rd  order pivots
def FractalH2nd =  HHLL(fractall,fractalh ).hhj;
def FractalL2nd  =  HHLL(fractall,fractalh ).llj ;
def pivotcal2nd  = CompoundValue(1,  if FractalH2nd then pivotcal2nd  [1] + 1 else if FractalL2nd  then pivotcal2nd[1] + 1 else pivotcal2nd[1], 1);
def Fractalh2ndc = CompoundValue(1, if FractalH2nd[1] then 1 else Fractalh2ndc[1] + 1, 1);
def Fractall2ndc = CompoundValue(1, if FractalL2nd [1] then 1 else Fractall2ndc[1] + 1, 1);
def FractalH2ndv  = if FractalH2nd  then high else FractalH2ndv[1];
def Fractall2ndv = if FractalL2nd  then low else Fractall2ndv[1];
def prvFr2dnH1 =  if  FractalH2ndv <>  FractalH2ndv[1] then  FractalH2ndv[1] else prvFr2dnH1[1];
def prvFr2dnH2 = if  prvFr2dnH1 <>  prvFr2dnH1[1] then  prvFr2dnH1[1] else prvFr2dnH2[1];
def prvFr2dnH3 =  if  prvFr2dnH2 <>  prvFr2dnH2[1] then  prvFr2dnH2[1] else prvFr2dnH3[1];
def prvFr2dnH4 =  if  prvFr2dnH3 <>  prvFr2dnH3[1] then  prvFr2dnH3[1] else prvFr2dnH3[1];
def prvFr2dnH5 = if  prvFr2dnH4 <>  prvFr2dnH4[1] then  prvFr2dnH4[1] else prvFr2dnH4[1];
def prvFr2dnL1 = if  FractalH2ndv <>  FractalH2ndv[1] then  FractalH2ndv[1] else prvFr2dnL1 [1];
def prvFr2dnL2 = if   prvFr2dnL1 <>   prvFr2dnL1[1] then   prvFr2dnL1[1] else  prvFr2dnL2 [1];
def prvFr2dnL3 =if   prvFr2dnL2 <>   prvFr2dnL2[1] then   prvFr2dnL2[1] else  prvFr2dnL3 [1];
def prvFr2dnL4 =if   prvFr2dnL3 <>   prvFr2dnL3[1] then   prvFr2dnL3[1] else  prvFr2dnL4 [1];
def prvFr2dnL5 =if   prvFr2dnL4 <>   prvFr2dnL4[1] then   prvFr2dnL4[1] else  prvFr2dnL5 [1];
def FrH2ndbn  = CompoundValue(1,  if FractalH2nd  then bn else FrH2ndbn[1], bn);
def FrH2nd2bn  = GetValue(bn, Fractalh2ndc[1]);
def Frl2ndbn = CompoundValue(1,  if FractalL2nd  then bn else Frl2ndbn[1], bn);
def Frl2nd2bn =  GetValue(bn, Fractall2ndc);
def  FractalH3rd  =  HHLL(FractalL2nd,FractalH2nd).hhj;
def FractalL3rd  =   HHLL(FractalL2nd,FractalH2nd).llj ;
def Fractalh3rdc = CompoundValue(1, if FractalH3rd[1] then 1 else Fractalh3rdc[1] + 1, 1);
def Fractall3rdc = CompoundValue(1, if FractalL3rd[1] then 1 else Fractall3rdc[1] + 1, 1);
def pivotcal3rd = CompoundValue(1, if  FractalH3rd then pivotcal3rd [1] + 1 else if FractalL3rd then pivotcal3rd[1] + 1 else pivotcal3rd[1], 1);
def Fractlh3rdc = CompoundValue(1, if FractalH3rd[1] then 1 else Fractlh3rdc[1] + 1, 1);
def Fractll3rdc = CompoundValue(1, if FractalL3rd [1] then 1 else Fractll3rdc[1] + 1, 1);
def FractalH3rdv  = if FractalH3rd  then high else FractalH3rdv[1];
def Fractall3rdv = if FractalL3rd  then low else Fractall3rdv[1];
def prvFrH3rd1 = if FractalH3rdv <> FractalH3rdv[1]  then FractalH3rdv[1]  else prvFrH3rd1[1];
def prvFrH3rd2 = if IsNaN(close) then 0 else GetValue(prvFrH1, Fractalh3rdc);
def prvFrH3rd3 = if IsNaN(close) then 0 else GetValue(prvFrH2, Fractalh3rdc);
def prvFrL3rd1 = if Fractall3rdv <> Fractall3rdv[1]  then Fractall3rdv[1]  else prvFrL3rd1[1];
def prvFrL3rd2 = if IsNaN(close) then 0 else GetValue(prvFrL3rd1, Fractall3rdc);
def prvFrL3rd3 = if IsNaN(close) then 0 else GetValue(prvFrL3rd2, Fractall3rdc);
def FrlH3rdbn  = CompoundValue(1,  if FractalH3rd  then bn else FrlH3rdbn[1], bn);
def FrlH3rdbn1 =  if  FrlH3rdbn > FrlH3rdbn[1] then FrlH3rdbn[1] else FrlH3rdbn1[1];
def Frll3rdbn = CompoundValue(1,  if FractalL3rd  then bn else Frll3rdbn[1], bn);
def FrlL3rdbn1 = if  Frll3rdbn > Frll3rdbn[1] then Frll3rdbn[1] else FrlL3rdbn1[1];
def zz1rst = if  (IsNaN(FractalL)  or  IsNaN(Fractalh)) then Double.NaN else if FractalL and low then (paint(Fractalh , FractalL).fva1 - low) / (paint(FractalL, Fractalh ).va2)
else if  Fractalh  and high then ((paint(Fractalh , FractalL).fva2 - high) / (paint(Fractalh, FractalL).va2)) else zz1rst[1];
def line1rst =  if FractalL then low else if Fractalh  then high else line1rst[1] + zz1rst;
def zz2nd =  if  FractalL2nd and low then (paint(FractalH2nd , FractalL2nd ).fva1 - low) / (paint( FractalL2nd, FractalH2nd ).va2) 
else if FractalH2nd  and high then ((paint(FractalH2nd , FractalL2nd).fva2 - high) / (paint(FractalH2nd , FractalL2nd).va2)) else zz2nd[1];
def line2nd = if  FractalL2nd then low else if FractalH2nd  then high else line2nd[1] + zz2nd;
def zz3rd =  if  (IsNaN(FractalL3rd)  or  IsNaN(FractalH3rd)) then Double.NaN else if  FractalL3rd  and low then (paint(FractalH3rd, FractalL3rd).fva1 - low) / (paint(FractalL3rd, FractalH3rd).va2)
else if  FractalH3rd  and high then  ((paint(FractalH3rd, FractalL3rd ).fva2 - high) / (paint(FractalH3rd, FractalL3rd).va2)) else zz3rd[1];
def line3rd = if  FractalL3rd then low else if  FractalH3rd then high else line3rd[1] + zz3rd;
DefineGlobalColor("in", CreateColor(132, 111, 122));
plot zz1rstline = if pivotcal <= 1 then Double.NaN else if show1rstzz then line1rst else Double.NaN;
zz1rstline.SetDefaultColor(Color.WHITE);
zz1rstline.AssignValueColor(GlobalColor("in"));
zz1rstline.SetStyle(Curve.SHORT_DASH);
zz1rstline.SetLineWeight(2);
plot zz2ndline  = if pivotcal2nd <= 1  then Double.NaN else if show2ndzz then line2nd else Double.NaN;
zz2ndline.SetDefaultColor(Color.WHITE);
zz2ndline.SetStyle(Curve.SHORT_DASH);
zz2ndline.SetLineWeight(2);
plot zz3rdline = if pivotcal3rd <= 1  then Double.NaN else if  show3rdzz  then line3rd else Double.NaN;
zz3rdline.SetDefaultColor(Color.CYAN);
zz3rdline.SetStyle(Curve.SHORT_DASH);
zz3rdline.SetLineWeight(3);
plot  FractalH1rst = if Fractalh  then high else Double.NaN;
FractalH1rst.SetPaintingStrategy(PaintingStrategy.POINTS);
FractalH1rst.SetDefaultColor(Color.LIGHT_GREEN);
FractalH1rst.SetLineWeight(2);
plot  FractalL1rst = if FractalL  then low else Double.NaN;
FractalL1rst.SetPaintingStrategy(PaintingStrategy.POINTS);
FractalL1rst.SetDefaultColor(Color.LIGHT_RED);
FractalL1rst.SetLineWeight(2);
plot  FractalH2nd_ = if FractalH2nd  then high else Double.NaN;
FractalH2nd_.SetPaintingStrategy(PaintingStrategy.POINTS);
FractalH2nd_.SetDefaultColor(Color.GREEN);
FractalH2nd_.SetLineWeight(4);
plot  FractalL2nd_ =  if FractalL2nd  then low else Double.NaN;
FractalL2nd_ .SetPaintingStrategy(PaintingStrategy.POINTS);
FractalL2nd_ .SetDefaultColor(Color.RED);
FractalL2nd_ .SetLineWeight(4);
plot  FractalH3rd_ = if FractalH3rd  then high else Double.NaN;
FractalH3rd_.SetPaintingStrategy(PaintingStrategy.POINTS);
FractalH3rd_.SetDefaultColor(Color.CYAN);
FractalH3rd_.SetLineWeight(5);
plot  Fractall3rd_ = if FractalL3rd  then low else Double.NaN;
Fractall3rd_ .SetPaintingStrategy(PaintingStrategy.POINTS);
Fractall3rd_ .SetDefaultColor(Color.CYAN);
Fractall3rd_ .SetLineWeight(5);
AddChartBubble(show1rstbubble && FractalH1rst,high,if between(FractalHv,FractalHv[1]*.99,FractalHv[1]*1.01) then "DT" else
if between(FractalHv,Fractallv[2]*.99,FractalHv[2]*1.01) then "RS" else
if FractalHv>FractalHv[1]*1.01 then "hh" else "lh",if RocketRSI(10, 8) > 1.5 then Color.ORANGE else Color.GRAY);
AddChartBubble(show1rstbubble and FractalL1rst,low,if between(Fractallv,Fractallv[1]*.99,Fractallv[1]*1.01) then "DB" else
if between(Fractallv,Fractalhv[2]*.99,Fractalhv[2]*1.01) then "SP" else
if Fractallv<Fractallv[1]*.99 then "ll" else "hl", if RocketRSI(10, 8) < -1.5 then Color.ORANGE else Color.GRAY);
AddChartBubble(show2ndbubble and FractalH2nd_, high,if between(FractalH2ndv,FractalH2ndv[1]*.99,FractalH2ndv[1]*1.01) then "DT" else
if between(FractalH2ndv,Fractall2ndv[2]*.99,Fractall2ndv[2]*1.01) then "RS" else
if FractalH2ndv>FractalH2ndv[1]*1.01 then "HH" else "LH",  if RocketRSI(10, 8) > 1.5 then Color.light_green else Color.GREEN);
AddChartBubble(show2ndbubble and FractalL2nd_,low,if between(Fractall2ndv,Fractall2ndv[1]*.99,Fractall2ndv[1]*1.01) then "DB" else
if between(Fractall2ndv,Fractalh2ndv[2]*.99,Fractalh2ndv[2]*1.01) then "SP" else
if Fractall2ndv<Fractall2ndv[1]*.99 then "LL" else "HL", if RocketRSI(10, 8) < -1.5 then Color.light_green else Color.GREEN);
AddChartBubble(show3rdbubble and FractalH3rd_,high,if between(FractalH3rdv,FractalH3rdv[1]*.99,FractalH3rdv[1]*1.01) then "DT" else
if between(FractalH3rdv,Fractall3rdv[2]*.99,Fractall3rdv[2]*1.01) then "RS" else
if FractalH3rdv>FractalH3rdv[1]*1.01 then "HH" else "LH",  if RocketRSI(10, 8) > 1.5 then Color.magenta else Color.CYAN);
AddChartBubble(show3rdbubble and Fractall3rd_, low ,if between(Fractall3rdv,Fractall3rdv[1]*.99,Fractall3rdv[1]*1.01) then "DB" else
if between(Fractall3rdv,Fractalh3rdv[2]*.99,Fractalh3rdv[2]*1.01) then "SP" else
if Fractall3rdv<Fractall3rdv[1]*1.01 then "LL" else "HL", if RocketRSI(10, 8) < -1.5 then Color.magenta else Color.CYAN);
#### count between higher fractals
def countreset1rst =  (Fractalh or FractalL);
def countreset1rst_ = if IsNaN(countreset1rst) then 0 else  countreset1rst;
def countreset2nd =  (FractalH2nd or FractalL2nd);
def countreset2nd_ = if IsNaN(countreset2nd) then 0 else  countreset2nd ;
def countbetween2nd  = CompoundValue(1,  if countreset2nd_ then 0 else if  (Fractalh or  FractalL) then countbetween2nd[1] + 1 else if countbetween2nd[1]>=3 && countreset2nd_[2] then 0 else countbetween2nd[1], 0);
def countreset3rd = (FractalL3rd or FractalH3rd);
def countreset3rd_ = if IsNaN(countreset3rd) then 0 else  countreset3rd;
def countbetween3rd  = CompoundValue(1,  if countreset3rd_ then 0 else if  (Fractalh or  FractalL) then countbetween3rd [1] + 1 else countbetween3rd [1], 0);
def countreset2nd3rd =  (FractalL3rd or FractalH3rd);
def countreset2nd3rd_ = if IsNaN(countreset3rd) then 0 else  countreset3rd;
def countbetween2nd3rd  = CompoundValue(1,  if countreset2nd3rd_ then 0 else if  (FractalH2nd_ or FractalL2nd_)  then countbetween2nd3rd[1] + 1 else countbetween2nd3rd[1], 0);
AddChartBubble(countbetween and ( Fractalh) &&  countbetween2nd , high, countbetween2nd, Color.light_green);
AddChartBubble(countbetween and ( FractalL) &&  countbetween2nd , low, countbetween2nd, Color.light_green);
AddChartBubble(countbetween and FractalH2nd_ && countbetween2nd3rd, high, countbetween2nd3rd, Color.ORANGE);
AddChartBubble(countbetween and FractalL2nd_ && countbetween2nd3rd, low,   countbetween2nd3rd, Color.ORANGE);
############################### 1).  first order fib
def bearfibpivot_ =  if IsNaN(close) then 0 else ( Fractalh && fib(FractalHv, Fractallv , prvFrH1 , tol) );
def bullfibpivot_ = if IsNaN(close) then 0 else (FractalL && fib(Fractallv , FractalHv, prvFrL1, tol ));
plot befibfractal = if bearfibpivot_  then high else Double.NaN;
befibfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
befibfractal.SetDefaultColor(Color.YELLOW);
befibfractal.SetLineWeight(5);
plot bufibfractal = if bullfibpivot_  then low else Double.NaN;
bufibfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
bufibfractal.SetDefaultColor(Color.YELLOW);
bufibfractal.SetLineWeight(5);
AddChartBubble(showFibpivotbubble and befibfractal,  high, "fib:" + fib(FractalHv, Fractallv, prvFrH1, tol).ratio, Color.LIGHT_RED);
AddChartBubble(showFibpivotbubble  and bufibfractal, low, "fib: " + fib(Fractallv, FractalHv, prvFrL1, tol).ratio, Color.LIGHT_GREEN);
###############################  2). first order fib relation to 2nd order
def maxvalue = if FrH2ndbn > Frl2ndbn then  FractalH2ndv  else  Fractall2ndv;
def maxvalue2 = if maxvalue == Fractall2ndv then FractalH2ndv  else Fractall2ndv  ;
def bearfibpivot2_ =  if IsNaN(close) then 0 else (Fractalh && fib(FractalHv, maxvalue , maxvalue2, tol))  ;
def bullfibpivot2_ = if IsNaN(close) then 0 else (FractalL && fib(Fractallv, maxvalue , maxvalue2, tol));
plot befibfractal2 = if bearfibpivot2_   then high else Double.NaN;
befibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
befibfractal2.SetDefaultColor(Color.CYAN);
befibfractal2.SetLineWeight(5);
plot bufibfractal2 = if bullfibpivot2_   then low else Double.NaN;
bufibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
bufibfractal2.SetDefaultColor(Color.CYAN);
bufibfractal2.SetLineWeight(5);
AddChartBubble(showFibrelationbubble and befibfractal2,  high, "2ndfib:" +  fib(FractalHv, maxvalue , maxvalue2, tol).ratio, Color.CYAN);
AddChartBubble(showFibrelationbubble  and bufibfractal2, low, "2ndfib: " + fib(Fractallv, maxvalue , maxvalue2, tol).ratio, Color.CYAN);
def maxlvalue = if FrlH3rdbn > Frll3rdbn then FractalH3rdv  else Fractall3rdv;
def maxlvalue2 = if maxlvalue == Fractall3rdv  then FractalH3rdv else Fractall3rdv ;
###############################  3). first order fib relation to 3rd order
def lbearfibpivot2_ =  if IsNaN(close) then 0 else (Fractalh && fib(FractalHv, maxlvalue , maxlvalue2, tol))  ;
def lbullfibpivot2_ = if IsNaN(close) then 0 else (FractalL && fib(Fractallv , maxlvalue , maxlvalue2, tol));
plot belfibfractal2 = if lbearfibpivot2_  then high else Double.NaN;
belfibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
belfibfractal2.SetDefaultColor(Color.ORANGE);
belfibfractal2.SetLineWeight(5);
plot bulfibfractal2 = if lbullfibpivot2_   then low else Double.NaN;
bulfibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
bulfibfractal2.SetDefaultColor(Color.ORANGE);
bulfibfractal2.SetLineWeight(5);
AddChartBubble(showFibrelationbubble and belfibfractal2,  high, "3rdfib:" +  fib(FractalHv, maxlvalue , maxlvalue2, tol).ratio, Color.CYAN);
AddChartBubble(showFibrelationbubble  and bulfibfractal2, low, "3rdfib: " + fib(Fractallv, maxlvalue , maxlvalue2, tol).ratio, Color.CYAN);
###############################  1). 2nd order fib relationship
def mbearfibpivot_ =  if IsNaN(close) then 0 else (FractalH2nd && fib(prvFr2dnH1, prvFr2dnL1, prvFr2dnH2  , tol))  ;
def mbullfibpivot_ = if IsNaN(close) then 0 else (FractalL2nd && fib(prvFr2dnL1, prvFr2dnH1, prvFr2dnL2, tol));
plot mbefibfractal = if mbearfibpivot_  then high else Double.NaN;
mbefibfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
mbefibfractal.SetDefaultColor(Color.YELLOW);
mbefibfractal.SetLineWeight(5);
plot mbufribfractal = if mbullfibpivot_  then low else Double.NaN;
mbufribfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
mbufribfractal.SetDefaultColor(Color.YELLOW);
mbufribfractal.SetLineWeight(5);
AddChartBubble(showFibpivotbubble  and mbefibfractal,  high, "fib: " + fib(FractalH2ndv, Fractall2ndv, prvFrH3rd1 , tol).ratio, Color.LIGHT_RED);
AddChartBubble(showFibpivotbubble  and mbufribfractal,  low, "fib: " + fib(Fractall2ndv, FractalH2ndv, prvFr2dnL1, tol).ratio, Color.LIGHT_GREEN);
############################### 1).  2nd order fib relation to 3rd order
def mlbearfibpivot2_ =  if IsNaN(close) then 0 else ( FractalH2nd && fib(FractalH2ndv, maxlvalue , maxlvalue2, tol));
def mlbullfibpivot2_ = if IsNaN(close) then 0 else (FractalL2nd && fib(Fractall2ndv, maxlvalue , maxlvalue2, tol));
plot mbelfibfractal2 = if mlbearfibpivot2_   then high else Double.NaN;
mbelfibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
mbelfibfractal2.SetDefaultColor(Color.ORANGE);
mbelfibfractal2.SetLineWeight(5);
plot mbulfibfractal2 = if mlbullfibpivot2_   then low else Double.NaN;
mbulfibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
mbulfibfractal2.SetDefaultColor(Color.ORANGE);
mbulfibfractal2.SetLineWeight(5);
AddChartBubble(showFibrelationbubble and mbelfibfractal2,  high, "3rdfib:" +  fib(FractalH2ndv, maxlvalue , maxlvalue2, tol).ratio, Color.CYAN);
AddChartBubble(showFibrelationbubble and mbulfibfractal2, low, "3rdfib: " + fib(Fractall2ndv, maxlvalue , maxlvalue2, tol).ratio, Color.CYAN);
###############################  2).3rd order fib relationship
def lbearfibpivot_ =  if IsNaN(close) then 0 else (FractalH3rd && fib(FractalH3rdv, Fractall3rdv, prvFrH3rd1, tol));
def lbullfibpivot_ = if IsNaN(close) then 0 else (FractalL3rd && fib(Fractall3rdv, FractalH3rdv, prvFrL3rd1, tol));
plot lbefibfractal = if lbearfibpivot_   then high else Double.NaN;
lbefibfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
lbefibfractal.SetDefaultColor(Color.YELLOW);
lbefibfractal.SetLineWeight(5);
plot lbufribfractal = if lbullfibpivot_   then low else Double.NaN;
lbufribfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
lbufribfractal.SetDefaultColor(Color.YELLOW);
lbufribfractal.SetLineWeight(5);
AddChartBubble(showFibpivotbubble  and lbefibfractal,  high, "fib: " + fib(FractalH3rdv, Fractall3rdv, prvFrH3rd1, tol).ratio, Color.LIGHT_RED);
AddChartBubble(showFibpivotbubble  and lbufribfractal,  low, "fib: " + fib(Fractall3rdv, FractalH3rdv, prvFrL3rd1, tol).ratio, Color.LIGHT_GREEN);
## higher highs and lower lows
def fr1lowconsect = if Fractall && Fractallv<Fractallv[1] then fr1lowconsect[1]+1 else if Fractall && Fractallv>Fractallv[1] then 0 else fr1lowconsect[1];
def fr1highconsect = if Fractalh && FractalHv>FractalHv[1] then fr1highconsect[1]+1 else if Fractalh && FractalHv<FractalHv[1] then 0 else fr1highconsect[1];
def fr1lowconsect2 = if Fractall && Fractall2ndv<Fractall2ndv[1] then fr1lowconsect2[1]+1 else if Fractall && Fractall2ndv>Fractall2ndv[1] then 0 else fr1lowconsect2[1];
def fr1highconsect2 = if Fractalh &&  Fractalh2ndv>Fractalh2ndv[1] then fr1highconsect2[1]+1 else if Fractalh && Fractalh2ndv<Fractalh2ndv[1] then 0 else fr1highconsect2[1];
AddChartBubble(showHHLLcount && FractalH1rst && fr1highconsect ,high,fr1highconsect, Color.white);
AddChartBubble(showHHLLcount and Fractall1rst&& fr1lowconsect,low,fr1lowconsect, Color.white);
AddChartBubble(showHHLLcount && FractalH2nd_ && fr1highconsect2,high,fr1highconsect2, Color.ORANGE);
AddChartBubble(showHHLLcount and Fractall2nd_ && fr1lowconsect2,low,fr1lowconsect2, Color.ORANGE);
input trendmult = 1.0;
def minbb = Min(HighestAll( FrlH3rdbn ), HighestAll(Frll3rdbn ));
def maxbb= Max(HighestAll( FrlH3rdbn ), HighestAll(Frll3rdbn ));
def stopmax =  bn ==maxbb;
def stopmax_ = if IsNaN(close) then 0 else stopmax;
def stopmin =  bn ==  minbb;
def stopmin_ = if IsNaN(close) then 0 else stopmin;
def countbetween3rdbar  = CompoundValue(1,  if countreset2nd3rd_ then bn else countbetween3rdbar [1], bn);
def countbetween3rdbar2  = CompoundValue(1,  if countbetween3rdbar > countbetween3rdbar[1] then countbetween3rdbar[1] else countbetween3rdbar2[1], bn);
def countbetween3rdbar3  = CompoundValue(1,  if countbetween3rdbar2 > countbetween3rdbar2[1] then countbetween3rdbar2[1] else countbetween3rdbar3[1], bn);
def countbetween3rdbar4  = CompoundValue(1,  if countbetween3rdbar3 > countbetween3rdbar3[1] then countbetween3rdbar3[1] else countbetween3rdbar4[1], bn);
def countbetween3rdbar5  = CompoundValue(1,  if countbetween3rdbar4 > countbetween3rdbar4[1] then countbetween3rdbar4[1] else countbetween3rdbar5[1], bn);
def countbetween3rdv  = CompoundValue(1,  if countreset2nd3rd_ && FractalH3rd  then high else  if countreset2nd3rd_ && FractalL3rd then low else countbetween3rdv [1], 1);
def countbetween3rdv2  = CompoundValue(1,  if countbetween3rdv <> countbetween3rdv[1] then countbetween3rdv[1] else countbetween3rdv2[1], 1);
def countbetween3rdv3  = CompoundValue(1,  if countbetween3rdv2 <> countbetween3rdv2[1] then countbetween3rdv2[1] else countbetween3rdv3[1], 1);
def countbetween3rdv4  = CompoundValue(1,  if countbetween3rdv3 <> countbetween3rdv3[1] then countbetween3rdv3[1] else countbetween3rdv4[1], 1);
def countbetween3rdv5  = CompoundValue(1,  if countbetween3rdv4 <> countbetween3rdv4[1] then countbetween3rdv4[1] else countbetween3rdv5[1], 1);
def lowl ;
def highl ;
def Xratio;
def Yratio;
input projectionOrder = {twothree, threefour, default fourfive};
switch (projectionOrder ) {
case twothree:
lowl = if countbetween3rdv && FractalL3rd then low else if countbetween3rdv && Fractalh3rd then high-AbsValue(countbetween3rdv2 - countbetween3rdv3) else lowl[1];
highl = if countbetween3rdv && Fractalh3rd then high else if countbetween3rdv && Fractall3rd then low+AbsValue(countbetween3rdv2- countbetween3rdv3) else highl[1];
Xratio = absvalue(lowl- highl);
Yratio = AbsValue(countbetween3rdbar2 - countbetween3rdbar3 );
case threefour:
lowl = if countbetween3rdv && FractalL3rd then low else if countbetween3rdv && Fractalh3rd then high-AbsValue(countbetween3rdv3 - countbetween3rdv4) else lowl[1];
highl = if countbetween3rdv && Fractalh3rd then high else if countbetween3rdv && Fractall3rd then low+AbsValue(countbetween3rdv3- countbetween3rdv4) else highl[1];
Xratio = absvalue(lowl- highl);
Yratio = AbsValue(countbetween3rdbar3 - countbetween3rdbar4 );
case fourfive:
lowl = if countbetween3rdv && FractalL3rd then low else if countbetween3rdv && Fractalh3rd then high-AbsValue(countbetween3rdv4- countbetween3rdv5) else lowl[1];
highl = if countbetween3rdv && Fractalh3rd then high else if countbetween3rdv && Fractall3rd then low+AbsValue(countbetween3rdv4- countbetween3rdv5) else highl[1];
Xratio = absvalue(lowl- highl);
Yratio = AbsValue(countbetween3rdbar4 - countbetween3rdbar5 );
}
def change =  Xratio*trendmult  / Yratio;
def maxcount = CompoundValue(1, if stopmax_ then 0 else maxcount[1] + 1, 0);
def Revmax = CompoundValue(1, if stopmax_ then (change) else Revmax[1], Double.NaN);
def dnline = CompoundValue(1, if stopmax_ &&  minbb== FrlH3rdbn then countbetween3rdv  else if maxcount > (yratio *trendmult) then Double.NaN else ( dnline[1] + Revmax ), Double.NaN);
def dnlinel = CompoundValue(1, if stopmax_ && minbb == Frll3rdbn then countbetween3rdv  else if maxcount > (yratio *trendmult) then Double.NaN else  (dnlinel[1] - Revmax) , Double.NaN);
def activepro =  if showprojection==no then double.nan else stopmax >= 0 && minbb == Frll3rdbn;
plot  dnlineProj = if activepro  then dnlinel else  dnline;
dnlineProj.SetDefaultColor(Color.CYAN);
dnlineProj.SetLineWeight(2);
dnlineProj.SetStyle(Curve.SHORT_DASH);
plot hiline =  if dnlineProj then highl
                else double.nan;
hiline.SetDefaultColor(color.red);
plot loline = if dnlineProj then lowl
                else double.nan;
loline.SetDefaultColor(color.cyan);
def range3rd = absvalue(loline - hiline) ;
plot fib146 = if activepro then  hiline - (range3rd *0.146)  else   loline +(range3rd *0.146) ;
fib146.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection  and !isNaN(fib146[1]) and isNaN(fib146[-1]),fib146,"fib146" ,color.cyan);
plot fib236 = if activepro then     hiline - (range3rd *0.236)  else   loline +(range3rd *0.236) ;
fib236.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection  and  !isnan(fib236[1]) && isnan(fib236[-1]),fib236,"fib236" ,color.cyan);
plot fib382 = if activepro then   hiline - (range3rd *0.382)  else  loline +(range3rd *0.382) ;
fib382.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib382[1]) and isNaN(fib382[-1]),fib382,"fib382" ,color.cyan);
plot fib50= if activepro then  hiline - (range3rd *0.50)  else   loline +(range3rd *0.50) ;
fib50.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib50[1]) and isNaN(fib50[-1]),fib50,"fib50" ,color.cyan);
plot fib618 = if activepro then  hiline - (range3rd *0.618)  else   loline +(range3rd *0.618) ;
fib618.SetDefaultColor(Color.GREEN);
plot  mid382 = if between(dnlineProj,fib382,fib618) or  between(dnlineProj,fib618,fib382) then fib382 else double.nan;
mid382.SetDefaultColor(Color.blue);
plot  mid618 = if between(dnlineProj,fib382,fib618)  or  between(dnlineProj,fib618,fib382) then fib618 else double.nan;
mid618.SetDefaultColor(Color.blue);
addcloud(mid382 ,mid618,color.green,color.green);
addcloud(fib382 ,fib618,color.cyan,color.cyan);
AddChartBubble( showprojection and !isNaN(fib618[1]) and isNaN(fib618[-1]),fib618,"fib618" ,color.cyan);
plot fib786= if activepro then   hiline - (range3rd *0.786)  else   loline +(range3rd *0.786) ;
fib786.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib786[1]) and isNaN(fib786[-1]),fib786,"fib786" ,color.cyan);
plot fib886= if activepro then   hiline - (range3rd *0.886)  else   loline +(range3rd *0.886) ;
fib886.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib886[1]) and isNaN(fib886[-1]),fib886,"fib886" ,color.cyan);
plot fib1= if activepro then   hiline - (range3rd *1)  else   loline +(range3rd *1) ;
fib1.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib1[1]) and isNaN(fib1[-1]),fib1,"fib1" ,color.cyan);
plot fib1236= if activepro then  hiline - (range3rd *1.236)  else   loline +(range3rd *1.236) ;
fib1236.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib1236[1]) and isNaN(fib1236[-1]),fib1236,"fib1236" ,color.cyan);
AddLabel(yes, " 1rstPcount :  " + pivotcal , Color.WHITE);
AddLabel(yes, "2ndPcount:  " + pivotcal2nd, Color.CYAN);
AddLabel(yes, "3rdPcount:  " + pivotcal3rd , Color.ORANGE);
#### under construction
###input equityrisk =10000.00;
##input showriskreward = yes;
def fib2618 = 2.618;
def fib1618= 1.618;
def xatimebu = AbsValue(countbetween3rdbar2 - countbetween3rdbar3 );
def  xdtimebu = AbsValue(countbetween3rdbar5 - countbetween3rdbar3 );
def przrangebull =Xratio;
Def bullPRZ=absvalue(przrangebull/3) ;
def tradetimebu= round(((xatimebu+xdtimebu)/2)*fib2618,0);
def brsXADtimebu= CompoundValue(1, if activepro  then 1 else if maxcount==(tradetimebu) then 0 else brsXADtimebu[1], 0);
##plot bullstop = if  brsXADtimebu then hiline else loline;
##plot bullentry1 =  if brsXADtimebu then ( bullstop +bullPRZ) else ( bullstop -bullPRZ);
##plot bullPRZa =  if brsXADtimebu  then ( bullstop -bullPRZ)  else ( bullstop +bullPRZ);
##AddCloud(bullentry1, bullPRZa, Color.gray, Color.gray);
 
Code:
###  Resources:
## 1). interpretation of "Scientific Guide to Price Action and Pattern Trading" Author: Young Ho Seo
## 2). https://www.mql5.com/en/articles/1470
## 3). Semafor_3Level_ZZ
### TOS Script resources:
## 1). initial pivot from Mobius Pivot Array
## 2). folds from Dmonkey Examples && JQ's one note
## 3). Mobius Trend Pivots shared 2/4/2019
## 4). ZZtop JQ onenote Author unidentified
## 5). #StudyName: Dilbert_1st2nd3rdOrderPivotFibs_V4
## script guide:
##  1). Inputs:
##    a.  first logic cycles pivots to ZZ:  lines 140-144
##    b.  HHLL script cycles higher highs until a lower low occurs and vice versa  lines 88-130
##    c.  show 1rst,2nd and 3rd bubble show Double bottom, Double Top, Resistance, support and rocketRSI color  : lines 264-281
##    d.  countbetween counts pivots between higher pivots : lines 380-388
## 2). fib relationships  with higher to lower pivots  : lines 299-379
## 3).  : lines 389-374
##    a. fib projection initiates when 3rd order pivot equals 2nd order pivot
##    b. projection order input is similiar to harmonic XABCD trading
##       - twothree equals BC trend with fib levels CD
##       - threefour equals AB trend with fib levels BC
##       - fourfive equals XA trend with fib levels AB
### issues and needed additions:
## 1). fibrelationships to higher pivots need work
## 2). projections may need extended lines when price fails to breach High and low
## 3). ***** need scan for active projection bar  ...  help
## 4). needs over bought and over sold trend lines
## 5). needs linear lines for ascending and descending trends
## 6).  i have a condensed version of a patternfinder(trangles, doubletops, doublebottoms, head and shoulders, wedge,etc) which paints required trendlines if needed, request..
input n = 4;
input tol = .04; ## ratio tolerance
input show1rstzz = yes;
input show2ndzz = yes;
input show3rdzz = yes;
input show1rstbubble = no;
input show2ndbubble = yes;
input show3rdbubble =yes;
input showFibpivotbubble = no;
input showFibrelationbubble = no;
input showprojection = no;
input showHHLLcount = no;
input countbetween = yes;
def bn = BarNumber();
def lbar = HighestAll( if IsNaN(close) then bn else 0);
script fib {
    input v1 = 0;
    input v2 = 0;
    input v3 = 0;
    input tol = 0;
    def ratio = Round((AbsValue(v1 - v2) / AbsValue(v2 - v3)), 2);
## Standard numbers: 0-0.146-0.236-0.382-0.5-0.618-0.764-0.854-1.0-1.236-1.618-2.618-4. 236-6.854
### Pesavento numbers: 0-0.382-0.5-0.618-0.707-0.786-0.854-0.886-1.0-1.128-1.272-1. 414-1.618-2.0-2.414-2. 618-4.0
### Bryce gilmore:  0.25 - 0.382 - 0.5 - 0.618 - 0.667 - 0.786 - 1.0 - 1.272 - 1.618 - 1.732 - 1.75 - 2.0 - 2.236 - 2.5 - 2.618 - 3.0 - 3.33 - 4.236 - 6.854
    def retrace = Between(ratio, (0.146 - tol), (0.146 + tol)) or Between(ratio, (.236 - tol), (.236 + tol)) or Between(ratio, (.382 - tol), (.382 + tol)) or
    Between(ratio, (.50 - tol), (.50 + tol)) or Between(ratio, (.618 - tol), (.618 + tol)) or  Between(ratio, (.667 - tol), (.667 + tol)) or Between(ratio, (.707 - tol), (.707 + tol)) or
    Between(ratio, (.764 - tol), (.764 + tol)) or Between(ratio, (.786 - tol), (.786 + tol)) or Between(ratio, (.854 - tol), (.854 + tol)) or Between(ratio, (.886 - tol), (.886 + tol)) or
    Between(ratio, (1 - tol), (1 + tol)) or Between(ratio, (1.236 - tol), (1.236 + tol)) or Between(ratio, (1.41 - tol), (1.414 + tol)) or Between(ratio, (1.618 - tol), (1.618 + tol)) or
    Between(ratio, (1.732 - tol), (1.732 + tol)) or Between(ratio, (1.75 - tol), (1.75 + tol)) or Between(ratio, (2.00 - tol), (2.000 + tol)) or Between(ratio, (2.236 - tol), (2.236 + tol)) or
    Between(ratio, (2.414 - tol), (2.414 + tol)) or Between(ratio, (2.50 - tol), (2.50 + tol)) or Between(ratio, (2.618 - tol), (2.618 + tol))  or Between(ratio, (3 - tol), (3 + tol)) or
    Between(ratio, (3.33 - tol), (3.33 + tol)) or Between(ratio, (4 - tol), (4 + tol)) or Between(ratio, (4.236 - tol), (4.236 + tol))  or Between(ratio, (6.85 - tol), (6.85 + tol));
    plot one = if IsNaN(0) then  0 else retrace ;
    def getratio = if one then ratio else Double.NaN;
}
script F {
    input value = close ;
    def bn = BarNumber();
    def lbar = HighestAll(if IsNaN(close) then 0 else bn);
    plot xd = fold di = 1 to lbar with da = Double.NaN while IsNaN(da) do if GetValue(value, -di) then di else Double.NaN;
}
script paint {
    input v1 = 0.0 ;
    input v2 = 0.0;
    def bn = BarNumber();
    def lv1bar = HighestAll(if v1 then bn else 0);
    def lv2bar = HighestAll(if v2 then bn else 0);
    def lbar = HighestAll(if IsNaN(close) then 0 else bn);
    plot va1 = if bn >= lv1bar then 0 else fold id = 1 to lbar with t = 0 while t == 0 do if GetValue(v1, -id) then id else 0;
    plot va2 = if bn >= lv2bar  then 0 else fold sd = 1 to lbar with x = 0 while x == 0 do if GetValue(v2, -sd) then sd else 0;
    def for =  GetValue(v2, -va1);
    def for2 =  GetValue(for, - va1);
    plot for3 =  GetValue(for2, - va1);
    plot fva1 =  GetValue(high, -va1);
    plot fva2 = GetValue(low, -va2);
}
#### plot highest high trend until low trend and vice versa
script HHLL {
    input fractall= 0.0;
    input fractalh = 0.0;
def Fractallv = if fractall then low else Fractallv[1];
def Fractalhv = if fractalh then high else Fractalhv[1];
def bn = BarNumber();
def lhighestbar = highestall( if FractalL then bn else 0);
def hhighestbar= highestall( if Fractalh then bn else 0);
def lbar = HighestAll(if IsNaN(close) then 0 else bn);
                def fr1lowc1 = compoundValue(1,if Fractall && Fractallv<=Fractallv[1] then fr1lowc1 [1]+1 else if Fractall && Fractallv>Fractallv[1] then 0 else fr1lowc1 [1],0);
                def fr1highc1= compoundValue(1,if Fractalh && FractalHv>=FractalHv[1] then fr1highc1[1]+1 else if Fractalh && FractalHv<FractalHv[1] then 0 else fr1highc1[1],0);
                def maxl = if Fractall && fr1lowc1>=1  && lhighestbar  then 1 else Double.NaN;
                def maxh = if Fractalh && fr1highc1>=1&& hhighestbar then 1 else Double.NaN;
                def maxlv = if  !isnan(maxl) then low else  maxlv[1];
                def maxhv=  if  !isnan(maxh) then high  else maxhv[1];
                def maxlhighestbar = highestall( if !isnan(maxl) then bn else 0);
                def maxhhighestbar= highestall( if !isnan(maxh)then bn else 0);
                def maxlct =  compoundValue(1,if   !isnan(maxh[1]) then 0 else if (Fractall && !isnan(maxl)) then  maxlct[1]+1 else maxlct[1],0);
                def maxhct = compoundValue(1, if  !isnan(maxl[1]) then 0 else if (Fractalh && !isnan(maxh)) then  maxhct[1]+1 else maxhct[1],0);
                def maxld = fold dig = 1 to lbar with dag = Double.NaN while IsNaN(dag) do if GetValue(maxl, -dig) then dig else Double.NaN;
                def maxhd = fold die = 1 to lbar with dae = Double.NaN while IsNaN(dae) do if GetValue(maxh, -die) then die else Double.NaN;
def LLja  = if bn>=maxlhighestbar &&maxl then 1 else
                (if !isnan(maxl) && getvalue(maxlct,(-maxld))>1 && getvalue(low,(-maxld))< maxlv or
                !isnan(maxl) && getvalue(maxlct,(-maxld))>maxlct && maxlv < maxlv[1] or
                !isnan(maxl) &&  maxlct>1 && maxlv> maxlv[1]or
                !isnan(maxh) then  double.nan else if
                !isnan(maxl) &&  maxlct<=1  && getvalue(maxlct,(-maxld))==1 or
                !isnan(maxl) && getvalue(maxlct,(-maxld))>maxlct && getvalue(low,(-maxld))> maxlv or
                !isnan(maxl) &&  maxlct>=1  && getvalue(maxhct,(-fractalh))>=1 then 1 else 0 ) ;
def hhja  = if bn>=maxhhighestbar  then 1 else        
              (if !isnan(maxh) && getvalue(maxhct,(-maxhd))>maxhct &&  getvalue(high,(-maxhd))> maxhv or
               !isnan(maxh) && getvalue(maxhct,(-maxhd))>maxhct && maxhv > maxhv[1] or
              !isnan(maxh) &&  maxhct>1 &&  maxhv< maxhv[1]or
               !isnan(maxl) then  double.nan else if
               !isnan(maxh) &&  maxhct<=1 && getvalue(maxhct,(-maxhd))==1 or
               !isnan(maxh) && getvalue(maxhct,(-maxhd))>maxhct && getvalue(high,(-maxhd))< maxhv or
                 !isnan(maxh) &&  maxhct>=1 && getvalue(maxlct,(-maxld))>=1 then 1 else 0 ) ;
def LLJb =  (!isnan(maxl) && Fractall && !isnan(LLja)) ;
def HHJb =  (!isnan(maxh) && Fractalh && !isnan(HHja )) ;
def LLJc= if isnan(LLJb) then 0 else  (LLJb ) ;
def  HHJc= if isnan(HHJb) then 0 else  (HHJb ) ;
plot hhj =   if IsNaN(close) then 0 else( HHjc ) ;
plot llj  =  if IsNaN(close) then 0 else ( LLjc ) ;
}
## mobius pivots
def hh = fold i = 1 to n with p = 1 while p do high > GetValue(high, -i);
def PivotH = if (bn > n and   high == Highest(high, n) and     hh)   then high   else Double.NaN;
def ll = fold jk = 1 to n with q = 1 while q  do low < GetValue(low, -jk);
def PivotL = if (bn > n and   low == Lowest(low, n) and  ll) then low  else Double.NaN;
def fractalhigh1 = if IsNaN(PivotH ) then 0 else PivotH ;
def fractallow1 = if IsNaN(PivotL ) then 0 else PivotL ;
def fractc = CompoundValue(1, if IsNaN(close) then Double.NaN  else if fractalhigh1 then fractc[1] + 1 else if fractallow1  then fractc[1] + 1 else fractc[1], 1);
#### offset rotation to ZZ
def frh2v2noffset8 = if fractallow1[1] then high else if high > frh2v2noffset8[1] then high else if  fractalhigh1 then Double.NaN else if high >= frh2v2noffset8[1] then high else  frh2v2noffset8[1];
def frl2v2noffset8 = if  fractalhigh1[1] then low else if low < frl2v2noffset8[1] then low else if  fractallow1  then Double.NaN else if low <= frl2v2noffset8[1]  then low else  frl2v2noffset8[1];
def frh2v2noffsetv = if high==getvalue(frh2v2noffset8[1],-f(fractallow1)) then 1 else double.nan;
def frl2v2noffsetv = if low==getvalue(frl2v2noffset8[1],-f(fractalhigh1)) then 1 else double.nan;
#### 1rst order pivots
def Fractalh = if IsNaN(close)  then 0 else (fractalhigh1 or !isnan(frh2v2noffsetv));
def FractalL =  if  IsNaN(close) then 0 else (fractallow1  or  !isnan(frl2v2noffsetv));
def pivotcal = CompoundValue(1, if Fractalh then pivotcal [1] + 1 else if FractalL then pivotcal [1] + 1 else pivotcal [1], 1);
def FractalHc = CompoundValue(1, if Fractalh[1] then 1 else FractalHc[1] + 1, 1);
def FractalLc = CompoundValue(1, if FractalL[1] then 1 else FractalLc[1] + 1, 1);
#### 2nd order pivots
def FractalHv  = if Fractalh  then high else FractalHv[1];
def Fractallv = if FractalL  then low else Fractallv[1];
def prvFrH1 = if FractalHv <> FractalHv[1] then FractalHv[1] else prvFrH1[1];
def prvFrH2 = if FractalHv <> FractalHv[1] then prvFrH1 [1] else prvFrH2[1];
def prvFrH3 = if FractalHv <> FractalHv[1] then prvFrH2 [1] else prvFrH3[1];
def prvFrH4 = if FractalHv <> FractalHv[1] then prvFrH3 [1] else prvFrH4[1];
def FrHbn  = CompoundValue(1,  if Fractalh then bn else FrHbn[1], bn);
def FrHbn1 =  if  FrHbn > FrHbn[1] then FrHbn[1] else FrHbn1[1];
def FrHbn2 = GetValue(FrHbn1, FractalHc);
def FrHbn3 = GetValue(FrHbn2, FractalHc);
def FrHbn4 = GetValue(FrHbn3, FractalHc);
def FrHbn5 = GetValue(FrHbn4, FractalHc);
def FrHbn6 = GetValue(FrHbn5, FractalHc);
def prvFrL1 =  if Fractallv <> Fractallv[1] then Fractallv[1] else prvFrL1[1];
def prvFrL2 =  if Fractallv <> Fractallv[1] then prvFrL1[1] else prvFrL2[1];
def prvFrL3 =   if Fractallv <> Fractallv[1] then prvFrL2[1] else prvFrL3[1];
def prvFrL4 =   if Fractallv <> Fractallv[1] then prvFrL3[1] else prvFrL4[1];
def prvFrL5 =  if Fractallv <> Fractallv[1] then prvFrL4[1] else prvFrL5[1];
def Frlbn = CompoundValue(1,  if FractalL then bn else Frlbn[1], bn);
def FrLbn1 =  if  Frlbn > Frlbn[1] then Frlbn[1] else FrLbn1[1];
def FrLbn2 =  GetValue(FrLbn1, FractalLc);
def FrLbn3 = GetValue(FrLbn2, FractalLc);
def FrLbn4 = GetValue(FrLbn3, FractalLc);
def FrLbn5 = GetValue(FrLbn4, FractalLc);
#### 3rd  order pivots
def FractalH2nd =  HHLL(fractall,fractalh ).hhj;
def FractalL2nd  =  HHLL(fractall,fractalh ).llj ;
def pivotcal2nd  = CompoundValue(1,  if FractalH2nd then pivotcal2nd  [1] + 1 else if FractalL2nd  then pivotcal2nd[1] + 1 else pivotcal2nd[1], 1);
def Fractalh2ndc = CompoundValue(1, if FractalH2nd[1] then 1 else Fractalh2ndc[1] + 1, 1);
def Fractall2ndc = CompoundValue(1, if FractalL2nd [1] then 1 else Fractall2ndc[1] + 1, 1);
def FractalH2ndv  = if FractalH2nd  then high else FractalH2ndv[1];
def Fractall2ndv = if FractalL2nd  then low else Fractall2ndv[1];
def prvFr2dnH1 =  if  FractalH2ndv <>  FractalH2ndv[1] then  FractalH2ndv[1] else prvFr2dnH1[1];
def prvFr2dnH2 = if  prvFr2dnH1 <>  prvFr2dnH1[1] then  prvFr2dnH1[1] else prvFr2dnH2[1];
def prvFr2dnH3 =  if  prvFr2dnH2 <>  prvFr2dnH2[1] then  prvFr2dnH2[1] else prvFr2dnH3[1];
def prvFr2dnH4 =  if  prvFr2dnH3 <>  prvFr2dnH3[1] then  prvFr2dnH3[1] else prvFr2dnH3[1];
def prvFr2dnH5 = if  prvFr2dnH4 <>  prvFr2dnH4[1] then  prvFr2dnH4[1] else prvFr2dnH4[1];
def prvFr2dnL1 = if  FractalH2ndv <>  FractalH2ndv[1] then  FractalH2ndv[1] else prvFr2dnL1 [1];
def prvFr2dnL2 = if   prvFr2dnL1 <>   prvFr2dnL1[1] then   prvFr2dnL1[1] else  prvFr2dnL2 [1];
def prvFr2dnL3 =if   prvFr2dnL2 <>   prvFr2dnL2[1] then   prvFr2dnL2[1] else  prvFr2dnL3 [1];
def prvFr2dnL4 =if   prvFr2dnL3 <>   prvFr2dnL3[1] then   prvFr2dnL3[1] else  prvFr2dnL4 [1];
def prvFr2dnL5 =if   prvFr2dnL4 <>   prvFr2dnL4[1] then   prvFr2dnL4[1] else  prvFr2dnL5 [1];
def FrH2ndbn  = CompoundValue(1,  if FractalH2nd  then bn else FrH2ndbn[1], bn);
def FrH2nd2bn  = GetValue(bn, Fractalh2ndc[1]);
def Frl2ndbn = CompoundValue(1,  if FractalL2nd  then bn else Frl2ndbn[1], bn);
def Frl2nd2bn =  GetValue(bn, Fractall2ndc);
def  FractalH3rd  =  HHLL(FractalL2nd,FractalH2nd).hhj;
def FractalL3rd  =   HHLL(FractalL2nd,FractalH2nd).llj ;
def Fractalh3rdc = CompoundValue(1, if FractalH3rd[1] then 1 else Fractalh3rdc[1] + 1, 1);
def Fractall3rdc = CompoundValue(1, if FractalL3rd[1] then 1 else Fractall3rdc[1] + 1, 1);
def pivotcal3rd = CompoundValue(1, if  FractalH3rd then pivotcal3rd [1] + 1 else if FractalL3rd then pivotcal3rd[1] + 1 else pivotcal3rd[1], 1);
def Fractlh3rdc = CompoundValue(1, if FractalH3rd[1] then 1 else Fractlh3rdc[1] + 1, 1);
def Fractll3rdc = CompoundValue(1, if FractalL3rd [1] then 1 else Fractll3rdc[1] + 1, 1);
def FractalH3rdv  = if FractalH3rd  then high else FractalH3rdv[1];
def Fractall3rdv = if FractalL3rd  then low else Fractall3rdv[1];
def prvFrH3rd1 = if FractalH3rdv <> FractalH3rdv[1]  then FractalH3rdv[1]  else prvFrH3rd1[1];
def prvFrH3rd2 = if IsNaN(close) then 0 else GetValue(prvFrH1, Fractalh3rdc);
def prvFrH3rd3 = if IsNaN(close) then 0 else GetValue(prvFrH2, Fractalh3rdc);
def prvFrL3rd1 = if Fractall3rdv <> Fractall3rdv[1]  then Fractall3rdv[1]  else prvFrL3rd1[1];
def prvFrL3rd2 = if IsNaN(close) then 0 else GetValue(prvFrL3rd1, Fractall3rdc);
def prvFrL3rd3 = if IsNaN(close) then 0 else GetValue(prvFrL3rd2, Fractall3rdc);
def FrlH3rdbn  = CompoundValue(1,  if FractalH3rd  then bn else FrlH3rdbn[1], bn);
def FrlH3rdbn1 =  if  FrlH3rdbn > FrlH3rdbn[1] then FrlH3rdbn[1] else FrlH3rdbn1[1];
def Frll3rdbn = CompoundValue(1,  if FractalL3rd  then bn else Frll3rdbn[1], bn);
def FrlL3rdbn1 = if  Frll3rdbn > Frll3rdbn[1] then Frll3rdbn[1] else FrlL3rdbn1[1];
def zz1rst = if  (IsNaN(FractalL)  or  IsNaN(Fractalh)) then Double.NaN else if FractalL and low then (paint(Fractalh , FractalL).fva1 - low) / (paint(FractalL, Fractalh ).va2)
else if  Fractalh  and high then ((paint(Fractalh , FractalL).fva2 - high) / (paint(Fractalh, FractalL).va2)) else zz1rst[1];
def line1rst =  if FractalL then low else if Fractalh  then high else line1rst[1] + zz1rst;
def zz2nd =  if  FractalL2nd and low then (paint(FractalH2nd , FractalL2nd ).fva1 - low) / (paint( FractalL2nd, FractalH2nd ).va2)
else if FractalH2nd  and high then ((paint(FractalH2nd , FractalL2nd).fva2 - high) / (paint(FractalH2nd , FractalL2nd).va2)) else zz2nd[1];
def line2nd = if  FractalL2nd then low else if FractalH2nd  then high else line2nd[1] + zz2nd;
def zz3rd =  if  (IsNaN(FractalL3rd)  or  IsNaN(FractalH3rd)) then Double.NaN else if  FractalL3rd  and low then (paint(FractalH3rd, FractalL3rd).fva1 - low) / (paint(FractalL3rd, FractalH3rd).va2)
else if  FractalH3rd  and high then  ((paint(FractalH3rd, FractalL3rd ).fva2 - high) / (paint(FractalH3rd, FractalL3rd).va2)) else zz3rd[1];
def line3rd = if  FractalL3rd then low else if  FractalH3rd then high else line3rd[1] + zz3rd;
DefineGlobalColor("in", CreateColor(132, 111, 122));
plot zz1rstline = if pivotcal <= 1 then Double.NaN else if show1rstzz then line1rst else Double.NaN;
zz1rstline.SetDefaultColor(Color.WHITE);
zz1rstline.AssignValueColor(GlobalColor("in"));
zz1rstline.SetStyle(Curve.SHORT_DASH);
zz1rstline.SetLineWeight(2);
plot zz2ndline  = if pivotcal2nd <= 1  then Double.NaN else if show2ndzz then line2nd else Double.NaN;
zz2ndline.SetDefaultColor(Color.WHITE);
zz2ndline.SetStyle(Curve.SHORT_DASH);
zz2ndline.SetLineWeight(2);
plot zz3rdline = if pivotcal3rd <= 1  then Double.NaN else if  show3rdzz  then line3rd else Double.NaN;
zz3rdline.SetDefaultColor(Color.CYAN);
zz3rdline.SetStyle(Curve.SHORT_DASH);
zz3rdline.SetLineWeight(3);
plot  FractalH1rst = if Fractalh  then high else Double.NaN;
FractalH1rst.SetPaintingStrategy(PaintingStrategy.POINTS);
FractalH1rst.SetDefaultColor(Color.LIGHT_GREEN);
FractalH1rst.SetLineWeight(2);
plot  FractalL1rst = if FractalL  then low else Double.NaN;
FractalL1rst.SetPaintingStrategy(PaintingStrategy.POINTS);
FractalL1rst.SetDefaultColor(Color.LIGHT_RED);
FractalL1rst.SetLineWeight(2);
plot  FractalH2nd_ = if FractalH2nd  then high else Double.NaN;
FractalH2nd_.SetPaintingStrategy(PaintingStrategy.POINTS);
FractalH2nd_.SetDefaultColor(Color.GREEN);
FractalH2nd_.SetLineWeight(4);
plot  FractalL2nd_ =  if FractalL2nd  then low else Double.NaN;
FractalL2nd_ .SetPaintingStrategy(PaintingStrategy.POINTS);
FractalL2nd_ .SetDefaultColor(Color.RED);
FractalL2nd_ .SetLineWeight(4);
plot  FractalH3rd_ = if FractalH3rd  then high else Double.NaN;
FractalH3rd_.SetPaintingStrategy(PaintingStrategy.POINTS);
FractalH3rd_.SetDefaultColor(Color.CYAN);
FractalH3rd_.SetLineWeight(5);
plot  Fractall3rd_ = if FractalL3rd  then low else Double.NaN;
Fractall3rd_ .SetPaintingStrategy(PaintingStrategy.POINTS);
Fractall3rd_ .SetDefaultColor(Color.CYAN);
Fractall3rd_ .SetLineWeight(5);
AddChartBubble(show1rstbubble && FractalH1rst,high,if between(FractalHv,FractalHv[1]*.99,FractalHv[1]*1.01) then "DT" else
if between(FractalHv,Fractallv[2]*.99,FractalHv[2]*1.01) then "RS" else
if FractalHv>FractalHv[1]*1.01 then "hh" else "lh",if RocketRSI(10, 8) > 1.5 then Color.ORANGE else Color.GRAY);
AddChartBubble(show1rstbubble and FractalL1rst,low,if between(Fractallv,Fractallv[1]*.99,Fractallv[1]*1.01) then "DB" else
if between(Fractallv,Fractalhv[2]*.99,Fractalhv[2]*1.01) then "SP" else
if Fractallv<Fractallv[1]*.99 then "ll" else "hl", if RocketRSI(10, 8) < -1.5 then Color.ORANGE else Color.GRAY);
AddChartBubble(show2ndbubble and FractalH2nd_, high,if between(FractalH2ndv,FractalH2ndv[1]*.99,FractalH2ndv[1]*1.01) then "DT" else
if between(FractalH2ndv,Fractall2ndv[2]*.99,Fractall2ndv[2]*1.01) then "RS" else
if FractalH2ndv>FractalH2ndv[1]*1.01 then "HH" else "LH",  if RocketRSI(10, 8) > 1.5 then Color.light_green else Color.GREEN);
AddChartBubble(show2ndbubble and FractalL2nd_,low,if between(Fractall2ndv,Fractall2ndv[1]*.99,Fractall2ndv[1]*1.01) then "DB" else
if between(Fractall2ndv,Fractalh2ndv[2]*.99,Fractalh2ndv[2]*1.01) then "SP" else
if Fractall2ndv<Fractall2ndv[1]*.99 then "LL" else "HL", if RocketRSI(10, 8) < -1.5 then Color.light_green else Color.GREEN);
AddChartBubble(show3rdbubble and FractalH3rd_,high,if between(FractalH3rdv,FractalH3rdv[1]*.99,FractalH3rdv[1]*1.01) then "DT" else
if between(FractalH3rdv,Fractall3rdv[2]*.99,Fractall3rdv[2]*1.01) then "RS" else
if FractalH3rdv>FractalH3rdv[1]*1.01 then "HH" else "LH",  if RocketRSI(10, 8) > 1.5 then Color.magenta else Color.CYAN);
AddChartBubble(show3rdbubble and Fractall3rd_, low ,if between(Fractall3rdv,Fractall3rdv[1]*.99,Fractall3rdv[1]*1.01) then "DB" else
if between(Fractall3rdv,Fractalh3rdv[2]*.99,Fractalh3rdv[2]*1.01) then "SP" else
if Fractall3rdv<Fractall3rdv[1]*1.01 then "LL" else "HL", if RocketRSI(10, 8) < -1.5 then Color.magenta else Color.CYAN);
#### count between higher fractals
def countreset1rst =  (Fractalh or FractalL);
def countreset1rst_ = if IsNaN(countreset1rst) then 0 else  countreset1rst;
def countreset2nd =  (FractalH2nd or FractalL2nd);
def countreset2nd_ = if IsNaN(countreset2nd) then 0 else  countreset2nd ;
def countbetween2nd  = CompoundValue(1,  if countreset2nd_ then 0 else if  (Fractalh or  FractalL) then countbetween2nd[1] + 1 else if countbetween2nd[1]>=3 && countreset2nd_[2] then 0 else countbetween2nd[1], 0);
def countreset3rd = (FractalL3rd or FractalH3rd);
def countreset3rd_ = if IsNaN(countreset3rd) then 0 else  countreset3rd;
def countbetween3rd  = CompoundValue(1,  if countreset3rd_ then 0 else if  (Fractalh or  FractalL) then countbetween3rd [1] + 1 else countbetween3rd [1], 0);
def countreset2nd3rd =  (FractalL3rd or FractalH3rd);
def countreset2nd3rd_ = if IsNaN(countreset3rd) then 0 else  countreset3rd;
def countbetween2nd3rd  = CompoundValue(1,  if countreset2nd3rd_ then 0 else if  (FractalH2nd_ or FractalL2nd_)  then countbetween2nd3rd[1] + 1 else countbetween2nd3rd[1], 0);
AddChartBubble(countbetween and ( Fractalh) &&  countbetween2nd , high, countbetween2nd, Color.light_green);
AddChartBubble(countbetween and ( FractalL) &&  countbetween2nd , low, countbetween2nd, Color.light_green);
AddChartBubble(countbetween and FractalH2nd_ && countbetween2nd3rd, high, countbetween2nd3rd, Color.ORANGE);
AddChartBubble(countbetween and FractalL2nd_ && countbetween2nd3rd, low,   countbetween2nd3rd, Color.ORANGE);
############################### 1).  first order fib
def bearfibpivot_ =  if IsNaN(close) then 0 else ( Fractalh && fib(FractalHv, Fractallv , prvFrH1 , tol) );
def bullfibpivot_ = if IsNaN(close) then 0 else (FractalL && fib(Fractallv , FractalHv, prvFrL1, tol ));
plot befibfractal = if bearfibpivot_  then high else Double.NaN;
befibfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
befibfractal.SetDefaultColor(Color.YELLOW);
befibfractal.SetLineWeight(5);
plot bufibfractal = if bullfibpivot_  then low else Double.NaN;
bufibfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
bufibfractal.SetDefaultColor(Color.YELLOW);
bufibfractal.SetLineWeight(5);
AddChartBubble(showFibpivotbubble and befibfractal,  high, "fib:" + fib(FractalHv, Fractallv, prvFrH1, tol).ratio, Color.LIGHT_RED);
AddChartBubble(showFibpivotbubble  and bufibfractal, low, "fib: " + fib(Fractallv, FractalHv, prvFrL1, tol).ratio, Color.LIGHT_GREEN);
###############################  2). first order fib relation to 2nd order
def maxvalue = if FrH2ndbn > Frl2ndbn then  FractalH2ndv  else  Fractall2ndv;
def maxvalue2 = if maxvalue == Fractall2ndv then FractalH2ndv  else Fractall2ndv  ;
def bearfibpivot2_ =  if IsNaN(close) then 0 else (Fractalh && fib(FractalHv, maxvalue , maxvalue2, tol))  ;
def bullfibpivot2_ = if IsNaN(close) then 0 else (FractalL && fib(Fractallv, maxvalue , maxvalue2, tol));
plot befibfractal2 = if bearfibpivot2_   then high else Double.NaN;
befibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
befibfractal2.SetDefaultColor(Color.CYAN);
befibfractal2.SetLineWeight(5);
plot bufibfractal2 = if bullfibpivot2_   then low else Double.NaN;
bufibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
bufibfractal2.SetDefaultColor(Color.CYAN);
bufibfractal2.SetLineWeight(5);
AddChartBubble(showFibrelationbubble and befibfractal2,  high, "2ndfib:" +  fib(FractalHv, maxvalue , maxvalue2, tol).ratio, Color.CYAN);
AddChartBubble(showFibrelationbubble  and bufibfractal2, low, "2ndfib: " + fib(Fractallv, maxvalue , maxvalue2, tol).ratio, Color.CYAN);
def maxlvalue = if FrlH3rdbn > Frll3rdbn then FractalH3rdv  else Fractall3rdv;
def maxlvalue2 = if maxlvalue == Fractall3rdv  then FractalH3rdv else Fractall3rdv ;
###############################  3). first order fib relation to 3rd order
def lbearfibpivot2_ =  if IsNaN(close) then 0 else (Fractalh && fib(FractalHv, maxlvalue , maxlvalue2, tol))  ;
def lbullfibpivot2_ = if IsNaN(close) then 0 else (FractalL && fib(Fractallv , maxlvalue , maxlvalue2, tol));
plot belfibfractal2 = if lbearfibpivot2_  then high else Double.NaN;
belfibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
belfibfractal2.SetDefaultColor(Color.ORANGE);
belfibfractal2.SetLineWeight(5);
plot bulfibfractal2 = if lbullfibpivot2_   then low else Double.NaN;
bulfibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
bulfibfractal2.SetDefaultColor(Color.ORANGE);
bulfibfractal2.SetLineWeight(5);
AddChartBubble(showFibrelationbubble and belfibfractal2,  high, "3rdfib:" +  fib(FractalHv, maxlvalue , maxlvalue2, tol).ratio, Color.CYAN);
AddChartBubble(showFibrelationbubble  and bulfibfractal2, low, "3rdfib: " + fib(Fractallv, maxlvalue , maxlvalue2, tol).ratio, Color.CYAN);
###############################  1). 2nd order fib relationship
def mbearfibpivot_ =  if IsNaN(close) then 0 else (FractalH2nd && fib(prvFr2dnH1, prvFr2dnL1, prvFr2dnH2  , tol))  ;
def mbullfibpivot_ = if IsNaN(close) then 0 else (FractalL2nd && fib(prvFr2dnL1, prvFr2dnH1, prvFr2dnL2, tol));
plot mbefibfractal = if mbearfibpivot_  then high else Double.NaN;
mbefibfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
mbefibfractal.SetDefaultColor(Color.YELLOW);
mbefibfractal.SetLineWeight(5);
plot mbufribfractal = if mbullfibpivot_  then low else Double.NaN;
mbufribfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
mbufribfractal.SetDefaultColor(Color.YELLOW);
mbufribfractal.SetLineWeight(5);
AddChartBubble(showFibpivotbubble  and mbefibfractal,  high, "fib: " + fib(FractalH2ndv, Fractall2ndv, prvFrH3rd1 , tol).ratio, Color.LIGHT_RED);
AddChartBubble(showFibpivotbubble  and mbufribfractal,  low, "fib: " + fib(Fractall2ndv, FractalH2ndv, prvFr2dnL1, tol).ratio, Color.LIGHT_GREEN);
############################### 1).  2nd order fib relation to 3rd order
def mlbearfibpivot2_ =  if IsNaN(close) then 0 else ( FractalH2nd && fib(FractalH2ndv, maxlvalue , maxlvalue2, tol));
def mlbullfibpivot2_ = if IsNaN(close) then 0 else (FractalL2nd && fib(Fractall2ndv, maxlvalue , maxlvalue2, tol));
plot mbelfibfractal2 = if mlbearfibpivot2_   then high else Double.NaN;
mbelfibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
mbelfibfractal2.SetDefaultColor(Color.ORANGE);
mbelfibfractal2.SetLineWeight(5);
plot mbulfibfractal2 = if mlbullfibpivot2_   then low else Double.NaN;
mbulfibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
mbulfibfractal2.SetDefaultColor(Color.ORANGE);
mbulfibfractal2.SetLineWeight(5);
AddChartBubble(showFibrelationbubble and mbelfibfractal2,  high, "3rdfib:" +  fib(FractalH2ndv, maxlvalue , maxlvalue2, tol).ratio, Color.CYAN);
AddChartBubble(showFibrelationbubble and mbulfibfractal2, low, "3rdfib: " + fib(Fractall2ndv, maxlvalue , maxlvalue2, tol).ratio, Color.CYAN);
###############################  2).3rd order fib relationship
def lbearfibpivot_ =  if IsNaN(close) then 0 else (FractalH3rd && fib(FractalH3rdv, Fractall3rdv, prvFrH3rd1, tol));
def lbullfibpivot_ = if IsNaN(close) then 0 else (FractalL3rd && fib(Fractall3rdv, FractalH3rdv, prvFrL3rd1, tol));
plot lbefibfractal = if lbearfibpivot_   then high else Double.NaN;
lbefibfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
lbefibfractal.SetDefaultColor(Color.YELLOW);
lbefibfractal.SetLineWeight(5);
plot lbufribfractal = if lbullfibpivot_   then low else Double.NaN;
lbufribfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
lbufribfractal.SetDefaultColor(Color.YELLOW);
lbufribfractal.SetLineWeight(5);
AddChartBubble(showFibpivotbubble  and lbefibfractal,  high, "fib: " + fib(FractalH3rdv, Fractall3rdv, prvFrH3rd1, tol).ratio, Color.LIGHT_RED);
AddChartBubble(showFibpivotbubble  and lbufribfractal,  low, "fib: " + fib(Fractall3rdv, FractalH3rdv, prvFrL3rd1, tol).ratio, Color.LIGHT_GREEN);
## higher highs and lower lows
def fr1lowconsect = if Fractall && Fractallv<Fractallv[1] then fr1lowconsect[1]+1 else if Fractall && Fractallv>Fractallv[1] then 0 else fr1lowconsect[1];
def fr1highconsect = if Fractalh && FractalHv>FractalHv[1] then fr1highconsect[1]+1 else if Fractalh && FractalHv<FractalHv[1] then 0 else fr1highconsect[1];
def fr1lowconsect2 = if Fractall && Fractall2ndv<Fractall2ndv[1] then fr1lowconsect2[1]+1 else if Fractall && Fractall2ndv>Fractall2ndv[1] then 0 else fr1lowconsect2[1];
def fr1highconsect2 = if Fractalh &&  Fractalh2ndv>Fractalh2ndv[1] then fr1highconsect2[1]+1 else if Fractalh && Fractalh2ndv<Fractalh2ndv[1] then 0 else fr1highconsect2[1];
AddChartBubble(showHHLLcount && FractalH1rst && fr1highconsect ,high,fr1highconsect, Color.white);
AddChartBubble(showHHLLcount and Fractall1rst&& fr1lowconsect,low,fr1lowconsect, Color.white);
AddChartBubble(showHHLLcount && FractalH2nd_ && fr1highconsect2,high,fr1highconsect2, Color.ORANGE);
AddChartBubble(showHHLLcount and Fractall2nd_ && fr1lowconsect2,low,fr1lowconsect2, Color.ORANGE);
input trendmult = 1.0;
def minbb = Min(HighestAll( FrlH3rdbn ), HighestAll(Frll3rdbn ));
def maxbb= Max(HighestAll( FrlH3rdbn ), HighestAll(Frll3rdbn ));
def stopmax =  bn ==maxbb;
def stopmax_ = if IsNaN(close) then 0 else stopmax;
def stopmin =  bn ==  minbb;
def stopmin_ = if IsNaN(close) then 0 else stopmin;
def countbetween3rdbar  = CompoundValue(1,  if countreset2nd3rd_ then bn else countbetween3rdbar [1], bn);
def countbetween3rdbar2  = CompoundValue(1,  if countbetween3rdbar > countbetween3rdbar[1] then countbetween3rdbar[1] else countbetween3rdbar2[1], bn);
def countbetween3rdbar3  = CompoundValue(1,  if countbetween3rdbar2 > countbetween3rdbar2[1] then countbetween3rdbar2[1] else countbetween3rdbar3[1], bn);
def countbetween3rdbar4  = CompoundValue(1,  if countbetween3rdbar3 > countbetween3rdbar3[1] then countbetween3rdbar3[1] else countbetween3rdbar4[1], bn);
def countbetween3rdbar5  = CompoundValue(1,  if countbetween3rdbar4 > countbetween3rdbar4[1] then countbetween3rdbar4[1] else countbetween3rdbar5[1], bn);
def countbetween3rdv  = CompoundValue(1,  if countreset2nd3rd_ && FractalH3rd  then high else  if countreset2nd3rd_ && FractalL3rd then low else countbetween3rdv [1], 1);
def countbetween3rdv2  = CompoundValue(1,  if countbetween3rdv <> countbetween3rdv[1] then countbetween3rdv[1] else countbetween3rdv2[1], 1);
def countbetween3rdv3  = CompoundValue(1,  if countbetween3rdv2 <> countbetween3rdv2[1] then countbetween3rdv2[1] else countbetween3rdv3[1], 1);
def countbetween3rdv4  = CompoundValue(1,  if countbetween3rdv3 <> countbetween3rdv3[1] then countbetween3rdv3[1] else countbetween3rdv4[1], 1);
def countbetween3rdv5  = CompoundValue(1,  if countbetween3rdv4 <> countbetween3rdv4[1] then countbetween3rdv4[1] else countbetween3rdv5[1], 1);
def lowl ;
def highl ;
def Xratio;
def Yratio;
input projectionOrder = {twothree, threefour, default fourfive};
switch (projectionOrder ) {
case twothree:
lowl = if countbetween3rdv && FractalL3rd then low else if countbetween3rdv && Fractalh3rd then high-AbsValue(countbetween3rdv2 - countbetween3rdv3) else lowl[1];
highl = if countbetween3rdv && Fractalh3rd then high else if countbetween3rdv && Fractall3rd then low+AbsValue(countbetween3rdv2- countbetween3rdv3) else highl[1];
Xratio = absvalue(lowl- highl);
Yratio = AbsValue(countbetween3rdbar2 - countbetween3rdbar3 );
case threefour:
lowl = if countbetween3rdv && FractalL3rd then low else if countbetween3rdv && Fractalh3rd then high-AbsValue(countbetween3rdv3 - countbetween3rdv4) else lowl[1];
highl = if countbetween3rdv && Fractalh3rd then high else if countbetween3rdv && Fractall3rd then low+AbsValue(countbetween3rdv3- countbetween3rdv4) else highl[1];
Xratio = absvalue(lowl- highl);
Yratio = AbsValue(countbetween3rdbar3 - countbetween3rdbar4 );
case fourfive:
lowl = if countbetween3rdv && FractalL3rd then low else if countbetween3rdv && Fractalh3rd then high-AbsValue(countbetween3rdv4- countbetween3rdv5) else lowl[1];
highl = if countbetween3rdv && Fractalh3rd then high else if countbetween3rdv && Fractall3rd then low+AbsValue(countbetween3rdv4- countbetween3rdv5) else highl[1];
Xratio = absvalue(lowl- highl);
Yratio = AbsValue(countbetween3rdbar4 - countbetween3rdbar5 );
}
def change =  Xratio*trendmult  / Yratio;
def maxcount = CompoundValue(1, if stopmax_ then 0 else maxcount[1] + 1, 0);
def Revmax = CompoundValue(1, if stopmax_ then (change) else Revmax[1], Double.NaN);
def dnline = CompoundValue(1, if stopmax_ &&  minbb== FrlH3rdbn then countbetween3rdv  else if maxcount > (yratio *trendmult) then Double.NaN else ( dnline[1] + Revmax ), Double.NaN);
def dnlinel = CompoundValue(1, if stopmax_ && minbb == Frll3rdbn then countbetween3rdv  else if maxcount > (yratio *trendmult) then Double.NaN else  (dnlinel[1] - Revmax) , Double.NaN);
def activepro =  if showprojection==no then double.nan else stopmax >= 0 && minbb == Frll3rdbn;
plot  dnlineProj = if activepro  then dnlinel else  dnline;
dnlineProj.SetDefaultColor(Color.CYAN);
dnlineProj.SetLineWeight(2);
dnlineProj.SetStyle(Curve.SHORT_DASH);
plot hiline =  if dnlineProj then highl
                else double.nan;
hiline.SetDefaultColor(color.red);
plot loline = if dnlineProj then lowl
                else double.nan;
loline.SetDefaultColor(color.cyan);
def range3rd = absvalue(loline - hiline) ;
plot fib146 = if activepro then  hiline - (range3rd *0.146)  else   loline +(range3rd *0.146) ;
fib146.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection  and !isNaN(fib146[1]) and isNaN(fib146[-1]),fib146,"fib146" ,color.cyan);
plot fib236 = if activepro then     hiline - (range3rd *0.236)  else   loline +(range3rd *0.236) ;
fib236.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection  and  !isnan(fib236[1]) && isnan(fib236[-1]),fib236,"fib236" ,color.cyan);
plot fib382 = if activepro then   hiline - (range3rd *0.382)  else  loline +(range3rd *0.382) ;
fib382.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib382[1]) and isNaN(fib382[-1]),fib382,"fib382" ,color.cyan);
plot fib50= if activepro then  hiline - (range3rd *0.50)  else   loline +(range3rd *0.50) ;
fib50.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib50[1]) and isNaN(fib50[-1]),fib50,"fib50" ,color.cyan);
plot fib618 = if activepro then  hiline - (range3rd *0.618)  else   loline +(range3rd *0.618) ;
fib618.SetDefaultColor(Color.GREEN);
plot  mid382 = if between(dnlineProj,fib382,fib618) or  between(dnlineProj,fib618,fib382) then fib382 else double.nan;
mid382.SetDefaultColor(Color.blue);
plot  mid618 = if between(dnlineProj,fib382,fib618)  or  between(dnlineProj,fib618,fib382) then fib618 else double.nan;
mid618.SetDefaultColor(Color.blue);
addcloud(mid382 ,mid618,color.green,color.green);
addcloud(fib382 ,fib618,color.cyan,color.cyan);
AddChartBubble( showprojection and !isNaN(fib618[1]) and isNaN(fib618[-1]),fib618,"fib618" ,color.cyan);
plot fib786= if activepro then   hiline - (range3rd *0.786)  else   loline +(range3rd *0.786) ;
fib786.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib786[1]) and isNaN(fib786[-1]),fib786,"fib786" ,color.cyan);
plot fib886= if activepro then   hiline - (range3rd *0.886)  else   loline +(range3rd *0.886) ;
fib886.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib886[1]) and isNaN(fib886[-1]),fib886,"fib886" ,color.cyan);
plot fib1= if activepro then   hiline - (range3rd *1)  else   loline +(range3rd *1) ;
fib1.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib1[1]) and isNaN(fib1[-1]),fib1,"fib1" ,color.cyan);
plot fib1236= if activepro then  hiline - (range3rd *1.236)  else   loline +(range3rd *1.236) ;
fib1236.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib1236[1]) and isNaN(fib1236[-1]),fib1236,"fib1236" ,color.cyan);
AddLabel(yes, " 1rstPcount :  " + pivotcal , Color.WHITE);
AddLabel(yes, "2ndPcount:  " + pivotcal2nd, Color.CYAN);
AddLabel(yes, "3rdPcount:  " + pivotcal3rd , Color.ORANGE);
#### under construction
###input equityrisk =10000.00;
##input showriskreward = yes;
def fib2618 = 2.618;
def fib1618= 1.618;
def xatimebu = AbsValue(countbetween3rdbar2 - countbetween3rdbar3 );
def  xdtimebu = AbsValue(countbetween3rdbar5 - countbetween3rdbar3 );
def przrangebull =Xratio;
Def bullPRZ=absvalue(przrangebull/3) ;
def tradetimebu= round(((xatimebu+xdtimebu)/2)*fib2618,0);
def brsXADtimebu= CompoundValue(1, if activepro  then 1 else if maxcount==(tradetimebu) then 0 else brsXADtimebu[1], 0);
##plot bullstop = if  brsXADtimebu then hiline else loline;
##plot bullentry1 =  if brsXADtimebu then ( bullstop +bullPRZ) else ( bullstop -bullPRZ);
##plot bullPRZa =  if brsXADtimebu  then ( bullstop -bullPRZ)  else ( bullstop +bullPRZ);
##AddCloud(bullentry1, bullPRZa, Color.gray, Color.gray);


please include all headers from the original scripter. It's considered theft by many. Addtionally, if the coder was gracious enough to openly share their work, the least you could do is give them credit.

here is the header:

#################################### Peak_ Trough_analysis_Mcdon030_V3 10/12/2019 ######################################

REgards, the author
 
Code:
###  Resources:
## 1). interpretation of "Scientific Guide to Price Action and Pattern Trading" Author: Young Ho Seo
## 2). https://www.mql5.com/en/articles/1470
## 3). Semafor_3Level_ZZ
### TOS Script resources:
## 1). initial pivot from Mobius Pivot Array
## 2). folds from Dmonkey Examples && JQ's one note
## 3). Mobius Trend Pivots shared 2/4/2019
## 4). ZZtop JQ onenote Author unidentified
## 5). #StudyName: Dilbert_1st2nd3rdOrderPivotFibs_V4
## script guide:
##  1). Inputs:
##    a.  first logic cycles pivots to ZZ:  lines 140-144
##    b.  HHLL script cycles higher highs until a lower low occurs and vice versa  lines 88-130
##    c.  show 1rst,2nd and 3rd bubble show Double bottom, Double Top, Resistance, support and rocketRSI color  : lines 264-281
##    d.  countbetween counts pivots between higher pivots : lines 380-388
## 2). fib relationships  with higher to lower pivots  : lines 299-379
## 3).  : lines 389-374
##    a. fib projection initiates when 3rd order pivot equals 2nd order pivot
##    b. projection order input is similiar to harmonic XABCD trading
##       - twothree equals BC trend with fib levels CD
##       - threefour equals AB trend with fib levels BC
##       - fourfive equals XA trend with fib levels AB
### issues and needed additions:
## 1). fibrelationships to higher pivots need work
## 2). projections may need extended lines when price fails to breach High and low
## 3). ***** need scan for active projection bar  ...  help
## 4). needs over bought and over sold trend lines
## 5). needs linear lines for ascending and descending trends
## 6).  i have a condensed version of a patternfinder(trangles, doubletops, doublebottoms, head and shoulders, wedge,etc) which paints required trendlines if needed, request..
input n = 4;
input tol = .04; ## ratio tolerance
input show1rstzz = yes;
input show2ndzz = yes;
input show3rdzz = yes;
input show1rstbubble = no;
input show2ndbubble = yes;
input show3rdbubble =yes;
input showFibpivotbubble = no;
input showFibrelationbubble = no;
input showprojection = no;
input showHHLLcount = no;
input countbetween = yes;
def bn = BarNumber();
def lbar = HighestAll( if IsNaN(close) then bn else 0);
script fib {
    input v1 = 0;
    input v2 = 0;
    input v3 = 0;
    input tol = 0;
    def ratio = Round((AbsValue(v1 - v2) / AbsValue(v2 - v3)), 2);
## Standard numbers: 0-0.146-0.236-0.382-0.5-0.618-0.764-0.854-1.0-1.236-1.618-2.618-4. 236-6.854
### Pesavento numbers: 0-0.382-0.5-0.618-0.707-0.786-0.854-0.886-1.0-1.128-1.272-1. 414-1.618-2.0-2.414-2. 618-4.0
### Bryce gilmore:  0.25 - 0.382 - 0.5 - 0.618 - 0.667 - 0.786 - 1.0 - 1.272 - 1.618 - 1.732 - 1.75 - 2.0 - 2.236 - 2.5 - 2.618 - 3.0 - 3.33 - 4.236 - 6.854
    def retrace = Between(ratio, (0.146 - tol), (0.146 + tol)) or Between(ratio, (.236 - tol), (.236 + tol)) or Between(ratio, (.382 - tol), (.382 + tol)) or
    Between(ratio, (.50 - tol), (.50 + tol)) or Between(ratio, (.618 - tol), (.618 + tol)) or  Between(ratio, (.667 - tol), (.667 + tol)) or Between(ratio, (.707 - tol), (.707 + tol)) or
    Between(ratio, (.764 - tol), (.764 + tol)) or Between(ratio, (.786 - tol), (.786 + tol)) or Between(ratio, (.854 - tol), (.854 + tol)) or Between(ratio, (.886 - tol), (.886 + tol)) or
    Between(ratio, (1 - tol), (1 + tol)) or Between(ratio, (1.236 - tol), (1.236 + tol)) or Between(ratio, (1.41 - tol), (1.414 + tol)) or Between(ratio, (1.618 - tol), (1.618 + tol)) or
    Between(ratio, (1.732 - tol), (1.732 + tol)) or Between(ratio, (1.75 - tol), (1.75 + tol)) or Between(ratio, (2.00 - tol), (2.000 + tol)) or Between(ratio, (2.236 - tol), (2.236 + tol)) or
    Between(ratio, (2.414 - tol), (2.414 + tol)) or Between(ratio, (2.50 - tol), (2.50 + tol)) or Between(ratio, (2.618 - tol), (2.618 + tol))  or Between(ratio, (3 - tol), (3 + tol)) or
    Between(ratio, (3.33 - tol), (3.33 + tol)) or Between(ratio, (4 - tol), (4 + tol)) or Between(ratio, (4.236 - tol), (4.236 + tol))  or Between(ratio, (6.85 - tol), (6.85 + tol));
    plot one = if IsNaN(0) then  0 else retrace ;
    def getratio = if one then ratio else Double.NaN;
}
script F {
    input value = close ;
    def bn = BarNumber();
    def lbar = HighestAll(if IsNaN(close) then 0 else bn);
    plot xd = fold di = 1 to lbar with da = Double.NaN while IsNaN(da) do if GetValue(value, -di) then di else Double.NaN;
}
script paint {
    input v1 = 0.0 ;
    input v2 = 0.0;
    def bn = BarNumber();
    def lv1bar = HighestAll(if v1 then bn else 0);
    def lv2bar = HighestAll(if v2 then bn else 0);
    def lbar = HighestAll(if IsNaN(close) then 0 else bn);
    plot va1 = if bn >= lv1bar then 0 else fold id = 1 to lbar with t = 0 while t == 0 do if GetValue(v1, -id) then id else 0;
    plot va2 = if bn >= lv2bar  then 0 else fold sd = 1 to lbar with x = 0 while x == 0 do if GetValue(v2, -sd) then sd else 0;
    def for =  GetValue(v2, -va1);
    def for2 =  GetValue(for, - va1);
    plot for3 =  GetValue(for2, - va1);
    plot fva1 =  GetValue(high, -va1);
    plot fva2 = GetValue(low, -va2);
}
#### plot highest high trend until low trend and vice versa
script HHLL {
    input fractall= 0.0;
    input fractalh = 0.0;
def Fractallv = if fractall then low else Fractallv[1];
def Fractalhv = if fractalh then high else Fractalhv[1];
def bn = BarNumber();
def lhighestbar = highestall( if FractalL then bn else 0);
def hhighestbar= highestall( if Fractalh then bn else 0);
def lbar = HighestAll(if IsNaN(close) then 0 else bn);
                def fr1lowc1 = compoundValue(1,if Fractall && Fractallv<=Fractallv[1] then fr1lowc1 [1]+1 else if Fractall && Fractallv>Fractallv[1] then 0 else fr1lowc1 [1],0);
                def fr1highc1= compoundValue(1,if Fractalh && FractalHv>=FractalHv[1] then fr1highc1[1]+1 else if Fractalh && FractalHv<FractalHv[1] then 0 else fr1highc1[1],0);
                def maxl = if Fractall && fr1lowc1>=1  && lhighestbar  then 1 else Double.NaN;
                def maxh = if Fractalh && fr1highc1>=1&& hhighestbar then 1 else Double.NaN;
                def maxlv = if  !isnan(maxl) then low else  maxlv[1];
                def maxhv=  if  !isnan(maxh) then high  else maxhv[1];
                def maxlhighestbar = highestall( if !isnan(maxl) then bn else 0);
                def maxhhighestbar= highestall( if !isnan(maxh)then bn else 0);
                def maxlct =  compoundValue(1,if   !isnan(maxh[1]) then 0 else if (Fractall && !isnan(maxl)) then  maxlct[1]+1 else maxlct[1],0);
                def maxhct = compoundValue(1, if  !isnan(maxl[1]) then 0 else if (Fractalh && !isnan(maxh)) then  maxhct[1]+1 else maxhct[1],0);
                def maxld = fold dig = 1 to lbar with dag = Double.NaN while IsNaN(dag) do if GetValue(maxl, -dig) then dig else Double.NaN;
                def maxhd = fold die = 1 to lbar with dae = Double.NaN while IsNaN(dae) do if GetValue(maxh, -die) then die else Double.NaN;
def LLja  = if bn>=maxlhighestbar &&maxl then 1 else
                (if !isnan(maxl) && getvalue(maxlct,(-maxld))>1 && getvalue(low,(-maxld))< maxlv or
                !isnan(maxl) && getvalue(maxlct,(-maxld))>maxlct && maxlv < maxlv[1] or
                !isnan(maxl) &&  maxlct>1 && maxlv> maxlv[1]or
                !isnan(maxh) then  double.nan else if
                !isnan(maxl) &&  maxlct<=1  && getvalue(maxlct,(-maxld))==1 or
                !isnan(maxl) && getvalue(maxlct,(-maxld))>maxlct && getvalue(low,(-maxld))> maxlv or
                !isnan(maxl) &&  maxlct>=1  && getvalue(maxhct,(-fractalh))>=1 then 1 else 0 ) ;
def hhja  = if bn>=maxhhighestbar  then 1 else        
              (if !isnan(maxh) && getvalue(maxhct,(-maxhd))>maxhct &&  getvalue(high,(-maxhd))> maxhv or
               !isnan(maxh) && getvalue(maxhct,(-maxhd))>maxhct && maxhv > maxhv[1] or
              !isnan(maxh) &&  maxhct>1 &&  maxhv< maxhv[1]or
               !isnan(maxl) then  double.nan else if
               !isnan(maxh) &&  maxhct<=1 && getvalue(maxhct,(-maxhd))==1 or
               !isnan(maxh) && getvalue(maxhct,(-maxhd))>maxhct && getvalue(high,(-maxhd))< maxhv or
                 !isnan(maxh) &&  maxhct>=1 && getvalue(maxlct,(-maxld))>=1 then 1 else 0 ) ;
def LLJb =  (!isnan(maxl) && Fractall && !isnan(LLja)) ;
def HHJb =  (!isnan(maxh) && Fractalh && !isnan(HHja )) ;
def LLJc= if isnan(LLJb) then 0 else  (LLJb ) ;
def  HHJc= if isnan(HHJb) then 0 else  (HHJb ) ;
plot hhj =   if IsNaN(close) then 0 else( HHjc ) ;
plot llj  =  if IsNaN(close) then 0 else ( LLjc ) ;
}
## mobius pivots
def hh = fold i = 1 to n with p = 1 while p do high > GetValue(high, -i);
def PivotH = if (bn > n and   high == Highest(high, n) and     hh)   then high   else Double.NaN;
def ll = fold jk = 1 to n with q = 1 while q  do low < GetValue(low, -jk);
def PivotL = if (bn > n and   low == Lowest(low, n) and  ll) then low  else Double.NaN;
def fractalhigh1 = if IsNaN(PivotH ) then 0 else PivotH ;
def fractallow1 = if IsNaN(PivotL ) then 0 else PivotL ;
def fractc = CompoundValue(1, if IsNaN(close) then Double.NaN  else if fractalhigh1 then fractc[1] + 1 else if fractallow1  then fractc[1] + 1 else fractc[1], 1);
#### offset rotation to ZZ
def frh2v2noffset8 = if fractallow1[1] then high else if high > frh2v2noffset8[1] then high else if  fractalhigh1 then Double.NaN else if high >= frh2v2noffset8[1] then high else  frh2v2noffset8[1];
def frl2v2noffset8 = if  fractalhigh1[1] then low else if low < frl2v2noffset8[1] then low else if  fractallow1  then Double.NaN else if low <= frl2v2noffset8[1]  then low else  frl2v2noffset8[1];
def frh2v2noffsetv = if high==getvalue(frh2v2noffset8[1],-f(fractallow1)) then 1 else double.nan;
def frl2v2noffsetv = if low==getvalue(frl2v2noffset8[1],-f(fractalhigh1)) then 1 else double.nan;
#### 1rst order pivots
def Fractalh = if IsNaN(close)  then 0 else (fractalhigh1 or !isnan(frh2v2noffsetv));
def FractalL =  if  IsNaN(close) then 0 else (fractallow1  or  !isnan(frl2v2noffsetv));
def pivotcal = CompoundValue(1, if Fractalh then pivotcal [1] + 1 else if FractalL then pivotcal [1] + 1 else pivotcal [1], 1);
def FractalHc = CompoundValue(1, if Fractalh[1] then 1 else FractalHc[1] + 1, 1);
def FractalLc = CompoundValue(1, if FractalL[1] then 1 else FractalLc[1] + 1, 1);
#### 2nd order pivots
def FractalHv  = if Fractalh  then high else FractalHv[1];
def Fractallv = if FractalL  then low else Fractallv[1];
def prvFrH1 = if FractalHv <> FractalHv[1] then FractalHv[1] else prvFrH1[1];
def prvFrH2 = if FractalHv <> FractalHv[1] then prvFrH1 [1] else prvFrH2[1];
def prvFrH3 = if FractalHv <> FractalHv[1] then prvFrH2 [1] else prvFrH3[1];
def prvFrH4 = if FractalHv <> FractalHv[1] then prvFrH3 [1] else prvFrH4[1];
def FrHbn  = CompoundValue(1,  if Fractalh then bn else FrHbn[1], bn);
def FrHbn1 =  if  FrHbn > FrHbn[1] then FrHbn[1] else FrHbn1[1];
def FrHbn2 = GetValue(FrHbn1, FractalHc);
def FrHbn3 = GetValue(FrHbn2, FractalHc);
def FrHbn4 = GetValue(FrHbn3, FractalHc);
def FrHbn5 = GetValue(FrHbn4, FractalHc);
def FrHbn6 = GetValue(FrHbn5, FractalHc);
def prvFrL1 =  if Fractallv <> Fractallv[1] then Fractallv[1] else prvFrL1[1];
def prvFrL2 =  if Fractallv <> Fractallv[1] then prvFrL1[1] else prvFrL2[1];
def prvFrL3 =   if Fractallv <> Fractallv[1] then prvFrL2[1] else prvFrL3[1];
def prvFrL4 =   if Fractallv <> Fractallv[1] then prvFrL3[1] else prvFrL4[1];
def prvFrL5 =  if Fractallv <> Fractallv[1] then prvFrL4[1] else prvFrL5[1];
def Frlbn = CompoundValue(1,  if FractalL then bn else Frlbn[1], bn);
def FrLbn1 =  if  Frlbn > Frlbn[1] then Frlbn[1] else FrLbn1[1];
def FrLbn2 =  GetValue(FrLbn1, FractalLc);
def FrLbn3 = GetValue(FrLbn2, FractalLc);
def FrLbn4 = GetValue(FrLbn3, FractalLc);
def FrLbn5 = GetValue(FrLbn4, FractalLc);
#### 3rd  order pivots
def FractalH2nd =  HHLL(fractall,fractalh ).hhj;
def FractalL2nd  =  HHLL(fractall,fractalh ).llj ;
def pivotcal2nd  = CompoundValue(1,  if FractalH2nd then pivotcal2nd  [1] + 1 else if FractalL2nd  then pivotcal2nd[1] + 1 else pivotcal2nd[1], 1);
def Fractalh2ndc = CompoundValue(1, if FractalH2nd[1] then 1 else Fractalh2ndc[1] + 1, 1);
def Fractall2ndc = CompoundValue(1, if FractalL2nd [1] then 1 else Fractall2ndc[1] + 1, 1);
def FractalH2ndv  = if FractalH2nd  then high else FractalH2ndv[1];
def Fractall2ndv = if FractalL2nd  then low else Fractall2ndv[1];
def prvFr2dnH1 =  if  FractalH2ndv <>  FractalH2ndv[1] then  FractalH2ndv[1] else prvFr2dnH1[1];
def prvFr2dnH2 = if  prvFr2dnH1 <>  prvFr2dnH1[1] then  prvFr2dnH1[1] else prvFr2dnH2[1];
def prvFr2dnH3 =  if  prvFr2dnH2 <>  prvFr2dnH2[1] then  prvFr2dnH2[1] else prvFr2dnH3[1];
def prvFr2dnH4 =  if  prvFr2dnH3 <>  prvFr2dnH3[1] then  prvFr2dnH3[1] else prvFr2dnH3[1];
def prvFr2dnH5 = if  prvFr2dnH4 <>  prvFr2dnH4[1] then  prvFr2dnH4[1] else prvFr2dnH4[1];
def prvFr2dnL1 = if  FractalH2ndv <>  FractalH2ndv[1] then  FractalH2ndv[1] else prvFr2dnL1 [1];
def prvFr2dnL2 = if   prvFr2dnL1 <>   prvFr2dnL1[1] then   prvFr2dnL1[1] else  prvFr2dnL2 [1];
def prvFr2dnL3 =if   prvFr2dnL2 <>   prvFr2dnL2[1] then   prvFr2dnL2[1] else  prvFr2dnL3 [1];
def prvFr2dnL4 =if   prvFr2dnL3 <>   prvFr2dnL3[1] then   prvFr2dnL3[1] else  prvFr2dnL4 [1];
def prvFr2dnL5 =if   prvFr2dnL4 <>   prvFr2dnL4[1] then   prvFr2dnL4[1] else  prvFr2dnL5 [1];
def FrH2ndbn  = CompoundValue(1,  if FractalH2nd  then bn else FrH2ndbn[1], bn);
def FrH2nd2bn  = GetValue(bn, Fractalh2ndc[1]);
def Frl2ndbn = CompoundValue(1,  if FractalL2nd  then bn else Frl2ndbn[1], bn);
def Frl2nd2bn =  GetValue(bn, Fractall2ndc);
def  FractalH3rd  =  HHLL(FractalL2nd,FractalH2nd).hhj;
def FractalL3rd  =   HHLL(FractalL2nd,FractalH2nd).llj ;
def Fractalh3rdc = CompoundValue(1, if FractalH3rd[1] then 1 else Fractalh3rdc[1] + 1, 1);
def Fractall3rdc = CompoundValue(1, if FractalL3rd[1] then 1 else Fractall3rdc[1] + 1, 1);
def pivotcal3rd = CompoundValue(1, if  FractalH3rd then pivotcal3rd [1] + 1 else if FractalL3rd then pivotcal3rd[1] + 1 else pivotcal3rd[1], 1);
def Fractlh3rdc = CompoundValue(1, if FractalH3rd[1] then 1 else Fractlh3rdc[1] + 1, 1);
def Fractll3rdc = CompoundValue(1, if FractalL3rd [1] then 1 else Fractll3rdc[1] + 1, 1);
def FractalH3rdv  = if FractalH3rd  then high else FractalH3rdv[1];
def Fractall3rdv = if FractalL3rd  then low else Fractall3rdv[1];
def prvFrH3rd1 = if FractalH3rdv <> FractalH3rdv[1]  then FractalH3rdv[1]  else prvFrH3rd1[1];
def prvFrH3rd2 = if IsNaN(close) then 0 else GetValue(prvFrH1, Fractalh3rdc);
def prvFrH3rd3 = if IsNaN(close) then 0 else GetValue(prvFrH2, Fractalh3rdc);
def prvFrL3rd1 = if Fractall3rdv <> Fractall3rdv[1]  then Fractall3rdv[1]  else prvFrL3rd1[1];
def prvFrL3rd2 = if IsNaN(close) then 0 else GetValue(prvFrL3rd1, Fractall3rdc);
def prvFrL3rd3 = if IsNaN(close) then 0 else GetValue(prvFrL3rd2, Fractall3rdc);
def FrlH3rdbn  = CompoundValue(1,  if FractalH3rd  then bn else FrlH3rdbn[1], bn);
def FrlH3rdbn1 =  if  FrlH3rdbn > FrlH3rdbn[1] then FrlH3rdbn[1] else FrlH3rdbn1[1];
def Frll3rdbn = CompoundValue(1,  if FractalL3rd  then bn else Frll3rdbn[1], bn);
def FrlL3rdbn1 = if  Frll3rdbn > Frll3rdbn[1] then Frll3rdbn[1] else FrlL3rdbn1[1];
def zz1rst = if  (IsNaN(FractalL)  or  IsNaN(Fractalh)) then Double.NaN else if FractalL and low then (paint(Fractalh , FractalL).fva1 - low) / (paint(FractalL, Fractalh ).va2)
else if  Fractalh  and high then ((paint(Fractalh , FractalL).fva2 - high) / (paint(Fractalh, FractalL).va2)) else zz1rst[1];
def line1rst =  if FractalL then low else if Fractalh  then high else line1rst[1] + zz1rst;
def zz2nd =  if  FractalL2nd and low then (paint(FractalH2nd , FractalL2nd ).fva1 - low) / (paint( FractalL2nd, FractalH2nd ).va2)
else if FractalH2nd  and high then ((paint(FractalH2nd , FractalL2nd).fva2 - high) / (paint(FractalH2nd , FractalL2nd).va2)) else zz2nd[1];
def line2nd = if  FractalL2nd then low else if FractalH2nd  then high else line2nd[1] + zz2nd;
def zz3rd =  if  (IsNaN(FractalL3rd)  or  IsNaN(FractalH3rd)) then Double.NaN else if  FractalL3rd  and low then (paint(FractalH3rd, FractalL3rd).fva1 - low) / (paint(FractalL3rd, FractalH3rd).va2)
else if  FractalH3rd  and high then  ((paint(FractalH3rd, FractalL3rd ).fva2 - high) / (paint(FractalH3rd, FractalL3rd).va2)) else zz3rd[1];
def line3rd = if  FractalL3rd then low else if  FractalH3rd then high else line3rd[1] + zz3rd;
DefineGlobalColor("in", CreateColor(132, 111, 122));
plot zz1rstline = if pivotcal <= 1 then Double.NaN else if show1rstzz then line1rst else Double.NaN;
zz1rstline.SetDefaultColor(Color.WHITE);
zz1rstline.AssignValueColor(GlobalColor("in"));
zz1rstline.SetStyle(Curve.SHORT_DASH);
zz1rstline.SetLineWeight(2);
plot zz2ndline  = if pivotcal2nd <= 1  then Double.NaN else if show2ndzz then line2nd else Double.NaN;
zz2ndline.SetDefaultColor(Color.WHITE);
zz2ndline.SetStyle(Curve.SHORT_DASH);
zz2ndline.SetLineWeight(2);
plot zz3rdline = if pivotcal3rd <= 1  then Double.NaN else if  show3rdzz  then line3rd else Double.NaN;
zz3rdline.SetDefaultColor(Color.CYAN);
zz3rdline.SetStyle(Curve.SHORT_DASH);
zz3rdline.SetLineWeight(3);
plot  FractalH1rst = if Fractalh  then high else Double.NaN;
FractalH1rst.SetPaintingStrategy(PaintingStrategy.POINTS);
FractalH1rst.SetDefaultColor(Color.LIGHT_GREEN);
FractalH1rst.SetLineWeight(2);
plot  FractalL1rst = if FractalL  then low else Double.NaN;
FractalL1rst.SetPaintingStrategy(PaintingStrategy.POINTS);
FractalL1rst.SetDefaultColor(Color.LIGHT_RED);
FractalL1rst.SetLineWeight(2);
plot  FractalH2nd_ = if FractalH2nd  then high else Double.NaN;
FractalH2nd_.SetPaintingStrategy(PaintingStrategy.POINTS);
FractalH2nd_.SetDefaultColor(Color.GREEN);
FractalH2nd_.SetLineWeight(4);
plot  FractalL2nd_ =  if FractalL2nd  then low else Double.NaN;
FractalL2nd_ .SetPaintingStrategy(PaintingStrategy.POINTS);
FractalL2nd_ .SetDefaultColor(Color.RED);
FractalL2nd_ .SetLineWeight(4);
plot  FractalH3rd_ = if FractalH3rd  then high else Double.NaN;
FractalH3rd_.SetPaintingStrategy(PaintingStrategy.POINTS);
FractalH3rd_.SetDefaultColor(Color.CYAN);
FractalH3rd_.SetLineWeight(5);
plot  Fractall3rd_ = if FractalL3rd  then low else Double.NaN;
Fractall3rd_ .SetPaintingStrategy(PaintingStrategy.POINTS);
Fractall3rd_ .SetDefaultColor(Color.CYAN);
Fractall3rd_ .SetLineWeight(5);
AddChartBubble(show1rstbubble && FractalH1rst,high,if between(FractalHv,FractalHv[1]*.99,FractalHv[1]*1.01) then "DT" else
if between(FractalHv,Fractallv[2]*.99,FractalHv[2]*1.01) then "RS" else
if FractalHv>FractalHv[1]*1.01 then "hh" else "lh",if RocketRSI(10, 8) > 1.5 then Color.ORANGE else Color.GRAY);
AddChartBubble(show1rstbubble and FractalL1rst,low,if between(Fractallv,Fractallv[1]*.99,Fractallv[1]*1.01) then "DB" else
if between(Fractallv,Fractalhv[2]*.99,Fractalhv[2]*1.01) then "SP" else
if Fractallv<Fractallv[1]*.99 then "ll" else "hl", if RocketRSI(10, 8) < -1.5 then Color.ORANGE else Color.GRAY);
AddChartBubble(show2ndbubble and FractalH2nd_, high,if between(FractalH2ndv,FractalH2ndv[1]*.99,FractalH2ndv[1]*1.01) then "DT" else
if between(FractalH2ndv,Fractall2ndv[2]*.99,Fractall2ndv[2]*1.01) then "RS" else
if FractalH2ndv>FractalH2ndv[1]*1.01 then "HH" else "LH",  if RocketRSI(10, 8) > 1.5 then Color.light_green else Color.GREEN);
AddChartBubble(show2ndbubble and FractalL2nd_,low,if between(Fractall2ndv,Fractall2ndv[1]*.99,Fractall2ndv[1]*1.01) then "DB" else
if between(Fractall2ndv,Fractalh2ndv[2]*.99,Fractalh2ndv[2]*1.01) then "SP" else
if Fractall2ndv<Fractall2ndv[1]*.99 then "LL" else "HL", if RocketRSI(10, 8) < -1.5 then Color.light_green else Color.GREEN);
AddChartBubble(show3rdbubble and FractalH3rd_,high,if between(FractalH3rdv,FractalH3rdv[1]*.99,FractalH3rdv[1]*1.01) then "DT" else
if between(FractalH3rdv,Fractall3rdv[2]*.99,Fractall3rdv[2]*1.01) then "RS" else
if FractalH3rdv>FractalH3rdv[1]*1.01 then "HH" else "LH",  if RocketRSI(10, 8) > 1.5 then Color.magenta else Color.CYAN);
AddChartBubble(show3rdbubble and Fractall3rd_, low ,if between(Fractall3rdv,Fractall3rdv[1]*.99,Fractall3rdv[1]*1.01) then "DB" else
if between(Fractall3rdv,Fractalh3rdv[2]*.99,Fractalh3rdv[2]*1.01) then "SP" else
if Fractall3rdv<Fractall3rdv[1]*1.01 then "LL" else "HL", if RocketRSI(10, 8) < -1.5 then Color.magenta else Color.CYAN);
#### count between higher fractals
def countreset1rst =  (Fractalh or FractalL);
def countreset1rst_ = if IsNaN(countreset1rst) then 0 else  countreset1rst;
def countreset2nd =  (FractalH2nd or FractalL2nd);
def countreset2nd_ = if IsNaN(countreset2nd) then 0 else  countreset2nd ;
def countbetween2nd  = CompoundValue(1,  if countreset2nd_ then 0 else if  (Fractalh or  FractalL) then countbetween2nd[1] + 1 else if countbetween2nd[1]>=3 && countreset2nd_[2] then 0 else countbetween2nd[1], 0);
def countreset3rd = (FractalL3rd or FractalH3rd);
def countreset3rd_ = if IsNaN(countreset3rd) then 0 else  countreset3rd;
def countbetween3rd  = CompoundValue(1,  if countreset3rd_ then 0 else if  (Fractalh or  FractalL) then countbetween3rd [1] + 1 else countbetween3rd [1], 0);
def countreset2nd3rd =  (FractalL3rd or FractalH3rd);
def countreset2nd3rd_ = if IsNaN(countreset3rd) then 0 else  countreset3rd;
def countbetween2nd3rd  = CompoundValue(1,  if countreset2nd3rd_ then 0 else if  (FractalH2nd_ or FractalL2nd_)  then countbetween2nd3rd[1] + 1 else countbetween2nd3rd[1], 0);
AddChartBubble(countbetween and ( Fractalh) &&  countbetween2nd , high, countbetween2nd, Color.light_green);
AddChartBubble(countbetween and ( FractalL) &&  countbetween2nd , low, countbetween2nd, Color.light_green);
AddChartBubble(countbetween and FractalH2nd_ && countbetween2nd3rd, high, countbetween2nd3rd, Color.ORANGE);
AddChartBubble(countbetween and FractalL2nd_ && countbetween2nd3rd, low,   countbetween2nd3rd, Color.ORANGE);
############################### 1).  first order fib
def bearfibpivot_ =  if IsNaN(close) then 0 else ( Fractalh && fib(FractalHv, Fractallv , prvFrH1 , tol) );
def bullfibpivot_ = if IsNaN(close) then 0 else (FractalL && fib(Fractallv , FractalHv, prvFrL1, tol ));
plot befibfractal = if bearfibpivot_  then high else Double.NaN;
befibfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
befibfractal.SetDefaultColor(Color.YELLOW);
befibfractal.SetLineWeight(5);
plot bufibfractal = if bullfibpivot_  then low else Double.NaN;
bufibfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
bufibfractal.SetDefaultColor(Color.YELLOW);
bufibfractal.SetLineWeight(5);
AddChartBubble(showFibpivotbubble and befibfractal,  high, "fib:" + fib(FractalHv, Fractallv, prvFrH1, tol).ratio, Color.LIGHT_RED);
AddChartBubble(showFibpivotbubble  and bufibfractal, low, "fib: " + fib(Fractallv, FractalHv, prvFrL1, tol).ratio, Color.LIGHT_GREEN);
###############################  2). first order fib relation to 2nd order
def maxvalue = if FrH2ndbn > Frl2ndbn then  FractalH2ndv  else  Fractall2ndv;
def maxvalue2 = if maxvalue == Fractall2ndv then FractalH2ndv  else Fractall2ndv  ;
def bearfibpivot2_ =  if IsNaN(close) then 0 else (Fractalh && fib(FractalHv, maxvalue , maxvalue2, tol))  ;
def bullfibpivot2_ = if IsNaN(close) then 0 else (FractalL && fib(Fractallv, maxvalue , maxvalue2, tol));
plot befibfractal2 = if bearfibpivot2_   then high else Double.NaN;
befibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
befibfractal2.SetDefaultColor(Color.CYAN);
befibfractal2.SetLineWeight(5);
plot bufibfractal2 = if bullfibpivot2_   then low else Double.NaN;
bufibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
bufibfractal2.SetDefaultColor(Color.CYAN);
bufibfractal2.SetLineWeight(5);
AddChartBubble(showFibrelationbubble and befibfractal2,  high, "2ndfib:" +  fib(FractalHv, maxvalue , maxvalue2, tol).ratio, Color.CYAN);
AddChartBubble(showFibrelationbubble  and bufibfractal2, low, "2ndfib: " + fib(Fractallv, maxvalue , maxvalue2, tol).ratio, Color.CYAN);
def maxlvalue = if FrlH3rdbn > Frll3rdbn then FractalH3rdv  else Fractall3rdv;
def maxlvalue2 = if maxlvalue == Fractall3rdv  then FractalH3rdv else Fractall3rdv ;
###############################  3). first order fib relation to 3rd order
def lbearfibpivot2_ =  if IsNaN(close) then 0 else (Fractalh && fib(FractalHv, maxlvalue , maxlvalue2, tol))  ;
def lbullfibpivot2_ = if IsNaN(close) then 0 else (FractalL && fib(Fractallv , maxlvalue , maxlvalue2, tol));
plot belfibfractal2 = if lbearfibpivot2_  then high else Double.NaN;
belfibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
belfibfractal2.SetDefaultColor(Color.ORANGE);
belfibfractal2.SetLineWeight(5);
plot bulfibfractal2 = if lbullfibpivot2_   then low else Double.NaN;
bulfibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
bulfibfractal2.SetDefaultColor(Color.ORANGE);
bulfibfractal2.SetLineWeight(5);
AddChartBubble(showFibrelationbubble and belfibfractal2,  high, "3rdfib:" +  fib(FractalHv, maxlvalue , maxlvalue2, tol).ratio, Color.CYAN);
AddChartBubble(showFibrelationbubble  and bulfibfractal2, low, "3rdfib: " + fib(Fractallv, maxlvalue , maxlvalue2, tol).ratio, Color.CYAN);
###############################  1). 2nd order fib relationship
def mbearfibpivot_ =  if IsNaN(close) then 0 else (FractalH2nd && fib(prvFr2dnH1, prvFr2dnL1, prvFr2dnH2  , tol))  ;
def mbullfibpivot_ = if IsNaN(close) then 0 else (FractalL2nd && fib(prvFr2dnL1, prvFr2dnH1, prvFr2dnL2, tol));
plot mbefibfractal = if mbearfibpivot_  then high else Double.NaN;
mbefibfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
mbefibfractal.SetDefaultColor(Color.YELLOW);
mbefibfractal.SetLineWeight(5);
plot mbufribfractal = if mbullfibpivot_  then low else Double.NaN;
mbufribfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
mbufribfractal.SetDefaultColor(Color.YELLOW);
mbufribfractal.SetLineWeight(5);
AddChartBubble(showFibpivotbubble  and mbefibfractal,  high, "fib: " + fib(FractalH2ndv, Fractall2ndv, prvFrH3rd1 , tol).ratio, Color.LIGHT_RED);
AddChartBubble(showFibpivotbubble  and mbufribfractal,  low, "fib: " + fib(Fractall2ndv, FractalH2ndv, prvFr2dnL1, tol).ratio, Color.LIGHT_GREEN);
############################### 1).  2nd order fib relation to 3rd order
def mlbearfibpivot2_ =  if IsNaN(close) then 0 else ( FractalH2nd && fib(FractalH2ndv, maxlvalue , maxlvalue2, tol));
def mlbullfibpivot2_ = if IsNaN(close) then 0 else (FractalL2nd && fib(Fractall2ndv, maxlvalue , maxlvalue2, tol));
plot mbelfibfractal2 = if mlbearfibpivot2_   then high else Double.NaN;
mbelfibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
mbelfibfractal2.SetDefaultColor(Color.ORANGE);
mbelfibfractal2.SetLineWeight(5);
plot mbulfibfractal2 = if mlbullfibpivot2_   then low else Double.NaN;
mbulfibfractal2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
mbulfibfractal2.SetDefaultColor(Color.ORANGE);
mbulfibfractal2.SetLineWeight(5);
AddChartBubble(showFibrelationbubble and mbelfibfractal2,  high, "3rdfib:" +  fib(FractalH2ndv, maxlvalue , maxlvalue2, tol).ratio, Color.CYAN);
AddChartBubble(showFibrelationbubble and mbulfibfractal2, low, "3rdfib: " + fib(Fractall2ndv, maxlvalue , maxlvalue2, tol).ratio, Color.CYAN);
###############################  2).3rd order fib relationship
def lbearfibpivot_ =  if IsNaN(close) then 0 else (FractalH3rd && fib(FractalH3rdv, Fractall3rdv, prvFrH3rd1, tol));
def lbullfibpivot_ = if IsNaN(close) then 0 else (FractalL3rd && fib(Fractall3rdv, FractalH3rdv, prvFrL3rd1, tol));
plot lbefibfractal = if lbearfibpivot_   then high else Double.NaN;
lbefibfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
lbefibfractal.SetDefaultColor(Color.YELLOW);
lbefibfractal.SetLineWeight(5);
plot lbufribfractal = if lbullfibpivot_   then low else Double.NaN;
lbufribfractal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
lbufribfractal.SetDefaultColor(Color.YELLOW);
lbufribfractal.SetLineWeight(5);
AddChartBubble(showFibpivotbubble  and lbefibfractal,  high, "fib: " + fib(FractalH3rdv, Fractall3rdv, prvFrH3rd1, tol).ratio, Color.LIGHT_RED);
AddChartBubble(showFibpivotbubble  and lbufribfractal,  low, "fib: " + fib(Fractall3rdv, FractalH3rdv, prvFrL3rd1, tol).ratio, Color.LIGHT_GREEN);
## higher highs and lower lows
def fr1lowconsect = if Fractall && Fractallv<Fractallv[1] then fr1lowconsect[1]+1 else if Fractall && Fractallv>Fractallv[1] then 0 else fr1lowconsect[1];
def fr1highconsect = if Fractalh && FractalHv>FractalHv[1] then fr1highconsect[1]+1 else if Fractalh && FractalHv<FractalHv[1] then 0 else fr1highconsect[1];
def fr1lowconsect2 = if Fractall && Fractall2ndv<Fractall2ndv[1] then fr1lowconsect2[1]+1 else if Fractall && Fractall2ndv>Fractall2ndv[1] then 0 else fr1lowconsect2[1];
def fr1highconsect2 = if Fractalh &&  Fractalh2ndv>Fractalh2ndv[1] then fr1highconsect2[1]+1 else if Fractalh && Fractalh2ndv<Fractalh2ndv[1] then 0 else fr1highconsect2[1];
AddChartBubble(showHHLLcount && FractalH1rst && fr1highconsect ,high,fr1highconsect, Color.white);
AddChartBubble(showHHLLcount and Fractall1rst&& fr1lowconsect,low,fr1lowconsect, Color.white);
AddChartBubble(showHHLLcount && FractalH2nd_ && fr1highconsect2,high,fr1highconsect2, Color.ORANGE);
AddChartBubble(showHHLLcount and Fractall2nd_ && fr1lowconsect2,low,fr1lowconsect2, Color.ORANGE);
input trendmult = 1.0;
def minbb = Min(HighestAll( FrlH3rdbn ), HighestAll(Frll3rdbn ));
def maxbb= Max(HighestAll( FrlH3rdbn ), HighestAll(Frll3rdbn ));
def stopmax =  bn ==maxbb;
def stopmax_ = if IsNaN(close) then 0 else stopmax;
def stopmin =  bn ==  minbb;
def stopmin_ = if IsNaN(close) then 0 else stopmin;
def countbetween3rdbar  = CompoundValue(1,  if countreset2nd3rd_ then bn else countbetween3rdbar [1], bn);
def countbetween3rdbar2  = CompoundValue(1,  if countbetween3rdbar > countbetween3rdbar[1] then countbetween3rdbar[1] else countbetween3rdbar2[1], bn);
def countbetween3rdbar3  = CompoundValue(1,  if countbetween3rdbar2 > countbetween3rdbar2[1] then countbetween3rdbar2[1] else countbetween3rdbar3[1], bn);
def countbetween3rdbar4  = CompoundValue(1,  if countbetween3rdbar3 > countbetween3rdbar3[1] then countbetween3rdbar3[1] else countbetween3rdbar4[1], bn);
def countbetween3rdbar5  = CompoundValue(1,  if countbetween3rdbar4 > countbetween3rdbar4[1] then countbetween3rdbar4[1] else countbetween3rdbar5[1], bn);
def countbetween3rdv  = CompoundValue(1,  if countreset2nd3rd_ && FractalH3rd  then high else  if countreset2nd3rd_ && FractalL3rd then low else countbetween3rdv [1], 1);
def countbetween3rdv2  = CompoundValue(1,  if countbetween3rdv <> countbetween3rdv[1] then countbetween3rdv[1] else countbetween3rdv2[1], 1);
def countbetween3rdv3  = CompoundValue(1,  if countbetween3rdv2 <> countbetween3rdv2[1] then countbetween3rdv2[1] else countbetween3rdv3[1], 1);
def countbetween3rdv4  = CompoundValue(1,  if countbetween3rdv3 <> countbetween3rdv3[1] then countbetween3rdv3[1] else countbetween3rdv4[1], 1);
def countbetween3rdv5  = CompoundValue(1,  if countbetween3rdv4 <> countbetween3rdv4[1] then countbetween3rdv4[1] else countbetween3rdv5[1], 1);
def lowl ;
def highl ;
def Xratio;
def Yratio;
input projectionOrder = {twothree, threefour, default fourfive};
switch (projectionOrder ) {
case twothree:
lowl = if countbetween3rdv && FractalL3rd then low else if countbetween3rdv && Fractalh3rd then high-AbsValue(countbetween3rdv2 - countbetween3rdv3) else lowl[1];
highl = if countbetween3rdv && Fractalh3rd then high else if countbetween3rdv && Fractall3rd then low+AbsValue(countbetween3rdv2- countbetween3rdv3) else highl[1];
Xratio = absvalue(lowl- highl);
Yratio = AbsValue(countbetween3rdbar2 - countbetween3rdbar3 );
case threefour:
lowl = if countbetween3rdv && FractalL3rd then low else if countbetween3rdv && Fractalh3rd then high-AbsValue(countbetween3rdv3 - countbetween3rdv4) else lowl[1];
highl = if countbetween3rdv && Fractalh3rd then high else if countbetween3rdv && Fractall3rd then low+AbsValue(countbetween3rdv3- countbetween3rdv4) else highl[1];
Xratio = absvalue(lowl- highl);
Yratio = AbsValue(countbetween3rdbar3 - countbetween3rdbar4 );
case fourfive:
lowl = if countbetween3rdv && FractalL3rd then low else if countbetween3rdv && Fractalh3rd then high-AbsValue(countbetween3rdv4- countbetween3rdv5) else lowl[1];
highl = if countbetween3rdv && Fractalh3rd then high else if countbetween3rdv && Fractall3rd then low+AbsValue(countbetween3rdv4- countbetween3rdv5) else highl[1];
Xratio = absvalue(lowl- highl);
Yratio = AbsValue(countbetween3rdbar4 - countbetween3rdbar5 );
}
def change =  Xratio*trendmult  / Yratio;
def maxcount = CompoundValue(1, if stopmax_ then 0 else maxcount[1] + 1, 0);
def Revmax = CompoundValue(1, if stopmax_ then (change) else Revmax[1], Double.NaN);
def dnline = CompoundValue(1, if stopmax_ &&  minbb== FrlH3rdbn then countbetween3rdv  else if maxcount > (yratio *trendmult) then Double.NaN else ( dnline[1] + Revmax ), Double.NaN);
def dnlinel = CompoundValue(1, if stopmax_ && minbb == Frll3rdbn then countbetween3rdv  else if maxcount > (yratio *trendmult) then Double.NaN else  (dnlinel[1] - Revmax) , Double.NaN);
def activepro =  if showprojection==no then double.nan else stopmax >= 0 && minbb == Frll3rdbn;
plot  dnlineProj = if activepro  then dnlinel else  dnline;
dnlineProj.SetDefaultColor(Color.CYAN);
dnlineProj.SetLineWeight(2);
dnlineProj.SetStyle(Curve.SHORT_DASH);
plot hiline =  if dnlineProj then highl
                else double.nan;
hiline.SetDefaultColor(color.red);
plot loline = if dnlineProj then lowl
                else double.nan;
loline.SetDefaultColor(color.cyan);
def range3rd = absvalue(loline - hiline) ;
plot fib146 = if activepro then  hiline - (range3rd *0.146)  else   loline +(range3rd *0.146) ;
fib146.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection  and !isNaN(fib146[1]) and isNaN(fib146[-1]),fib146,"fib146" ,color.cyan);
plot fib236 = if activepro then     hiline - (range3rd *0.236)  else   loline +(range3rd *0.236) ;
fib236.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection  and  !isnan(fib236[1]) && isnan(fib236[-1]),fib236,"fib236" ,color.cyan);
plot fib382 = if activepro then   hiline - (range3rd *0.382)  else  loline +(range3rd *0.382) ;
fib382.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib382[1]) and isNaN(fib382[-1]),fib382,"fib382" ,color.cyan);
plot fib50= if activepro then  hiline - (range3rd *0.50)  else   loline +(range3rd *0.50) ;
fib50.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib50[1]) and isNaN(fib50[-1]),fib50,"fib50" ,color.cyan);
plot fib618 = if activepro then  hiline - (range3rd *0.618)  else   loline +(range3rd *0.618) ;
fib618.SetDefaultColor(Color.GREEN);
plot  mid382 = if between(dnlineProj,fib382,fib618) or  between(dnlineProj,fib618,fib382) then fib382 else double.nan;
mid382.SetDefaultColor(Color.blue);
plot  mid618 = if between(dnlineProj,fib382,fib618)  or  between(dnlineProj,fib618,fib382) then fib618 else double.nan;
mid618.SetDefaultColor(Color.blue);
addcloud(mid382 ,mid618,color.green,color.green);
addcloud(fib382 ,fib618,color.cyan,color.cyan);
AddChartBubble( showprojection and !isNaN(fib618[1]) and isNaN(fib618[-1]),fib618,"fib618" ,color.cyan);
plot fib786= if activepro then   hiline - (range3rd *0.786)  else   loline +(range3rd *0.786) ;
fib786.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib786[1]) and isNaN(fib786[-1]),fib786,"fib786" ,color.cyan);
plot fib886= if activepro then   hiline - (range3rd *0.886)  else   loline +(range3rd *0.886) ;
fib886.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib886[1]) and isNaN(fib886[-1]),fib886,"fib886" ,color.cyan);
plot fib1= if activepro then   hiline - (range3rd *1)  else   loline +(range3rd *1) ;
fib1.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib1[1]) and isNaN(fib1[-1]),fib1,"fib1" ,color.cyan);
plot fib1236= if activepro then  hiline - (range3rd *1.236)  else   loline +(range3rd *1.236) ;
fib1236.SetDefaultColor(Color.GREEN);
AddChartBubble( showprojection and !isNaN(fib1236[1]) and isNaN(fib1236[-1]),fib1236,"fib1236" ,color.cyan);
AddLabel(yes, " 1rstPcount :  " + pivotcal , Color.WHITE);
AddLabel(yes, "2ndPcount:  " + pivotcal2nd, Color.CYAN);
AddLabel(yes, "3rdPcount:  " + pivotcal3rd , Color.ORANGE);
#### under construction
###input equityrisk =10000.00;
##input showriskreward = yes;
def fib2618 = 2.618;
def fib1618= 1.618;
def xatimebu = AbsValue(countbetween3rdbar2 - countbetween3rdbar3 );
def  xdtimebu = AbsValue(countbetween3rdbar5 - countbetween3rdbar3 );
def przrangebull =Xratio;
Def bullPRZ=absvalue(przrangebull/3) ;
def tradetimebu= round(((xatimebu+xdtimebu)/2)*fib2618,0);
def brsXADtimebu= CompoundValue(1, if activepro  then 1 else if maxcount==(tradetimebu) then 0 else brsXADtimebu[1], 0);
##plot bullstop = if  brsXADtimebu then hiline else loline;
##plot bullentry1 =  if brsXADtimebu then ( bullstop +bullPRZ) else ( bullstop -bullPRZ);
##plot bullPRZa =  if brsXADtimebu  then ( bullstop -bullPRZ)  else ( bullstop +bullPRZ);
##AddCloud(bullentry1, bullPRZa, Color.gray, Color.gray);
hi bro @Ninja Bull , please, how are you scan stocks with "LL color blue and LL color Green", or "HH color blue and HH color green" ?

LL-blue-and-LL-Green.png


HH-Blue-and-HH-Green.png

thanks ,)
 
How laggy is the pattern recognition for intraday trading?
I would like to know this as well. I wonder if it's a lagging indicator where it tells you the buy signal far after the stock has moved already.

EDIT: I went ahead and tested it, and it lags by 2 candle sticks. So the bull/bear stick is displayed two candle sticks from the candle stick you're currently on.

Say it's 1 min chart you're looking at. At 9:32:00AM, if a bull/bear pattern displays, it will mark the pattern as occurring at 9:30:00AM, two 1 min bars before where you currently are. It needs two candlesticks to confirm a pattern, at least the bear gartley, bear bat, and bear ABCD.
 
Last edited:
harmonics are for 4hr+ and up charts. I dont believe they are for day trading. At least that is what Mitch Ray teaches
 
harmonics are for 4hr+ and up charts. I dont believe they are for day trading. At least that is what Mitch Ray te
I regularly use harmonics from 5min up to the days it depends on where the patterns form really often times if they exist you need to check the time frames but the script is not public domain.

10 min chart

bETbQxx.png
 
Last edited:
@balishag21

Code:
#StudyName:     RSI_Laguerre_Lines_wTargets
#Version/Date:  v1 5/30/17                                           
#TOS.mx Link:
#Type:          [Study]                                               
#Description:   RSI in Laguerre Time MTF plotted on Upper chart
#Author:        jcseattle
#Copyright:     Copyright jcseattle/amalia 2016. All rights reserved.
#Copyleft:      This program is free software: you can redistribute it and/or modify
#               it under the terms of the GNU General Public License as published by
#               the Free Software Foundation, either version 3 of the License, or
#               (at your option) any later version. See <[URL]http://www.gnu.org/licenses/[/URL]>
#Requested By:   ""
#History:       Ver  Date        Auth  Change
#First draft    v1   5/30/17     jcseattle - No changes
# Notes           :Based off original script in 
#                  RSI in Laguerre Time MTF Option_v3
#                  Mobius
#                  V02.07.2014
#                  translation of J Elher's code
# Annotation      : ""
# Trading Notes   : ""

#                                        Start Code 

def na = Double.NaN;
script R {
    input gamma              = .2;
    input usecandletype      = {candle_hybrid, default candle};
    input usehigheraggperiod = {default "Current", "Higher"};
    input outputformat       = {default Rounded, "Not Rounded"};
    ;#Hint outputformat: 'Not Rounded' is used for notes, bonds (eg: 109'110), forex, etc type format. 
    input atrlength          = 21;
    input agg                = AggregationPeriod.TWO_MIN;
    input overbought         = .8;
    input oversold           = .2;
    def o;
    def h;
    def l;
    def c;
    def CU1;
    def CU2;
    def CU;
    def CD1;
    def CD2;
    def CD;
    def L0;
    def L1;
    def L2;
    def L3;
    plot RSI;
    plot OS;
    plot OB;
    def error = usehigheraggperiod == usehigheraggperiod."Higher" and GetAggregationPeriod() > agg;
    switch (usehigheraggperiod) {
    case Current:
        if usecandletype == usecandletype.candle_hybrid {
            o = (open + close[1]) / 2;
            h = Max(high, close[1]);
            l = Min(low, close[1]);
            c = (o + h + l + close) / 4;
        } else {
            o = open;
            h = high;
            l = low;
            c = close;
        }
    case Higher:
        if error {
            o = Double.NaN;
            h = Double.NaN;
            l = Double.NaN;
            c = Double.NaN;
        } else {
            if usecandletype == usecandletype.candle_hybrid {
                o = (open(period = agg)     + close(period = agg)[1]) / 2;
                h = Max(high(period = agg)  , close(period = agg)[1]);
                l = Min(low(period = agg)   , close(period = agg)[1]);
                c = ((open(period = agg)    + close(period = agg)[1]) / 2 
            + Max(high(period = agg), close(period = agg)[1]) 
            + Min(low(period = agg) , close(period = agg)[1]) 
            + close(period = agg)) / 4;
            } else {
                o = open(period = agg);
                h = high(period = agg);
                l = low(period = agg);
                c = close(period = agg);
            }
        }
}
    L0 = (1 – gamma) * c + gamma * L0[1];
    L1 = -gamma * L0 + L0[1] + gamma * L1[1];
    L2 = -gamma * L1 + L1[1] + gamma * L2[1];
    L3 = -gamma * L2 + L2[1] + gamma * L3[1];
    if L0 >= L1
    then {
        CU1 = L0 - L1;
        CD1 = 0;
    } else {
        CD1 = L1 - L0;
        CU1 = 0;
    }
    if L1 >= L2
    then {
        CU2 = CU1 + L1 - L2;
        CD2 = CD1;
    } else {
        CD2 = CD1 + L2 - L1;
        CU2 = CU1;
    }
    if L2 >= L3
    then {
        CU = CU2 + L2 - L3;
        CD = CD2;
    } else {
        CU = CU2;
        CD = CD2 + L3 - L2;
    }

    RSI = if IsNaN(close) then Double.NaN
      else if CU + CD <> 0
      then    CU / (CU + CD) else 0;
    OS  = if IsNaN(close)
      then Double.NaN else oversold;
    OB  = if IsNaN(close)
      then Double.NaN
      else overbought;
    def mid = if IsNaN(close) then Double.NaN else 0.5;
    def lineh = 1.2;
    def linel = -.2;
    
# End Code Basic RSI Laguerre - Author: Mobius

}

def Up = if R() crosses above R().OS then 1 else 0;
def u = if R() crosses above R().OB then 1 else 0;

def Dn = if R() crosses below R().OB then 1 else 0;
def d = if R() crosses below R().OS then 1 else 0;

def Green = if Up 
#or u 
then low else 0;
def Red = if Dn 
#or d 
then high else 0;

def showLines = 1;


def trendchange = if Green then low else if Red then high else trendchange[1];

def PL = if !IsNaN(trendchange) 
             then trendchange 
             else PL[1];

plot pivotLine = if PL > 0
                       then PL
                       else Double.NaN;
pivotLine.SetPaintingStrategy(PaintingStrategy.LINE);
pivotLine.SetHiding(!showLines);

input showArrows = yes;
plot ArrUp = if showArrows and Green then low else 0;
ArrUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
ArrUp.SetDefaultColor(Color.GREEN);

plot ArrDn = if showArrows and Red then high else 0;
ArrDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
ArrDn.SetDefaultColor(Color.RED);

pivotLine.AssignValueColor(if R() < R().OB then Color.RED else if R() > R().OS then Color.GREEN else Color.BLACK);

input ShowLabels = yes;
#Multipliers for ATR targets
input firsttgt = 1.618;
input secondtgt = 3.447;
input thirdtgt = 4.25;
input ATRLength = 8;#default is 14

def agg = if GetAggregationPeriod() > AggregationPeriod.FIFTEEN_MIN then GetAggregationPeriod() else AggregationPeriod.FIFTEEN_MIN;#You can edit this to just (GetAggregationPeriod())
def ATR = Average(TrueRange(high(period = agg), close(period = agg), low(period = agg)), ATRLength);
AddLabel(ShowLabels, "ATR = " + Round((ATR) / TickSize(), 0) * TickSize(), Color.GRAY);

input showStrategy = yes;
def co = if PL[1] and R()[1] > R().OS[1] then 1 else 0;
def firstlongtarget = if co then (pivotLine + ATR * firsttgt) else 0;
def secondlongtarget = if co then (pivotLine + ATR * secondtgt) else 0;
def thirdlongtarget = if co then (pivotLine + ATR * thirdtgt) else 0;

def sto = if PL and R() < R().OB then 1 else 0;
def firstshorttarget = if sto then (pivotLine - ATR * firsttgt) else 0;
def secondshorttarget = if sto then (pivotLine - ATR * secondtgt) else 0;
def thirdshorttarget = if sto then (pivotLine - ATR * thirdtgt) else 0;


#    Internal Script Reference
#    Author: Mobius

def LineLimit = 30;
def Detrend = 0;
def OnExpansion = yes;
def data = firstlongtarget;
def bar = 0;
def ShowAllPlots = 0;
def ThisBar = HighestAll(bar) - Detrend;
def cLine   = if ShowAllPlots == 0 
            then if bar == ThisBar 
                 then data
                 else Double.NaN
            else data;
def cond1 = CompoundValue(1, if IsNaN(data)
                                then cond1[1] 
                                else data, data);
def P = if ShowAllPlots == 0 
            then if ThisBar - LineLimit <= bar
            then HighestAll(cLine)
            else Double.NaN
            else cLine;
plot firstLTarget = if OnExpansion and 
                     IsNaN(data[-1]) 
                  then cond1 
                  else Double.NaN;
firstLTarget.SetDefaultColor(Color.GREEN);
AddLabel(ShowLabels, if R() > R().OS then "Long Target = " + Round((firstLTarget) / TickSize(), 0) * TickSize() else "", Color.GREEN);
AddChartBubble(IsNaN(close) and !IsNaN(close[1]), 
               firstLTarget, 
              "First Target = " + Round((firstLTarget) / TickSize(), 0) * TickSize(), 
               Color.GREEN, 
               yes);

def difflongtarget = if close < firstlongtarget then (firstlongtarget - close) else (close - firstlongtarget);
AddLabel(ShowLabels, if R() > R().OS then "Diff 1st L tgt = " + (Round((difflongtarget) / TickSize(), 0) * TickSize()) else "", Color.LIGHT_GREEN);


def LineLimit3 = 30;
def Detrend3 = 0;
def OnExpansion3 = yes;
def data3 = secondlongtarget;
def bar3 = 0;
def ShowAllPlots3 = 0;
def ThisBar3 = HighestAll(bar) - Detrend;
def cLine3   = if ShowAllPlots3 == 0 
            then if bar3 == ThisBar3 
                 then data3
                 else Double.NaN
            else data3;
def cond3 = CompoundValue(1, if IsNaN(data3)
                                then cond3[1] 
                                else data3, data3);
def P3 = if ShowAllPlots3 == 0 
            then if ThisBar3 - LineLimit3 <= bar3
            then HighestAll(cLine3)
            else Double.NaN
            else cLine3;
plot secondLTarget = if OnExpansion3 and 
                     IsNaN(data3[-1]) 
                  then cond3 
                  else Double.NaN;
secondLTarget.SetDefaultColor(Color.GREEN);
AddLabel(ShowLabels, if R() > R().OS then "2nd Long Target = " + Round((secondLTarget) / TickSize(), 0) * TickSize() else "", Color.GREEN);
AddChartBubble(IsNaN(close) and !IsNaN(close[1]), 
               secondLTarget, 
              "2nd Long Target = " + Round((secondLTarget) / TickSize(), 0) * TickSize(), 
               Color.GREEN, 
               yes);

def difflongtarget2 = if close < secondlongtarget then (secondlongtarget - close) else (close - secondlongtarget);
AddLabel(ShowLabels, if R() > R().OS then "Diff 2nd L tgt = " + Round((difflongtarget2) / TickSize(), 0) * TickSize() else "", Color.LIGHT_GREEN);


def LineLimit9 = 30;
def Detrend9 = 0;
def OnExpansion9 = yes;
def data9 = thirdlongtarget;
def bar9 = 0;
def ShowAllPlots9 = 0;
def ThisBar9 = HighestAll(bar9) - Detrend9;
def cLine9   = if ShowAllPlots9 == 0 
            then if bar9 == ThisBar9 
                 then data9
                 else Double.NaN
            else data9;
def cond9 = CompoundValue(1, if IsNaN(data9)
                                then cond9[1] 
                                else data9, data9);
def P9 = if ShowAllPlots9 == 0 
            then if ThisBar9 - LineLimit9 <= bar9
            then HighestAll(cLine9)
            else Double.NaN
            else cLine9;
plot thirdLTarget = if OnExpansion9 and 
                     IsNaN(data9[-1]) 
                  then cond9 
                  else Double.NaN;
thirdLTarget.SetDefaultColor(Color.GREEN);
AddLabel(ShowLabels, if R() > R().OS then "3rd Long Target = " + Round((thirdLTarget) / TickSize(), 0) * TickSize() else "", Color.GREEN);
AddChartBubble(IsNaN(close) and !IsNaN(close[1]), 
              thirdLTarget, 
            "3rd Long Target = " + Round((thirdLTarget) / TickSize(), 0) * TickSize(), 
               Color.GREEN, 
               yes);

def difflongtarget9 = if close < thirdlongtarget then (thirdlongtarget - close) else (close - thirdlongtarget);
AddLabel(ShowLabels, if R() > R().OS then "Diff 3rd L tgt = " + Round((difflongtarget9) / TickSize(), 0) * TickSize() else "", Color.LIGHT_GREEN);


def LineLimit2 = 30;
def Detrend2 = 0;
def OnExpansion2 = yes;
def data2 = firstshorttarget;
def bar2 = 0;
def ShowAllPlots2 = 0;
def ThisBar2 = HighestAll(bar2) - Detrend2;
def cLine2   = if ShowAllPlots2 == 0 
            then if bar2 == ThisBar2 
                 then data2
                 else Double.NaN
           else data2;
def cond2 = CompoundValue(1, if IsNaN(data2)
                                then cond2[1] 
                                else data2, data2);
def P2 = if ShowAllPlots2 == 0 
            then if ThisBar2 - LineLimit2 <= bar2
            then HighestAll(cLine2)
            else Double.NaN
            else cLine2;
plot firstSTarget = if OnExpansion2 and 
                     IsNaN(data2[-1]) 
                  then cond2 
                  else Double.NaN;
firstSTarget.SetDefaultColor(Color.RED);
AddLabel(ShowLabels, if R() < R().OB then "1st Short Target = " + Round((firstSTarget) / TickSize(), 0) * TickSize() else "", Color.RED);
AddChartBubble(IsNaN(close) and !IsNaN(close[1]),
                firstSTarget,
                "Short Target = " + Round((firstSTarget) / TickSize(), 0) * TickSize(),
                Color.RED,
                no);

def diffshorttarget = if close < data2 then (data2 - close) else (close - data2);
AddLabel(ShowLabels, if R() < R().OB then "Diff 1st S tgt = " + Round((diffshorttarget) / TickSize(), 0) * TickSize() else "", Color.LIGHT_RED);
#

def LineLimit4 = 30;
def Detrend4 = 0;
def OnExpansion4 = yes;
def data4 = secondshorttarget;
def bar4 = 0;
def ShowAllPlots4 = 0;
def ThisBar4 = HighestAll(bar4) - Detrend4;
def cLine4   = if ShowAllPlots4 == 0 
            then if bar4 == ThisBar4 
                 then data4
                 else Double.NaN
           else data4;
def cond4 = CompoundValue(1, if IsNaN(data4)
                                then cond4[1] 
                                else data4, data4);
def P4 = if ShowAllPlots4 == 0 
            then if ThisBar4 - LineLimit4 <= bar4
            then HighestAll(cLine4)
            else Double.NaN
            else cLine4;
plot secondSTarget = if OnExpansion4 and 
                  IsNaN(data4[-1]) 
                  then cond4 #
                  else Double.NaN;
secondSTarget.SetDefaultColor(Color.RED);
#Round((close + (atr)) / TickSize(), 0) * TickSize()
AddLabel(ShowLabels, if R() < R().OB then "2nd Short Target = " + Round((secondSTarget) / TickSize(), 0) * TickSize() else "", Color.RED);
AddChartBubble(IsNaN(close) and !IsNaN(close[1]),
                secondSTarget,
                "2nd Short Target = " + Round((secondSTarget) / TickSize(), 0) * TickSize(),
                Color.RED,
                no);

def diffshorttgt2 = if close < secondshorttarget then (secondshorttarget - close) else (close - secondshorttarget);
AddLabel(ShowLabels, if R() < R().OB then "Diff 2nd S tgt = " + Round((diffshorttgt2) / TickSize(), 0) * TickSize() else "", Color.LIGHT_RED);

def LineLimit8 = 30;
def Detrend8 = 0;
def OnExpansion8 = yes;
def data8 = thirdshorttarget;
def bar8 = 0;
def ShowAllPlots8 = 0;
def ThisBar8 = HighestAll(bar8) - Detrend8;
def cLine8  = if ShowAllPlots8 == 0 
            then if bar8 == ThisBar8 
                 then data8
                 else Double.NaN
            else data8;
def cond8 = CompoundValue(1, if IsNaN(data8)
                                then cond8[1] 
                                else data8, data8);
def P8 = if ShowAllPlots8 == 0 
            then if ThisBar8 - LineLimit8 <= bar8
            then HighestAll(cLine8)
            else Double.NaN
            else cLine8;
plot thirdSTarget = if OnExpansion8 and 
                     IsNaN(data8[-1]) 
                  then cond8 
                  else Double.NaN;
thirdSTarget.SetDefaultColor(Color.RED);
AddLabel(ShowLabels, if R() < R().OB then "3rd Short Target = " + Round((thirdSTarget) / TickSize(), 0) * TickSize() else "", Color.RED);
AddChartBubble(IsNaN(close) and !IsNaN(close[1]), 
               thirdSTarget, 
              "3rd Short Target = " + Round((thirdSTarget) / TickSize(), 0) * TickSize(), 
               Color.RED, 
               no);

def diffshorttarget3 = if close < thirdshorttarget then (thirdshorttarget - close) else (close - thirdshorttarget);
AddLabel(ShowLabels, if R() < R().OB then "Diff 3rd S tgt = " + Round((diffshorttarget3) / TickSize(), 0) * TickSize() else "", Color.LIGHT_RED);

#Inputs: 
input nFE = 34;#hint nFE: length for Fractal Energy calculation.
input PriceColor = yes;
plot gamma1 = Log(Sum((Max(high, close[1]) - Min(low, close[1])), nFE) / 
        (Highest(high, nFE) - Lowest(low, nFE))) / Log(nFE);
gamma1.AssignNormGradientColor(nFE, Color.GREEN, Color.RED);
gamma1.SetLineWeight(2);

AddLabel(ShowLabels, if ShowLabels and gamma1 < 0.382 then "Trending =" + gamma1 else if gamma1 > 0.618 then "Non-Trending =" + gamma1 else "FE = " + gamma1, if gamma1 < 0.382 then Color.WHITE else if gamma1 > 0.618 then Color.CYAN else Color.GRAY);

AssignPriceColor(if PriceColor and gamma1 
#crosses below 
<
.382 then Color.WHITE else if PriceColor and  gamma1 
#crosses above 
[QUOTE][/QUOTE]
.618 then Color.CYAN else Color.CURRENT);


#Me
 
Thank you for sharing. I copied the code into TOS and there's an error that shows up.

The part of the code that reads:

Code:
AssignPriceColor(if PriceColor and gamma1
#crosses below
<
.382 then Color.WHITE else if PriceColor and gamma1
#crosses above
.618 then Color.CYAN else Color.CURRENT);

Is highlighted in RED in ThinkScript Editor so I think its reading it as an error. Is there a solution?
Below it reads: "Invalid statement: AssignPriceColor at 425:1"

Would it be easier to just share a chart with the Study embedded?
 
Last edited:

New Indicator: Buy the Dip

Check out our Buy the Dip indicator and see how it can help you find profitable swing trading ideas. Scanner, watchlist columns, and add-ons are included.

Download the indicator

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
504 Online
Create Post

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