#DECLARATIONS
declare lower;
#USER INPUTS
## Upper and Lower EMAD Lines
input fastLength = 10;
input slowLength = 35;
input smoothLength = 12;
input smoothLength2 = 14;
input priceType = close;
input emadLineWeight = 2;
input showEMACloud = yes;
input showBubbles = yes;
input averageType = AverageType.WILDERS;
## Upper and Lower Band Lines
input bandLength = 100;
input bandLength2 = 200;
#GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(192, 192, 192));
DefineGlobalColor("Yellow", CreateColor(231, 190, 0));
#DEFINITIONS / CALCULATIONS
## UPPER EMAD LINE
def fastExpAvg = ExpAverage(priceType, fastLength);
def slowExpAvg = ExpAverage(priceType, slowLength);
def EMAD = (priceType - fastExpAvg);
def EMAD2 = (priceType - slowExpAvg);
def EMADAvg = (EMAD + EMAD2) / 2;
def upperEMADLine = ExpAverage(EMADAvg, smoothLength);
## LOWER EMAD LINE
def emadOpen = (upperEMADLine + upperEMADLine[1]) / 2;
def emadHigh = Max(upperEMADLine, upperEMADLine[1]);
def emadLow = Min(upperEMADLine, upperEMADLine[1]);
def emadClose = upperEMADLine;
def bottom = Min(emadClose[1], emadLow);
def tr = TrueRange(emadHigh, emadClose, emadLow);
def ptr = tr / (bottom + tr / 2);
def APTR = MovingAverage(averageType, ptr, smoothLength2);
def upperBand = emadClose[1] + (APTR * emadOpen);
def lowerBand = emadClose[1] - (APTR * emadOpen);
def lowerEMADLine = (upperBand + lowerBand) / 2;
## TOP AND BOTTOM BANDS
def zeroLineData = if IsNaN(close) then Double.NaN else 0;
def EMADSUp = upperEMADLine > zeroLineData;
def EMADSDown = upperEMADLine < zeroLineData;
def EMADdown = (lowerEMADLine > upperEMADLine);
def EMADup = (upperEMADLine >= lowerEMADLine);
def topBand = Highest(lowerEMADLine, bandLength);
def bottomBand = Lowest(lowerEMADLine, bandLength);
## BAND DIRECTION (USED ONLY FOR COLORING - NOT USED FOR PLOTS)
def topBandStepDown = if topBand < topBand[1] then 1 else 0;
def topBandStepUp = if topBand > topBand[1] then 1 else 0;
def bottomBandStepDown = if bottomBand < bottomBand[1] then 1 else 0;
def bottomBandStepUp = if bottomBand > bottomBand[1] then 1 else 0;
def bothBandsDown = bottomBandStepDown and topBandStepDown;
def bothBandsUp = bottomBandStepUp and topBandStepUp;
def bullBias = (bottomBand > zeroLineData);
def bearBias = (topBand < zeroLineData);
## BUBBLE CALCULATIONS (USED ONLY FOR BUBBLES)
def midBand = (upperBand + lowerBand) / 2;
def crossesUp = if (midBand[1] > upperEMADLine[1])and(midBand < upperEMADLine) then 1 else 0;
def crossesDown = if (upperEMADLine[1] > midBand[1])and(upperEMADLine < midBand) then 1 else 0;
def valueUp = if crossesUp then midBand else 0;
def valueDown = if crossesDown then midBand else 0;
def crossesUpline = if (valueUp - bottomBand) == 0 then 1 else 0;
def crossesDownline = if (valueDown - topBand) == 0 then 1 else 0;
def crossesUpline_filter = if crossesUpline and (crossesUpline[1] or crossesUpline[2] or crossesUpline[3] or crossesUpline[4])then 0 else 1;
def crossesDownline_filter = if crossesDownline and (crossesDownline[1] or crossesDownline[2] or crossesDownline[3] or crossesDownline[4])then 0 else 1;
#PLOTS
plot zeroLine = zeroLineData;
plot upperEMADLinePlot = upperEMADLine;
plot lowerEMADLinePlot = lowerEMADLine;
plot masterEMADLinePlot = (upperEMADLinePlot + lowerEMADLinePlot) / 2;
plot topBandPlot = topBand;
plot bottomBandPlot = bottomBand;
#FORMATTING
upperEMADLinePlot.HideTitle();
upperEMADLinePlot.HideBubble();
upperEMADLinePlot.AssignValueColor(
if (lowerEMADLinePlot > upperEMADLinePlot) then GlobalColor("Red")
else if (lowerEMADLinePlot < upperEMADLinePlot) then GlobalColor("Green")
else GlobalColor("Gray")
);
lowerEMADLinePlot.HideTitle();
lowerEMADLinePlot.HideBubble();
lowerEMADLinePlot.AssignValueColor(
if (lowerEMADLinePlot > upperEMADLinePlot) then GlobalColor("Red")
else if (lowerEMADLinePlot < upperEMADLinePlot) then GlobalColor("Green")
else GlobalColor("Gray")
);
masterEMADLinePlot.HideTitle();
masterEMADLinePlot.HideBubble();
masterEMADLinePlot.SetLineWeight(emadLineWeight);
masterEMADLinePlot.AssignValueColor(
if masterEMADLinePlot > masterEMADLinePlot[2] then GlobalColor("Green")
else if masterEMADLinePlot < masterEMADLinePlot[2] then GlobalColor("Red")
else GlobalColor("Gray")
);
zeroLine.HideTitle();
zeroLine.HideBubble();
zeroLine.AssignValueColor(
if (upperEMADLinePlot > zeroLine) then GlobalColor("Green")
else if (upperEMADLinePlot < zeroLine) then GlobalColor("Red")
else GlobalColor("Yellow")
);
topBandPlot.HideTitle();
topBandPlot.HideBubble();
topBandPlot.AssignValueColor(
if bothBandsDown then Color.dark_red
else if bothBandsUp then color.Dark_green
else if topBandStepUp then GlobalColor("Green")
else if topBandStepDown then GlobalColor("Red")
else if bearBias then GlobalColor("Yellow")
else GlobalColor("Gray")
);
bottomBandPlot.HideTitle();
bottomBandPlot.HideBubble();
bottomBandPlot.AssignValueColor(
if bothBandsDown then Color.dark_red
else if bothBandsUp then color.Dark_green
else if bottomBandStepUp then GlobalColor("Green")
else if bottomBandStepDown then GlobalColor("Red")
else if bullBias then GlobalColor("Yellow")
else GlobalColor("Gray")
);
#CLOUDS
AddCloud(if showEMACloud and (lowerEMADLinePlot > upperEMADLinePlot) then lowerEMADLinePlot else Double.NaN, upperEMADLinePlot, GlobalColor("Red"), Color.CURRENT);
AddCloud(if showEMACloud and (upperEMADLinePlot >= lowerEMADLinePlot) then upperEMADLinePlot else Double.NaN, lowerEMADLinePlot, GlobalColor("Green"), Color.CURRENT);
#BUBBLES
AddChartBubble(showBubbles and crossesUpline and crossesUpline_filter, midBand, "Wait for HL", GlobalColor("Green"), no);
AddChartBubble(showBubbles and crossesDownline and crossesDownline_filter, midBand,"Wait for LH" , GlobalColor("Red"), yes);