declare upper;
 #=======================================================
#               pivot points code
#=======================================================
#Inputs
#addchartbubble(PercentOfDayAvg > 200, close, PercentofDayAvg + "%", color.red, no);
input     showbubble = no;
input     Pivot_length = 10;
input     Show_Pivot = yes;
input     DisplayLabel = no;    #JQ 7.8.2018 added
def     RVSD = RelativeVolumeStDev()."RelVol";
def     Mobo = FW_DPO_MOBO()."DPO";
def     vHigh = high;  # creates the variable vHigh.  Use of the variable reduce data calls to tos iData server
def     vLow = low;
def     vOpen = open;
def     vClose = close;
def     vVolume = volume;
def     nan = Double.NaN;
# Bar Time & Date
def     bn = BarNumber();
def     currentBar = HighestAll(if !IsNaN(vHigh) then bn else nan);
def     PPS = IsNaN("value" = PPS().sellsignal) is false;
# pivot points
def     PH;
def     hh =
        fold i = 1 to  Pivot_length + 1
        with p = 1
        while p      
        do PPS > getValue(PPS, -i);
PH =
        if (bn >  Pivot_length and
            PPS == highest(PPS,  Pivot_length) and
            hh)
        then
            PPS
        else
            double.NaN;
def     PHBar = if !isNaN(PH)
                   then bn
                   else PHBar[1];
def     PHL = if !isNaN(PH)
                 then PH
                 else PHL[1];
def     priorPHBar = if PHL != PHL[1]
                        then PHBar[1]
                        else priorPHBar[1];
def     HighPivots = bn >= highestAll(priorPHBar);
def     DynamicLength =
                        if
                        bn - phbar < 2
                        then bn - priorPHBar
                        else bn - phbar;
#AddChartBubble(PH, close, "PH: " + PH
#                            + "\n" + "PHL: " + PHL
#                            + "\n" + "PHBar: " + PHBar  
#                            + "\n" + "hh: " + hh
#                            + "\n" + "PriorPH: " + PriorPHBar
#                            , Color.CYAN, yes);
plot Pivot = PH and show_pivot;
pivot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_aRROW_DOWN);
pivot.SetDefaultColor(Color.orange);
pivot.SetLineWeight(5);
#============================================================
#          DIVERGENCE CODE
#============================================================
def na = Double.NaN;
input lowlevel = 0.75;
input highlevel = 1.25;
input fallbelow = 1.5;
input drop_percent = 75;
input override_drop_percent = 200;
input compbars = 20;  # pull in trend for longest period.  reduce within the specific groupings below
def ttmtrendup = reference ttm_trend( compBars = compbars ).trendup;
def ttmtrenddwn = reference ttm_trend( compBars = compBars ).trenddown;
#======= 1 week - 5 bar factors (fast length manipulation)  *probably won't use because too volatile and unreliable
def     w1_length = 3;
def     w1_Range = Highest(high,w1_length) - Lowest(low, w1_length);
def     w1_Ratio = w1_Range / w1_Range[w1_length];
def     w1_RatioMax = highest(w1_Ratio, 2);
def     w1_Drop = if
                ( w1_RatioMax - w1_Ratio ) >  ( drop_percent / 100 )
                #and w1_Ratio > w2_Ratio
                then 1 else 0;
def     w1_Slope = w1_Ratio[6] - w1_Ratio[1];
def     w1_Sign = sign(w1_Slope);
def     w1_trendup = sum(ttmtrendup > 0, w1_length );  # num of upsignals in last 5
def     w1_TrendDwn = sum(ttmtrenddwn > 0, w1_length ); #num of downsignals in last 5
#==================================================================
#======= 2 week - 10 bar factors (medium length manipulation)
#==================================================================
def     w2_length = 9;
def     w2_Range = Highest(high, w2_length) - Lowest(low, w2_length);
def     w2_Ratio = w2_Range / w2_Range[w2_length];
def     w2_RatioMax = highest(w2_Ratio, 5);
def     w2_Drop = if
                ( w2_RatioMax - w2_Ratio ) / w2_RatioMax > .2
                and w1_Ratio > w2_Ratio
                then 1 else 0;
def     w2_Slope = w2_Ratio[w2_length + 1] - w2_Ratio[1];
def     w2_Sign = sign(w2_Slope);
def     w2_trendup = sum(ttmtrendup > 0, w2_length );  # num of upsignals in last 9
def     w2_TrendDwn = sum(ttmtrenddwn > 0,w2_length ); #num of downsignals in last 9
def     w2_Diverge = if
            dynamiclength > w1_length #and dynamiclength <= w2_length + 3
            and w2_Drop
            and w2_Sign < 0
            and w2_ratiomax >= highlevel * .9
            and w2_ratio <= fallbelow
            and w2_TrendDwn >= w2_length *.5
            and mobo > mobo[1]
            and RVSD <= 1
            then 1 else
            0;
 
AddChartBubble(w2_Diverge and showbubble, close,
                            "W2 " + Close
#                             "BarNum: " + bn
#                            + "\n" + "start: " + w2_Ratio[w2_length]
#                           + "\n" + "end: " + w2_Ratio[1]
#                          + "\n" + "sign: " + w2_sign                          
#                         + "\n" + "slope: " + w2_slope
#                            + "\n" + "D%: " +  ( w2_RatioMax - w2_Ratio ) / w2_ratiomax
#                            + "\n" + "D%: min " +  drop_percent
#                            + "\n" + "drop: " +  ( w2_RatioMax - w2_Ratio )
#                            + "\n" + "max: " + w2_RatioMax
#                            + "\n" + "cur: " + w2_Ratio
                            , Color.orange, yes);
#======= 3 week - 15 bar factors (slow length manipulation)
def     w3_length = 14;
def     w3_Range = Highest(high,w3_length) - Lowest(low, w3_length);
def     w3_Ratio = w3_Range / w3_Range[w3_length];
def     w3_RatioMax = highest(w3_Ratio, 3);
def     w3_Drop = if
                (w3_Ratio / w3_RatioMax ) / W3_RatioMax  >  .1
                #and w2_Ratio > w3_Ratio
                then 1 else
                #if ( w2_RatioMax - w2_Ratio ) / w2_ratiomax >  .35 then 1 else
                0;
def     w3_Slope = w3_Ratio[w2_length + 1] - w3_Ratio;
def     w3_Sign = sign(w3_Slope);
def     w3_trendup = sum(ttmtrendup > 0, w3_length );  # num of upsignals in last 10
def     w3_TrendDwn = sum(ttmtrenddwn > 0,w3_length ); #num of downsignals in last 10
def     w3_Diverge = if
            dynamiclength >= w3_length -3
            and w3_Drop
            and w3_Sign < 0        
            and w3_ratiomax >= highlevel
            and w2_ratio <= fallbelow    #w3 ratio too slow to use this but w2 is still good indication
            and w3_TrendDwn >= w3_length *.5
            and RVSD <= 2
            then 1 else
            0;
 
           
AddChartBubble(w3_Diverge and showbubble, close,
                            "W3 " + Close
#                            + "BarNum: " + bn
#                            + "\n" + "down: " + w3_TrendDwn
#                            + "\n" + "sign: " + w3_sign                          
#                            + "\n" + "slope: " + w3_slope
#                            + "\n" + "D%: " +  (w3_Ratio / w3_RatioMax ) / W3_RatioMax
#                            + "\n" + "drop: " +  ( w3_RatioMax - w3_Ratio )
#                            + "\n" + "max: " + w3_RatioMax
#                            + "\n" + "cur: " + w3_Ratio
                            , Color.orange, yes);
# ============================
def divergealert = if w2_diverge then 1 else    #for the scanner version
                   if w3_diverge then 1 else 0;
plot w2_alert = w2_diverge;
w2_alert.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
w2_alert.SetDefaultColor(Color.BLUE);
w2_alert.SetLineWeight(5);
plot w_3alert = w3_diverge;
w_3alert.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS);
w_3alert.SetDefaultColor(Color.orange);
w_3alert.SetLineWeight(5);