# arrowsfrom_emalevels_01
# ---------------
# halcyonguy
# 21-06-08
# draw arrows when conditions are triggered
# ---------------
# draw arrows when
# closest row of arrows:
# If candle is above 9ema show green up arrow, if not then red down arrow
# next row of arrows:
# If candle is above 20ema show green up arrow, if not then red down arrow
# if the 9ema and 20ema crossings are different , then draw cyan arrows
# next row of arrows:
# If 2+ of last 3 candles are bull, show yellow up arrow, if not then blue down arrow
# next row of arrows:
# If bull engulfing candle show white up arrow. If bear engulfing show magenta down arrow
# didn't do this
# ?? if bull candle closed high price = start of bear candle at exact price show 2 red arrows (indicating a strong resistance similar to engulfing)
input show_average_lines = yes;
input price = close;
def na = double.nan;
# spacing factor between rows of arrows
input arrow_spacing = 0.005;
def up = (close > open);
def down = (close < open);
input length1 = 9;
input avg_type1 = AverageType.EXPONENTIAL;
def avg1 = MovingAverage(avg_type1, price, length1);
input length2 = 20;
input avg_type2 = AverageType.EXPONENTIAL;
def avg2 = MovingAverage(avg_type2, price, length2);
# show average lines
plot av1 = if show_average_lines then avg1 else na;
av1.SetDefaultColor(Color.cyan);
plot av2 = if show_average_lines then avg2 else na;
av2.SetDefaultColor(Color.yellow);
def aboveavg1 = (price > avg1);
def belowavg1 = (price < avg1);
def aboveavg2 = (price > avg2);
def belowavg2 = (price < avg2);
# are the 2 average conditions opposite ?
def avg1avg2opp = (aboveavg1 and belowavg2) or (belowavg1 and aboveavg2);
# ------------------------
# If candle is above 9ema show green arrow, if not then red arrow
def incr1 = 1;
plot aboveav1 = if aboveavg1 then (low * (1 - (incr1 * arrow_spacing))) else na;
aboveav1.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
aboveav1.DefineColor("Up", color.green);
aboveav1.DefineColor("opp", color.cyan);
aboveav1.AssignValueColor(if avg1avg2opp then aboveav1.color("opp") else aboveav1.color("up"));
#aboveav1.SetDefaultColor(Color.green);
aboveav1.setlineweight(1);
aboveav1.hidebubble();
plot belowav1 = if belowavg1 then (high * (1 + (incr1 * arrow_spacing))) else na;
belowav1.SetPaintingStrategy(PaintingStrategy.ARROW_down);
belowav1.DefineColor("Down", color.red);
belowav1.DefineColor("opp", color.cyan);
belowav1.AssignValueColor(if avg1avg2opp then belowav1.color("opp") else belowav1.color("down"));
#belowav1.SetDefaultColor(Color.red);
belowav1.setlineweight(1);
belowav1.hidebubble();
# If candle is above 20ema show green arrow, if not then red arrow
def incr2 = 2;
plot aboveav2 = if aboveavg2 then (low * (1 - (incr2 * arrow_spacing))) else na;
aboveav2.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
aboveav2.DefineColor("Up", color.green);
aboveav2.DefineColor("opp", color.cyan);
aboveav2.AssignValueColor(if avg1avg2opp then aboveav2.color("opp") else aboveav2.color("up"));
#aboveav2.SetDefaultColor(Color.green);
aboveav2.setlineweight(1);
aboveav2.hidebubble();
plot belowav2 = if belowavg2 then (high * (1 + (incr2 * arrow_spacing))) else na;
belowav2.SetPaintingStrategy(PaintingStrategy.ARROW_down);
belowav2.DefineColor("Down", color.red);
belowav2.DefineColor("opp", color.cyan);
belowav2.AssignValueColor(if avg1avg2opp then belowav2.color("opp") else belowav2.color("down"));
#belowav2.SetDefaultColor(Color.red);
belowav2.setlineweight(1);
belowav2.hidebubble();
# ------------------------------------------
# 2 out of last 3 bars are bull/bear
def bullbearlen = 3;
def bull3bars = (sum(up, bullbearlen) >= 2);
def bear3bars = (sum(down, bullbearlen) >= 2);
def incr3 = 3;
plot bull3 = if bull3bars then (low * (1 - (incr3 * arrow_spacing))) else na;
bull3.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bull3.SetDefaultColor(Color.yellow);
bull3.setlineweight(4);
bull3.hidebubble();
plot bear3 = if bear3bars then (high * (1 + (incr3 * arrow_spacing))) else na;
bear3.SetPaintingStrategy(PaintingStrategy.ARROW_down);
bear3.SetDefaultColor(Color.blue);
bear3.setlineweight(4);
bear3.hidebubble();
# ------------------------------------------
# If bull engulfing candle show white arrow , If bear engulfing show magenta arrow
# bullish engulfing: after a red bar, a green bar, opens below prev close, and closes above prev open
def bullengulfing = down[1] and (open < close[1]) and ( close > open[1]);
# bearish engulfing: after a green bar, a red bar, opens above prev close, and closes below prev open
def bearengulfing = up[1] and (open > close[1]) and ( close < open[1]);
def incr4 = 4;
plot bulleng = if bullengulfing then (low * (1 - (incr4 * arrow_spacing))) else na;
bulleng.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bulleng.SetDefaultColor(Color.white);
bulleng.setlineweight(4);
bulleng.hidebubble();
plot beareng = if bearengulfing then (high * (1 + (incr4 * arrow_spacing))) else na;
beareng.SetPaintingStrategy(PaintingStrategy.ARROW_down);
beareng.SetDefaultColor(Color.magenta);
beareng.setlineweight(4);
beareng.hidebubble();
#