# from UseThinkScript.com
# https://usethinkscript.com/threads/moving-average-breakout.12746/?utm_source=threadloom&utm_medium=email&utm_campaign=ed1906&utm_content=iss74#post-108756
# scripted by SleepyZ
#1. If the day opens below the EMA Line = Red
#2. If day opens above EMA Line = Green
#3. If above and Green only 2 bar breakouts below to red are possible.
#4. If below and Red only 2 bar breakouts above to green are possible.
#5. Pictures show where these things do not happen
## Test removed by ZupDog
input paintbars = no;
input Label = yes;
input showAlerts = no;
input length1 = 20;
input avg = AverageType.EXPONENTIAL;
input price1 = close;
plot ma1 = MovingAverage(averagetype = avg, price1, length1);
#input test = no;
def o = if GetDay() != GetDay()[1] then open else o[1];
def ma = if GetDay() != GetDay()[1] then ma1 else ma[1];
def trend = if SecondsFromTime(1600) <= 0 and o > ma then 1 else if SecondsFromTime(1600) <= 0 and o < ma then -1 else trend[1];
#plot xtrend = if !test then Double.NaN else trend;
#xtrend.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
DefineGlobalColor("Long", Color.CYAN);
DefineGlobalColor("Short", Color.YELLOW);
DefineGlobalColor("Else", Color.WHITE);
# 2 consective bars red bars above ma1, including cross above
def currentbarAbove = if close >= open and close > ma1 then 1 else 0;
def onebarafterAbove = if close >= open and currentbarAbove[1] and close > ma1 then 1 else 0;
# 2 consective bars red bars below ma1, including cross below
def currentbarBelow = if close < open and close < ma1 then 1 else 0;
def onebarafterBelow = if currentbarBelow[1] and close < open and close < ma1 then 1 else 0;
def malinecolor = if IsNaN(malinecolor[1])
then 0
else if GetDay() != GetDay()[1] and trend == 1
then 1
else if trend == 1 and onebarafterBelow[-1] or onebarafterBelow
then 0
else if GetDay() != GetDay()[1] and trend == -1
then 0
else if trend == -1 and onebarafterAbove[-1] or onebarafterAbove
then 1
else malinecolor[1];
def lastbar = BarNumber() == HighestAll(if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN);
ma1.DefineColor("Up", GetColor(1));
ma1.DefineColor("Down", GetColor(0));
#ma1.AssignValueColor(if lastbar and malinecolor[1] == 1 then Color.WHITE else if lastbar and malinecolor[1] == 0 then Color.PLUM else if malinecolor then Color.WHITE else if malinecolor == 0 then Color.PLUM else GlobalColor("Else"));
ma1.AssignValueColor(if lastbar and malinecolor[1] == 1 then ma1.Color("Up") else if lastbar and malinecolor[1] == 0 then ma1.Color("Down") else if malinecolor then ma1.Color("Up") else if malinecolor == 0 then ma1.Color("Down") else GlobalColor("Else"));
ma1.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
ma1.SetLineWeight(5);
Alert( ((showAlerts and lastbar and malinecolor[1] crosses above 1 ) ) , " Trigun1127_MA Bullish", Alert.BAR, Sound.Ring );
Alert( ((showAlerts and lastbar and malinecolor[1] crosses below 0 ) ) , " Trigun1127_MA Bearish", Alert.BAR, Sound.Ring );
#AddLabel(Label, "Trigun1127_MA " , if lastbar and malinecolor[1] == 1 then Color.CYAN else Color.YELLOW );
## my 1st label count attempt :
rec countup = if lastbar and malinecolor[1] == 1 then 1 else countup[1] + 1;
rec countdn = if lastbar and malinecolor[1] == 0 then -1 else countdn[1] - 1;
AddLabel(Label, Concat("Trigun1127_MA ", if lastbar and malinecolor[1] == 1 then countup else countdn), if lastbar and malinecolor[1] == 1 then Color.GREEN else if lastbar and malinecolor[1] == 0 then Color.PLUM else Color.WHITE );
## My second label count attempt :
#rec countup = if lastbar and malinecolor[1] crosses above 1 then 1 else countup[1] + 1;
#rec countdn = if lastbar and malinecolor[1] crosses below 0 then -1 else countdn[1] - 1;
#AddLabel(label, Concat("Trigun1127_MA ", if lastbar and malinecolor[1] > 1 then countup else countdn), if lastbar and malinecolor[1] > 1 then Color.GREEN else if lastbar and malinecolor[1] < 0 then Color.PLUM else Color.WHITE );
input lookback = 0;
def ymd = GetDay();
def candles = !IsNaN(close);
def capture = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay = (HighestAll(dayCount) - dayCount) ;
plot xup = if thisDay > lookback then 0 else if thisDay == lookback and malinecolor == 1 then 1 else 0;
def x1 = if thisDay == lookback and GetValue(xup, 1) == 1 then x1[1] + 1 else x1[1];
plot xx = x1;
xx.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
AddLabel(1, x1, Color.cyan);