# avg_gap_compare_01
# compare gap of 9ema to 50ema
# look for 10 smaller gaps, then bigger
def na = double.nan;
def bn = barnumber();
# last bar (most recent)
# def lastbar = !isnan(close[0]) and isnan(close[-1]);
# def valid = !isnan(close);
# emas ---------------------------------------
def price = close;
input ma1_len = 9;
input ma1_type = AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);
input ma2_len = 50;
input ma2_type = AverageType.EXPONENTIAL;
def ma2 = MovingAverage(ma2_type, price, ma2_len);
input show_average_lines = yes;
plot z1 = if show_average_lines then ma1 else na;
plot z2 = if show_average_lines then ma2 else na;
z1.SetDefaultColor(color.cyan);
z2.SetDefaultColor(color.magenta);
#-----------------------------------
input show_avg_gap_cloud = yes;
def z1b = if show_avg_gap_cloud then ma1 else na;
addcloud(z1b, ma2, color.green, color.red);
#-----------------------------------
def gap = ma1 - ma2;
def abs_gap = absvalue(gap);
# gap is smaller
def gap_decres = abs_gap[1] > abs_gap[0];
# gap is bigger
def gap_incres = abs_gap[1] < abs_gap[0];
input show_rule1_arrows = { only_first_reversal , all, default none };
input show_rule2_arrows = yes;
input rule2_min_qty_smaller_gaps = 10;
#-----------------------------------
# rules
# rule1: recent gap change is bigger. the change before it was smaller.
def rule1 = if ( abs_gap > abs_gap[1] and abs_gap[2] > abs_gap[1] ) then 1 else 0;
# rule2: 10 bars of decreasing gap, then a bigger gap
def rule2 = if (
sum(gap_decres[1], rule2_min_qty_smaller_gaps) == rule2_min_qty_smaller_gaps
and abs_gap[0] > abs_gap[1] )
then 1 else 0;
#-----------------------------------
def gapdir = if bn == 1 then 0
else if (rule1 and gap > 0) then 1
else if (rule1 and gap < 0) then -1
else gapdir[1];
def diffdir = gapdir <> gapdir[1];
#-----------------------------------
#input show_rule1_arrows = { only_first_reversal , default all, none };
def arrows_all;
def arrows_first;
switch (show_rule1_arrows) {
case only_first_reversal:
arrows_all = 0;
arrows_first = 1;
case all:
arrows_all = 1;
arrows_first = 0;
case none:
arrows_all = 0;
arrows_first = 0;
}
def showup = (arrows_all and rule1 and gap > 0) or (arrows_first and rule1 and diffdir and gap > 0);
plot wup = if showup then (min(ma1, ma2)*0.995) else na;
wup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
wup.SetDefaultColor(Color.green);
wup.setlineweight(3);
wup.hidebubble();
def showdwn = (arrows_all and rule1 and gap < 0) or (arrows_first and rule1 and diffdir and gap < 0);
plot wdwn = if showdwn then (max(ma1, ma2)*1.005) else na;
wdwn.SetPaintingStrategy(PaintingStrategy.ARROW_down);
wdwn.SetDefaultColor(Color.red);
wdwn.setlineweight(3);
wdwn.hidebubble();
#
#-------------------------------------
# rule2 - 10 smaller gaps, then a bigger
#input show_rule2_arrows = yes;
plot xbars_down = if (show_rule2_arrows and rule2) then (min(ma1, ma2)*0.990) else na;
xbars_down.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
xbars_down.SetDefaultColor(Color.white);
xbars_down.setlineweight(3);
xbars_down.hidebubble();
#-------------------------------------
input test_rule1_bubble = no;
addchartbubble(test_rule1_bubble, (max(ma1, ma2)*1.026),
gap + " gap\n" +
gapdir + " dir\n" +
diffdir + " diff\n" +
rule1 + " rule1"
, (if rule1 then color.yellow else color.gray), yes);
input test_rule2_bubbles = no;
addchartbubble(test_rule2_bubbles, (max(ma1, ma2)*1.01),
gap + " gap\n" +
rule2 + " rule2"
, (if rule2 then color.cyan else color.gray), yes);
#-------------------------------------
#