# Lower Indicator
# This indicator shows individually the following requirements:
# 1. Close crosses above/below Tenkan (from Ichimoku cloud)
# 2. Close crosses above/below Parabolic SAR
# 3. MACD Histogram crosses above/below zero line
# 4. Awesome Oscillator crosses above/below zero line
# 5. EMA21 as a trend master
#based on a code from SleepyZ =>
# => https://usethinkscript.com/threads/ichimoku-macd-and-psar-dashboard-for-thinkorswim.14928/
declare lower;
def tenkan = if close crosses above reference Ichimoku()
then 1
else if close crosses below Ichimoku()
then 0
else tenkan[1];
def sar = if close crosses above reference ParabolicSAR()
then 1
else if close crosses below reference ParabolicSAR()
then 0
else sar[1];
def macd = if reference MACD().Diff crosses above 0
then 1
else if reference MACD().Diff crosses below 0
then 0
else macd[1] ;
def aweosc = if reference AwesomeOscillator() crosses above 0
then 1
else if reference AwesomeOscillator() crosses below 0
then 0
else aweosc[1];
# though AO is originally used for trend, I decided to use em21 as master trend indicator to allow for bullish or bearish signals
def trend = if close > expAverage(close,21) then 1 else 0;
plot line1 = if IsNaN(close) then Double.NaN else 1;
line1.AssignValueColor(if tenkan == 1 then Color.GREEN else Color.RED);
line1.SetPaintingStrategy(PaintingStrategy.POINTS);
plot line2 = if IsNaN(close) then Double.NaN else 2;
line2.AssignValueColor(if sar == 1 then Color.GREEN else Color.RED);
line2.SetPaintingStrategy(PaintingStrategy.POINTS);
plot line3 = if IsNaN(close) then Double.NaN else 3;
line3.AssignValueColor(if macd == 1 then Color.GREEN else Color.RED);
line3.SetPaintingStrategy(PaintingStrategy.POINTS);
plot line4 = if IsNaN(close) then Double.NaN else 4;
line4.AssignValueColor(if aweosc == 1 then Color.GREEN else Color.RED);
line4.SetPaintingStrategy(PaintingStrategy.POINTS);
plot line5 = if IsNaN(close) then Double.NaN else 5;
line5.AssignValueColor(if trend == 1 then Color.GREEN else Color.RED);
line5.SetPaintingStrategy(PaintingStrategy.POINTS);
#changed the purpose of the ALL plot to show bullish, bearish and undefined status by requiaring a 75% agreement of the original indicators and the new trend criteria
def subtot = tenkan + sar + macd + aweosc;
def sum = if subtot >= 3 and trend ==1 then 1 else if subtot <= 1 and trend == 0 then 0 else 8;
#original calculation
#def sum = if tenkan + sar + macd + aweosc == 4 then 1
# else if tenkan + sar + macd + aweosc == 0 then 0
# else sum[1];
plot line6 = if IsNaN(close) then Double.NaN else 6;
#line6.AssignValueColor(if sum == 1 then Color.GREEN else if sum == 0 then Color.RED else Color.WHITE);
line6.AssignValueColor(if sum == 1 then Color.GREEN else if sum == 0 then Color.RED else Color.BLaCK);
line6.SetPaintingStrategy(PaintingStrategy.POINTS);
plot line7 = if IsNaN(close) then Double.NaN else 7;
line7.SetDefaultColor(Color.BLACK);
input showbubbles = yes;
input bubblemover = 2;
def b = bubblemover;
def b1 = b + 1;
def mover = showbubbles and IsNaN(close[b]) and !IsNaN(close[b1]);
AddChartBubble(mover, line1[b1], "Tenkan", line1.TakeValueColor());
AddChartBubble(mover, line2[b1], "SAR", line2.TakeValueColor());
AddChartBubble(mover, line3[b1], "MACD", line3.TakeValueColor());
AddChartBubble(mover, line4[b1], "AweOsc", line4.TakeValueColor());
AddChartBubble(mover, line5[b1], "Trend", line5.TakeValueColor());
AddChartBubble(mover, line6[b1], "All", line6.TakeValueColor());
plot zeroline = if IsNaN(close) then Double.NaN else 0;
zeroline.SetDefaultColor(Color.BLACK);
#mainly for scanning purposes
plot trendo = if sum == 1 then 0.5 else if sum == 0 then -0.5 else Double.NaN;
trendo.SetPaintingStrategy(PaintingStrategy.POINTS);