# upper_cross_gain3per_0
# look for an ema cross over,
# then a gain 3% on one of the next three bars
def na = double.nan;
input avg1len = 21;
input avg1type = AverageType.EXPONENTIAL;
def avg1 = MovingAverage(avg1type, close, avg1len);
def up = close > open;
input gain_percent = 3.0;
input gain_bars_back = 3;
def iscross = if close crosses above avg1 then 1 else 0;
def diff = high - low;
def gainper = if up then round(100*diff/low,1) else na;
def isgain = if gainper >= gain_percent then 1 else 0;
def goff = GetMaxValueOffset(iscross, gain_bars_back + 1);
def gain_en = if goff > 0 and goff <= gain_bars_back then 1 else 0;
def iscross_gain = if sum(iscross[1],gain_bars_back) > 0 and isgain then 1 else 0;
# output for scanner
#plot zcg = iscross_gain;
# ------------------------------------------
# upper chart code. delete code below for a scanner
input show_average_line = yes;
plot za = if show_average_line then avg1 else na;
za.SetDefaultColor(Color.yellow);
za.hidebubble();
input show_wedge_on_cross_up = yes;
# wedge on crossup
plot zx = if show_wedge_on_cross_up and iscross then 1 else na;
zx.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_up);
zx.SetDefaultColor(Color.cyan);
# zx.setlineweight(1);
zx.hidebubble();
input show_arrow_on_gain = yes;
plot zg = if show_arrow_on_gain and iscross_gain then low*0.995 else na;
zg.SetPaintingStrategy(PaintingStrategy.arrow_up);
zg.SetDefaultColor(Color.green);
zg.setlineweight(3);
zg.hidebubble();
input show_percent_gain_on_all = no;
input show_percent_on_gain = yes;
plot zp = if (show_percent_on_gain and iscross_gain) or show_percent_gain_on_all then gainper else na;
zp.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
zp.AssignValueColor(if isgain then Color.green else Color.gray);
zp.hidebubble();
# -------------------------------------
input test1_bubbles = no;
addchartbubble(test1_bubbles, low*0.98,
iscross + "\n" +
gainper + " %\n" +
isgain + " is\n" +
goff + " off\n" +
gain_en + " en\n"
, color.yellow, no);
#