The Confirmation Trend Chart Setup | The End All Be All | For ThinkOrSwim

Status
Not open for further replies.
Thanks! I notice you don't use the V4 version of EMAD with the labels? Furthermore, I notice the EMAD lower study in the style has white arrows. Do the white arrows correspond to the entry points of higher lows and lower highs?
The basic study for EMAD remains the same… the other versions add a histogram with coloring for Alert4, Triple Exhaustion, etc. The regular buy regular sell extreme buy extreme sell should be un checked (as in do not show) (which is how I have it) (the bars are colored so no need for the arrows I need to do away with the arrow plots for them) anyway… the white arrows are meant to show the HL LH but it is near impossible to code them to appear exactly where I would like… honestly I don’t know why I keep them turned on as I do not need them because I will see it either way.

Oh and the EMAD in the link shared above has more colors for the upper and lower lines which are colored based on the “market phases” (I will not leave it that way it’s just for testing at the moment)

That being said I don’t really care about any arrow… more so the levels and price action.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

RwRzaKz.png


EMAD Variant Mobile: http://tos.mx/UhGOjQf (updated link... removed some unnecessary code and changed some default settings 9:15am 1/11/24)

Added the line coloring like the desktop version for the following conditions
:
1. Zeroline turns green or red when EMAs cross above or below the zeroline
2. Top and bottom bands turn green or red when stepping up or down individually or at the same time
3. EMAs colored based on being crossed up or down (whereas before in mobile there were two EMAs one green and the other red)
4. The other moving average (called "made" in settings) is colored also however the gray portions of the line are not working quite as they should but good enough for now
5. Top and bottom bands turn yellow when the zeroline is below the bottom band or when the zeroline is above the top band.

Notes concerning settings:
  • The "UP" and "DOWN" plots will need to be changed from lines to arrows in settings
  • The "Squeeze" plot can remain a solid line (or dots) but you will need to increase the line weight to 3 or 4 in settings

unJRHwt.jpg


Code:
   ##########################################################################################
   ##########################################################################################
   ###                                                                                    ###
   ###       #############################           #########               #########    ###
   ###     #################################         #########               #########    ###     
   ###    ####################################       #########               #########    ### 
   ###   ########                     #########      #########               #########    ###          
   ###   ########   EMAD for MOBILE   #########      #########               #########    ###
   ###   ########   @Christopher84    #########      #########               #########    ###        
   ###   ########       @HODL         #########      #########               #########    ###      
   ###   ########                     #########      #########               #########    ###         
   ###   ######################################      ##################################   ###       
   ###    ####################################       ##################################   ###    
   ###     #################################         ##################################   ### 
   ###    ####################################       ##################################   ###      
   ###   ########                     #########                              #########    ###
   ###   ########     @Horserider     #########                              #########    ###
   ###   ########  HORSERIDER VOLUME  #########                              #########    ###             
   ###   ########  TRIPLE EXHAUSTION  #########                              #########    ###         
   ###   ########      @chence27      #########                              #########    ###              
   ###   ########                     #########                              #########    ###          
   ###    ####################################                               #########    ###        
   ###     ##################################                                #########    ###         
   ###      ###############################                                  #########    ###       
   ###                                                                                    ###
   ##########################################################################################
   ##########################################################################################

#DECLARATIONS
declare lower;


#USER INPUTS
input use_alert4_filter = yes; #hint show alert4 arrow only when it appears with an UP arrow
input showverticalline = no;
input Ehlers_length = 34;
input Length_MAD = 4;
input emadLineWeight = 2;
input filterBounceArrows = 1; #hint lookback period for bothbandsup/down etc. fold
input cloudlinelookback = 20;  #hint lookback period for "made" average crossover fold used for arrows
input Steplookback = 10; #hint lookback period for top/bottomstepup/down etc. fold
input filterZeroline = 20; #hint lookback period for crosses up/down at zeroline etc. fold
input PreviousStepsDown = 2; #hint filter for hiding arrows
input PreviousStepsUp = 2; #hint filter for hiding arrows
input tradeDaytimeOnly = yes; #hint tradeDaytimeOnly: used to hide arrows
input OpenTime = 0730; #hint OpenTime: Opening time of market hides arrows
input CloseTime = 1800; #hint CloseTime: Closing time of market hides arrows
input firstTrade_Sq = {default long, short};
input trailType = {default modified, unmodified};
input firstTrade = {default long, short};

def bn = barnumber();
def Begin = SecondsFromTime(OpenTime);
def End = SecondsTillTime(CloseTime);
def isIntraDay = if GetAggregationPeriod() > 14400000 or GetAggregationPeriod() == 0 then 0 else 1;
def MarketOpen = if !tradeDaytimeOnly or !isIntraDay then 1 else if tradeDaytimeOnly and isIntraDay and Begin > 0 and End > 0 then 1 else 0;
def fastLength = 10;
def slowLength = 35;
def lengthBAD = 3;
def ATRPeriod = 5;
def ATRFactor = 2.0;
def length_3x = 1000;
def priceType = close;
def showTripleExh = yes;
def showHLLH = no;
def showtestlabels = no;
def bandLength = 100;
def bandLength2 = 100;
def smoothLength = 12;
def smoothLength2 = 14;
def averageType = AverageType.WILDERS;
def averageType_Sq = AverageType.SIMPLE;

#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));

## 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);
def zerolinedatacondition = if (zerolinedata > topBand) then topBand else if (zerolinedata < bottomBand) then bottomBand else 0;
## Master
def masteremadline = (upperEMADLine + lowerEMADLine) / 2;
## 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;
###############################################################
def crossesUpZeroline = if (valueUp) == 0 then 1 else 0;
def crossesDownZeroline = if (valueDown) == 0 then 1 else 0;
###############################################################
def crossesUpBottomlinebn = if crossesUpline and !crossesUpline[1] then bn else crossesUpBottomlinebn[1];
def crossesDownToplinebn = if crossesDownline and !crossesDownline[1] then bn else crossesDownToplinebn[1];
def crossuplinewithin = if crossesUpBottomlinebn < bn then bn - crossesUpBottomlinebn else crossuplinewithin[1];
def crossdownlinewithin = if crossesDownToplinebn < bn then bn - crossesDownToplinebn else crossdownlinewithin[1];
#def crosslinefirst = if crossuplinewithin < crossdownlinewithin then 1 else 0;
def crosslinelogic = crossesUpBottomlinebn > crossesDownToplinebn;
def HL = crossesUpline == 0 and crossesDownline == 0 and upperEMADLine crosses above lowerEMADLine;  #Normal HL LH condition
def LH = crossesDownline == 0 and upperEMADLine crosses below lowerEMADLine; #Normal HL LH condition
def bounceUpOnce = crossesUpline <> crossesUpline[1]; #XUpBtm happened previous bar or on current bar
def bounceDnOnce = crossesDownline <> crossesDownline[1];
def trackCrUp = if bounceUpOnce then 0 else if HL then 1 else trackCrUp[1];
def trackCrDn = if bounceDnOnce then 0 else if LH then 1 else trackCrDn[1];
## SECOND HL AND LH
def HLalready = if trackCrUp and !trackCrUp[1] then 1 else 0;
def LHalready = if trackCrDn and !trackCrDn[1] then 1 else 0;
def HL2 = HLalready == 0 and upperEMADLine crosses above lowerEMADLine;
def LH2 = LHalready == 0 and upperEMADLine crosses below lowerEMADLine;
def crossedUpOnce = HLalready <> HLalready[1];
def crossedDnOnce = LHalready <> LHalready[1];
def trackCrUp2 = if crossedUpOnce then 0 else if HL2 then 1 else trackCrUp2[1];
def trackCrDn2 = if crossedDnOnce then 0 else if LH2 then 1 else trackCrDn2[1];
#####################################################
##                                                 ##
#####################################################
def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
def HRef = if low <= high[1]
    then high - close[1]
    else (high - close[1]) - 0.5 * (low - high[1]);
def LRef = if high >= low[1]
    then close[1] - low
    else (close[1] - low) - 0.5 * (low[1] - high);
def trueRange_Sq;
switch (trailType) {
case modified:
    trueRange_Sq = Max(HiLo, Max(HRef, LRef));
case unmodified:
    trueRange_Sq = TrueRange(high, close, low);
}
def loss_Sq = ATRFactor * MovingAverage(averageType_Sq, trueRange_Sq, ATRPeriod);
def state_Sq = {default init, long, short};
def trail_Sq;
switch (state_Sq[1]) {
case init:
    if (!IsNaN(loss_Sq)) {
        switch (firstTrade) {
        case long:
            state_Sq = state_Sq.long;
            trail_Sq =  close - loss_Sq;
        case short:
            state_Sq = state_Sq.short;
            trail_Sq = close + loss_Sq;
    }
    } else {
        state_Sq = state_Sq.init;
        trail_Sq = Double.NaN;
    }
case long:
    if (close > trail_Sq[1]) {
        state_Sq = state_Sq.long;
        trail_Sq = Max(trail_Sq[1], close - loss_Sq);
    } else {
        state_Sq = state_Sq.short;
        trail_Sq = close + loss_Sq;
    }
case short:
    if (close < trail_Sq[1]) {
        state_Sq = state_Sq.short;
        trail_Sq = Min(trail_Sq[1], close + loss_Sq);
    } else {
        state_Sq = state_Sq.long;
        trail_Sq =  close - loss_Sq;
    }
}
def TrailingStop_Sq = trail_Sq;
def H = Highest(TrailingStop_Sq, 12);
def L = Lowest(TrailingStop_Sq, 12);
def BulgeLengthPrice = 100;
def SqueezeLengthPrice = 100;
def BandwidthC3 = (H - L);
def IntermResistance2 = Highest(BandwidthC3, BulgeLengthPrice);
def IntermSupport2 = Lowest(BandwidthC3, SqueezeLengthPrice);
def sqzTrigger = BandwidthC3 <= IntermSupport2;
def sqzLevel = if !sqzTrigger[1] and sqzTrigger then hl2 else if !sqzTrigger then Double.NaN else sqzLevel[1];
def Squeeze_Alert_1 = if sqzLevel then (upperEMADLine + lowerEMADLine) / 2 else (upperEMADLine + lowerEMADLine) / 2 ;
#####################################################
##                                                 ##
#####################################################
def over_bought_3x = 80;
def over_sold_3x = 20;
def KPeriod_3x = 10;
def DPeriod_3x = 10;
def priceH1 = emadhigh;
def priceL1 =  emadlow;
def priceC1 =  emadclose;
def priceO1 =  emadclose;
def priceH_3x =  emadhigh;
def priceL_3x =  emadlow;
def priceC_3x =  emadclose;    
def SlowK_3x = reference StochasticFull(over_bought_3x,  over_sold_3x,  KPeriod_3x,  DPeriod_3x,  priceH_3x,  priceL_3x,  priceC_3x,  3, if (averageType == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;
def MACD_3x = reference MACD()."Value";
def priceMean_3x = Average(MACD_3x, length_3x);
def MACD_stdev_3x =  (MACD_3x - priceMean_3x) / StDev(MACD_3x, length_3x);
def dPlus_3x = reference DMI()."DI+";
def dMinus_3x = reference DMI()."DI-";
def sellerRegular = SlowK_3x < 20 and MACD_stdev_3x < -1 and dPlus_3x < 15;
def sellerExtreme = SlowK_3x < 20 and MACD_stdev_3x < -2 and dPlus_3x < 15;
def buyerRegular = SlowK_3x > 80 and MACD_stdev_3x > 1 and dMinus_3x < 15;
def buyerExtreme = SlowK_3x > 80 and MACD_stdev_3x > 2 and dMinus_3x < 15;    
def ExtremeBuy1 = if sellerExtreme[1] and !sellerExtreme then 1 else 0;
def ExtremeSell1 = if buyerExtreme[1] and !buyerExtreme then 1 else 0;
def RegularSell1 = if buyerRegular[1] and !buyerRegular then 1 else 0;
def RegularBuy1= if sellerRegular[1] and !sellerRegular then 1 else 0;
#####################################################
##                                                 ##
#####################################################
def priceE = (emadhigh + emadlow) / 2;
def coeff = Ehlers_length * priceE * priceE - 2 * priceE * sum(priceE, Ehlers_length)[1] + sum(priceE * priceE, Ehlers_length)[1];
def Ehlers =  sum(coeff * priceE, Ehlers_length) / sum(coeff, Ehlers_length);
#####################################################
##                                                 ##
#####################################################
def dataA = masterEMADLine;
def dataA1 =
    fold aD = 0
    to LengtH_MAD
    with k = 0
    do k + dataA[aD];
def MAEMAD = dataA1 / LengtH_MAD;
def MAD = MAEMAD;
def ElCrossDN = ehlers crosses above masterEMADline;
def ElCrossUP = ehlers crosses below masterEMADline;
def bullBias2 = (emadsup);
def bearBias2 = (emadsdown);
def MADehlers = (MAD - Ehlers)/2;
def Madebad = madehlers + ehlers;
def madebadavg = average(madebad, lengthBAD);
def crossmadeup = madebad > madebad[1];
def crossmadedn = madebad < madebad[1];
def crossmade2up = if (madebad < masterEMADLine) and crossmadeup and (bullBias2 > 0) then 1 else 0;
def crossmade2dn = if (madebad > masterEMADLine) and crossmadedn and (bearBias2 > 0) then 1 else 0;
def crossmade2 = if (madebad < masterEMADLine) and crossmadeup and (bullBias2 > 0) then 1 else if (madebad > masterEMADLine) and crossmadedn and (bearBias2 > 0) then -1 else 0;
def crossmade0 = if (!crossmade2[1] or !crossmade2[1]) and (crossmade2 == 0) then 1 else 0;
#####################################################
##                                                 ##
#####################################################
def pd = 22;
def bbl = 20;
def mult = 2.0;
def lb = 50;
def ph = 0.85;
def ltLB = 40;
def mtLB = 14;
def str = 3;
def wvf = ((Highest(close, pd) - low) / (Highest(close, pd))) * 100;
def sDev = mult * StDev(wvf, bbl);
def midLine1 = SimpleMovingAvg(wvf, bbl);
def lowerBand1 = midLine1 - sDev;
def upperBand1 = midLine1 + sDev;
def rangeHigh = (Highest(wvf, lb)) * ph;
def upRange = low > low[1] and close > high[1];
def upRange_Aggr = close > close[1] and close > open[1];
def filtered = ((wvf[1] >= upperBand1[1] or wvf[1] >= rangeHigh[1]) and (wvf < upperBand1 and wvf < rangeHigh));
def filtered_Aggr = (wvf[1] >= upperBand1[1] or wvf[1] >= rangeHigh[1]);
def alert4 = upRange_Aggr and close > close[str] and (close < close[ltLB] or close < close[mtLB]) and filtered_Aggr;
def alert4close = if alert4 then close else 0;
def crossmade = if (madebad < masterEMADLine) and crossmadeup then 1 else if (madebad > masterEMADLine) and crossmadedn then -1 else 0;
def madedn = (madebad > masterEMADLine);
def madeup = (madebad < masterEMADLine);
def made2 = madebadavg;
def made2_abovezero = (made2 > zerolinedata);
def made2_belowzero = (made2 < zerolinedata);
def made3 = if !madedn and made2_belowzero then madebadavg else double.nan;
def made4 = if !madeup and made2_abovezero then madebadavg else double.nan;
def made5 = if (made2 == zerolinedata) then madebadavg else double.nan;
def madecrossupavg = if made2 crosses below masterEMADLine then 1 else 0;
def madecrossdownavg = if made2 crosses above masterEMADLine then 1 else 0;
def crossmadelogicup = if madecrossupavg and !madecrossupavg[1] then 1 else 0;
def crossmadelogicdown = if madecrossdownavg and !madecrossdownavg[1] then 1 else 0;
def crossmademorelogicup = if crossmadelogicup and crosslinelogic and emadup[1] then 1 else 0;
def crossmademorelogicdown = if crossmadelogicdown and !crosslinelogic and emaddown[1] then 1 else 0;
def EmaPushStepUp = if topBandStepUp and EmadUp and (((upperEMADLine) - bottomband) == 0) then 1 else 0;;
def EmaPushStepDown = if bottomBandStepDown and EmadDown and (((lowerEMADLine) - bottomband) == 0) then 1 else 0;
def HLFilter = if HL[1] < HL then 1 else 0;
def LHFilter = if LH[1] > LH then 1 else 0;
def HLFilter2 = if HL[2] < HL[1] then 1 else 0;
def LHFilter2 = if LH[2] > LH[1] then 1 else 0;
#####################################################
##                                                 ##
#####################################################
def topBandStepDown_lookback = fold index3 = 1 to Steplookback with x3 do x3 + topBandStepDown[index3];
def topBandStepUp_lookback = fold index4 = 1 to Steplookback with x4 do x4 + topBandStepUp[index4];
def bottomBandStepDown_lookback = fold index5 = 1 to Steplookback with x5 do x5 + bottomBandStepDown[index5];
def bottomBandStepUp_lookback = fold index6 = 1 to Steplookback with x6 do x6 + bottomBandStepUp[index6];
def bothBandsDown_lookback = fold index7 = 1 to filterBounceArrows with x7 do x7 + bothBandsDown[index7];
def bothBandsUp_lookback = fold index8 = 1 to filterBounceArrows with x8 do x8 + bothBandsUp[index8];
def bullBias_lookback = fold index9 = 1 to filterBounceArrows with x9 do x9 + bullBias[index9];
def bearBias_lookback = fold index10 = 1 to filterBounceArrows with x10 do x10 + bearBias[index10];
######################################################################################################################
def crossesUpZeroline_lookback = fold index11 = 1 to filterZeroline with x11 do x11 + crossesUpZeroline[index11];
def crossesDownZeroline_lookback = fold index12 = 1 to filterZeroline with x12 do x12 + crossesDownZeroline[index12];
######################################################################################################################
def crossesUpBottomline_lookback = fold index13 = 1 to filterBounceArrows with x13 do x13 + crossesUpline[index13];
def crossesDownTopline_lookback = fold index14 = 1 to filterBounceArrows with x14 do x14 + crossesDownline[index14];
def crossmade0_lookback = fold index33 = 1 to cloudlinelookback with x33 do x33 + crossmade0[index33];
#####################################################
##                                                 ##
#####################################################
def bubblehidertopdown = topBandStepDown_lookback > PreviousStepsDown;
def bubblehidertopup = topBandStepup_lookback > PreviousStepsUp;
def bubblehiderbottomdown = bottomBandStepDown_lookback > PreviousStepsDown;
def bubblehiderbottomup = bottomBandStepup_lookback > PreviousStepsUp;
def UP1 = if marketopen and (!bubblehidertopdown or !bubblehiderbottomdown) and !EmaPushStepDown and crossmademorelogicup then midband else double.nan;
def DOWN1 = if marketopen and (!bubblehidertopup or !bubblehiderbottomup) and !EmaPushStepUp and crossmademorelogicdown then midband else double.nan;
def made1 = made4;
def made = made3;
def madecolor_1 = if crossmade2 == 1 then made3 else double.nan;
def madecolor_2 = if crossmade2 == -1 then made3 else double.nan;
def madecolor_3 = if crossmade0_lookback > 1 and crosslinelogic then made3 else double.nan;
def madecolor = if crossmade2 == 0 then made3 else double.nan;
def madecolor1_1 = if crossmade2 == 1 then made4 else double.nan;
def madecolor1_2 = if crossmade2 == -1 then made4 else double.nan;
def madecolor1_3 = if crossmade0_lookback > 1 and crosslinelogic then made4 else double.nan;
def madecolor1 = if  crossmade2 == 0 then made4 else double.nan;
def UP_Alert4_1 = if (alert4) then midband else double.nan;
def masterEMADLinePlot2_1 = (upperEMADLine + lowerEMADLine) / 2;
def masterEMADLinePlot2_Up = if (masterEMADLinePlot2_1 >= masterEMADLinePlot2_1[1]) then masterEMADLinePlot2_1 else double.nan;
def masterEMADLinePlot2_Down = if (masterEMADLinePlot2_1 < masterEMADLinePlot2_1[1]) then masterEMADLinePlot2_1 else double.nan;
def TopBottomSpan = absvalue(topband) + absvalue(bottomband);
def TopBottomOffset_2 = absvalue(topband)/5;
#####################################################
##                                                 ##
#####################################################
plot zeroLine = if (upperEMADLine > zerolinedata) then zerolinedata else double.nan;
plot zeroLine_2 = if (upperEMADLine < zerolinedata) then zerolinedata else double.nan;
plot zeroLine_3 = if (!zeroline and !zeroline_2) then zerolinedata else double.nan;
plot Squeeze_Alert = if sqzLevel then (upperEMADLine + lowerEMADLine) / 2 else (upperEMADLine + lowerEMADLine) / 2 ;
plot ExtremeBuy = if showTripleExh and sellerExtreme[1] and !sellerExtreme then midband else Double.NaN;
plot ExtremeSell = if showTripleExh and buyerExtreme[1] and !buyerExtreme then midband else Double.NaN;
plot RegularSell = if showTripleExh and buyerRegular[1] and !buyerRegular then midband else Double.NaN;
plot RegularBuy = if showTripleExh and sellerRegular[1] and !sellerRegular then midband else Double.NaN;
plot topBandPlot_offset = topBand +  TopBottomOffset_2;
plot topBandPlot = if !bothBandsDown and !bothBandsUp and !topBandStepUp and !topBandStepDown then topBand else double.nan;
plot topBandPlot_1 = if bothBandsDown then topBand else double.nan;
plot topBandPlot_2 = if bothBandsup then topBand else double.nan;
plot topBandPlot_3 = if topBandStepUp then topBand else double.nan;
plot topBandPlot_4 = if topBandStepDown then topBand else double.nan;
plot topBandPlot_5 = if bearbias then topBand else double.nan;
plot bottomBandPlot_offset = bottomBand - TopBottomOffset_2;
plot bottomBandPlot = if (!bothBandsDown and !bothBandsUp and !bottomBandStepUp and !bottomBandStepDown) or (bottomband == bottomband[1]) then bottomBand else double.nan;
plot bottomBandPlot_1 = if bothBandsDown then bottomBand else double.nan;
plot bottomBandPlot_2 = if bothBandsUp then bottomBand else double.nan;
plot bottomBandPlot_3 = if bottomBandStepUp then bottomBand else double.nan;
plot bottomBandPlot_4 = if bottomBandStepDown then bottomBand else double.nan;
plot bottomBandPlot_5 = if bullbias then bottomBand else double.nan;
plot upperEMADLinePlot_3 = upperEMADLine;
plot lowerEMADLinePlot_3 = lowerEMADline;
plot masterEMADLinePlot =  masterEMADLinePlot2_1;
plot masterEMADLinePlot_Up =  masterEMADLinePlot2_Up;
plot masterEMADLinePlot_Down =  masterEMADLinePlot2_Down;
plot made55 = made5;
plot made1_1 = madecolor_1;
plot made1_2 = madecolor_2;
plot made1_3 = madecolor_3;
plot made1_0 = madecolor;
plot made_1 = madecolor1_1;
plot made_2 = madecolor1_2;
plot made_3 = madecolor1_3;
plot made_0 = madecolor1;
plot UP_Alert4 = UP_Alert4_1;
plot UP = UP1;
plot DOWN = DOWN1;
#####################################################
##                                                 ##
#####################################################
UP_Alert4.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UP_Alert4.SetLineWeight(1);
UP_Alert4.SetDefaultColor(Color.cyan);
UP_Alert4.HideBubble();
UP_Alert4.HideTitle();
#####################################################
##                                                 ##
#####################################################
UP.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UP.SetLineWeight(1);
UP.SetDefaultColor(Color.white);
UP.HideBubble();
UP.HideTitle();
#####################################################
##                                                 ##
#####################################################
DOWN.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DOWN.SetLineWeight(1);
DOWN.SetDefaultColor(Color.white);
DOWN.HideBubble();
DOWN.HideTitle();
#####################################################
##                                                 ##
#####################################################
made55.SetDefaultColor(color.gray);
#####################################################
##                                                 ##
#####################################################
made1_1.SetDefaultColor(color.dark_green);
#####################################################
##                                                 ##
#####################################################
made1_2.SetDefaultColor(color.dark_red);
#####################################################
##                                                 ##
#####################################################
made1_3.SetDefaultColor(color.gray);
#####################################################
##                                                 ##
#####################################################
made1_0.SetDefaultColor(color.dark_green);
#####################################################
##                                                 ##
#####################################################
made_1.SetDefaultColor(color.dark_green);
#####################################################
##                                                 ##
#####################################################
made_2.SetDefaultColor(color.dark_red);
#####################################################
##                                                 ##
#####################################################
made_3.SetDefaultColor(color.gray);
#####################################################
##                                                 ##
#####################################################
made_0.SetDefaultColor(color.dark_red);
#####################################################
##                                                 ##
#####################################################
Squeeze_Alert.SetPaintingStrategy(PaintingStrategy.line);
Squeeze_Alert.SetLineWeight(3);
Squeeze_Alert.SetDefaultColor(Color.YELLOW);
#####################################################
##                                                 ##
#####################################################
RegularBuy.SetPaintingStrategy(PaintingStrategy.Histogram);
ExtremeBuy.SetPaintingStrategy(PaintingStrategy.Histogram);
RegularSell.SetPaintingStrategy(PaintingStrategy.Histogram);
ExtremeSell.SetPaintingStrategy(PaintingStrategy.Histogram);
#####################################################
##                                                 ##
#####################################################
RegularBuy.SetLineWeight((1));
ExtremeBuy.SetLineWeight((1));
RegularSell.SetLineWeight((1));
ExtremeSell.SetLineWeight((1));
#####################################################
##                                                 ##
#####################################################
ExtremeSell.SetDefaultColor((Color.red));
ExtremeBuy.SetDefaultColor((Color.green));
RegularBuy.SetDefaultColor((Color.green));
RegularSell.SetDefaultColor((Color.red));
#####################################################
##                                                 ##
#####################################################
upperEMADLinePlot_3.HideTitle();
upperEMADLinePlot_3.HideBubble();
upperEMADLinePlot_3.SetLineWeight((1));
upperEMADLinePlot_3.SetDefaultColor(color.dark_gray
);
lowerEMADLinePlot_3.HideTitle();
lowerEMADLinePlot_3.HideBubble();
lowerEMADLinePlot_3.SetLineWeight((1));
lowerEMADLinePlot_3.SetDefaultColor(color.dark_gray
);
masterEMADLinePlot_Up.HideTitle();
masterEMADLinePlot_Up.HideBubble();
masterEMADLinePlot_Up.SetLineWeight((2));
masterEMADLinePlot_Up.SetDefaultColor(color.green
);
masterEMADLinePlot_Down.HideTitle();
masterEMADLinePlot_Down.HideBubble();
masterEMADLinePlot_Down.SetLineWeight((1));
masterEMADLinePlot_Down.SetDefaultColor(color.red
);
masterEMADLinePlot.HideTitle();
masterEMADLinePlot.HideBubble();
masterEMADLinePlot.SetLineWeight((1));
masterEMADLinePlot.SetDefaultColor(color.gray
);
zeroLine.HideTitle();
zeroLine.HideBubble();
zeroLine.SetDefaultColor(Color.green
);
zeroLine_2.HideTitle();
zeroLine_2.HideBubble();
zeroLine_2.SetDefaultColor(color.red
);
zeroLine_3.HideTitle();
zeroLine_3.HideBubble();
zeroLine_3.SetDefaultColor(color.gray
);
topBandPlot_offset.SetPaintingStrategy(PaintingStrategy.liNE_VS_POINTS);
topBandPlot_offset.HideTitle();
topBandPlot_offset.HideBubble();
topBandPlot_offset.SetDefaultColor(Color.black
);
topBandPlot.HideTitle();
topBandPlot.HideBubble();
topBandPlot.SetDefaultColor(Color.gray
);
topBandPlot_1.HideTitle();
topBandPlot_1.HideBubble();
topBandPlot_1.SetdefaultColor(Color.red
);
topBandPlot_2.HideTitle();
topBandPlot_2.HideBubble();
topBandPlot_2.SetDefaultColor(Color.green
);
topBandPlot_3.HideTitle();
topBandPlot_3.HideBubble();
topBandPlot_3.SetDefaultColor(Color.green
);
topBandPlot_4.HideTitle();
topBandPlot_4.HideBubble();
topBandPlot_4.SetDefaultColor(Color.red
);
topBandPlot_5.HideTitle();
topBandPlot_5.HideBubble();
topBandPlot_5.SetDefaultColor(Color.yellow
);
BottomBandPlot_offset.SetPaintingStrategy(PaintingStrategy.liNE_VS_POINTS);
BottomBandPlot_offset.HideTitle();
BottomBandPlot_offset.HideBubble();
BottomBandPlot_offset.SetDefaultColor(Color.black
);
BottomBandPlot.HideTitle();
BottomBandPlot.HideBubble();
BottomBandPlot.SetDefaultColor(Color.gray
);
BottomBandPlot_1.HideTitle();
BottomBandPlot_1.HideBubble();
BottomBandPlot_1.SetDefaultColor(Color.red
);
BottomBandPlot_2.HideTitle();
BottomBandPlot_2.HideBubble();
BottomBandPlot_2.SetDefaultColor(Color.green
);
BottomBandPlot_3.HideTitle();
BottomBandPlot_3.HideBubble();
BottomBandPlot_3.SetDefaultColor(Color.green
);
BottomBandPlot_4.HideTitle();
BottomBandPlot_4.HideBubble();
BottomBandPlot_4.SetDefaultColor(Color.red
);
BottomBandPlot_5.HideTitle();
BottomBandPlot_5.HideBubble();
BottomBandPlot_5.SetDefaultColor(Color.yellow
);
#####################################################
##                                                 ##
#####################################################
AddVerticalLine(showVerticalline and crossesUpline and crossesUpline_filter, "", GlobalColor("Green"), no);
AddVerticalLine(showVerticalline and crossesDownline and crossesDownline_filter,"" , GlobalColor("Red"), yes);
 
Last edited:
C3_BIG_MAD & EMAD

Chart Style: http://tos.mx/akTHv1k (updated the link and code found an issue)

No life altering changes yet... Just cleaned up the code and removed un-used code and made a few changes.

The new C3_BIG_MAD now includes the arrows for LH HL LH2 HL2 and UP and DOWN (the white arrows) from the EMAD lower study. Also the Spark, Big4, and TS_V9 arrows are separate (whereas I had combined them into a single indication previously... which I am still working on something of that nature).

Code:
   ##########################################################################################
   ##########################################################################################
   ###                                                                                    ###
   ###       #############################           #########               #########    ###
   ###     #################################         #########               #########    ###   
   ###    ####################################       #########               #########    ###
   ###   ########                     #########      #########               #########    ###       
   ###   ########    EMAD Variant     #########      #########               #########    ###
   ###   ########   @Christopher84    #########      #########               #########    ###     
   ###   ########       @HODL         #########      #########               #########    ###   
   ###   ########                     #########      #########               #########    ###       
   ###   ######################################      ##################################   ###     
   ###    ####################################       ##################################   ### 
   ###     #################################         ##################################   ###
   ###    ####################################       ##################################   ###   
   ###   ########                     #########                              #########    ###
   ###   ########     @Horserider     #########                              #########    ###
   ###   ########  HORSERIDER VOLUME  #########                              #########    ###           
   ###   ########  TRIPLE EXHAUSTION  #########                              #########    ###       
   ###   ########      @chence27      #########                              #########    ###           
   ###   ########                     #########                              #########    ###       
   ###    ####################################                               #########    ###     
   ###     ##################################                                #########    ###       
   ###      ###############################                                  #########    ###     
   ###                                                                                    ###
   ##########################################################################################
   ##########################################################################################

declare lower;

########################################################
## INPUTS                                             ##
########################################################
input Color_Candles = no;
input showbubbles = no;
input x = 5;
input Show_EMA_Cloud = yes;
input Show_HL_LH = no;
input Show_TripleExh_Arrows = no;
input Band_Length = 100;
input Smooth_Length = 12;
input Smooth_Length_2 = 14;
input Ehlers_Length = 34;
Input Length_MAD = 4;
input Cloud_Lookback = 20;
input HL_LH_Lookback = 20;
input Previous_Steps_Down = 2; #hint filter for hiding arrows
input Previous_Steps_Up = 2; #hint filter for hiding arrows
input TradeDaytimeOnly = yes; #hint tradeDaytimeOnly: (IntraDay Only) Only perform trades during hours stated
input OpenTime = 0730; #hint OpenTime: Opening time of market hides arrows
input CloseTime = 1800; #hint CloseTime: Closing time of market hides arrows
input FirstTrade_Sq = {default long, short};
input TrailType = {default modified, unmodified};
input FirstTrade = {default long, short};
########################################################
## 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));
########################################################
##                                                    ##
########################################################
def bn = barnumber();
def Begin = SecondsFromTime(OpenTime);
def End = SecondsTillTime(CloseTime);
def isIntraDay = if GetAggregationPeriod() > 14400000 or GetAggregationPeriod() == 0 then 0 else 1;
def MarketOpen = if !tradeDaytimeOnly or !isIntraDay then 1 else if tradeDaytimeOnly and isIntraDay and Begin > 0 and End > 0 then 1 else 0;
########################################################
##                                                    ##
########################################################
def Fast_Length = 10;
def Slow_Length = 35;
def Made_Avg_Length = 3;
def ATR_Period = 5;
def ATR_Factor = 2.0;
def Length_3x = 1000;
def AverageType = AverageType.WILDERS;
def AverageType_Sq = AverageType.SIMPLE;
def EMAD_EMA_lineweight = 2;
def Step_lookback = 10;
def FastExpAvg = ExpAverage(close, fast_Length);
def SlowExpAvg = ExpAverage(close, slow_Length);
def EMAD_Fast = (close - fastExpAvg);
def EMAD_Slow = (close - slowExpAvg);
def EMAD_Avg = (EMAD_Fast + EMAD_Slow) / 2;
#####################################################
## UPPER EMA                                       ##
#####################################################
def Upper_EMAD_EMA = ExpAverage(EMAD_Avg, smooth_Length);
def EMAD_Open = (Upper_EMAD_EMA + Upper_EMAD_EMA[1]) / 2;
def EMAD_High = Max(Upper_EMAD_EMA, Upper_EMAD_EMA[1]);
def EMAD_Low = Min(Upper_EMAD_EMA, Upper_EMAD_EMA[1]);
def EMAD_Close = Upper_EMAD_EMA;
def Bottom = Min(EMAD_Close[1], EMAD_Low);
def TR = TrueRange(EMAD_High, EMAD_Close, EMAD_Low);
def PTR = tr / (bottom + tr / 2);
def APTR = MovingAverage(averageType, ptr, smooth_Length_2);
def UpperBand = EMAD_Close[1] + (APTR * EMAD_Open);
def LowerBand = EMAD_Close[1] - (APTR * EMAD_Open);
#####################################################
## LOWER EMA                                       ##
#####################################################
def Lower_EMAD_EMA = (upperBand + lowerBand) / 2;
#####################################################
## TOP BAND                                        ##
#####################################################
def TopBand = Highest(Lower_EMAD_EMA, band_Length);
#####################################################
## BOTTOM BAND                                     ##
#####################################################
def BottomBand = Lowest(Lower_EMAD_EMA, band_Length);
#####################################################
## ZERO LINE                                       ##
#####################################################
def ZeroLineData = if IsNaN(close) then Double.NaN else 0;
def EMAD_Above_Zero = Upper_EMAD_EMA > zeroLineData;
def EMAD_Below_Zero = Upper_EMAD_EMA < zeroLineData;
def EMAD_EMA_down = (Lower_EMAD_EMA > Upper_EMAD_EMA);
def EMAD_EMA_up = (Upper_EMAD_EMA >= Lower_EMAD_EMA);
#####################################################
## MASTER EMA                                      ##
#####################################################
def Master_EMAD_EMA = (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2;
def TopBand_StepDown = if topBand < topBand[1] then 1 else 0;
def TopBand_StepUp = if topBand > topBand[1] then 1 else 0;
def BottomBand_StepDown = if bottomBand < bottomBand[1] then 1 else 0;
def BottomBand_StepUp = if bottomBand > bottomBand[1] then 1 else 0;
def BothBands_Down = bottomBand_StepDown and TopBand_StepDown;
def BothBands_Up = bottomBand_StepUp and TopBand_StepUp;
def BottomBand_Above_Zero = (bottomBand > zeroLineData);
def TopBand_below_Zero = (topBand < zeroLineData);
def TopBand_PushUp =  Master_EMAD_EMA >= TopBand;
def TopBand_PushUp_2 = TopBand_PushUp within x bars;
def BottomBand_PushDown = Master_EMAD_EMA <= bottomBand;
def BottomBand_PushDown_2 = BottomBand_PushDown within x bars;
def MidBand = (upperBand + lowerBand) / 2;
def EMA_CrossUp = if (midBand[1] > Upper_EMAD_EMA[1])and(midBand < Upper_EMAD_EMA) then 1 else 0;
def EMA_CrossDown = if (Upper_EMAD_EMA[1] > midBand[1])and(Upper_EMAD_EMA < midBand) then 1 else 0;
def Value_At_CrossUp = if EMA_CrossUp then midBand else Value_At_CrossUp[1];
def Value_At_CrossDown = if EMA_CrossDown then midBand else Value_At_CrossDown[1];
def CrossUp_BottomBand = if (Value_At_CrossUp - bottomBand) == 0 then 1 else 0;
def CrossDown_TopBand = if (Value_At_CrossDown - topBand) == 0 then 1 else 0;
def CrossUp_Zeroline = if (Value_At_CrossUp) == 0 then 1 else 0;
def CrossDown_Zeroline = if (Value_At_CrossDown) == 0 then 1 else 0;

AddChartBubble(showBubbles and CrossUp_Zeroline, midBand, " XUp @ 0 ", GlobalColor("Green"), no);
AddChartBubble(showBubbles and CrossDown_Zeroline, midBand," XDown @ 0 " , GlobalColor("Red"), yes);

def CrossUp_BottomBand_bn = if CrossUp_Bottomband and !CrossUp_Bottomband[1] then bn else CrossUp_Bottomband_bn[1];
def CrossDown_TopBand_bn = if CrossDown_topband and !CrossDown_topband[1] then bn else CrossDown_TopBand_bn[1];
def CrossUp_BottomBand_Within = if CrossUp_Bottomband_bn < bn then bn - CrossUp_Bottomband_bn else CrossUp_BottomBand_Within[1];
def CrossDown_TopBand_Within = if CrossDown_TopBand_bn < bn then bn - CrossDown_TopBand_bn else CrossDown_TopBand_Within[1];
def Band_Crossed_First = CrossUp_Bottomband_bn > CrossDown_TopBand_bn;
#####################################################
## HIGHER LOW - LOWER HIGH                         ##
#####################################################
def HL = CrossUp_Bottomband == 0 and CrossDown_topband == 0 and Upper_EMAD_EMA crosses above Lower_EMAD_EMA;  #Normal HL LH condition
def LH = CrossDown_topband == 0 and CrossUp_Bottomband == 0 and Upper_EMAD_EMA crosses below Lower_EMAD_EMA; #Normal HL LH condition
def bounceUpOnce = CrossUp_Bottomband <> CrossUp_Bottomband[1]; #XUpBtm happened previous bar or on current bar (<> = is NOT equal to)
def bounceDnOnce = CrossDown_topband <> CrossDown_topband[1];
def trackCrUp = if bounceUpOnce then 0 else if HL then 1 else trackCrUp[1];
def trackCrDn = if bounceDnOnce then 0 else if LH then 1 else trackCrDn[1];
def HLalready = if trackCrUp and !trackCrUp[1] then 1 else 0;
def LHalready = if trackCrDn and !trackCrDn[1] then 1 else 0;
def HL2 = HLalready == 0 and Upper_EMAD_EMA crosses above Lower_EMAD_EMA;
def LH2 = LHalready == 0 and Upper_EMAD_EMA crosses below Lower_EMAD_EMA;
def crossedUpOnce = HLalready <> HLalready[1];
def crossedDnOnce = LHalready <> LHalready[1];
def trackCrUp2 = if crossedUpOnce then 0 else if HL2 then 1 else trackCrUp2[1];
def trackCrDn2 = if crossedDnOnce then 0 else if LH2 then 1 else trackCrDn2[1];
#####################################################
## SQUEEZE                                         ##
#####################################################
def HiLo = Min(high - low, 1.5 * Average(high - low, ATR_Period));
def HRef = if low <= high[1]
    then high - close[1]
    else (high - close[1]) - 0.5 * (low - high[1]);
def LRef = if high >= low[1]
    then close[1] - low
    else (close[1] - low) - 0.5 * (low[1] - high);
def trueRange_Sq;
switch (trailType) {
case modified:
    trueRange_Sq = Max(HiLo, Max(HRef, LRef));
case unmodified:
    trueRange_Sq = TrueRange(high, close, low);
}
def loss_Sq = ATR_Factor * MovingAverage(averageType_Sq, trueRange_Sq, ATR_Period);
def state_Sq = {default init, long, short};
def trail_Sq;
switch (state_Sq[1]) {
case init:
    if (!IsNaN(loss_Sq)) {
        switch (firstTrade) {
        case long:
            state_Sq = state_Sq.long;
            trail_Sq =  close - loss_Sq;
        case short:
            state_Sq = state_Sq.short;
            trail_Sq = close + loss_Sq;
    }
    } else {
        state_Sq = state_Sq.init;
        trail_Sq = Double.NaN;
    }
case long:
    if (close > trail_Sq[1]) {
        state_Sq = state_Sq.long;
        trail_Sq = Max(trail_Sq[1], close - loss_Sq);
    } else {
        state_Sq = state_Sq.short;
        trail_Sq = close + loss_Sq;
    }
case short:
    if (close < trail_Sq[1]) {
        state_Sq = state_Sq.short;
        trail_Sq = Min(trail_Sq[1], close + loss_Sq);
    } else {
        state_Sq = state_Sq.long;
        trail_Sq =  close - loss_Sq;
    }
}
def TrailingStop_Sq = trail_Sq;
def H = Highest(TrailingStop_Sq, 12);
def L = Lowest(TrailingStop_Sq, 12);
def BandwidthC3 = (H - L);
def IntermResistance2 = Highest(BandwidthC3, Band_Length);
def IntermSupport2 = Lowest(BandwidthC3, Band_Length);
def Squeeze_Trigger = BandwidthC3 <= IntermSupport2;
def Squeeze_Level = if !Squeeze_Trigger[1] and Squeeze_Trigger then hl2 else if !Squeeze_Trigger then Double.NaN else Squeeze_Level[1];
def Squeeze_Alert_1 = if Squeeze_Level then (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2 else (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2 ;
#####################################################
## TRIPLE EXHAUSTION                               ##
#####################################################
def OB_3x = 80;
def OS_3x = 20;
def KPeriod_3x = 10;
def DPeriod_3x = 10; 
def SlowK_3x = reference StochasticFull(OB_3x,  OS_3x,  KPeriod_3x,  DPeriod_3x,  EMAD_High, EMAD_Low, EMAD_Close,  3, if (averageType == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;
def MACD_3x = reference MACD()."Value";
def Price_Avg_3x = Average(MACD_3x, length_3x);
def MACD_stdev_3x =  (MACD_3x - Price_Avg_3x) / StDev(MACD_3x, length_3x);
def DPlus_3x = reference DMI()."DI+";
def DMinus_3x = reference DMI()."DI-";
def SellerRegular = SlowK_3x < 20 and MACD_stdev_3x < -1 and dPlus_3x < 15;
def SellerExtreme = SlowK_3x < 20 and MACD_stdev_3x < -2 and dPlus_3x < 15;
def BuyerRegular = SlowK_3x > 80 and MACD_stdev_3x > 1 and dMinus_3x < 15;
def BuyerExtreme = SlowK_3x > 80 and MACD_stdev_3x > 2 and dMinus_3x < 15;   
def Extreme_Buy = if sellerExtreme[1] and !sellerExtreme then 1 else 0;
def Extreme_Sell = if buyerExtreme[1] and !buyerExtreme then 1 else 0;
def Regular_Sell = if buyerRegular[1] and !buyerRegular then 1 else 0;
def Regular_Buy = if sellerRegular[1] and !sellerRegular then 1 else 0;
#####################################################
## EHLERS                                          ##
#####################################################
def Price_Ehlers = (EMAD_High + EMAD_Low) / 2;
def Coeff = Ehlers_length * Price_Ehlers * Price_Ehlers - 2 * Price_Ehlers * sum(Price_Ehlers, Ehlers_length)[1] + sum(Price_Ehlers * Price_Ehlers, Ehlers_length)[1];
def Ehlers =  sum(coeff * Price_Ehlers, Ehlers_length) / sum(coeff, Ehlers_length);
#####################################################
## MADE AVERAGE                                    ##
#####################################################
def dataA = Master_EMAD_EMA;
def dataA1 = fold aD = 0 to Length_MAD with k = 0 do k + dataA[aD];
def MAEMAD = dataA1 / LengtH_MAD;
def MADehlers = (MAEMAD - Ehlers)/2;
def Madebad = madehlers + ehlers;
def Madebad_Avg = average(madebad, Made_Avg_Length);
def Madebad_CrossUp = if (madebad < Master_EMAD_EMA) and (madebad > madebad[1]) and (EMAD_Above_Zero > 0) then 1 else 0;
def Madebad_CrossDown = if (madebad > Master_EMAD_EMA) and (madebad < madebad[1]) and (EMAD_Below_Zero > 0) then 1 else 0;
def Madebad_CrossCondition = if (madebad < Master_EMAD_EMA) and (madebad > madebad[1]) and (EMAD_Above_Zero > 0) then 1
                        else if (madebad > Master_EMAD_EMA) and (madebad < madebad[1]) and (EMAD_Below_Zero > 0) then -1 else 0;
def Madebad_Condition = if (!Madebad_CrossCondition[1] or !Madebad_CrossCondition[2]) and (Madebad_CrossCondition == 0) then 1 else 0;
#####################################################
## ALERT4                                          ##
#####################################################
def PD = 22;
def BBL = 20;
def MULT = 2.0;
def LB = 50;
def PH = 0.85;
def LTLB = 40;
def MTLB = 14;
def STR = 3;
def WVF = ((Highest(close, pd) - low) / (Highest(close, pd))) * 100;
def StDev = mult * StDev(wvf, bbl);
def MidLine1 = SimpleMovingAvg(wvf, bbl);
def UpperBand1_Alert4 = midLine1 + StDev;
def Range_High = (Highest(wvf, lb)) * ph;
def UpRange = low > low[1] and close > high[1];
def UpRange_Aggr = close > close[1] and close > open[1];
def Filtered = ((wvf[1] >= UpperBand1_Alert4[1] or wvf[1] >= Range_High[1]) and (wvf < UpperBand1_Alert4 and wvf < Range_High));
def Filtered_Aggr = (wvf[1] >= UpperBand1_Alert4[1] or wvf[1] >= Range_High[1]);
def Alert4 = upRange_Aggr and close > close[str] and (close < close[ltLB] or close < close[mtLB]) and filtered_Aggr;
def Alert4close = if alert4 then close else 0;
def Made_Down = (madebad > Master_EMAD_EMA);
def Made_Up = (madebad < Master_EMAD_EMA);
def Made_Bottom_MA = if !Made_Down and (Madebad_Avg <= zerolinedata) then Madebad_Avg else double.nan;
def Made_Top_MA = if !Made_Up and (Madebad_Avg >= zerolinedata) then Madebad_Avg else double.nan;
def MadeAvg_Master_Cross_Up = if Madebad_Avg crosses below Master_EMAD_EMA then 1 else 0;
def MadeAvg_Master_Cross_Down = if Madebad_Avg crosses above Master_EMAD_EMA then 1 else 0;
def Cross_Up_MadeAvg_Master = if MadeAvg_Master_Cross_Up and !MadeAvg_Master_Cross_Up[1] then 1 else 0;
def Cross_Down_MadeAvg_Master = if MadeAvg_Master_Cross_Down and !MadeAvg_Master_Cross_Down[1] then 1 else 0;
def MadeAvg_Master_Up_Condition = if Cross_Up_MadeAvg_Master and Band_Crossed_First and EMAD_EMA_Up[1] then 1 else 0;
def MadeAvg_Master_Down_Condition = if Cross_Down_MadeAvg_Master and !Band_Crossed_First and EMAD_EMA_down[1] then 1 else 0;
def EMA_Push_StepUp = if topBand_StepUp and EMAD_EMA_Up and (((Upper_EMAD_EMA) - topband) == 0) then 1 else 0;;
def EMA_Push_StepDown = if bottomBand_StepDown and EMAD_EMA_down and (((Lower_EMAD_EMA) - bottomband) == 0) then 1 else 0;
#####################################################
## MARKET PHASES                                   ##
#####################################################
def Fastavg_Phases = 50;
def Slowavg_Phases = 200;
def Fastsma_Phases = Average( close, Fastavg_Phases);
def Slowsma_Phases = Average(close, Slowavg_Phases);
def Bullphase = fastsma_Phases > slowsma_Phases && close > fastsma_Phases && close > slowsma_Phases; # Accumulation Phase : close > 50 SMA, close > 200 SMA, 50 SMA < 200 SMA
def Accphase = fastsma_Phases < slowsma_Phases && close > fastsma_Phases && close > slowsma_Phases;# Recovery Phase : close > 50 SMA, close < 200 SMA, 50 SMA < 200 SMA
def Recphase = fastsma_Phases < slowsma_Phases && close < slowsma_Phases && close > fastsma_Phases;# Bearish Phase : close < 50 SMA, close < 200 SMA, 50 SMA < 200 SMA
def Bearphase = fastsma_Phases < slowsma_Phases && close < fastsma_Phases && close < slowsma_Phases;# Distribution Phase : close < 50 SMA, close < 200 SMA, 50 SMA > 200 SMA
def Distphase = fastsma_Phases > slowsma_Phases && close < fastsma_Phases && close < slowsma_Phases;# Warning Phase : close < 50 SMA, close > 200 SMA, 50 SMA > 200 SMA
def Warnphase = fastsma_Phases> slowsma_Phases && close > slowsma_Phases && close < fastsma_Phases;
#####################################################
##                                                 ##
#####################################################
def FirstHL2 = if trackCrUp and !trackCrUp[1] then 1 else 0;
def FirstLH2 = if trackCrDn and !trackCrDn[1] then 1 else 0;
def HLBuy = if firstHL2 and Regular_Sell or Extreme_Sell then 1 else 0;
def LHSell = if firstHL2 and Regular_Buy or Extreme_Buy then 1 else 0;
def LHsellnear = LHsell within x bars;
def UP2_cond = if (bottomBand_Stepup and alert4) or (CrossUp_bottomband and alert4) or (alert4 and LHsellnear) then 1 else 0;
def First_HL_1 = if Marketopen and Show_HL_LH and trackCrUp and !trackCrUp[1] then Lower_EMAD_EMA else double.nan;
def First_LH_1 = if Marketopen and Show_HL_LH and trackCrDn and !trackCrDn[1] then Lower_EMAD_EMA else double.nan;
def Second_HL_1 = if Marketopen and Show_HL_LH and trackCrUp2 and !trackCrUp2[1] and !CrossUp_Bottomband then Lower_EMAD_EMA else double.nan;
def Second_LH_1 = if Marketopen and Show_HL_LH and trackCrDn2 and !trackCrDn2[1] and !CrossDown_Topband then Lower_EMAD_EMA else double.nan;
#####################################################
## FOLD                                            ##
#####################################################
def TopBandStepDown_lookback = fold index1 = 1 to Step_lookback with x1 do x1 + topBand_StepDown[index1];
def TopBandStepUp_lookback = fold index2 = 1 to Step_lookback with x2 do x2 + topBand_StepUp[index2];
def BottomBandStepDown_lookback = fold index3 = 1 to Step_lookback with x3 do x3 + bottomBand_StepDown[index3];
def BottomBandStepUp_lookback = fold index4 = 1 to Step_lookback with x4 do x4 + bottomBand_StepUp[index4];
def Madebad_Condition_lookback = fold index5 = 1 to Cloud_lookback with x5 do x5 + Madebad_Condition[index5];
def HL_lookback = fold index6 = 1 to HL_LH_Lookback with x6 do x6 + First_HL_1[index6];
def LH_lookback = fold index7 = 1 to HL_LH_Lookback with x7 do x7 + First_LH_1[index7];
def HL2_lookback = fold index8 = 1 to HL_LH_Lookback with x8 do x8 + Second_HL_1[index8];
def LH2_lookback = fold index9 = 1 to HL_LH_Lookback with x9 do x9 + Second_LH_1[index9];
#####################################################
##                                                 ##
#####################################################
def ArrowFilter_TopBand_StepDown = topBandStepDown_lookback > Previous_Steps_Down;
def ArrowFilter_TopBand_StepUp = topBandStepup_lookback > Previous_Steps_Up;
def ArrowFilter_BottomBand_StepDown = bottomBandStepDown_lookback > Previous_Steps_Down;
def ArrowFilter_BottomBand_StepUp = bottomBandStepup_lookback > Previous_Steps_Up;
def UP_1 = if marketopen and (!ArrowFilter_TopBand_StepDown or !ArrowFilter_BottomBand_StepDown) and !EMA_Push_StepDown and MadeAvg_Master_Up_Condition then midband else double.nan;
def DOWN_1 = if marketopen and (!ArrowFilter_TopBand_StepUp or !ArrowFilter_BottomBand_StepUp) and !EMA_Push_StepUp and MadeAvg_Master_Down_Condition then midband else double.nan;
def TopBottomOffset_2 = absvalue(topband)/5;
#####################################################
## PLOTS                                           ##
#####################################################
plot ZeroLine = zerolinedata;
plot Squeeze_Alert = if Squeeze_Level then (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2 else (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2 ;
plot ExtremeBuy = if Show_TripleExh_Arrows and sellerExtreme[1] and !sellerExtreme then midband else Double.NaN;
plot ExtremeSell = if Show_TripleExh_Arrows and buyerExtreme[1] and !buyerExtreme then midband else Double.NaN;
plot RegularSell = if Show_TripleExh_Arrows and buyerRegular[1] and !buyerRegular then midband else Double.NaN;
plot RegularBuy = if Show_TripleExh_Arrows and sellerRegular[1] and !sellerRegular then midband else Double.NaN;
plot TopBandPlot_offset = topBand + TopBottomOffset_2;
plot BottomBandPlot_offset = bottomBand - TopBottomOffset_2;
plot TopBandPlot = topBand;
plot BottomBandPlot = bottomBand;
plot UpperEMADLinePlot = Upper_EMAD_EMA;
plot LowerEMADLinePlot = Lower_EMAD_EMA;
plot MasterEMADLinePlot_2 = (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2;
plot MasterEMADLinePlot = ((upperEMADLinePlot + lowerEMADLinePlot) / 2);
plot Made_1 = made_top_ma;
plot Made = made_bottom_ma;
plot UP = UP_1;
plot DOWN = DOWN_1;
plot First_HL = if Marketopen and Show_HL_LH and trackCrUp and !trackCrUp[1] then Lower_EMAD_EMA else double.nan;
plot First_LH = if Marketopen and Show_HL_LH and trackCrDn and !trackCrDn[1] then Lower_EMAD_EMA else double.nan;
plot Second_HL = if Marketopen and Show_HL_LH and trackCrUp2 and !trackCrUp2[1] and !CrossUp_Bottomband then Lower_EMAD_EMA else double.nan;
plot Second_LH = if Marketopen and Show_HL_LH and trackCrDn2 and !trackCrDn2[1] and !CrossDown_Topband then Lower_EMAD_EMA else double.nan;
plot UP_2 = if UP2_cond and !UP2_Cond[1] then midband else double.nan;;
#####################################################
##                                                 ##
#####################################################
UP_2.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UP_2.SetLineWeight(1);
UP_2.SetDefaultColor(Color.cyan);
UP_2.HideBubble();
UP_2.HideTitle();
#####################################################
##                                                 ##
#####################################################
UP.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UP.SetLineWeight(1);
UP.assignvaluecolor(if alert4 then color.yellow else Color.white);
UP.HideBubble();
UP.HideTitle();
#####################################################
##                                                 ##
#####################################################
DOWN.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DOWN.SetLineWeight(1);
DOWN.SetDefaultColor(Color.white);
DOWN.HideBubble();
DOWN.HideTitle();
#####################################################
##                                                 ##
#####################################################
First_HL.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
First_HL.SetLineWeight(1);
First_HL.SetDefaultColor(Color.magenta);
First_HL.HideBubble();
First_HL.HideTitle();
#####################################################
##                                                 ##
#####################################################
First_LH.SetPaintingStrategy(PaintingStrategy.ARROW_down);
First_LH.SetLineWeight(1);
First_LH.SetDefaultColor(Color.magenta);
First_LH.HideBubble();
First_LH.HideTitle();
#####################################################
##                                                 ##
#####################################################
Second_HL.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Second_HL.SetLineWeight(1);
Second_HL.SetDefaultColor(Color.orange);
Second_HL.HideBubble();
Second_HL.HideTitle();
#####################################################
##                                                 ##
#####################################################
Second_LH.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
Second_LH.SetLineWeight(1);
Second_LH.SetDefaultColor(Color.orange);
Second_LH.HideBubble();
Second_LH.HideTitle();
#####################################################
##                                                 ##
#####################################################
Made_1.AssignValueColor(
     if Madebad_CrossCondition == 1 then color.dark_green
else if Madebad_CrossCondition == -1 then color.dark_red
else if Madebad_Condition_lookback > 1 and Band_Crossed_First then Color.gray
else color.dark_red);
#####################################################
##                                                 ##
#####################################################
Made.AssignValueColor(
     if Madebad_CrossCondition == 1 then color.dark_green
else if Madebad_CrossCondition == -1 then color.dark_red
else if Madebad_Condition_lookback > 1 and Band_Crossed_First then Color.gray
else color.dark_red);
#####################################################
##                                                 ##
#####################################################
Squeeze_Alert.SetPaintingStrategy(PaintingStrategy.line);
Squeeze_Alert.SetLineWeight(2);
Squeeze_Alert.SetDefaultColor(Color.YELLOW);
#####################################################
##                                                 ##
#####################################################
TopBandPlot.HideTitle();
TopBandPlot.HideBubble();
TopBandPlot.AssignValueColor(
    if bothBands_Down then Color.dark_red
    else if bothBands_Up then color.Dark_green
    else if topBand_StepUp then Color.green
    else if topBand_StepDown then Color.red
    else if Topband_Below_Zero then  Color.yellow
    else if warnphase then color.orange
    else if recphase then Color.magenta
    else if bullphase then Color.light_green
    else if bearphase then Color.light_red
    else GlobalColor("Gray")
);
#####################################################
##                                                 ##
#####################################################
BottomBandPlot.HideTitle();
BottomBandPlot.HideBubble();
BottomBandPlot.AssignValueColor(
    if bothBands_Down then Color.dark_red
    else if bothBands_Up then color.Dark_green
    else if bottomBand_StepUp then Color.green
    else if bottomBand_StepDown then Color.red
    else if Bottomband_Above_Zero then Color.yellow
    else if warnphase then color.orange
    else if recphase then Color.magenta
    else if bullphase then Color.light_green
    else if bearphase then Color.light_red
    else  GlobalColor("Gray")
);
#####################################################
##                                                 ##
#####################################################
MasterEMADLinePlot.SetPaintingStrategy(PaintingStrategy.histogram);
MasterEMADLinePlot.HideBubble();
MasterEMADLinePlot.SetLineWeight(1);
MasterEMADLinePlot.AssignValueColor(
     if alert4 then color.cyan
else if HLBuy then color.light_green
else if LHSell then color.light_red
else if Madebad_CrossUp then Color.dark_green
else if Madebad_CrossDown then color.dark_red
else if Madebad_Condition_lookback > 1 and Band_Crossed_First then Color.dark_gray
else color.dark_red
);
#####################################################
##                                                 ##
#####################################################
RegularBuy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
ExtremeBuy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
RegularSell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ExtremeSell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#####################################################
##                                                 ##
#####################################################
RegularBuy.SetLineWeight((1));
ExtremeBuy.SetLineWeight((1));
RegularSell.SetLineWeight((1));
ExtremeSell.SetLineWeight((1));
#####################################################
##                                                 ##
#####################################################
ExtremeSell.SetDefaultColor((Color.WHITE));
ExtremeBuy.SetDefaultColor((Color.WHITE));
RegularBuy.SetDefaultColor((Color.white));
RegularSell.SetDefaultColor((Color.white));
#####################################################
##                                                 ##
#####################################################
UpperEMADLinePlot.HideTitle();
UpperEMADLinePlot.HideBubble();
UpperEMADLinePlot.AssignValueColor(
    if (lowerEMADLinePlot > upperEMADLinePlot) and (lowerEMADLinePlot[1] > upperEMADLinePlot[1]) then createcolor(153, 0, 0)
    else if (lowerEMADLinePlot < upperEMADLinePlot) and (lowerEMADLinePlot[1] < upperEMADLinePlot[1]) then createcolor(0, 167, 0)
    else GlobalColor("Gray")
);
#####################################################
##                                                 ##
#####################################################
LowerEMADLinePlot.HideTitle();
LowerEMADLinePlot.HideBubble();
LowerEMADLinePlot.AssignValueColor(
    if (lowerEMADLinePlot > upperEMADLinePlot) and (lowerEMADLinePlot[1] > upperEMADLinePlot[1]) then createcolor(153, 0, 0)
    else if (lowerEMADLinePlot < upperEMADLinePlot) and (lowerEMADLinePlot[1] < upperEMADLinePlot[1]) then createcolor(0, 167, 0)
    else GlobalColor("Gray")
);
#####################################################
##                                                 ##
#####################################################
MasterEMADLinePlot_2.HideTitle();
MasterEMADLinePlot_2.HideBubble();
MasterEMADLinePlot_2.SetLineWeight(EMAD_EMA_lineweight);
MasterEMADLinePlot_2.AssignValueColor(
    if masterEMADLinePlot > masterEMADLinePlot[2] then createColor(0, 186, 0)
    else if masterEMADLinePlot < masterEMADLinePlot[2] then createColor(174, 0, 0)
    else GlobalColor("Gray")
);
#####################################################
##                                                 ##
#####################################################
ZeroLine.HideTitle();
ZeroLine.HideBubble();
ZeroLine.AssignValueColor(
    if (upperEMADLinePlot > zeroLine) then  Color.green
    else if (upperEMADLinePlot < zeroLine) then  Color.red
    else GlobalColor("Yellow")
);
#####################################################
##                                                 ##
#####################################################
TopBandPlot_offset.HideTitle();
TopBandPlot_offset.HideBubble();
TopBandPlot_offset.SetDefaultColor(Color.black
);
#####################################################
##                                                 ##
#####################################################
BottomBandPlot_offset.HideTitle();
BottomBandPlot_offset.HideBubble();
BottomBandPlot_offset.SetDefaultColor(Color.black
);
#####################################################
## CANDLE COLOR                                    ##
#####################################################
AssignPriceColor(if Color_Candles then
     if ALERT4 THEN COLOR.cyan
else if BottomBand_PushDown THEN COLOR.red
else if TopBand_PushUp THEN COLOR.green
else if Madebad_CrossCondition == 1 then color.dark_green
else if Madebad_CrossCondition == -1 then color.dark_red
else if Madebad_Condition_lookback > 1 and Band_Crossed_First and EMAD_below_Zero and EMAD_EMA_up then color.gray
else if Madebad_Condition_lookback > 1 and Band_Crossed_First and EMAD_above_Zero and EMAD_EMA_down then color.gray
else if Madebad_Condition_lookback > 1 and Band_Crossed_First and EMAD_below_Zero then color.light_red
else if Madebad_Condition_lookback > 1 and Band_Crossed_First and EMAD_above_Zero then color.light_green
else Color.GRAY
else Color.CURRENT);
#####################################################
## VERTICAL CLOUD                                  ##
#####################################################
AddCloud(if TopBand_StepUp and TopBand_PushUp and !TopBand_below_Zero then topBandPlot else double.nan, bottomBandPlot, color.dark_green, color.dark_green);
AddCloud(if BottomBand_Above_Zero and !TopBand_PushUp then topBandPlot else double.nan, bottomBandPlot, color.dark_gray, color.dark_gray);
AddCloud(if BottomBand_StepDown and BottomBand_PushDown and !BottomBand_Above_Zero then topBandPlot else double.nan, bottomBandPlot, color.dark_red, color.dark_red);
AddCloud(if TopBand_below_Zero and !BottomBand_PushDown then topBandPlot else double.nan, bottomBandPlot, color.dark_gray, color.dark_gray);
AddCloud(if TopBand_StepDown and !EMAD_Above_Zero then topBandPlot else double.nan, bottomBandPlot, color.dark_red, color.dark_red);
AddCloud(if BottomBand_StepUp and !EMAD_Below_Zero then topBandPlot else double.nan, bottomBandPlot, color.dark_green, color.dark_green);
#####################################################
## EMA CLOUD                                       ##
#####################################################
AddCloud(if Show_EMA_Cloud and (made_1 > masterEMADLinePlot) then made_1 else Double.NaN, masterEMADLinePlot, Color.dark_red, Color.gray);
AddCloud(if Show_EMA_Cloud and (masterEMADLinePlot >= made) then masterEMADLinePlot else Double.NaN, made,  Color.dark_green, Color.gray);
#####################################################
##                                                 ##
#####################################################


Code:
   ##########################################################################################
   ##########################################################################################
   ###                                                                                    ###
   ###       #############################           #########               #########    ###
   ###     #################################         #########               #########    ###    
   ###    ####################################       #########               #########    ###
   ###   ########                     #########      #########               #########    ###         
   ###   ########  C3_BIG_SPARK_MAX   #########      #########               #########    ###
   ###   ########       +TSV9         #########      #########               #########    ###       
   ###   ########   @Christopher84    #########      #########               #########    ###     
   ###   ########                     #########      #########               #########    ###        
   ###   ######################################      ##################################   ###      
   ###    ####################################       ##################################   ###   
   ###     #################################         ##################################   ###
   ###    ####################################       ##################################   ###     
   ###   ########                     #########                              #########    ###
   ###   ########     @Horserider     #########                              #########    ###
   ###   ########  HORSERIDER VOLUME  #########                              #########    ###            
   ###   ########  TRIPLE EXHAUSTION  #########                              #########    ###        
   ###   ########      @chence27      #########                              #########    ###             
   ###   ########                     #########                              #########    ###         
   ###    ####################################                               #########    ###       
   ###     ##################################                                #########    ###        
   ###      ###############################                                  #########    ###      
   ###                                                                                    ###
   ##########################################################################################
   ##########################################################################################

declare upper;

input Filter = {default EMAD_Filter, Phase_Filter, No_Filter};
input x = 5;
input Ehlers_Length = 34;
input Big4_Confirmation_Factor = 4; #hint Big4
input ATR_Period_TSV9 = 11;
input ATR_Factor_TSV9 = 2.2;
input Color_Candles = yes;
input Show_EMA_Cloud = yes;
input Show_HL_LH = no;
input Show_EMAD_Arrows = no;
input Show_Labels = yes;
input TradeDaytimeOnly = yes; #hint tradeDaytimeOnly: (IntraDay Only) Only perform trades during hours stated
input OpenTime = 0730; #hint OpenTime: Opening time of market hides arrows
input CloseTime = 1800; #hint CloseTime: Closing time of market hides arrows
input FirstTrade_Squeeze = {default long, short};
input TrailType = {default modified, unmodified};
input FirstTrade = {default long, short};
input MACD_AverageType = {SMA, default EMA};
input DMI_averageType = AverageType.WILDERS;
input AvgType = AverageType.HULL;
########################################################
## 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));
########################################################
##                                                    ##
########################################################
def bn = barnumber();
def Begin = SecondsFromTime(OpenTime);
def End = SecondsTillTime(CloseTime);
def isIntraDay = if GetAggregationPeriod() > 14400000 or GetAggregationPeriod() == 0 then 0 else 1;
def MarketOpen = if !tradeDaytimeOnly or !isIntraDay then 1 else if tradeDaytimeOnly and isIntraDay and Begin > 0 and End > 0 then 1 else 0;
def LongTrades = yes; #hint LongTrades: perform long tradesvb  balanceOfMarketPower AbandonedBaby b           bbbbbbbbg
def ShortTrades = yes; #hint ShortTrades: perform short trades
def useStops = no;     #hint useStops: use stop orders
def useAlerts = no;    #hint useAlerts: use alerts on signals
########################################################
##                                                    ##
########################################################
def Cloud_Lookback = 20;
def HL_LH_Lookback = 20;
def Previous_Steps_Down = 2; #hint filter for hiding arrows
def Previous_Steps_Up = 2; #hint filter for hiding arrows
def Length_MAD = 4;
def Band_Length = 100;
def Smooth_Length = 12;
def Smooth_Length_2 = 14;
def Fast_Length = 10;
def Slow_Length = 35;
def Made_Avg_Length = 3;
def ATR_Period_Squeeze = 5;
def ATR_Factor_Squeeze = 2.0;
def Length_3x = 1000;
def AverageType = AverageType.WILDERS;
def AverageType_Squeeze = AverageType.SIMPLE;
def EMAD_EMA_lineweight = 2;
def Step_lookback = 10;
def FastExpAvg = ExpAverage(close, fast_Length);
def SlowExpAvg = ExpAverage(close, slow_Length);
def EMAD_Fast = (close - fastExpAvg);
def EMAD_Slow = (close - slowExpAvg);
def EMAD_Avg = (EMAD_Fast + EMAD_Slow) / 2;
#####################################################
## UPPER EMA                                       ##
#####################################################
def Upper_EMAD_EMA = ExpAverage(EMAD_Avg, smooth_Length);
def EMAD_Open = (Upper_EMAD_EMA + Upper_EMAD_EMA[1]) / 2;
def EMAD_High = Max(Upper_EMAD_EMA, Upper_EMAD_EMA[1]);
def EMAD_Low = Min(Upper_EMAD_EMA, Upper_EMAD_EMA[1]);
def EMAD_Close = Upper_EMAD_EMA;
def Bottom = Min(EMAD_Close[1], EMAD_Low);
def TR = TrueRange(EMAD_High, EMAD_Close, EMAD_Low);
def PTR = tr / (bottom + tr / 2);
def APTR = MovingAverage(averageType, ptr, smooth_Length_2);
def UpperBand = EMAD_Close[1] + (APTR * EMAD_Open);
def LowerBand = EMAD_Close[1] - (APTR * EMAD_Open);
#####################################################
## LOWER EMA                                       ##
#####################################################
def Lower_EMAD_EMA = (upperBand + lowerBand) / 2;
#####################################################
## TOP BAND                                        ##
#####################################################
def TopBand = Highest(Lower_EMAD_EMA, band_Length);
#####################################################
## BOTTOM BAND                                     ##
#####################################################
def BottomBand = Lowest(Lower_EMAD_EMA, band_Length);
#####################################################
## ZERO LINE                                       ##
#####################################################
def ZeroLineData = if IsNaN(close) then Double.NaN else 0;
def EMAD_Above_Zero = Upper_EMAD_EMA > zeroLineData;
def EMAD_Below_Zero = Upper_EMAD_EMA < zeroLineData;
def EMAD_EMA_down = (Lower_EMAD_EMA > Upper_EMAD_EMA);
def EMAD_EMA_up = (Upper_EMAD_EMA >= Lower_EMAD_EMA);
#####################################################
## MASTER EMA                                      ##
#####################################################
def Master_EMAD_EMA = (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2;
def TopBand_StepDown = if topBand < topBand[1] then 1 else 0;
def TopBand_StepUp = if topBand > topBand[1] then 1 else 0;
def BottomBand_StepDown = if bottomBand < bottomBand[1] then 1 else 0;
def BottomBand_StepUp = if bottomBand > bottomBand[1] then 1 else 0;
def BothBands_Down = bottomBand_StepDown and TopBand_StepDown;
def BothBands_Up = bottomBand_StepUp and TopBand_StepUp;
def BottomBand_Above_Zero = (bottomBand > zeroLineData);
def TopBand_below_Zero = (topBand < zeroLineData);
def TopBand_PushUp =  Master_EMAD_EMA >= TopBand;
def BottomBand_PushDown = Master_EMAD_EMA <= bottomBand;
def MidBand = (upperBand + lowerBand) / 2;
def EMA_CrossUp = if (midBand[1] > Upper_EMAD_EMA[1]) and (midBand < Upper_EMAD_EMA) then 1 else 0;
def EMA_CrossDown = if (Upper_EMAD_EMA[1] > midBand[1]) and (Upper_EMAD_EMA < midBand) then 1 else 0;
def Value_At_CrossUp = if EMA_CrossUp then midBand else 0;
def Value_At_CrossDown = if EMA_CrossDown then midBand else 0;
def CrossUp_BottomBand = if (Value_At_CrossUp - bottomBand) == 0 then 1 else 0;
def CrossDown_TopBand = if (Value_At_CrossDown - topBand) == 0 then 1 else 0;
def CrossUp_Zeroline = if (Value_At_CrossUp) == 0 then 1 else 0;
def CrossDown_Zeroline = if (Value_At_CrossDown) == 0 then 1 else 0;
def CrossUp_BottomBand_bn = if CrossUp_Bottomband and !CrossUp_Bottomband[1] then bn else CrossUp_Bottomband_bn[1];
def CrossDown_TopBand_bn = if CrossDown_topband and !CrossDown_topband[1] then bn else CrossDown_TopBand_bn[1];
def CrossUp_BottomBand_Within = if CrossUp_Bottomband_bn < bn then bn - CrossUp_Bottomband_bn else CrossUp_BottomBand_Within[1];
def CrossDown_TopBand_Within = if CrossDown_TopBand_bn < bn then bn - CrossDown_TopBand_bn else CrossDown_TopBand_Within[1];
def Band_Crossed_First = CrossUp_Bottomband_bn > CrossDown_TopBand_bn;
#####################################################
## EMAD CLOUD CONDITIONS                           ##
#####################################################
def EMAD_Green_Cloud_1 = if TopBand_StepUp and TopBand_PushUp and !TopBand_below_Zero then 1 else 0;
def EMAD_Green_Cloud_2 = if BottomBand_StepUp and !EMAD_Below_Zero then 1 else 0;
def EMAD_Red_Cloud_1 = if BottomBand_StepDown and BottomBand_PushDown and !BottomBand_Above_Zero then 1 else 0;
def EMAD_Red_Cloud_2 = if TopBand_StepDown and !EMAD_Above_Zero then 1 else 0;
def EMAD_Gray_Cloud_1 = if BottomBand_Above_Zero and !TopBand_PushUp then 1 else 0;
def EMAD_Gray_Cloud_2 = if TopBand_below_Zero and !BottomBand_PushDown then 1 else 0;
#####################################################
## HIGHER LOW - LOWER HIGH                         ##
#####################################################
def HL = CrossUp_Bottomband == 0 and CrossDown_topband == 0 and Upper_EMAD_EMA crosses above Lower_EMAD_EMA;  #Normal HL LH condition
def LH = CrossDown_topband == 0 and CrossUp_Bottomband == 0 and Upper_EMAD_EMA crosses below Lower_EMAD_EMA; #Normal HL LH condition
def bounceUpOnce = CrossUp_Bottomband <> CrossUp_Bottomband[1]; #XUpBtm happened previous bar or on current bar (<> = is NOT equal to)
def bounceDnOnce = CrossDown_topband <> CrossDown_topband[1];
def trackCrUp = if bounceUpOnce then 0 else if HL then 1 else trackCrUp[1];
def trackCrDn = if bounceDnOnce then 0 else if LH then 1 else trackCrDn[1];
def HLalready = if trackCrUp and !trackCrUp[1] then 1 else 0;
def LHalready = if trackCrDn and !trackCrDn[1] then 1 else 0;
def HL2_EMAD = HLalready == 0 and Upper_EMAD_EMA crosses above Lower_EMAD_EMA;
def LH2_EMAD  = LHalready == 0 and Upper_EMAD_EMA crosses below Lower_EMAD_EMA;
def crossedUpOnce = HLalready <> HLalready[1];
def crossedDnOnce = LHalready <> LHalready[1];
def trackCrUp2 = if crossedUpOnce then 0 else if HL2_EMAD then 1 else trackCrUp2[1];
def trackCrDn2 = if crossedDnOnce then 0 else if LH2_EMAD then 1 else trackCrDn2[1];
#####################################################
## SQUEEZE                                         ##
#####################################################
def HiLo_Squeeze = Min(high - low, 1.5 * Average(high - low, ATR_Period_Squeeze));
def HRef_Squeeze = if low <= high[1]
    then high - close[1]
    else (high - close[1]) - 0.5 * (low - high[1]);
def LRef_Squeeze = if high >= low[1]
    then close[1] - low
    else (close[1] - low) - 0.5 * (low[1] - high);
def trueRange_Squeeze;
switch (trailType) {
case modified:
    trueRange_Squeeze = Max(HiLo_Squeeze, Max(HRef_Squeeze, LRef_Squeeze));
case unmodified:
    trueRange_Squeeze = TrueRange(high, close, low);
}
def loss_Squeeze = ATR_Factor_Squeeze * MovingAverage(averageType_Squeeze, trueRange_Squeeze, ATR_Period_Squeeze);
def state_Squeeze = {default init, long, short};
def trail_Squeeze;
switch (state_Squeeze[1]) {
case init:
    if (!IsNaN(loss_Squeeze)) {
        switch (firstTrade) {
        case long:
            state_Squeeze = state_Squeeze.long;
            trail_Squeeze =  close - loss_Squeeze;
        case short:
            state_Squeeze = state_Squeeze.short;
            trail_Squeeze = close + loss_Squeeze;
    }
    } else {
        state_Squeeze = state_Squeeze.init;
        trail_Squeeze = Double.NaN;
    }
case long:
    if (close > trail_Squeeze[1]) {
        state_Squeeze = state_Squeeze.long;
        trail_Squeeze = Max(trail_Squeeze[1], close - loss_Squeeze);
    } else {
        state_Squeeze = state_Squeeze.short;
        trail_Squeeze = close + loss_Squeeze;
    }
case short:
    if (close < trail_Squeeze[1]) {
        state_Squeeze = state_Squeeze.short;
        trail_Squeeze = Min(trail_Squeeze[1], close + loss_Squeeze);
    } else {
        state_Squeeze = state_Squeeze.long;
        trail_Squeeze =  close - loss_Squeeze;
    }
}
def TrailingStop_Squeeze = trail_Squeeze;
def H = Highest(TrailingStop_Squeeze, 12);
def L = Lowest(TrailingStop_Squeeze, 12);
def Bandwidth_Squeeze = (H - L);
def IntermSupport2 = Lowest(Bandwidth_Squeeze, Band_Length);
def Squeeze_Trigger = Bandwidth_Squeeze <= IntermSupport2;
def Squeeze_Level = if !Squeeze_Trigger[1] and Squeeze_Trigger then hl2 else if !Squeeze_Trigger then Double.NaN else Squeeze_Level[1];
def Squeeze_Alert_1 = if Squeeze_Level then (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2 else (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2 ;
#####################################################
## TS V9                                           ##
#####################################################
def HiLo_TSV9 = Min(high - low, 1.5 * Average(high - low, ATR_Period_TSV9));
def HRef_TSV9 = if low <= high[1]
    then high - close[1]
    else (high - close[1]) - 0.5 * (low - high[1]);
def LRef_TSV9 = if high >= low[1]
    then close[1] - low
    else (close[1] - low) - 0.5 * (low[1] - high);
def trueRange_TSV9;
switch (trailType) {
case modified:
    trueRange_TSV9 = Max(HiLo_TSV9, Max(HRef_TSV9, LRef_TSV9));
case unmodified:
    trueRange_TSV9 = TrueRange(high, close, low);
}
def loss_TSV9 = ATR_Factor_TSV9 * MovingAverage(averageType, trueRange_TSV9, ATR_Period_TSV9);
def state_TSV9 = {default init, long, short};
def trail_TSV9;
switch (state_TSV9[1]) {
case init:
    if (!IsNaN(loss_TSV9)) {
        switch (firstTrade) {
        case long:
            state_TSV9 = state_TSV9.long;
            trail_TSV9 =  close - loss_TSV9;
        case short:
            state_TSV9 = state_TSV9.short;
            trail_TSV9 = close + loss_TSV9;
    }
    } else {
        state_TSV9 = state_TSV9.init;
        trail_TSV9 = Double.NaN;
    }
case long:
    if (close > trail_TSV9[1]) {
        state_TSV9 = state_TSV9.long;
        trail_TSV9 = Max(trail_TSV9[1], close - loss_TSV9);
    } else {
        state_TSV9 = state_TSV9.short;
        trail_TSV9 = close + loss_TSV9;
    }
case short:
    if (close < trail_TSV9[1]) {
        state_TSV9 = state_TSV9.short;
        trail_TSV9 = Min(trail_TSV9[1], close + loss_TSV9);
    } else {
        state_TSV9 = state_TSV9.long;
        trail_TSV9 =  close - loss_TSV9;
    }
}
def Price = close;
def TrailingStop_TSV9 = trail_TSV9;
def LongEnter = (price crosses above TrailingStop_TSV9);
def LongExit = (price crosses below TrailingStop_TSV9);
def Upsignal =  (price crosses above TrailingStop_TSV9);
def Downsignal = (price crosses below TrailingStop_TSV9);
def PLBuyStop  = if !useStops then 0 else if  (0 > 0) then 1 else 0  ; # insert condition to stop in place of the 0<0
def PLSellStop = if !useStops then 0 else if (0 > 0) then 1 else 0  ; # insert condition to stop in place of the 0>0
def PLMktStop = if MarketOpen[-1] == 0 then 1 else 0; # If tradeDaytimeOnly is set, then stop at end of day
def PLBuySignal = if  MarketOpen and (upsignal) then 1 else 0 ; # insert condition to create long position in place of the 0>0
def PLSellSignal =  if MarketOpen and (downsignal) then 1 else 0; # insert condition to create short position in place 0>0
def CurrentPosition;  # holds whether flat = 0 long = 1 short = -1
if (BarNumber() == 1) or IsNaN(CurrentPosition[1]) {
    CurrentPosition = 0;
} else {
    if CurrentPosition[1] == 0 {            # FLAT
        if (PLBuySignal and LongTrades) {
            CurrentPosition = 1;
        } else if (PLSellSignal and ShortTrades) {
            CurrentPosition = -1;
        } else {
            CurrentPosition = CurrentPosition[1];
        }
    } else if CurrentPosition[1] == 1 {      # LONG
        if (PLSellSignal and ShortTrades) {
            CurrentPosition = -1;
        } else if ((PLBuyStop and useStops) or PLMktStop or (PLSellSignal and ShortTrades == 0)) {
            CurrentPosition = 0;
        } else {
            CurrentPosition = CurrentPosition[1];
        }
    } else if CurrentPosition[1] == -1 {     # SHORT
        if (PLBuySignal and LongTrades) {
            CurrentPosition = 1;
        } else if ((PLSellStop and useStops) or PLMktStop or (PLBuySignal and LongTrades == 0)) {
            CurrentPosition = 0;
        } else {
            CurrentPosition = CurrentPosition[1];
        }
    } else {
        CurrentPosition = CurrentPosition[1];
    }
}
def isLong  = if CurrentPosition == 1 then 1 else 0;
def isShort = if CurrentPosition == -1 then 1 else 0;
def isFlat  = if CurrentPosition == 0 then 1 else 0;
def BuySig = if marketopen and (((isShort[1] and LongTrades) or (isFlat[1] and LongTrades)) and PLBuySignal) then 1 else 0;
def SellSig = if marketopen and (((isLong[1] and ShortTrades) or (isFlat[1] and ShortTrades)) and PLSellSignal) then 1 else 0;
def BuySig_TSV9_bn = if upsignal[1] then BN else BuySig_TSV9_bn[1];   
def SellSig_TSV9_bn = if downsignal[1] then BN else SellSig_TSV9_bn[1];
def TS_Last = if BuySig_TSV9_bn > SellSig_TSV9_bn then 1 else 0;
def cond13_TS_Buy = Buysig;
def cond13_TS_Sell = Sellsig;
#####################################################
## TRIPLE EXHAUSTION                               ##
#####################################################
def OB_3x = 80;
def OS_3x = 20;
def KPeriod_3x = 10;
def DPeriod_3x = 10;  
def SlowK_3x = reference StochasticFull(OB_3x,  OS_3x,  KPeriod_3x,  DPeriod_3x,  EMAD_High, EMAD_Low, EMAD_Close,  3, if (averageType == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;
def MACD_3x = reference MACD()."Value";
def Price_Avg_3x = Average(MACD_3x, length_3x);
def MACD_stdev_3x =  (MACD_3x - Price_Avg_3x) / StDev(MACD_3x, length_3x);
def DPlus_3x = reference DMI()."DI+";
def DMinus_3x = reference DMI()."DI-";
def SellerRegular = SlowK_3x < 20 and MACD_stdev_3x < -1 and dPlus_3x < 15;
def SellerExtreme = SlowK_3x < 20 and MACD_stdev_3x < -2 and dPlus_3x < 15;
def BuyerRegular = SlowK_3x > 80 and MACD_stdev_3x > 1 and dMinus_3x < 15;
def BuyerExtreme = SlowK_3x > 80 and MACD_stdev_3x > 2 and dMinus_3x < 15;    
def Extreme_Buy = if sellerExtreme[1] and !sellerExtreme then 1 else 0;
def Extreme_Sell = if buyerExtreme[1] and !buyerExtreme then 1 else 0;
def Regular_Sell = if buyerRegular[1] and !buyerRegular then 1 else 0;
def Regular_Buy = if sellerRegular[1] and !sellerRegular then 1 else 0;
#####################################################
## C3 MAX YELLOW CANDLE                            ##
#####################################################
def Displace = 0;
def FactorK2 = 3.25;
def LengthK2 = 20;
def TrueRangeAverageType = AverageType.SIMPLE;
def ATR_length = 15;
def SMA_lengthS = 6;
def Multiplier_factor = 1.25;
def ValS = Average(price, SMA_lengthS);
def Average_true_range = Average(TrueRange(high, close, low), length = ATR_length);
def Upper_BandS = valS[-displace] + multiplier_factor * average_true_range[-displace];
def Middle_BandS = valS[-displace];
def Lower_BandS = valS[-displace] - multiplier_factor * average_true_range[-displace];
def shiftK2 = factorK2 * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), lengthK2);
def averageK2 = MovingAverage(averageType, price, lengthK2);
def AvgK2 = averageK2[-displace];
def Upper_BandK2 = averageK2[-displace] + shiftK2[-displace];
def Lower_BandK2 = averageK2[-displace] - shiftK2[-displace];
def Condition_BandRevDn = (Upper_BandS > Upper_BandK2);
def Condition_BandRevUp = (Lower_BandS < Lower_BandK2);
def FastLength = 12;
def SlowLength = 26;
def MACDLength = 9;
def FastEMA = ExpAverage(price, fastLength);
def SlowEMA = ExpAverage(price, slowLength);
def Value;
def Avg;
switch (MACD_AverageType) {
case SMA:
    Value = Average(price, fastLength) - Average(price, slowLength);
    Avg = Average(Value, MACDLength);
case EMA:
    Value = fastEMA - slowEMA;
    Avg = ExpAverage(Value, MACDLength);
}
def Diff = Value - Avg;
def MACDLevel = 0.0;
def Level = MACDLevel;
def condition1_Yellow_Candle = Value[1] <= Value;
def condition1D_Yellow_Candle = Value[1] > Value;
#####################################################
## RSI                                             ##
#####################################################
def RSI_length = 14;
def RSI_AverageType = AverageType.WILDERS;
def RSI_OB = 70;
def RSI_OS = 30;
def NetChgAvg = MovingAverage(RSI_AverageType, price - price[1], RSI_length);
def TotChgAvg = MovingAverage(RSI_AverageType, AbsValue(price - price[1]), RSI_length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);
def condition2_RSI = (RSI[3] < RSI) is true or (RSI >= 80) is true;
def condition2D_RSI = (RSI[3] > RSI) is true or (RSI < 20) is true;
def conditionOB1_RSI = RSI > RSI_OB;
def conditionOS1_RSI = RSI < RSI_OS;
#####################################################
## MONEY FLOW INDEX                                ##
#####################################################
def MFI_Length = 14;
def MFIover_Sold = 20;
def MFIover_Bought = 80;
def movingAvgLength = 1;
def MoneyFlowIndex = Average(MoneyFlow(high, close, low, volume, MFI_Length), movingAvgLength);
def MFIOverBought = MFIover_Bought;
def MFIOverSold = MFIover_Sold;
def condition3_MFI = (MoneyFlowIndex[2] < MoneyFlowIndex) is true or (MoneyFlowIndex > 85) is true;
def condition3D_MFI = (MoneyFlowIndex[2] > MoneyFlowIndex) is true or (MoneyFlowIndex < 20) is true;
def conditionOB2_MFI = MoneyFlowIndex > MFIover_Bought;
def conditionOS2_MFI = MoneyFlowIndex < MFIover_Sold;
#####################################################
## FORECAST                                        ##
#####################################################
def na = Double.NaN;
def MidLine = 50;
def Momentum = MarketForecast().Momentum;
def NearT =  MarketForecast().NearTerm;
def Intermed = MarketForecast().Intermediate;
def FOB = 80;
def FOS = 20;
def upperLine = 110;
def condition4_Forcast = (Intermed[1] <= Intermed) or (NearT >= MidLine);
def condition4D_Forcast = (Intermed[1] > Intermed) or (NearT < MidLine);
def conditionOB3_Forcast = Intermed > FOB;
def conditionOS3_Forcast = Intermed < FOS;
def conditionOB4_Forcast = NearT > FOB;
def conditionOS4_Forcast = NearT < FOS;
#####################################################
## CHANGE IN PRICE                                 ##
#####################################################
def lengthCIP = 5;
def CIP = (price - price[1]);
def AvgCIP = ExpAverage(CIP[-displace], lengthCIP);
def CIP_UP = AvgCIP > AvgCIP[1];
def CIP_DOWN = AvgCIP < AvgCIP[1];
def condition5_CIP = CIP_UP;
def condition5D_CIP = CIP_DOWN;
#####################################################
## EMA 1                                           ##
#####################################################
def EMA_length = 8;
def AvgExp = ExpAverage(price[-displace], EMA_length);
def condition6_EMA_1 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D_EMA_1 = (price < AvgExp) and (AvgExp[2] > AvgExp);
#####################################################
## EMA 2                                           ##
#####################################################
def EMA_2length = 20;
def displace2 = 0;
def AvgExp2 = ExpAverage(price[-displace2], EMA_2length);
def condition7_EMA_2 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp);
def condition7D_EMA_2 = (price < AvgExp2) and (AvgExp2[2] > AvgExp);
#####################################################
## DMI OSCILLATOR                                  ##
#####################################################
def DMI_length = 5; #Typically set to 10
def diPlus = DMI(DMI_length, DMI_averageType)."DI+";
def diMinus = DMI(DMI_length, DMI_averageType)."DI-";
def Osc = diPlus - diMinus;
def Hist = Osc;
def ZeroLine = 0;
def condition8_DMI = Osc >= ZeroLine;
def condition8D_DMI = Osc < ZeroLine;
#####################################################
## TREND PERIODS                                   ##
#####################################################
def TP_fastLength = 3;#Typically 7
def TP_slowLength = 4;#Typically 15
def Periods = Sign(ExpAverage(close, TP_fastLength) - ExpAverage(close, TP_slowLength));
def condition9_Trend = Periods > 0;
def condition9D_Trend = Periods < 0;
#####################################################
## POLARIZED FRACTAL EFFICIENCY                    ##
#####################################################
def PFE_length = 5;#Typically 10
def smoothingLength = 2.5;#Typically 5
def PFE_diff = close - close[PFE_length - 1];
def val = 100 * Sqrt(Sqr(PFE_diff) + Sqr(PFE_length)) / Sum(Sqrt(1 + Sqr(close - close[1])), PFE_length - 1);
def PFE = ExpAverage(if PFE_diff > 0 then val else -val, smoothingLength);
def UpperLevel = 50;
def LowerLevel = -50;
def condition10_PFE = PFE > 0;
def condition10D_PFE = PFE < 0;
def conditionOB5_PFE = PFE > UpperLevel;
def conditionOS5_PFE = PFE < LowerLevel;
#####################################################
## BOLLINGER BANDS                                 ##
#####################################################
input AverageType_BB = AverageType.SIMPLE;
def BBPB_length = 20; #Typically 20
def Num_Dev_Dn = -2.0;
def Num_Dev_up = 2.0;
def BBPB_OB = 100;
def BBPB_OS = 0;
def upperBand_BB = BollingerBands(price, displace, BBPB_length, Num_Dev_Dn, Num_Dev_up, AverageType_BB).UpperBand;
def lowerBand_BB = BollingerBands(price, displace, BBPB_length, Num_Dev_Dn, Num_Dev_up, AverageType_BB).LowerBand;
def PercentB = (price - lowerBand_BB) / (upperBand_BB - lowerBand_BB) * 100;
def HalfLine = 50;
def UnitLine = 100;
def condition11_BB = PercentB > HalfLine;
def condition11D_BB = PercentB < HalfLine;
def conditionOB6_BB = PercentB > BBPB_OB;
def conditionOS6_BB = PercentB < BBPB_OS;
def condition12_BB = (Upper_BandS[1] <= Upper_BandS) and (Lower_BandS[1] <= Lower_BandS);
def condition12D_BB = (Upper_BandS[1] > Upper_BandS) and (Lower_BandS[1] > Lower_BandS);
#####################################################
## KLINGER HISTOGRAM                               ##
#####################################################
def Klinger_Length = 13;
def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
def KVOH = KVOsc - Average(KVOsc, Klinger_Length);
def condition13_KH = (KVOH > 0);
def condition13D_KH = (KVOH < 0);
#####################################################
## PROJECTION OSCILLATOR                           ##
#####################################################
def ProjectionOsc_length = 30;#Typically 10
def MaxBound = HighestWeighted(high, ProjectionOsc_length, LinearRegressionSlope(price = high, length = ProjectionOsc_length));
def MinBound = LowestWeighted(low, ProjectionOsc_length, LinearRegressionSlope(price = low, length = ProjectionOsc_length));
def ProjectionOsc_diff = MaxBound - MinBound;
def PROSC = if ProjectionOsc_diff != 0 then 100 * (close - MinBound) / ProjectionOsc_diff else 0;
def PROSC_OB = 80;
def PROSC_OS = 20;
def condition14_PO = PROSC > 50;
def condition14D_PO = PROSC < 50;
def conditionOB7_PO = PROSC > PROSC_OB;
def conditionOS7_PO = PROSC < PROSC_OS;
#####################################################
## AK TREND                                        ##
#####################################################
def aktrend_input1 = 3;
def aktrend_input2 = 8;
def aktrend_price = close;
def aktrend_fastmaa = MovAvgExponential(aktrend_price, aktrend_input1);
def aktrend_fastmab = MovAvgExponential(aktrend_price, aktrend_input2);
def aktrend_bspread = (aktrend_fastmaa - aktrend_fastmab) * 1.001;
def cond1_UP_AK = if aktrend_bspread > 0 then 1 else 0;
def cond1_DN_AK = if aktrend_bspread <= 0 then -1 else 0;
#####################################################
## ZSCORE                                          ##
#####################################################
def zscore_price = close;
def zscore_length = 20;
def zscore_ZavgLength = 20;
def zscore_oneSD = StDev(zscore_price, zscore_length);
def zscore_avgClose = SimpleMovingAvg(zscore_price, zscore_length);
def zscore_ofoneSD = zscore_oneSD * zscore_price[1];
def zscore_Zscorevalue = ((zscore_price - zscore_avgClose) / zscore_oneSD);
def zscore_avgZv = Average(zscore_Zscorevalue, 20);
def zscore_Zscore = ((zscore_price - zscore_avgClose) / zscore_oneSD);
def zscore_avgZscore = Average(zscore_Zscorevalue, zscore_ZavgLength);
def cond2_UP_ZSCORE = if zscore_Zscore > 0 then 1 else 0;
def cond2_DN_ZSCORE = if zscore_Zscore <= 0 then -1 else 0;
#####################################################
## EMAD EHLERS                                     ##
#####################################################
def Price_Ehlers_EMAD = (EMAD_High + EMAD_Low) / 2;
def Coeff = Ehlers_length * Price_Ehlers_EMAD * Price_Ehlers_EMAD - 2 * Price_Ehlers_EMAD * sum(Price_Ehlers_EMAD, Ehlers_length)[1] + sum(Price_Ehlers_EMAD * Price_Ehlers_EMAD, Ehlers_length)[1];
def Ehlers_EMAD =  sum(coeff * Price_Ehlers_EMAD, Ehlers_length) / sum(coeff, Ehlers_length);
#####################################################
## EHLERS                                          ##
#####################################################
def Price_Ehlers = (high + low) / 2;
def Ehlers_Coeff = ehlers_length * Price_Ehlers * Price_Ehlers - 2 * Price_Ehlers * Sum(Price_Ehlers, ehlers_length)[1] + Sum(Price_Ehlers * Price_Ehlers, ehlers_length)[1];
def Ehlers = Sum(ehlers_coeff * Price_Ehlers, ehlers_length) / Sum(ehlers_coeff, ehlers_length);
def Ehlers_Avg_Length = 9;
def Ehlers_Avg = average(Ehlers, Ehlers_Avg_Length);
def cond3_UP_Ehlers = if close > Ehlers_Avg then 1 else 0;
def cond3_DN_Ehlers = if close <= Ehlers_Avg then -1 else 0;
#####################################################
## ANCHORED MOMENTUM                               ##
#####################################################
def amom_src = close;
def amom_MomentumPeriod = 10;
def amom_SignalPeriod = 8;
def amom_SmoothMomentum = no;
def amom_SmoothingPeriod = 7;
def amom_p = 2 * amom_MomentumPeriod + 1;
def amom_t_amom = if amom_SmoothMomentum == yes then ExpAverage(amom_src, amom_SmoothingPeriod) else amom_src;
def amom_amom = 100 * ( (amom_t_amom / ( Average(amom_src, amom_p)) - 1));
def amom_amoms = Average(amom_amom, amom_SignalPeriod);
def cond4_UP_AM = if amom_amom > 0 then 1 else 0;
def cond4_DN_AM = if amom_amom <= 0 then -1 else 0;
#####################################################
## TMO                                             ##
#####################################################
def tmo_length = 30; #def 14
def tmo_calcLength = 6; #def 5
def tmo_smoothLength = 6; #def 3
def tmo_data = fold i = 0 to tmo_length with s do s + (if close > GetValue(open, i) then 1 else if close < GetValue(open, i) then - 1 else 0);
def tmo_EMA5 = ExpAverage(tmo_data, tmo_calcLength);
def tmo_Main = ExpAverage(tmo_EMA5, tmo_smoothLength);
def tmo_Signal = ExpAverage(tmo_Main, tmo_smoothLength);
def tmo_color = if tmo_Main > tmo_Signal then 1 else -1;
def cond5_UP_TMO = if tmo_Main <= 0 then 1 else 0;
def cond5_DN_TMO = if tmo_Main >= 0 then -1 else 0;
#####################################################
## MADE AVERAGE (EMAD)                             ##
#####################################################
def dataA = Master_EMAD_EMA;
def dataA1 =
    fold aD = 0
    to Length_MAD
    with k = 0   
    do k + dataA[aD];
def MAEMAD = dataA1 / LengtH_MAD;
def MADehlers = (MAEMAD - Ehlers_EMAD)/2;
def Madebad = madehlers + Ehlers_EMAD;
def Madebad_Avg = average(madebad, Made_Avg_Length);
def Madebad_CrossUp = if (madebad < Master_EMAD_EMA) and (madebad > madebad[1]) and (EMAD_Above_Zero > 0) then 1 else 0;
def Madebad_CrossDown = if (madebad > Master_EMAD_EMA) and (madebad < madebad[1]) and (EMAD_Below_Zero > 0) then 1 else 0;
def Madebad_CrossCondition = if (madebad < Master_EMAD_EMA) and (madebad > madebad[1]) and (EMAD_Above_Zero > 0) then 1
            else if (madebad > Master_EMAD_EMA) and (madebad < madebad[1]) and (EMAD_Below_Zero > 0) then -1
            else 0;
def Madebad_Condition = if (!Madebad_CrossCondition[1] or !Madebad_CrossCondition[2]) and (Madebad_CrossCondition == 0) then 1 else 0;
#####################################################
## ALERT4                                          ##
#####################################################
def PD = 22;
def BBL = 20;
def MULT = 2.0;
def LB = 50;
def PH = 0.85;
def LTLB = 40;
def MTLB = 14;
def STR = 3;
def WVF = ((Highest(close, pd) - low) / (Highest(close, pd))) * 100;
def StDev = mult * StDev(wvf, bbl);
def MidLine1 = SimpleMovingAvg(wvf, bbl);
def UpperBand1_Alert4 = midLine1 + StDev;
def Range_High = (Highest(wvf, lb)) * ph;
def UpRange = low > low[1] and close > high[1];
def UpRange_Aggr = close > close[1] and close > open[1];
def Filtered = ((wvf[1] >= UpperBand1_Alert4[1] or wvf[1] >= Range_High[1]) and (wvf < UpperBand1_Alert4 and wvf < Range_High));
def Filtered_Aggr = (wvf[1] >= UpperBand1_Alert4[1] or wvf[1] >= Range_High[1]);
def Alert4 = upRange_Aggr and close > close[str] and (close < close[ltLB] or close < close[mtLB]) and filtered_Aggr;
def Alert4close = if alert4 then close else 0;
def Made_Down = (madebad > Master_EMAD_EMA);
def Made_Up = (madebad < Master_EMAD_EMA);
def Made_Bottom_MA = if !Made_Down and (Madebad_Avg <= zerolinedata) then Madebad_Avg else double.nan;
def Made_Top_MA = if !Made_Up and (Madebad_Avg >= zerolinedata) then Madebad_Avg else double.nan;
def MadeAvg_Master_Cross_Up = if Madebad_Avg crosses below Master_EMAD_EMA then 1 else 0;
def MadeAvg_Master_Cross_Down = if Madebad_Avg crosses above Master_EMAD_EMA then 1 else 0;
def Cross_Up_MadeAvg_Master = if MadeAvg_Master_Cross_Up and !MadeAvg_Master_Cross_Up[1] then 1 else 0;
def Cross_Down_MadeAvg_Master = if MadeAvg_Master_Cross_Down and !MadeAvg_Master_Cross_Down[1] then 1 else 0;
def MadeAvg_Master_Up_Condition = if Cross_Up_MadeAvg_Master and Band_Crossed_First and EMAD_EMA_Up[1] then 1 else 0;
def MadeAvg_Master_Down_Condition = if Cross_Down_MadeAvg_Master and !Band_Crossed_First and EMAD_EMA_down[1] then 1 else 0;
def EMA_Push_StepUp = if topBand_StepUp and EMAD_EMA_Up and (((Upper_EMAD_EMA) - topband) == 0) then 1 else 0;;
def EMA_Push_StepDown = if bottomBand_StepDown and EMAD_EMA_down and (((Lower_EMAD_EMA) - bottomband) == 0) then 1 else 0;
def First_HL_1 = if Marketopen and Show_HL_LH and trackCrUp and !trackCrUp[1] then low else double.nan;
def First_LH_1 = if Marketopen and Show_HL_LH and trackCrDn and !trackCrDn[1] then high else double.nan;
def Second_HL_1 = if Marketopen and Show_HL_LH and trackCrUp2 and !trackCrUp2[1] and !CrossUp_Bottomband then low else double.nan;
def Second_LH_1 = if Marketopen and Show_HL_LH and trackCrDn2 and !trackCrDn2[1] and !CrossDown_Topband then high else double.nan;
#####################################################
## FOLD CONDITIONS (EMAD)                          ##
#####################################################
def TopBandStepDown_lookback = fold index1 = 1 to Step_lookback with x1 do x1 + topBand_StepDown[index1];
def TopBandStepUp_lookback = fold index2 = 1 to Step_lookback with x2 do x2 + topBand_StepUp[index2];
def BottomBandStepDown_lookback = fold index3 = 1 to Step_lookback with x3 do x3 + bottomBand_StepDown[index3];
def BottomBandStepUp_lookback = fold index4 = 1 to Step_lookback with x4 do x4 + bottomBand_StepUp[index4];
def Madebad_Condition_lookback = fold index5 = 1 to Cloud_lookback with x5 do x5 + Madebad_Condition[index5];
def HL_lookback = fold index6 = 1 to HL_LH_Lookback with x6 do x6 + First_HL_1[index6];
def LH_lookback = fold index7 = 1 to HL_LH_Lookback with x7 do x7 + First_LH_1[index7];
def HL2_lookback = fold index8 = 1 to HL_LH_Lookback with x8 do x8 + Second_HL_1[index8];
def LH2_lookback = fold index9 = 1 to HL_LH_Lookback with x9 do x9 + Second_LH_1[index9];
#####################################################
## HIDE ARROW CONDITIONS                           ##
#####################################################
def Hide_Arrow_TopBand_StepDown = topBandStepDown_lookback > Previous_Steps_Down;
def Hide_Arrow_TopBand_StepUp = topBandStepup_lookback > Previous_Steps_Up;
def Hide_Arrow_BottomBand_StepDown = bottomBandStepDown_lookback > Previous_Steps_Down;
def Hide_Arrow_BottomBand_StepUp = bottomBandStepup_lookback > Previous_Steps_Up;
def UP1 = if marketopen and Show_EMAD_Arrows and (!Hide_Arrow_TopBand_StepDown or !Hide_Arrow_BottomBand_StepDown) and !EMA_Push_StepDown and MadeAvg_Master_Up_Condition then 1 else 0;
def DOWN1 = if marketopen and Show_EMAD_Arrows and (!Hide_Arrow_TopBand_StepUp or !Hide_Arrow_BottomBand_StepUp) and !EMA_Push_StepUp and MadeAvg_Master_Down_Condition then 1 else 0;
def UP1_1 = if UP1 then lowest(low,2) else double.nan;
def DOWN1_1 = if DOWN1 then highest(high,2) else double.nan;
#####################################################
## MARKET PHASES                                   ##
#####################################################
def Fastavg_Phases = 50;
def Slowavg_Phases = 200;
def Fastsma_Phases = Average( close, Fastavg_Phases);
def Slowsma_Phases = Average(close, Slowavg_Phases);
def Bullphase = fastsma_Phases > slowsma_Phases && close > fastsma_Phases && close > slowsma_Phases; # Accumulation Phase : close > 50 SMA, close > 200 SMA, 50 SMA < 200 SMA
def Accphase = fastsma_Phases < slowsma_Phases && close > fastsma_Phases && close > slowsma_Phases;# Recovery Phase : close > 50 SMA, close < 200 SMA, 50 SMA < 200 SMA
def Recphase = fastsma_Phases < slowsma_Phases && close < slowsma_Phases && close > fastsma_Phases;# Bearish Phase : close < 50 SMA, close < 200 SMA, 50 SMA < 200 SMA
def Bearphase = fastsma_Phases < slowsma_Phases && close < fastsma_Phases && close < slowsma_Phases;# Distribution Phase : close < 50 SMA, close < 200 SMA, 50 SMA > 200 SMA
def Distphase = fastsma_Phases > slowsma_Phases && close < fastsma_Phases && close < slowsma_Phases;# Warning Phase : close < 50 SMA, close > 200 SMA, 50 SMA > 200 SMA
def Warnphase = fastsma_Phases> slowsma_Phases && close > slowsma_Phases && close < fastsma_Phases;
#####################################################
## BIG 4 DIRECTION                                 ##
#####################################################
def Strategy_FilterWithTMO = no;
def Strategy_FilterWithTMO_arrows = yes;
def Strategy_HoldTrend = no;
def Strategy_ColoredCandlesOn = yes;
def coloredCandlesOn = yes;
def cond_UP = cond1_UP_AK + cond2_UP_ZSCORE + cond3_UP_Ehlers + cond4_UP_AM;
def cond_DN = cond1_DN_AK + cond2_DN_ZSCORE + cond3_DN_Ehlers + cond4_DN_AM;
def direction = if cond_UP >= Big4_Confirmation_Factor and (!Strategy_FilterWithTMO or cond5_UP_TMO) then 1
else if cond_DN <= - Big4_Confirmation_Factor and (!Strategy_FilterWithTMO or cond5_DN_TMO) then -1
    else if !Strategy_HoldTrend and direction[1] == 1 and cond_UP < Big4_Confirmation_Factor and cond_DN > - Big4_Confirmation_Factor then 0
        else if !Strategy_HoldTrend and direction[1] == -1 and cond_DN > -Big4_Confirmation_Factor and cond_UP < Big4_Confirmation_Factor then 0
            else direction[1];
def direction2 = if cond_UP >= Big4_Confirmation_Factor and (!Strategy_FilterWithTMO_arrows or cond5_UP_TMO) then 1
    else if cond_DN <= - Big4_Confirmation_Factor and (!Strategy_FilterWithTMO_arrows or cond5_DN_TMO) then -1
        else if !Strategy_HoldTrend and direction2[1] == 1 and cond_UP < Big4_Confirmation_Factor and cond_DN > - Big4_Confirmation_Factor then 0
            else if !Strategy_HoldTrend and direction2[1] == -1 and cond_DN > - Big4_Confirmation_Factor and cond_UP < Big4_Confirmation_Factor then 0
                else direction2[1];
def direction_Equals_zero = direction == 0;
#####################################################
## TREND CONFIRMATION CALCULATOR                   ##
#####################################################
def Confirmation_Factor = 7;
def Agreement_LevelOB = 12;
def Agreement_LevelOS = 2;
def HideBoxLines = no;
def HideCloud = no;
def HideLabels = no;
def factorK = 2.0;
def lengthK = 20;
def shift = factorK * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), lengthK);
def averageK = MovingAverage(averageType, price, lengthK);
def AvgK = averageK[-displace];
def Upper_BandK = averageK[-displace] + shift[-displace];
def Lower_BandK = averageK[-displace] - shift[-displace];
def conditionK1UP = price >= Upper_BandK;
def conditionK2UP = (Upper_BandK[1] < Upper_BandK) and (Lower_BandK[1] < Lower_BandK);
def conditionK3DN = (Upper_BandK[1] > Upper_BandK) and (Lower_BandK[1] > Lower_BandK);
def conditionK4DN = price < Lower_BandK;
#######################################################
## AGREEMENT LEVEL                                   ##
#######################################################
def Agreement_Level = condition1_Yellow_Candle + condition2_RSI + condition3_MFI + condition4_Forcast
                    + condition5_CIP + condition6_EMA_1 + condition7_EMA_2 + condition8_DMI + condition9_Trend
                    + condition10_PFE + condition11_BB + condition12_BB + condition13_KH + condition14_PO
                    + conditionK1UP + conditionK2UP;
def Agreement_LevelD = (condition1D_Yellow_Candle + condition2D_RSI + condition3D_MFI + condition4D_Forcast + condition5D_CIP
                    + condition6D_EMA_1 + condition7D_EMA_2 + condition8D_DMI + condition9D_Trend + condition10D_PFE
                    + condition11D_BB + condition12D_BB + condition13D_KH + condition14D_PO + conditionK3DN
                    + conditionK4DN);
#######################################################
## CONSENSUS LEVEL                                   ##
#######################################################
def Consensus_Level = Agreement_Level - Agreement_LevelD;
def Consensus_UP = 6;
def Consensus_DOWN = -6;
def UP_CL = Consensus_Level >= Consensus_UP;
def DOWN_CL = Consensus_Level < Consensus_DOWN;
def priceColor = if UP_CL then 1 else if DOWN_CL then -1 else priceColor[1];
def Consensus_Level_OB = 14;
def Consensus_Level_OS = -12;
def OB_Level = conditionOB1_RSI + conditionOB2_MFI + conditionOB3_Forcast + conditionOB4_Forcast + conditionOB5_PFE + conditionOB6_BB + conditionOB7_PO;
def OS_Level = conditionOS1_RSI + conditionOS2_MFI + conditionOS3_Forcast + conditionOS4_Forcast + conditionOS5_PFE + conditionOS6_BB + conditionOS7_PO;
def Consensus_Line = OB_Level - OS_Level;
def MomentumUP = Consensus_Level[1] < Consensus_Level;
def MomentumDOWN = Consensus_Level[1] > Consensus_Level;
def conditionOB_CL = (Consensus_Level >= 12) and (Consensus_Line >= 4);
def conditionOS_CL = (Consensus_Level <= -12) and (Consensus_Line <= -3);
def Super_OB = 4;
def Super_OS = -4;
def DOWN_OB = (Agreement_Level > Agreement_LevelOB) and (Consensus_Line > Super_OB) and (Consensus_Level > Consensus_Level_OB);
def UP_OS = (Agreement_Level < Agreement_LevelOS) and (Consensus_Line < Super_OS) and (Consensus_Level < Consensus_Level_OS);
def YHOB = if ((open > Upper_BandS) and (condition_BandRevDn)) then high else Double.NaN;
def YHOS = if ((open < Lower_BandS) and (condition_BandRevUp)) then high else Double.NaN;
def YLOB = if ((open > Upper_BandS) and (condition_BandRevDn)) then low else Double.NaN;
def YLOS = if ((open < Lower_BandS) and (condition_BandRevUp)) then low else Double.NaN;
def YCOB = if !IsNaN(YHOB) then hl2 else Double.NaN;
def YHextOB = if IsNaN(YCOB) then YHextOB[1] else YCOB;
def YHextlineOB = YHextOB;
def YCOS = if !IsNaN(YHOS) then hl2 else Double.NaN;
def YHextOS = if IsNaN(YCOS) then YHextOS[1] else YCOS;
def YHextlineOS = YHextOS;
def BarsUsedForRange = 2;
def BarsRequiredToRemainInRange = 2;
def trig = 20;# for Blast-off candle color
def YC = coloredCandlesOn and priceColor == 1 and open > Upper_BandS and condition_BandRevDn;
def HH = Highest(high[1], BarsUsedForRange);
def LL = Lowest(low[1], BarsUsedForRange);
def maxH = Highest(HH, BarsRequiredToRemainInRange);
def maxL = Lowest(LL, BarsRequiredToRemainInRange);
def HHn = if maxH == maxH[1] or maxL == maxL then maxH else HHn[1];
def LLn = if maxH == maxH[1] or maxL == maxL then maxL else LLn[1];
def Bh = if high <= HHn and HHn == HHn[1] then HHn else Double.NaN;
def Bl = if low >= LLn and LLn == LLn[1] then LLn else Double.NaN;
def CountH = if IsNaN(Bh) or IsNaN(Bl) then 2 else CountH[1] + 1;
def CountL = if IsNaN(Bh) or IsNaN(Bl) then 2 else CountL[1] + 1;
def ExpH = if BarNumber() == 1 then Double.NaN else
           if CountH[-BarsRequiredToRemainInRange] >= BarsRequiredToRemainInRange then HHn[-BarsRequiredToRemainInRange] else
           if high <= ExpH[1] then ExpH[1] else Double.NaN;
def ExpL = if BarNumber() == 1 then Double.NaN else
           if CountL[-BarsRequiredToRemainInRange] >= BarsRequiredToRemainInRange then LLn[-BarsRequiredToRemainInRange] else
           if low >= ExpL[1] then ExpL[1] else Double.NaN;
def BoxHigh = if ((DOWN_OB) or (Upper_BandS crosses above Upper_BandK2) or (condition_BandRevDn) and (high > high[1]) and ((price > Upper_BandK2) or (price > Upper_BandS))) then Highest(ExpH) else Double.NaN;
def BoxLow = if (DOWN_OB) or ((Upper_BandS crosses above Upper_BandK2)) then Lowest(low) else Double.NaN;
def BoxHigh2 = if ((UP_OS) or ((Lower_BandS crosses below Lower_BandK2))) then Highest(ExpH) else Double.NaN;
def BH2 = if !IsNaN(BoxHigh2) then high else Double.NaN;
def BH2ext = if IsNaN(BH2) then BH2ext[1] else BH2;
def BH2extline = BH2ext;
def H_BH2extline2 = Lowest(BH2extline, 1);
def BoxLow2 = if ((UP_OS) or (Lower_BandS crosses below Lower_BandK2) or (condition_BandRevUp) and (low < low[1]) and ((price < Lower_BandK2) or (price < Lower_BandS))) or ((UP_OS[1]) and (low < low[1])) then Lowest(low) else Double.NaN;
def BH1 = if !IsNaN(BoxHigh) then high else Double.NaN;
def BH1ext = if IsNaN(BH1) then BH1ext[1] else BH1;
def BH1extline = BH1ext;
def BL1 = if !IsNaN(BoxLow) then low else Double.NaN;
def BL1ext = if IsNaN(BL1) then BL1ext[1] else BL1;
def BL1extline2 = BL1ext;
def BL2 = if !IsNaN(BoxLow2) then low else Double.NaN;
def BL2ext = if IsNaN(BL2) then BL2ext[1] else BL2;
def BL2extline2 = BL2ext;
#####################################################
## KEY LEVELS                                      ##
#####################################################
def KeylevelOB = if ((BH1ext >= YHextlineOB) and (BL1ext <= YHextlineOB)) then 1 else 0;
def KeylevelOS = if ((BH2ext >= YHextlineOS) and (BL2ext <= YHextlineOS)) then 1 else 0;
def keyobpaint = (hl2 < BH1ext) and (hl2 > BL1ext);
def keyospaint = (hl2 < BH2ext) and (hl2 > BL2ext);
def Keylevel2OB = (round(absvalue(close - yhextlineOB),1));
def Keylevel2OS = (round(absvalue(close - yhextlineOS),1));
def control = 13;
def Keylevel2cond = if KeylevelOS and (Keylevel2OS < Keylevel2OB) and (Keylevel2OS < control) then 1 else if KeylevelOB and (Keylevel2OB < Keylevel2OS) and (Keylevel2OB < control) then -1 else 0;
#####################################################
## TESTING                                         ##
#####################################################
#def OB_Cross_Below = if price crosses Below YHextlineOB then 1 else 0;
#def OS_Cross_Above = if price crosses Above YHextlineOS then 1 else 0;
#def Key_Cross_down = if marketopen and keylevelOB and OB_Cross_Below then 1 else 0;
#def Key_Cross_up = if marketopen and keylevelOS and OS_Cross_Above then 1 else 0;
#####################################################
##                                                 ##
#####################################################
#plot Key_UP = if Key_Cross_Up then low else double.nan;
#Key_UP.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#Key_UP.SetDefaultColor(Color.yellow);
#Key_UP.HideBubble();
#Key_UP.HideTitle();
#####################################################
##                                                 ##
#####################################################
#plot Key_down = if Key_Cross_Down then high else double.nan;;
#Key_down.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#Key_down.SetDefaultColor(Color.yellow);
#Key_down.HideBubble();
#Key_down.HideTitle();
#####################################################
## SPARK                                           ##
#####################################################
def Agperiod1 = GetAggregationPeriod();
def timeframe = if agperiod1 <= aggregationperiod.DAY then aggregationperiod.DAY else agperiod1;
def Length9 = 35;
def Length8 = 10;
def AvgExp8 = ExpAverage(price[-displace], length8);
def UPD = AvgExp8[1] < AvgExp8;
def AvgExp9 = ExpAverage(price[-displace], length9);
def UPW = AvgExp9[1] < AvgExp9;
def Below = AvgExp8 < AvgExp9;
def Spark = UPD + UPW + Below;
def UPEMA = AvgExp8[1] < AvgExp8;
def DOWNEMA = AvgExp8[1] > AvgExp8;
def BigUP = direction == 1;
def BigDN = direction == -1;
def BigNa = direction == 0;
def UPEMA2 = AvgExp9[1] < AvgExp9;
def DOWNEMA2 = AvgExp9[1] > AvgExp9;
def UP8 = UPEMA and UPEMA2;
def DOWN8 = DOWNEMA and DOWNEMA2;
def PriceColor8 = if UP8 then 1 else if DOWN8 then -1 else 0;
def UP11 = UPEMA;
def DOWN11 = DOWNEMA;
def PriceColor11 = if UP11 then 1 else if DOWN11 then -1 else 0;
def UP12 = UPEMA2;
def DOWN12 = DOWNEMA2;
def PriceColor12 = if UP12 then 1 else if DOWN12 then -1 else 0;
def CandleColor = if (priceColor == 1) and (priceColor12 == 1) and (Spark >= 2) then 1 else if (priceColor == -1) and (Spark < 1) then -1 else 0;
def SparkUP1 = (Spark == 3) and (CandleColor == 1);
def SparkDN1 = (Spark == 0) and (CandleColor == -1);
def Hide_SparkUP = if SparkUP1 and (SparkUP1[1] or SparkUP1[2] or SparkUP1[3] or SparkUP1[4] or SparkUP1[5]) or (bigdn) then 0 else 1;
def Hide_SparkDN = if SparkDN1 and (SparkDN1[1] or SparkDN1[2] or SparkDN1[3] or SparkDN1[4] or SparkDN1[5]) or (bigup) then 0 else 1; 
def Vol = volume(period = timeframe); 
def at_High = high(period = timeframe);
def at_Open = open(period = timeframe);
def at_Close = close(period = timeframe);
def at_Low = low(period = timeframe);
def Buy_Volume = RoundUp(Vol * (at_Close - at_Low) / (at_High - at_Low));
def Buy_percent = RoundUp((Buy_Volume / Vol) * 100);
def Sell_Volume = RoundDown(Vol * (at_High - at_Close) / (at_High - at_Low));
def Sell_percent = RoundUp((Sell_Volume / Vol) * 100);
def avg1 = ExpAverage(close(period = agperiod1), length8);
def height = avg1 - avg1[length8];
def avg2 = ExpAverage(close(period = agperiod1), length8);                                           
def Condition1UP = avg1 > avg2;
def Condition1DN = avg1 < avg2;
def Condition2UP = Buy_percent > 50;
def Condition2DN = Buy_percent < 50;
def Condition3UP = if buyerRegular then 1 else 0;
def Condition3DN =  if sellerRegular then 1 else 0;
def Condition4UP =  if buyerExtreme then 1 else 0;
def Condition4DN =  if sellerExtreme then 1 else 0;
#####################################################
## FILTER                                          ##
#####################################################
def Big4_up_1;
def Big4_dn_1;
def TS_UP_1;
def TS_DN_1;
def SparkUP_1;
def SparkDN_1;
switch (Filter) {
case EMAD_Filter:
    Big4_up_1 = marketopen and EMAD_Above_Zero and (direction2 == 1 and direction2[1] < 1);
    Big4_dn_1 = marketopen and EMAD_Below_Zero and (direction2 == -1 and direction2[1] > -1);
    TS_UP_1 = if EMAD_Above_Zero and (upsignal and !direction_Equals_zero) then 1 else 0;   
    TS_DN_1 = if EMAD_Below_Zero and (downsignal and !direction_Equals_zero) then 1 else 0; 
    SparkUP_1 = marketopen and EMAD_Above_Zero and (SparkUP1 and hide_SparkUP);
    SparkDN_1 = marketopen and EMAD_Above_Zero and (SparkDN1 and hide_SparkDN);
case Phase_Filter:
    Big4_up_1 = marketopen and bullphase and (direction2 == 1 and direction2[1] < 1);
    Big4_dn_1 = marketopen and bearphase and (direction2 == -1 and direction2[1] > -1);
    TS_UP_1 = if (upsignal and bullphase and !direction_Equals_zero) then 1 else 0;   
    TS_DN_1 = if (downsignal and bearphase and !direction_Equals_zero) then 1 else 0; 
    SparkUP_1 = marketopen and bullphase and (SparkUP1 and hide_SparkUP);
    SparkDN_1 = marketopen and bearphase and (SparkDN1 and hide_SparkDN);
case No_Filter:
    Big4_up_1 = marketopen and (direction2 == 1 and direction2[1] < 1);
    Big4_dn_1 = marketopen and  (direction2 == -1 and direction2[1] > -1);
    TS_UP_1 = if (upsignal and !direction_Equals_zero) then 1 else 0;   
    TS_DN_1 = if (downsignal and !direction_Equals_zero) then 1 else 0; 
    SparkUP_1 = marketopen and (SparkUP1 and hide_SparkUP);
    SparkDN_1 = marketopen and (SparkDN1 and hide_SparkDN);}
#####################################################
## DIRECTION                                       ##
#####################################################
def Big4_up_bn = if Big4_up_1 then bn else Big4_up_bn[1];
def Big4_dn_bn = if Big4_dn_1 then bn else Big4_dn_bn[1];
def TS_Up_bn = if TS_Up_1 then bn else TS_Up_bn[1];
def TS_Dn_bn = if TS_Dn_1 then bn else TS_Dn_bn[1];
def SparkUp_bn = if SparkUP_1 then bn else SparkUP_bn[1];
def SparkDn_bn = if SparkDN_1 then bn else SparkDN_bn[1];
def last_up_bn = if  Big4_up_1 or TS_Up_1 or SparkUP_1 then bn else last_up_bn[1];
def last_dn_bn = if  Big4_dn_1 or TS_dn_1 or SparkDN_1 then bn else last_dn_bn[1];
def Last_Arrow_Up = if last_up_bn > last_dn_bn then 1 else 0;
def Last_Arrow_Down = if last_dn_bn > last_up_bn then 1 else 0;
#####################################################
## CANDLE COLOR                                    ##
#####################################################
AssignPriceColor(if Color_Candles then
     if ALERT4 THEN COLOR.green
else if buyerRegular or buyerExtreme then Color.green
else if buyerRegular[1] or buyerExtreme[1] then Color.red
else if direction == 1 then Color.light_green
else if sellerRegular or sellerExtreme then Color.dark_red
else if sellerRegular[1] or sellerExtreme[1] then Color.green
else if direction == -1 then color.red
else if direction == 0 then Color.gray
else Color.GRAY
else Color.CURRENT);
#####################################################
## LINE PLOTS                                      ##
#####################################################
plot Squeeze_Alert = Squeeze_Level;
plot YCOB_PLOT = if !IsNaN(YHOB) then hl2 else Double.NaN;
plot YHextlineOB_PLOT = YHextlineOB;
plot YCOS_PLOT = if !IsNaN(YHOS) then hl2 else Double.NaN;
plot YHextlineOS_PLOT = YHextlineOS;
plot H_BH2extline = Lowest(BH2extline, 1);
plot BL1extline = BL1ext;
plot BL2extline = BL2ext;
plot H_BH1extline = Highest(BH1extline, 1);
plot L_BL1extline = Highest(BL1extline, 1);
plot L_BL2extline = Lowest(BL2extline, 1);
plot Ehlers_1 = Ehlers_Avg;
#####################################################
## ARROW PLOTS                                     ##
#####################################################
plot UP = UP1_1;
plot DOWN = DOWN1_1;
plot First_HL = First_HL_1;
plot First_LH = First_LH_1;
plot Second_HL = Second_HL_1;
plot Second_LH = Second_LH_1;
plot TS_UP = TS_UP_1;
plot TS_DN = TS_DN_1;
plot SparkUP = SparkUP_1;
plot SparkDN = SparkDN_1;
plot Big4_up = Big4_up_1;
plot Big4_dn = Big4_dn_1;
#####################################################
##                                                 ##
#####################################################
Ehlers_1.SetStyle(Curve.SHORT_DASH);
Ehlers_1.SetLineWeight(1);
Ehlers_1.AssignValueColor(color.gray);
#####################################################
##                                                 ##
#####################################################
Squeeze_Alert.SetPaintingStrategy(PaintingStrategy.POINTS);
Squeeze_Alert.SetLineWeight(3);
Squeeze_Alert.AssignValueColor(if(direction2 == 1) then color.yellow
                          else if direction2 == -1 then color.yellow
                          else if direction2 == 0 then color.light_Gray
                          else color.yellow);
#####################################################
##                                                 ##
#####################################################
YCOB_PLOT.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
YCOB_PLOT.SetDefaultColor(Color.GREEN);
YCOB_PLOT.SetLineWeight(1);
YCOB_PLOT.HideTitle();
#####################################################
##                                                 ##
#####################################################
YHextlineOB_PLOT.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
YHextlineOB_PLOT.SetDefaultColor(Color.ORANGE);
YHextlineOB_PLOT.SetLineWeight(2);
YHextlineOB_PLOT.HideTitle();
#####################################################
##                                                 ##
#####################################################
YCOS_PLOT.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
YCOS_PLOT.SetDefaultColor(Color.GREEN);
YCOS_PLOT.SetLineWeight(1);
YCOS_PLOT.HideTitle();
#####################################################
##                                                 ##
#####################################################
YHextlineOS_PLOT.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
YHextlineOS_PLOT.SetDefaultColor(Color.LIGHT_GREEN);
YHextlineOS_PLOT.SetLineWeight(2);
YHextlineOS_PLOT.HideTitle();
#####################################################
##                                                 ##
#####################################################
H_BH2extline.SetDefaultColor(Color.dark_GREEN);
H_BH2extline.SetLineWeight(1);
H_BH2extline.HideBubble();
H_BH2extline.HideTitle();
#####################################################
##                                                 ##
#####################################################
BL1extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BL1extline.SetDefaultColor(Color.dark_RED);
BL1extline.SetLineWeight(1);
BL1extline.HideTitle();
#####################################################
##                                                 ##
#####################################################
BL2extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BL2extline.SetDefaultColor(Color.dark_GREEN);
BL2extline.SetLineWeight(1);
BL2extline.HideTitle();
#####################################################
##                                                 ##
#####################################################
H_BH1extline.SetDefaultColor(Color.dark_RED);
H_BH1extline.SetLineWeight(1);
H_BH1extline.HideBubble();
H_BH1extline.HideTitle();
#####################################################
##                                                 ##
#####################################################
L_BL1extline.SetDefaultColor(Color.dark_RED);
L_BL1extline.SetLineWeight(1);
L_BL1extline.HideBubble();
L_BL1extline.HideTitle();
#####################################################
##                                                 ##
#####################################################
L_BL2extline.SetDefaultColor(Color.dark_GREEN);
L_BL2extline.SetLineWeight(1);
L_BL2extline.HideBubble();
L_BL2extline.HideTitle();
#####################################################
##                                                 ##
#####################################################
UP.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UP.assignvaluecolor(if alert4 then color.CYAN else Color.white);
UP.HideBubble();
UP.HideTitle();
#####################################################
##                                                 ##
#####################################################
DOWN.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DOWN.SetDefaultColor(Color.white);
DOWN.HideBubble();
DOWN.HideTitle();
#####################################################
##                                                 ##
#####################################################
First_HL.SetPaintingStrategy(PaintingStrategy.ARROW_Up);
First_HL.SetDefaultColor(Color.magenta);
First_HL.HideBubble();
First_HL.HideTitle();
#####################################################
##                                                 ##
#####################################################
First_LH.SetPaintingStrategy(PaintingStrategy.ARROW_down);
First_LH.SetDefaultColor(Color.magenta);
First_LH.HideBubble();
First_LH.HideTitle();
#####################################################
##                                                 ##
#####################################################
Second_HL.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Second_HL.SetDefaultColor(Color.orange);
Second_HL.HideBubble();
Second_HL.HideTitle();
#####################################################
##                                                 ##
#####################################################
Second_LH.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
Second_LH.SetDefaultColor(Color.orange);
Second_LH.HideBubble();
Second_LH.HideTitle();
#####################################################
##                                                 ##
#####################################################
Big4_up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Big4_up.AssignValueColor(if(direction2 == 1 and direction2[1] < 1)then color.blue else color.gray);
Big4_up.HideBubble();
Big4_up.HideTitle();
#####################################################
##                                                 ##
#####################################################
Big4_dn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Big4_dn.AssignValueColor(if(direction2 == -1 and direction2[1] > -1) then color.blue else color.gray);
Big4_dn.HideBubble();
Big4_dn.HideTitle();
#####################################################
##                                                 ##
#####################################################
TS_UP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
TS_UP.AssignValueColor(color.cyan);
TS_UP.HideBubble();
TS_UP.HideTitle();
#####################################################
##                                                 ##
#####################################################
TS_DN.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
TS_DN.AssignValueColor(color.cyan);
TS_DN.HideBubble();
TS_DN.HideTitle();
#####################################################
##                                                 ##
#####################################################
SparkUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
SparkUP.AssignValueColor(Color.green);
SparkUP.HideBubble();
SparkUP.HideTitle();
#####################################################
##                                                 ##
#####################################################
SparkDN.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SparkDN.AssignValueColor(Color.Light_red);  
SparkDN.HideBubble();
SparkDN.HideTitle();
#####################################################
##                                                 ##
#####################################################
AddCloud(if !HideCloud then BH1extline else Double.NaN, BL1extline, Color.DARK_RED, Color.GRAY);
AddCloud(if !HideCloud then BH2extline else Double.NaN, BL2extline, Color.DARK_GREEN, Color.GRAY);
#####################################################
##                                                 ##
##################################################### 
#AddCloud(if (EMAD_Green_Cloud_1 or EMAD_Green_Cloud_2) then H_BH1extline else double.nan, L_BL2extline, color.dark_green, color.dark_green);
AddCloud(if (EMAD_Gray_Cloud_1 or EMAD_Gray_Cloud_2) then H_BH1extline else double.nan, L_BL2extline, color.dark_gray, color.dark_gray);
#AddCloud(if (EMAD_Red_Cloud_1 or EMAD_Red_Cloud_2) then H_BH1extline else double.nan, L_BL2extline, color.dark_red, color.dark_red);
#####################################################
## lABELS                                          ##
##################################################### 
def Buy = UP_OS;
def Sell = DOWN_OB;
def conditionLTB = (conditionK2UP and (Consensus_Level < 0));
def conditionLTS = (conditionK3DN and (Consensus_Level > 0));
def conditionBO = ((Upper_BandS[1] < Upper_BandS))
              and ((Lower_BandS[1] < Lower_BandS))
              and ((Upper_BandK[1] < Upper_BandK))
              and ((Lower_BandK[1] < Lower_BandK));
def conditionBD = ((Upper_BandS[1] > Upper_BandS))
              and ((Lower_BandS[1] > Lower_BandS))
              and ((Upper_BandK[1] > Upper_BandK))
              and ((Lower_BandK[1] > Lower_BandK));  
AddLabel(yes, if conditionLTB then "LOOK TO BUY"
         else if conditionLTS then "LOOK TO SELL"
         else if conditionK2UP then "TREND: BULL"
         else if conditionK3DN then "TREND: BEAR" else "TREND: CONSOLIDATION",
              if conditionLTB then Color.YELLOW
         else if conditionLTS then Color.YELLOW
         else if conditionK2UP then Color.LIGHT_GREEN
         else if conditionK3DN then Color.RED else Color.GRAY);
AddLabel(yes, if conditionBD then "BREAKDOWN"
         else if conditionBO then "BREAKOUT" else "NO BREAK",
              if conditionBD then Color.RED
         else if conditionBO then Color.GREEN else Color.GRAY);
AddLabel(show_Labels, if (Spark == 3) then "SPARK: " + Round(Spark, 1)
         else if (Spark == 0) then  "SPARK: " + Round(Spark, 1) else "SPARK: " + Round(Spark, 1),
              if (Spark == 3) then Color.orange
         else if (Spark == 2) then Color.light_GREEN
         else if (Spark == 0) then Color.RED else Color.GRAY);
AddLabel(show_labels, if  Condition1UP==1 and Condition2UP == 1 and (Condition3UP == 1 or Condition4UP == 1) then "**CALLS ONLY!**"
else if Condition1UP == 1 and Condition2UP == 1 then "VERY BULLISH"  
else if direction == 1 then "BULLISH"               
else if Condition1DN == 1 and Condition2DN == 1 and (Condition3DN == 1 or Condition4DN == 1) then "**PUTS ONLY!**"
else if Condition1DN == 1 and Condition2DN == 1 then "VERY BEARISH"
else if direction == -1 then "BEARISH"             
else if ((avg[1] > avg) and (avg > avg2) and (Buy_percent > 50)) then "BULLISH RETRACEMENT"
else if ((avg[1] < avg) and (avg < avg2) and (Buy_percent < 50)) then "BEARISH RETRACEMENT" else "CHOP",
     if Condition1UP == 1 and Condition2UP == 1 and (Condition3UP == 1 or Condition4UP == 1) then Color.cyan
else if Condition1DN == 1 and Condition2DN == 1 and (Condition3DN == 1 or Condition4DN == 1) then Color.Magenta
else if Condition1UP == 1 and Condition2UP == 1 then Color.light_GREEN
else if direction == 1 then Color.light_green                         
else if Condition1DN == 1 and Condition2DN == 1 then Color.RED
else if direction == -1 then Color.red          
else Color.orange);
AddLabel(yes, if MomentumUP then "Consensus Increasing = " + Round(Consensus_Level, 1)
else if MomentumUP or MomentumDOWN and conditionOB_CL then "Consensus OVERBOUGHT = " + Round(Consensus_Level, 1)
else if MomentumDOWN then  "Consensus Decreasing = " + Round(Consensus_Level, 1)
else if MomentumUP or MomentumDOWN and conditionOS_CL then "Consensus OVERSOLD = " + Round(Consensus_Level, 1)
else "Consensus = " + Round(Consensus_Level, 1), if conditionOB_CL then Color.light_green
else if conditionOS_CL then Color.red else Color.GRAY);
AddLabel(squeeze_Alert, "SQUEEZE ALERT", if Squeeze_Alert then Color.YELLOW else Color.GRAY);
AddLabel(KeylevelOS and keyospaint," Near Key Support Level: $" + round(yhextlineOS,0), color.light_green);
AddLabel(KeylevelOB and keyobpaint," Near Key Resistance Level: $" + round(yhextlineOB,0), color.orange);
AddLabel(show_labels,
     if TS_Last == 1 then " TS Last Signal: BUY " 
else if TS_Last == 0 and ((alert4 or regular_Buy or extreme_buy) within 5 bars) then " TS Last Signal: SELL " 
else if TS_Last == 0 then " TS Last Signal: SELL "
else "",
     if TS_Last == 1 then color.light_gray
else if TS_Last == 0 and((alert4 or regular_Buy or extreme_buy) within 5 bars) then color.orange
else if TS_Last == 0 then color.light_gray
else color.white);
addlabel(yes, " Filter In Use: " + Filter, color.white);
#####################################################
## END                                             ##
#####################################################
 
Last edited:
RwRzaKz.png


EMAD Variant Mobile: http://tos.mx/UhGOjQf (updated link... removed some unnecessary code and changed some default settings 9:15am 1/11/24)

Added the line coloring like the desktop version for the following conditions
:
1. Zeroline turns green or red when EMAs cross above or below the zeroline
2. Top and bottom bands turn green or red when stepping up or down individually or at the same time
3. EMAs colored based on being crossed up or down (whereas before in mobile there were two EMAs one green and the other red)
4. The other moving average (called "made" in settings) is colored also however the gray portions of the line are not working quite as they should but good enough for now
5. Top and bottom bands turn yellow when the zeroline is below the bottom band or when the zeroline is above the top band.

Notes concerning settings:
  • The "UP" and "DOWN" plots will need to be changed from lines to arrows in settings
  • The "Squeeze" plot can remain a solid line (or dots) but you will need to increase the line weight to 3 or 4 in settings

unJRHwt.jpg


Code:
   ##########################################################################################
   ##########################################################################################
   ###                                                                                    ###
   ###       #############################           #########               #########    ###
   ###     #################################         #########               #########    ###    
   ###    ####################################       #########               #########    ###
   ###   ########                     #########      #########               #########    ###         
   ###   ########   EMAD for MOBILE   #########      #########               #########    ###
   ###   ########   @Christopher84    #########      #########               #########    ###       
   ###   ########       @HODL         #########      #########               #########    ###     
   ###   ########                     #########      #########               #########    ###        
   ###   ######################################      ##################################   ###      
   ###    ####################################       ##################################   ###   
   ###     #################################         ##################################   ###
   ###    ####################################       ##################################   ###     
   ###   ########                     #########                              #########    ###
   ###   ########     @Horserider     #########                              #########    ###
   ###   ########  HORSERIDER VOLUME  #########                              #########    ###            
   ###   ########  TRIPLE EXHAUSTION  #########                              #########    ###        
   ###   ########      @chence27      #########                              #########    ###             
   ###   ########                     #########                              #########    ###         
   ###    ####################################                               #########    ###       
   ###     ##################################                                #########    ###        
   ###      ###############################                                  #########    ###      
   ###                                                                                    ###
   ##########################################################################################
   ##########################################################################################

#DECLARATIONS
declare lower;


#USER INPUTS
input use_alert4_filter = yes; #hint show alert4 arrow only when it appears with an UP arrow
input showverticalline = no;
input Ehlers_length = 34;
input Length_MAD = 4;
input emadLineWeight = 2;
input filterBounceArrows = 1; #hint lookback period for bothbandsup/down etc. fold
input cloudlinelookback = 20;  #hint lookback period for "made" average crossover fold used for arrows
input Steplookback = 10; #hint lookback period for top/bottomstepup/down etc. fold
input filterZeroline = 20; #hint lookback period for crosses up/down at zeroline etc. fold
input PreviousStepsDown = 2; #hint filter for hiding arrows
input PreviousStepsUp = 2; #hint filter for hiding arrows
input tradeDaytimeOnly = yes; #hint tradeDaytimeOnly: used to hide arrows
input OpenTime = 0730; #hint OpenTime: Opening time of market hides arrows
input CloseTime = 1800; #hint CloseTime: Closing time of market hides arrows
input firstTrade_Sq = {default long, short};
input trailType = {default modified, unmodified};
input firstTrade = {default long, short};

def bn = barnumber();
def Begin = SecondsFromTime(OpenTime);
def End = SecondsTillTime(CloseTime);
def isIntraDay = if GetAggregationPeriod() > 14400000 or GetAggregationPeriod() == 0 then 0 else 1;
def MarketOpen = if !tradeDaytimeOnly or !isIntraDay then 1 else if tradeDaytimeOnly and isIntraDay and Begin > 0 and End > 0 then 1 else 0;
def fastLength = 10;
def slowLength = 35;
def lengthBAD = 3;
def ATRPeriod = 5;
def ATRFactor = 2.0;
def length_3x = 1000;
def priceType = close;
def showTripleExh = yes;
def showHLLH = no;
def showtestlabels = no;
def bandLength = 100;
def bandLength2 = 100;
def smoothLength = 12;
def smoothLength2 = 14;
def averageType = AverageType.WILDERS;
def averageType_Sq = AverageType.SIMPLE;

#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));

## 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);
def zerolinedatacondition = if (zerolinedata > topBand) then topBand else if (zerolinedata < bottomBand) then bottomBand else 0;
## Master
def masteremadline = (upperEMADLine + lowerEMADLine) / 2;
## 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;
###############################################################
def crossesUpZeroline = if (valueUp) == 0 then 1 else 0;
def crossesDownZeroline = if (valueDown) == 0 then 1 else 0;
###############################################################
def crossesUpBottomlinebn = if crossesUpline and !crossesUpline[1] then bn else crossesUpBottomlinebn[1];
def crossesDownToplinebn = if crossesDownline and !crossesDownline[1] then bn else crossesDownToplinebn[1];
def crossuplinewithin = if crossesUpBottomlinebn < bn then bn - crossesUpBottomlinebn else crossuplinewithin[1];
def crossdownlinewithin = if crossesDownToplinebn < bn then bn - crossesDownToplinebn else crossdownlinewithin[1];
#def crosslinefirst = if crossuplinewithin < crossdownlinewithin then 1 else 0;
def crosslinelogic = crossesUpBottomlinebn > crossesDownToplinebn;
def HL = crossesUpline == 0 and crossesDownline == 0 and upperEMADLine crosses above lowerEMADLine;  #Normal HL LH condition
def LH = crossesDownline == 0 and upperEMADLine crosses below lowerEMADLine; #Normal HL LH condition
def bounceUpOnce = crossesUpline <> crossesUpline[1]; #XUpBtm happened previous bar or on current bar
def bounceDnOnce = crossesDownline <> crossesDownline[1];
def trackCrUp = if bounceUpOnce then 0 else if HL then 1 else trackCrUp[1];
def trackCrDn = if bounceDnOnce then 0 else if LH then 1 else trackCrDn[1];
## SECOND HL AND LH
def HLalready = if trackCrUp and !trackCrUp[1] then 1 else 0;
def LHalready = if trackCrDn and !trackCrDn[1] then 1 else 0;
def HL2 = HLalready == 0 and upperEMADLine crosses above lowerEMADLine;
def LH2 = LHalready == 0 and upperEMADLine crosses below lowerEMADLine;
def crossedUpOnce = HLalready <> HLalready[1];
def crossedDnOnce = LHalready <> LHalready[1];
def trackCrUp2 = if crossedUpOnce then 0 else if HL2 then 1 else trackCrUp2[1];
def trackCrDn2 = if crossedDnOnce then 0 else if LH2 then 1 else trackCrDn2[1];
#####################################################
##                                                 ##
#####################################################
def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
def HRef = if low <= high[1]
    then high - close[1]
    else (high - close[1]) - 0.5 * (low - high[1]);
def LRef = if high >= low[1]
    then close[1] - low
    else (close[1] - low) - 0.5 * (low[1] - high);
def trueRange_Sq;
switch (trailType) {
case modified:
    trueRange_Sq = Max(HiLo, Max(HRef, LRef));
case unmodified:
    trueRange_Sq = TrueRange(high, close, low);
}
def loss_Sq = ATRFactor * MovingAverage(averageType_Sq, trueRange_Sq, ATRPeriod);
def state_Sq = {default init, long, short};
def trail_Sq;
switch (state_Sq[1]) {
case init:
    if (!IsNaN(loss_Sq)) {
        switch (firstTrade) {
        case long:
            state_Sq = state_Sq.long;
            trail_Sq =  close - loss_Sq;
        case short:
            state_Sq = state_Sq.short;
            trail_Sq = close + loss_Sq;
    }
    } else {
        state_Sq = state_Sq.init;
        trail_Sq = Double.NaN;
    }
case long:
    if (close > trail_Sq[1]) {
        state_Sq = state_Sq.long;
        trail_Sq = Max(trail_Sq[1], close - loss_Sq);
    } else {
        state_Sq = state_Sq.short;
        trail_Sq = close + loss_Sq;
    }
case short:
    if (close < trail_Sq[1]) {
        state_Sq = state_Sq.short;
        trail_Sq = Min(trail_Sq[1], close + loss_Sq);
    } else {
        state_Sq = state_Sq.long;
        trail_Sq =  close - loss_Sq;
    }
}
def TrailingStop_Sq = trail_Sq;
def H = Highest(TrailingStop_Sq, 12);
def L = Lowest(TrailingStop_Sq, 12);
def BulgeLengthPrice = 100;
def SqueezeLengthPrice = 100;
def BandwidthC3 = (H - L);
def IntermResistance2 = Highest(BandwidthC3, BulgeLengthPrice);
def IntermSupport2 = Lowest(BandwidthC3, SqueezeLengthPrice);
def sqzTrigger = BandwidthC3 <= IntermSupport2;
def sqzLevel = if !sqzTrigger[1] and sqzTrigger then hl2 else if !sqzTrigger then Double.NaN else sqzLevel[1];
def Squeeze_Alert_1 = if sqzLevel then (upperEMADLine + lowerEMADLine) / 2 else (upperEMADLine + lowerEMADLine) / 2 ;
#####################################################
##                                                 ##
#####################################################
def over_bought_3x = 80;
def over_sold_3x = 20;
def KPeriod_3x = 10;
def DPeriod_3x = 10;
def priceH1 = emadhigh;
def priceL1 =  emadlow;
def priceC1 =  emadclose;
def priceO1 =  emadclose;
def priceH_3x =  emadhigh;
def priceL_3x =  emadlow;
def priceC_3x =  emadclose;   
def SlowK_3x = reference StochasticFull(over_bought_3x,  over_sold_3x,  KPeriod_3x,  DPeriod_3x,  priceH_3x,  priceL_3x,  priceC_3x,  3, if (averageType == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;
def MACD_3x = reference MACD()."Value";
def priceMean_3x = Average(MACD_3x, length_3x);
def MACD_stdev_3x =  (MACD_3x - priceMean_3x) / StDev(MACD_3x, length_3x);
def dPlus_3x = reference DMI()."DI+";
def dMinus_3x = reference DMI()."DI-";
def sellerRegular = SlowK_3x < 20 and MACD_stdev_3x < -1 and dPlus_3x < 15;
def sellerExtreme = SlowK_3x < 20 and MACD_stdev_3x < -2 and dPlus_3x < 15;
def buyerRegular = SlowK_3x > 80 and MACD_stdev_3x > 1 and dMinus_3x < 15;
def buyerExtreme = SlowK_3x > 80 and MACD_stdev_3x > 2 and dMinus_3x < 15;   
def ExtremeBuy1 = if sellerExtreme[1] and !sellerExtreme then 1 else 0;
def ExtremeSell1 = if buyerExtreme[1] and !buyerExtreme then 1 else 0;
def RegularSell1 = if buyerRegular[1] and !buyerRegular then 1 else 0;
def RegularBuy1= if sellerRegular[1] and !sellerRegular then 1 else 0;
#####################################################
##                                                 ##
#####################################################
def priceE = (emadhigh + emadlow) / 2;
def coeff = Ehlers_length * priceE * priceE - 2 * priceE * sum(priceE, Ehlers_length)[1] + sum(priceE * priceE, Ehlers_length)[1];
def Ehlers =  sum(coeff * priceE, Ehlers_length) / sum(coeff, Ehlers_length);
#####################################################
##                                                 ##
#####################################################
def dataA = masterEMADLine;
def dataA1 =
    fold aD = 0
    to LengtH_MAD
    with k = 0
    do k + dataA[aD];
def MAEMAD = dataA1 / LengtH_MAD;
def MAD = MAEMAD;
def ElCrossDN = ehlers crosses above masterEMADline;
def ElCrossUP = ehlers crosses below masterEMADline;
def bullBias2 = (emadsup);
def bearBias2 = (emadsdown);
def MADehlers = (MAD - Ehlers)/2;
def Madebad = madehlers + ehlers;
def madebadavg = average(madebad, lengthBAD);
def crossmadeup = madebad > madebad[1];
def crossmadedn = madebad < madebad[1];
def crossmade2up = if (madebad < masterEMADLine) and crossmadeup and (bullBias2 > 0) then 1 else 0;
def crossmade2dn = if (madebad > masterEMADLine) and crossmadedn and (bearBias2 > 0) then 1 else 0;
def crossmade2 = if (madebad < masterEMADLine) and crossmadeup and (bullBias2 > 0) then 1 else if (madebad > masterEMADLine) and crossmadedn and (bearBias2 > 0) then -1 else 0;
def crossmade0 = if (!crossmade2[1] or !crossmade2[1]) and (crossmade2 == 0) then 1 else 0;
#####################################################
##                                                 ##
#####################################################
def pd = 22;
def bbl = 20;
def mult = 2.0;
def lb = 50;
def ph = 0.85;
def ltLB = 40;
def mtLB = 14;
def str = 3;
def wvf = ((Highest(close, pd) - low) / (Highest(close, pd))) * 100;
def sDev = mult * StDev(wvf, bbl);
def midLine1 = SimpleMovingAvg(wvf, bbl);
def lowerBand1 = midLine1 - sDev;
def upperBand1 = midLine1 + sDev;
def rangeHigh = (Highest(wvf, lb)) * ph;
def upRange = low > low[1] and close > high[1];
def upRange_Aggr = close > close[1] and close > open[1];
def filtered = ((wvf[1] >= upperBand1[1] or wvf[1] >= rangeHigh[1]) and (wvf < upperBand1 and wvf < rangeHigh));
def filtered_Aggr = (wvf[1] >= upperBand1[1] or wvf[1] >= rangeHigh[1]);
def alert4 = upRange_Aggr and close > close[str] and (close < close[ltLB] or close < close[mtLB]) and filtered_Aggr;
def alert4close = if alert4 then close else 0;
def crossmade = if (madebad < masterEMADLine) and crossmadeup then 1 else if (madebad > masterEMADLine) and crossmadedn then -1 else 0;
def madedn = (madebad > masterEMADLine);
def madeup = (madebad < masterEMADLine);
def made2 = madebadavg;
def made2_abovezero = (made2 > zerolinedata);
def made2_belowzero = (made2 < zerolinedata);
def made3 = if !madedn and made2_belowzero then madebadavg else double.nan;
def made4 = if !madeup and made2_abovezero then madebadavg else double.nan;
def made5 = if (made2 == zerolinedata) then madebadavg else double.nan;
def madecrossupavg = if made2 crosses below masterEMADLine then 1 else 0;
def madecrossdownavg = if made2 crosses above masterEMADLine then 1 else 0;
def crossmadelogicup = if madecrossupavg and !madecrossupavg[1] then 1 else 0;
def crossmadelogicdown = if madecrossdownavg and !madecrossdownavg[1] then 1 else 0;
def crossmademorelogicup = if crossmadelogicup and crosslinelogic and emadup[1] then 1 else 0;
def crossmademorelogicdown = if crossmadelogicdown and !crosslinelogic and emaddown[1] then 1 else 0;
def EmaPushStepUp = if topBandStepUp and EmadUp and (((upperEMADLine) - bottomband) == 0) then 1 else 0;;
def EmaPushStepDown = if bottomBandStepDown and EmadDown and (((lowerEMADLine) - bottomband) == 0) then 1 else 0;
def HLFilter = if HL[1] < HL then 1 else 0;
def LHFilter = if LH[1] > LH then 1 else 0;
def HLFilter2 = if HL[2] < HL[1] then 1 else 0;
def LHFilter2 = if LH[2] > LH[1] then 1 else 0;
#####################################################
##                                                 ##
#####################################################
def topBandStepDown_lookback = fold index3 = 1 to Steplookback with x3 do x3 + topBandStepDown[index3];
def topBandStepUp_lookback = fold index4 = 1 to Steplookback with x4 do x4 + topBandStepUp[index4];
def bottomBandStepDown_lookback = fold index5 = 1 to Steplookback with x5 do x5 + bottomBandStepDown[index5];
def bottomBandStepUp_lookback = fold index6 = 1 to Steplookback with x6 do x6 + bottomBandStepUp[index6];
def bothBandsDown_lookback = fold index7 = 1 to filterBounceArrows with x7 do x7 + bothBandsDown[index7];
def bothBandsUp_lookback = fold index8 = 1 to filterBounceArrows with x8 do x8 + bothBandsUp[index8];
def bullBias_lookback = fold index9 = 1 to filterBounceArrows with x9 do x9 + bullBias[index9];
def bearBias_lookback = fold index10 = 1 to filterBounceArrows with x10 do x10 + bearBias[index10];
######################################################################################################################
def crossesUpZeroline_lookback = fold index11 = 1 to filterZeroline with x11 do x11 + crossesUpZeroline[index11];
def crossesDownZeroline_lookback = fold index12 = 1 to filterZeroline with x12 do x12 + crossesDownZeroline[index12];
######################################################################################################################
def crossesUpBottomline_lookback = fold index13 = 1 to filterBounceArrows with x13 do x13 + crossesUpline[index13];
def crossesDownTopline_lookback = fold index14 = 1 to filterBounceArrows with x14 do x14 + crossesDownline[index14];
def crossmade0_lookback = fold index33 = 1 to cloudlinelookback with x33 do x33 + crossmade0[index33];
#####################################################
##                                                 ##
#####################################################
def bubblehidertopdown = topBandStepDown_lookback > PreviousStepsDown;
def bubblehidertopup = topBandStepup_lookback > PreviousStepsUp;
def bubblehiderbottomdown = bottomBandStepDown_lookback > PreviousStepsDown;
def bubblehiderbottomup = bottomBandStepup_lookback > PreviousStepsUp;
def UP1 = if marketopen and (!bubblehidertopdown or !bubblehiderbottomdown) and !EmaPushStepDown and crossmademorelogicup then midband else double.nan;
def DOWN1 = if marketopen and (!bubblehidertopup or !bubblehiderbottomup) and !EmaPushStepUp and crossmademorelogicdown then midband else double.nan;
def made1 = made4;
def made = made3;
def madecolor_1 = if crossmade2 == 1 then made3 else double.nan;
def madecolor_2 = if crossmade2 == -1 then made3 else double.nan;
def madecolor_3 = if crossmade0_lookback > 1 and crosslinelogic then made3 else double.nan;
def madecolor = if crossmade2 == 0 then made3 else double.nan;
def madecolor1_1 = if crossmade2 == 1 then made4 else double.nan;
def madecolor1_2 = if crossmade2 == -1 then made4 else double.nan;
def madecolor1_3 = if crossmade0_lookback > 1 and crosslinelogic then made4 else double.nan;
def madecolor1 = if  crossmade2 == 0 then made4 else double.nan;
def UP_Alert4_1 = if (alert4) then midband else double.nan;
def masterEMADLinePlot2_1 = (upperEMADLine + lowerEMADLine) / 2;
def masterEMADLinePlot2_Up = if (masterEMADLinePlot2_1 >= masterEMADLinePlot2_1[1]) then masterEMADLinePlot2_1 else double.nan;
def masterEMADLinePlot2_Down = if (masterEMADLinePlot2_1 < masterEMADLinePlot2_1[1]) then masterEMADLinePlot2_1 else double.nan;
def TopBottomSpan = absvalue(topband) + absvalue(bottomband);
def TopBottomOffset_2 = absvalue(topband)/5;
#####################################################
##                                                 ##
#####################################################
plot zeroLine = if (upperEMADLine > zerolinedata) then zerolinedata else double.nan;
plot zeroLine_2 = if (upperEMADLine < zerolinedata) then zerolinedata else double.nan;
plot zeroLine_3 = if (!zeroline and !zeroline_2) then zerolinedata else double.nan;
plot Squeeze_Alert = if sqzLevel then (upperEMADLine + lowerEMADLine) / 2 else (upperEMADLine + lowerEMADLine) / 2 ;
plot ExtremeBuy = if showTripleExh and sellerExtreme[1] and !sellerExtreme then midband else Double.NaN;
plot ExtremeSell = if showTripleExh and buyerExtreme[1] and !buyerExtreme then midband else Double.NaN;
plot RegularSell = if showTripleExh and buyerRegular[1] and !buyerRegular then midband else Double.NaN;
plot RegularBuy = if showTripleExh and sellerRegular[1] and !sellerRegular then midband else Double.NaN;
plot topBandPlot_offset = topBand +  TopBottomOffset_2;
plot topBandPlot = if !bothBandsDown and !bothBandsUp and !topBandStepUp and !topBandStepDown then topBand else double.nan;
plot topBandPlot_1 = if bothBandsDown then topBand else double.nan;
plot topBandPlot_2 = if bothBandsup then topBand else double.nan;
plot topBandPlot_3 = if topBandStepUp then topBand else double.nan;
plot topBandPlot_4 = if topBandStepDown then topBand else double.nan;
plot topBandPlot_5 = if bearbias then topBand else double.nan;
plot bottomBandPlot_offset = bottomBand - TopBottomOffset_2;
plot bottomBandPlot = if (!bothBandsDown and !bothBandsUp and !bottomBandStepUp and !bottomBandStepDown) or (bottomband == bottomband[1]) then bottomBand else double.nan;
plot bottomBandPlot_1 = if bothBandsDown then bottomBand else double.nan;
plot bottomBandPlot_2 = if bothBandsUp then bottomBand else double.nan;
plot bottomBandPlot_3 = if bottomBandStepUp then bottomBand else double.nan;
plot bottomBandPlot_4 = if bottomBandStepDown then bottomBand else double.nan;
plot bottomBandPlot_5 = if bullbias then bottomBand else double.nan;
plot upperEMADLinePlot_3 = upperEMADLine;
plot lowerEMADLinePlot_3 = lowerEMADline;
plot masterEMADLinePlot =  masterEMADLinePlot2_1;
plot masterEMADLinePlot_Up =  masterEMADLinePlot2_Up;
plot masterEMADLinePlot_Down =  masterEMADLinePlot2_Down;
plot made55 = made5;
plot made1_1 = madecolor_1;
plot made1_2 = madecolor_2;
plot made1_3 = madecolor_3;
plot made1_0 = madecolor;
plot made_1 = madecolor1_1;
plot made_2 = madecolor1_2;
plot made_3 = madecolor1_3;
plot made_0 = madecolor1;
plot UP_Alert4 = UP_Alert4_1;
plot UP = UP1;
plot DOWN = DOWN1;
#####################################################
##                                                 ##
#####################################################
UP_Alert4.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UP_Alert4.SetLineWeight(1);
UP_Alert4.SetDefaultColor(Color.cyan);
UP_Alert4.HideBubble();
UP_Alert4.HideTitle();
#####################################################
##                                                 ##
#####################################################
UP.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UP.SetLineWeight(1);
UP.SetDefaultColor(Color.white);
UP.HideBubble();
UP.HideTitle();
#####################################################
##                                                 ##
#####################################################
DOWN.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DOWN.SetLineWeight(1);
DOWN.SetDefaultColor(Color.white);
DOWN.HideBubble();
DOWN.HideTitle();
#####################################################
##                                                 ##
#####################################################
made55.SetDefaultColor(color.gray);
#####################################################
##                                                 ##
#####################################################
made1_1.SetDefaultColor(color.dark_green);
#####################################################
##                                                 ##
#####################################################
made1_2.SetDefaultColor(color.dark_red);
#####################################################
##                                                 ##
#####################################################
made1_3.SetDefaultColor(color.gray);
#####################################################
##                                                 ##
#####################################################
made1_0.SetDefaultColor(color.dark_green);
#####################################################
##                                                 ##
#####################################################
made_1.SetDefaultColor(color.dark_green);
#####################################################
##                                                 ##
#####################################################
made_2.SetDefaultColor(color.dark_red);
#####################################################
##                                                 ##
#####################################################
made_3.SetDefaultColor(color.gray);
#####################################################
##                                                 ##
#####################################################
made_0.SetDefaultColor(color.dark_red);
#####################################################
##                                                 ##
#####################################################
Squeeze_Alert.SetPaintingStrategy(PaintingStrategy.line);
Squeeze_Alert.SetLineWeight(3);
Squeeze_Alert.SetDefaultColor(Color.YELLOW);
#####################################################
##                                                 ##
#####################################################
RegularBuy.SetPaintingStrategy(PaintingStrategy.Histogram);
ExtremeBuy.SetPaintingStrategy(PaintingStrategy.Histogram);
RegularSell.SetPaintingStrategy(PaintingStrategy.Histogram);
ExtremeSell.SetPaintingStrategy(PaintingStrategy.Histogram);
#####################################################
##                                                 ##
#####################################################
RegularBuy.SetLineWeight((1));
ExtremeBuy.SetLineWeight((1));
RegularSell.SetLineWeight((1));
ExtremeSell.SetLineWeight((1));
#####################################################
##                                                 ##
#####################################################
ExtremeSell.SetDefaultColor((Color.red));
ExtremeBuy.SetDefaultColor((Color.green));
RegularBuy.SetDefaultColor((Color.green));
RegularSell.SetDefaultColor((Color.red));
#####################################################
##                                                 ##
#####################################################
upperEMADLinePlot_3.HideTitle();
upperEMADLinePlot_3.HideBubble();
upperEMADLinePlot_3.SetLineWeight((1));
upperEMADLinePlot_3.SetDefaultColor(color.dark_gray
);
lowerEMADLinePlot_3.HideTitle();
lowerEMADLinePlot_3.HideBubble();
lowerEMADLinePlot_3.SetLineWeight((1));
lowerEMADLinePlot_3.SetDefaultColor(color.dark_gray
);
masterEMADLinePlot_Up.HideTitle();
masterEMADLinePlot_Up.HideBubble();
masterEMADLinePlot_Up.SetLineWeight((2));
masterEMADLinePlot_Up.SetDefaultColor(color.green
);
masterEMADLinePlot_Down.HideTitle();
masterEMADLinePlot_Down.HideBubble();
masterEMADLinePlot_Down.SetLineWeight((1));
masterEMADLinePlot_Down.SetDefaultColor(color.red
);
masterEMADLinePlot.HideTitle();
masterEMADLinePlot.HideBubble();
masterEMADLinePlot.SetLineWeight((1));
masterEMADLinePlot.SetDefaultColor(color.gray
);
zeroLine.HideTitle();
zeroLine.HideBubble();
zeroLine.SetDefaultColor(Color.green
);
zeroLine_2.HideTitle();
zeroLine_2.HideBubble();
zeroLine_2.SetDefaultColor(color.red
);
zeroLine_3.HideTitle();
zeroLine_3.HideBubble();
zeroLine_3.SetDefaultColor(color.gray
);
topBandPlot_offset.SetPaintingStrategy(PaintingStrategy.liNE_VS_POINTS);
topBandPlot_offset.HideTitle();
topBandPlot_offset.HideBubble();
topBandPlot_offset.SetDefaultColor(Color.black
);
topBandPlot.HideTitle();
topBandPlot.HideBubble();
topBandPlot.SetDefaultColor(Color.gray
);
topBandPlot_1.HideTitle();
topBandPlot_1.HideBubble();
topBandPlot_1.SetdefaultColor(Color.red
);
topBandPlot_2.HideTitle();
topBandPlot_2.HideBubble();
topBandPlot_2.SetDefaultColor(Color.green
);
topBandPlot_3.HideTitle();
topBandPlot_3.HideBubble();
topBandPlot_3.SetDefaultColor(Color.green
);
topBandPlot_4.HideTitle();
topBandPlot_4.HideBubble();
topBandPlot_4.SetDefaultColor(Color.red
);
topBandPlot_5.HideTitle();
topBandPlot_5.HideBubble();
topBandPlot_5.SetDefaultColor(Color.yellow
);
BottomBandPlot_offset.SetPaintingStrategy(PaintingStrategy.liNE_VS_POINTS);
BottomBandPlot_offset.HideTitle();
BottomBandPlot_offset.HideBubble();
BottomBandPlot_offset.SetDefaultColor(Color.black
);
BottomBandPlot.HideTitle();
BottomBandPlot.HideBubble();
BottomBandPlot.SetDefaultColor(Color.gray
);
BottomBandPlot_1.HideTitle();
BottomBandPlot_1.HideBubble();
BottomBandPlot_1.SetDefaultColor(Color.red
);
BottomBandPlot_2.HideTitle();
BottomBandPlot_2.HideBubble();
BottomBandPlot_2.SetDefaultColor(Color.green
);
BottomBandPlot_3.HideTitle();
BottomBandPlot_3.HideBubble();
BottomBandPlot_3.SetDefaultColor(Color.green
);
BottomBandPlot_4.HideTitle();
BottomBandPlot_4.HideBubble();
BottomBandPlot_4.SetDefaultColor(Color.red
);
BottomBandPlot_5.HideTitle();
BottomBandPlot_5.HideBubble();
BottomBandPlot_5.SetDefaultColor(Color.yellow
);
#####################################################
##                                                 ##
#####################################################
AddVerticalLine(showVerticalline and crossesUpline and crossesUpline_filter, "", GlobalColor("Green"), no);
AddVerticalLine(showVerticalline and crossesDownline and crossesDownline_filter,"" , GlobalColor("Red"), yes);
Mind if I ask you which study you're using in the upper (The chart) on the mobile screenshot? Huge thanks for all your work
 
C3_BIG_MAD & EMAD

Chart Style: http://tos.mx/akTHv1k (updated the link and code found an issue)

No life altering changes yet... Just cleaned up the code and removed un-used code and made a few changes.

The new C3_BIG_MAD now includes the arrows for LH HL LH2 HL2 and UP and DOWN (the white arrows) from the EMAD lower study. Also the Spark, Big4, and TS_V9 arrows are separate (whereas I had combined them into a single indication previously... which I am still working on something of that nature).

Code:
   ##########################################################################################
   ##########################################################################################
   ###                                                                                    ###
   ###       #############################           #########               #########    ###
   ###     #################################         #########               #########    ###  
   ###    ####################################       #########               #########    ###
   ###   ########                     #########      #########               #########    ###      
   ###   ########    EMAD Variant     #########      #########               #########    ###
   ###   ########   @Christopher84    #########      #########               #########    ###    
   ###   ########       @HODL         #########      #########               #########    ###  
   ###   ########                     #########      #########               #########    ###      
   ###   ######################################      ##################################   ###    
   ###    ####################################       ##################################   ###
   ###     #################################         ##################################   ###
   ###    ####################################       ##################################   ###  
   ###   ########                     #########                              #########    ###
   ###   ########     @Horserider     #########                              #########    ###
   ###   ########  HORSERIDER VOLUME  #########                              #########    ###          
   ###   ########  TRIPLE EXHAUSTION  #########                              #########    ###      
   ###   ########      @chence27      #########                              #########    ###          
   ###   ########                     #########                              #########    ###      
   ###    ####################################                               #########    ###    
   ###     ##################################                                #########    ###      
   ###      ###############################                                  #########    ###    
   ###                                                                                    ###
   ##########################################################################################
   ##########################################################################################

declare lower;

########################################################
## INPUTS                                             ##
########################################################
input Color_Candles = no;
input showbubbles = no;
input x = 5;
input Show_EMA_Cloud = yes;
input Show_HL_LH = no;
input Show_TripleExh_Arrows = no;
input Band_Length = 100;
input Smooth_Length = 12;
input Smooth_Length_2 = 14;
input Ehlers_Length = 34;
Input Length_MAD = 4;
input Cloud_Lookback = 20;
input HL_LH_Lookback = 20;
input Previous_Steps_Down = 2; #hint filter for hiding arrows
input Previous_Steps_Up = 2; #hint filter for hiding arrows
input TradeDaytimeOnly = yes; #hint tradeDaytimeOnly: (IntraDay Only) Only perform trades during hours stated
input OpenTime = 0730; #hint OpenTime: Opening time of market hides arrows
input CloseTime = 1800; #hint CloseTime: Closing time of market hides arrows
input FirstTrade_Sq = {default long, short};
input TrailType = {default modified, unmodified};
input FirstTrade = {default long, short};
########################################################
## 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));
########################################################
##                                                    ##
########################################################
def bn = barnumber();
def Begin = SecondsFromTime(OpenTime);
def End = SecondsTillTime(CloseTime);
def isIntraDay = if GetAggregationPeriod() > 14400000 or GetAggregationPeriod() == 0 then 0 else 1;
def MarketOpen = if !tradeDaytimeOnly or !isIntraDay then 1 else if tradeDaytimeOnly and isIntraDay and Begin > 0 and End > 0 then 1 else 0;
########################################################
##                                                    ##
########################################################
def Fast_Length = 10;
def Slow_Length = 35;
def Made_Avg_Length = 3;
def ATR_Period = 5;
def ATR_Factor = 2.0;
def Length_3x = 1000;
def AverageType = AverageType.WILDERS;
def AverageType_Sq = AverageType.SIMPLE;
def EMAD_EMA_lineweight = 2;
def Step_lookback = 10;
def FastExpAvg = ExpAverage(close, fast_Length);
def SlowExpAvg = ExpAverage(close, slow_Length);
def EMAD_Fast = (close - fastExpAvg);
def EMAD_Slow = (close - slowExpAvg);
def EMAD_Avg = (EMAD_Fast + EMAD_Slow) / 2;
#####################################################
## UPPER EMA                                       ##
#####################################################
def Upper_EMAD_EMA = ExpAverage(EMAD_Avg, smooth_Length);
def EMAD_Open = (Upper_EMAD_EMA + Upper_EMAD_EMA[1]) / 2;
def EMAD_High = Max(Upper_EMAD_EMA, Upper_EMAD_EMA[1]);
def EMAD_Low = Min(Upper_EMAD_EMA, Upper_EMAD_EMA[1]);
def EMAD_Close = Upper_EMAD_EMA;
def Bottom = Min(EMAD_Close[1], EMAD_Low);
def TR = TrueRange(EMAD_High, EMAD_Close, EMAD_Low);
def PTR = tr / (bottom + tr / 2);
def APTR = MovingAverage(averageType, ptr, smooth_Length_2);
def UpperBand = EMAD_Close[1] + (APTR * EMAD_Open);
def LowerBand = EMAD_Close[1] - (APTR * EMAD_Open);
#####################################################
## LOWER EMA                                       ##
#####################################################
def Lower_EMAD_EMA = (upperBand + lowerBand) / 2;
#####################################################
## TOP BAND                                        ##
#####################################################
def TopBand = Highest(Lower_EMAD_EMA, band_Length);
#####################################################
## BOTTOM BAND                                     ##
#####################################################
def BottomBand = Lowest(Lower_EMAD_EMA, band_Length);
#####################################################
## ZERO LINE                                       ##
#####################################################
def ZeroLineData = if IsNaN(close) then Double.NaN else 0;
def EMAD_Above_Zero = Upper_EMAD_EMA > zeroLineData;
def EMAD_Below_Zero = Upper_EMAD_EMA < zeroLineData;
def EMAD_EMA_down = (Lower_EMAD_EMA > Upper_EMAD_EMA);
def EMAD_EMA_up = (Upper_EMAD_EMA >= Lower_EMAD_EMA);
#####################################################
## MASTER EMA                                      ##
#####################################################
def Master_EMAD_EMA = (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2;
def TopBand_StepDown = if topBand < topBand[1] then 1 else 0;
def TopBand_StepUp = if topBand > topBand[1] then 1 else 0;
def BottomBand_StepDown = if bottomBand < bottomBand[1] then 1 else 0;
def BottomBand_StepUp = if bottomBand > bottomBand[1] then 1 else 0;
def BothBands_Down = bottomBand_StepDown and TopBand_StepDown;
def BothBands_Up = bottomBand_StepUp and TopBand_StepUp;
def BottomBand_Above_Zero = (bottomBand > zeroLineData);
def TopBand_below_Zero = (topBand < zeroLineData);
def TopBand_PushUp =  Master_EMAD_EMA >= TopBand;
def TopBand_PushUp_2 = TopBand_PushUp within x bars;
def BottomBand_PushDown = Master_EMAD_EMA <= bottomBand;
def BottomBand_PushDown_2 = BottomBand_PushDown within x bars;
def MidBand = (upperBand + lowerBand) / 2;
def EMA_CrossUp = if (midBand[1] > Upper_EMAD_EMA[1])and(midBand < Upper_EMAD_EMA) then 1 else 0;
def EMA_CrossDown = if (Upper_EMAD_EMA[1] > midBand[1])and(Upper_EMAD_EMA < midBand) then 1 else 0;
def Value_At_CrossUp = if EMA_CrossUp then midBand else Value_At_CrossUp[1];
def Value_At_CrossDown = if EMA_CrossDown then midBand else Value_At_CrossDown[1];
def CrossUp_BottomBand = if (Value_At_CrossUp - bottomBand) == 0 then 1 else 0;
def CrossDown_TopBand = if (Value_At_CrossDown - topBand) == 0 then 1 else 0;
def CrossUp_Zeroline = if (Value_At_CrossUp) == 0 then 1 else 0;
def CrossDown_Zeroline = if (Value_At_CrossDown) == 0 then 1 else 0;

AddChartBubble(showBubbles and CrossUp_Zeroline, midBand, " XUp @ 0 ", GlobalColor("Green"), no);
AddChartBubble(showBubbles and CrossDown_Zeroline, midBand," XDown @ 0 " , GlobalColor("Red"), yes);

def CrossUp_BottomBand_bn = if CrossUp_Bottomband and !CrossUp_Bottomband[1] then bn else CrossUp_Bottomband_bn[1];
def CrossDown_TopBand_bn = if CrossDown_topband and !CrossDown_topband[1] then bn else CrossDown_TopBand_bn[1];
def CrossUp_BottomBand_Within = if CrossUp_Bottomband_bn < bn then bn - CrossUp_Bottomband_bn else CrossUp_BottomBand_Within[1];
def CrossDown_TopBand_Within = if CrossDown_TopBand_bn < bn then bn - CrossDown_TopBand_bn else CrossDown_TopBand_Within[1];
def Band_Crossed_First = CrossUp_Bottomband_bn > CrossDown_TopBand_bn;
#####################################################
## HIGHER LOW - LOWER HIGH                         ##
#####################################################
def HL = CrossUp_Bottomband == 0 and CrossDown_topband == 0 and Upper_EMAD_EMA crosses above Lower_EMAD_EMA;  #Normal HL LH condition
def LH = CrossDown_topband == 0 and CrossUp_Bottomband == 0 and Upper_EMAD_EMA crosses below Lower_EMAD_EMA; #Normal HL LH condition
def bounceUpOnce = CrossUp_Bottomband <> CrossUp_Bottomband[1]; #XUpBtm happened previous bar or on current bar (<> = is NOT equal to)
def bounceDnOnce = CrossDown_topband <> CrossDown_topband[1];
def trackCrUp = if bounceUpOnce then 0 else if HL then 1 else trackCrUp[1];
def trackCrDn = if bounceDnOnce then 0 else if LH then 1 else trackCrDn[1];
def HLalready = if trackCrUp and !trackCrUp[1] then 1 else 0;
def LHalready = if trackCrDn and !trackCrDn[1] then 1 else 0;
def HL2 = HLalready == 0 and Upper_EMAD_EMA crosses above Lower_EMAD_EMA;
def LH2 = LHalready == 0 and Upper_EMAD_EMA crosses below Lower_EMAD_EMA;
def crossedUpOnce = HLalready <> HLalready[1];
def crossedDnOnce = LHalready <> LHalready[1];
def trackCrUp2 = if crossedUpOnce then 0 else if HL2 then 1 else trackCrUp2[1];
def trackCrDn2 = if crossedDnOnce then 0 else if LH2 then 1 else trackCrDn2[1];
#####################################################
## SQUEEZE                                         ##
#####################################################
def HiLo = Min(high - low, 1.5 * Average(high - low, ATR_Period));
def HRef = if low <= high[1]
    then high - close[1]
    else (high - close[1]) - 0.5 * (low - high[1]);
def LRef = if high >= low[1]
    then close[1] - low
    else (close[1] - low) - 0.5 * (low[1] - high);
def trueRange_Sq;
switch (trailType) {
case modified:
    trueRange_Sq = Max(HiLo, Max(HRef, LRef));
case unmodified:
    trueRange_Sq = TrueRange(high, close, low);
}
def loss_Sq = ATR_Factor * MovingAverage(averageType_Sq, trueRange_Sq, ATR_Period);
def state_Sq = {default init, long, short};
def trail_Sq;
switch (state_Sq[1]) {
case init:
    if (!IsNaN(loss_Sq)) {
        switch (firstTrade) {
        case long:
            state_Sq = state_Sq.long;
            trail_Sq =  close - loss_Sq;
        case short:
            state_Sq = state_Sq.short;
            trail_Sq = close + loss_Sq;
    }
    } else {
        state_Sq = state_Sq.init;
        trail_Sq = Double.NaN;
    }
case long:
    if (close > trail_Sq[1]) {
        state_Sq = state_Sq.long;
        trail_Sq = Max(trail_Sq[1], close - loss_Sq);
    } else {
        state_Sq = state_Sq.short;
        trail_Sq = close + loss_Sq;
    }
case short:
    if (close < trail_Sq[1]) {
        state_Sq = state_Sq.short;
        trail_Sq = Min(trail_Sq[1], close + loss_Sq);
    } else {
        state_Sq = state_Sq.long;
        trail_Sq =  close - loss_Sq;
    }
}
def TrailingStop_Sq = trail_Sq;
def H = Highest(TrailingStop_Sq, 12);
def L = Lowest(TrailingStop_Sq, 12);
def BandwidthC3 = (H - L);
def IntermResistance2 = Highest(BandwidthC3, Band_Length);
def IntermSupport2 = Lowest(BandwidthC3, Band_Length);
def Squeeze_Trigger = BandwidthC3 <= IntermSupport2;
def Squeeze_Level = if !Squeeze_Trigger[1] and Squeeze_Trigger then hl2 else if !Squeeze_Trigger then Double.NaN else Squeeze_Level[1];
def Squeeze_Alert_1 = if Squeeze_Level then (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2 else (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2 ;
#####################################################
## TRIPLE EXHAUSTION                               ##
#####################################################
def OB_3x = 80;
def OS_3x = 20;
def KPeriod_3x = 10;
def DPeriod_3x = 10;
def SlowK_3x = reference StochasticFull(OB_3x,  OS_3x,  KPeriod_3x,  DPeriod_3x,  EMAD_High, EMAD_Low, EMAD_Close,  3, if (averageType == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;
def MACD_3x = reference MACD()."Value";
def Price_Avg_3x = Average(MACD_3x, length_3x);
def MACD_stdev_3x =  (MACD_3x - Price_Avg_3x) / StDev(MACD_3x, length_3x);
def DPlus_3x = reference DMI()."DI+";
def DMinus_3x = reference DMI()."DI-";
def SellerRegular = SlowK_3x < 20 and MACD_stdev_3x < -1 and dPlus_3x < 15;
def SellerExtreme = SlowK_3x < 20 and MACD_stdev_3x < -2 and dPlus_3x < 15;
def BuyerRegular = SlowK_3x > 80 and MACD_stdev_3x > 1 and dMinus_3x < 15;
def BuyerExtreme = SlowK_3x > 80 and MACD_stdev_3x > 2 and dMinus_3x < 15;  
def Extreme_Buy = if sellerExtreme[1] and !sellerExtreme then 1 else 0;
def Extreme_Sell = if buyerExtreme[1] and !buyerExtreme then 1 else 0;
def Regular_Sell = if buyerRegular[1] and !buyerRegular then 1 else 0;
def Regular_Buy = if sellerRegular[1] and !sellerRegular then 1 else 0;
#####################################################
## EHLERS                                          ##
#####################################################
def Price_Ehlers = (EMAD_High + EMAD_Low) / 2;
def Coeff = Ehlers_length * Price_Ehlers * Price_Ehlers - 2 * Price_Ehlers * sum(Price_Ehlers, Ehlers_length)[1] + sum(Price_Ehlers * Price_Ehlers, Ehlers_length)[1];
def Ehlers =  sum(coeff * Price_Ehlers, Ehlers_length) / sum(coeff, Ehlers_length);
#####################################################
## MADE AVERAGE                                    ##
#####################################################
def dataA = Master_EMAD_EMA;
def dataA1 = fold aD = 0 to Length_MAD with k = 0 do k + dataA[aD];
def MAEMAD = dataA1 / LengtH_MAD;
def MADehlers = (MAEMAD - Ehlers)/2;
def Madebad = madehlers + ehlers;
def Madebad_Avg = average(madebad, Made_Avg_Length);
def Madebad_CrossUp = if (madebad < Master_EMAD_EMA) and (madebad > madebad[1]) and (EMAD_Above_Zero > 0) then 1 else 0;
def Madebad_CrossDown = if (madebad > Master_EMAD_EMA) and (madebad < madebad[1]) and (EMAD_Below_Zero > 0) then 1 else 0;
def Madebad_CrossCondition = if (madebad < Master_EMAD_EMA) and (madebad > madebad[1]) and (EMAD_Above_Zero > 0) then 1
                        else if (madebad > Master_EMAD_EMA) and (madebad < madebad[1]) and (EMAD_Below_Zero > 0) then -1 else 0;
def Madebad_Condition = if (!Madebad_CrossCondition[1] or !Madebad_CrossCondition[2]) and (Madebad_CrossCondition == 0) then 1 else 0;
#####################################################
## ALERT4                                          ##
#####################################################
def PD = 22;
def BBL = 20;
def MULT = 2.0;
def LB = 50;
def PH = 0.85;
def LTLB = 40;
def MTLB = 14;
def STR = 3;
def WVF = ((Highest(close, pd) - low) / (Highest(close, pd))) * 100;
def StDev = mult * StDev(wvf, bbl);
def MidLine1 = SimpleMovingAvg(wvf, bbl);
def UpperBand1_Alert4 = midLine1 + StDev;
def Range_High = (Highest(wvf, lb)) * ph;
def UpRange = low > low[1] and close > high[1];
def UpRange_Aggr = close > close[1] and close > open[1];
def Filtered = ((wvf[1] >= UpperBand1_Alert4[1] or wvf[1] >= Range_High[1]) and (wvf < UpperBand1_Alert4 and wvf < Range_High));
def Filtered_Aggr = (wvf[1] >= UpperBand1_Alert4[1] or wvf[1] >= Range_High[1]);
def Alert4 = upRange_Aggr and close > close[str] and (close < close[ltLB] or close < close[mtLB]) and filtered_Aggr;
def Alert4close = if alert4 then close else 0;
def Made_Down = (madebad > Master_EMAD_EMA);
def Made_Up = (madebad < Master_EMAD_EMA);
def Made_Bottom_MA = if !Made_Down and (Madebad_Avg <= zerolinedata) then Madebad_Avg else double.nan;
def Made_Top_MA = if !Made_Up and (Madebad_Avg >= zerolinedata) then Madebad_Avg else double.nan;
def MadeAvg_Master_Cross_Up = if Madebad_Avg crosses below Master_EMAD_EMA then 1 else 0;
def MadeAvg_Master_Cross_Down = if Madebad_Avg crosses above Master_EMAD_EMA then 1 else 0;
def Cross_Up_MadeAvg_Master = if MadeAvg_Master_Cross_Up and !MadeAvg_Master_Cross_Up[1] then 1 else 0;
def Cross_Down_MadeAvg_Master = if MadeAvg_Master_Cross_Down and !MadeAvg_Master_Cross_Down[1] then 1 else 0;
def MadeAvg_Master_Up_Condition = if Cross_Up_MadeAvg_Master and Band_Crossed_First and EMAD_EMA_Up[1] then 1 else 0;
def MadeAvg_Master_Down_Condition = if Cross_Down_MadeAvg_Master and !Band_Crossed_First and EMAD_EMA_down[1] then 1 else 0;
def EMA_Push_StepUp = if topBand_StepUp and EMAD_EMA_Up and (((Upper_EMAD_EMA) - topband) == 0) then 1 else 0;;
def EMA_Push_StepDown = if bottomBand_StepDown and EMAD_EMA_down and (((Lower_EMAD_EMA) - bottomband) == 0) then 1 else 0;
#####################################################
## MARKET PHASES                                   ##
#####################################################
def Fastavg_Phases = 50;
def Slowavg_Phases = 200;
def Fastsma_Phases = Average( close, Fastavg_Phases);
def Slowsma_Phases = Average(close, Slowavg_Phases);
def Bullphase = fastsma_Phases > slowsma_Phases && close > fastsma_Phases && close > slowsma_Phases; # Accumulation Phase : close > 50 SMA, close > 200 SMA, 50 SMA < 200 SMA
def Accphase = fastsma_Phases < slowsma_Phases && close > fastsma_Phases && close > slowsma_Phases;# Recovery Phase : close > 50 SMA, close < 200 SMA, 50 SMA < 200 SMA
def Recphase = fastsma_Phases < slowsma_Phases && close < slowsma_Phases && close > fastsma_Phases;# Bearish Phase : close < 50 SMA, close < 200 SMA, 50 SMA < 200 SMA
def Bearphase = fastsma_Phases < slowsma_Phases && close < fastsma_Phases && close < slowsma_Phases;# Distribution Phase : close < 50 SMA, close < 200 SMA, 50 SMA > 200 SMA
def Distphase = fastsma_Phases > slowsma_Phases && close < fastsma_Phases && close < slowsma_Phases;# Warning Phase : close < 50 SMA, close > 200 SMA, 50 SMA > 200 SMA
def Warnphase = fastsma_Phases> slowsma_Phases && close > slowsma_Phases && close < fastsma_Phases;
#####################################################
##                                                 ##
#####################################################
def FirstHL2 = if trackCrUp and !trackCrUp[1] then 1 else 0;
def FirstLH2 = if trackCrDn and !trackCrDn[1] then 1 else 0;
def HLBuy = if firstHL2 and Regular_Sell or Extreme_Sell then 1 else 0;
def LHSell = if firstHL2 and Regular_Buy or Extreme_Buy then 1 else 0;
def LHsellnear = LHsell within x bars;
def UP2_cond = if (bottomBand_Stepup and alert4) or (CrossUp_bottomband and alert4) or (alert4 and LHsellnear) then 1 else 0;
def First_HL_1 = if Marketopen and Show_HL_LH and trackCrUp and !trackCrUp[1] then Lower_EMAD_EMA else double.nan;
def First_LH_1 = if Marketopen and Show_HL_LH and trackCrDn and !trackCrDn[1] then Lower_EMAD_EMA else double.nan;
def Second_HL_1 = if Marketopen and Show_HL_LH and trackCrUp2 and !trackCrUp2[1] and !CrossUp_Bottomband then Lower_EMAD_EMA else double.nan;
def Second_LH_1 = if Marketopen and Show_HL_LH and trackCrDn2 and !trackCrDn2[1] and !CrossDown_Topband then Lower_EMAD_EMA else double.nan;
#####################################################
## FOLD                                            ##
#####################################################
def TopBandStepDown_lookback = fold index1 = 1 to Step_lookback with x1 do x1 + topBand_StepDown[index1];
def TopBandStepUp_lookback = fold index2 = 1 to Step_lookback with x2 do x2 + topBand_StepUp[index2];
def BottomBandStepDown_lookback = fold index3 = 1 to Step_lookback with x3 do x3 + bottomBand_StepDown[index3];
def BottomBandStepUp_lookback = fold index4 = 1 to Step_lookback with x4 do x4 + bottomBand_StepUp[index4];
def Madebad_Condition_lookback = fold index5 = 1 to Cloud_lookback with x5 do x5 + Madebad_Condition[index5];
def HL_lookback = fold index6 = 1 to HL_LH_Lookback with x6 do x6 + First_HL_1[index6];
def LH_lookback = fold index7 = 1 to HL_LH_Lookback with x7 do x7 + First_LH_1[index7];
def HL2_lookback = fold index8 = 1 to HL_LH_Lookback with x8 do x8 + Second_HL_1[index8];
def LH2_lookback = fold index9 = 1 to HL_LH_Lookback with x9 do x9 + Second_LH_1[index9];
#####################################################
##                                                 ##
#####################################################
def ArrowFilter_TopBand_StepDown = topBandStepDown_lookback > Previous_Steps_Down;
def ArrowFilter_TopBand_StepUp = topBandStepup_lookback > Previous_Steps_Up;
def ArrowFilter_BottomBand_StepDown = bottomBandStepDown_lookback > Previous_Steps_Down;
def ArrowFilter_BottomBand_StepUp = bottomBandStepup_lookback > Previous_Steps_Up;
def UP_1 = if marketopen and (!ArrowFilter_TopBand_StepDown or !ArrowFilter_BottomBand_StepDown) and !EMA_Push_StepDown and MadeAvg_Master_Up_Condition then midband else double.nan;
def DOWN_1 = if marketopen and (!ArrowFilter_TopBand_StepUp or !ArrowFilter_BottomBand_StepUp) and !EMA_Push_StepUp and MadeAvg_Master_Down_Condition then midband else double.nan;
def TopBottomOffset_2 = absvalue(topband)/5;
#####################################################
## PLOTS                                           ##
#####################################################
plot ZeroLine = zerolinedata;
plot Squeeze_Alert = if Squeeze_Level then (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2 else (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2 ;
plot ExtremeBuy = if Show_TripleExh_Arrows and sellerExtreme[1] and !sellerExtreme then midband else Double.NaN;
plot ExtremeSell = if Show_TripleExh_Arrows and buyerExtreme[1] and !buyerExtreme then midband else Double.NaN;
plot RegularSell = if Show_TripleExh_Arrows and buyerRegular[1] and !buyerRegular then midband else Double.NaN;
plot RegularBuy = if Show_TripleExh_Arrows and sellerRegular[1] and !sellerRegular then midband else Double.NaN;
plot TopBandPlot_offset = topBand + TopBottomOffset_2;
plot BottomBandPlot_offset = bottomBand - TopBottomOffset_2;
plot TopBandPlot = topBand;
plot BottomBandPlot = bottomBand;
plot UpperEMADLinePlot = Upper_EMAD_EMA;
plot LowerEMADLinePlot = Lower_EMAD_EMA;
plot MasterEMADLinePlot_2 = (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2;
plot MasterEMADLinePlot = ((upperEMADLinePlot + lowerEMADLinePlot) / 2);
plot Made_1 = made_top_ma;
plot Made = made_bottom_ma;
plot UP = UP_1;
plot DOWN = DOWN_1;
plot First_HL = if Marketopen and Show_HL_LH and trackCrUp and !trackCrUp[1] then Lower_EMAD_EMA else double.nan;
plot First_LH = if Marketopen and Show_HL_LH and trackCrDn and !trackCrDn[1] then Lower_EMAD_EMA else double.nan;
plot Second_HL = if Marketopen and Show_HL_LH and trackCrUp2 and !trackCrUp2[1] and !CrossUp_Bottomband then Lower_EMAD_EMA else double.nan;
plot Second_LH = if Marketopen and Show_HL_LH and trackCrDn2 and !trackCrDn2[1] and !CrossDown_Topband then Lower_EMAD_EMA else double.nan;
plot UP_2 = if UP2_cond and !UP2_Cond[1] then midband else double.nan;;
#####################################################
##                                                 ##
#####################################################
UP_2.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UP_2.SetLineWeight(1);
UP_2.SetDefaultColor(Color.cyan);
UP_2.HideBubble();
UP_2.HideTitle();
#####################################################
##                                                 ##
#####################################################
UP.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UP.SetLineWeight(1);
UP.assignvaluecolor(if alert4 then color.yellow else Color.white);
UP.HideBubble();
UP.HideTitle();
#####################################################
##                                                 ##
#####################################################
DOWN.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DOWN.SetLineWeight(1);
DOWN.SetDefaultColor(Color.white);
DOWN.HideBubble();
DOWN.HideTitle();
#####################################################
##                                                 ##
#####################################################
First_HL.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
First_HL.SetLineWeight(1);
First_HL.SetDefaultColor(Color.magenta);
First_HL.HideBubble();
First_HL.HideTitle();
#####################################################
##                                                 ##
#####################################################
First_LH.SetPaintingStrategy(PaintingStrategy.ARROW_down);
First_LH.SetLineWeight(1);
First_LH.SetDefaultColor(Color.magenta);
First_LH.HideBubble();
First_LH.HideTitle();
#####################################################
##                                                 ##
#####################################################
Second_HL.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Second_HL.SetLineWeight(1);
Second_HL.SetDefaultColor(Color.orange);
Second_HL.HideBubble();
Second_HL.HideTitle();
#####################################################
##                                                 ##
#####################################################
Second_LH.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
Second_LH.SetLineWeight(1);
Second_LH.SetDefaultColor(Color.orange);
Second_LH.HideBubble();
Second_LH.HideTitle();
#####################################################
##                                                 ##
#####################################################
Made_1.AssignValueColor(
     if Madebad_CrossCondition == 1 then color.dark_green
else if Madebad_CrossCondition == -1 then color.dark_red
else if Madebad_Condition_lookback > 1 and Band_Crossed_First then Color.gray
else color.dark_red);
#####################################################
##                                                 ##
#####################################################
Made.AssignValueColor(
     if Madebad_CrossCondition == 1 then color.dark_green
else if Madebad_CrossCondition == -1 then color.dark_red
else if Madebad_Condition_lookback > 1 and Band_Crossed_First then Color.gray
else color.dark_red);
#####################################################
##                                                 ##
#####################################################
Squeeze_Alert.SetPaintingStrategy(PaintingStrategy.line);
Squeeze_Alert.SetLineWeight(2);
Squeeze_Alert.SetDefaultColor(Color.YELLOW);
#####################################################
##                                                 ##
#####################################################
TopBandPlot.HideTitle();
TopBandPlot.HideBubble();
TopBandPlot.AssignValueColor(
    if bothBands_Down then Color.dark_red
    else if bothBands_Up then color.Dark_green
    else if topBand_StepUp then Color.green
    else if topBand_StepDown then Color.red
    else if Topband_Below_Zero then  Color.yellow
    else if warnphase then color.orange
    else if recphase then Color.magenta
    else if bullphase then Color.light_green
    else if bearphase then Color.light_red
    else GlobalColor("Gray")
);
#####################################################
##                                                 ##
#####################################################
BottomBandPlot.HideTitle();
BottomBandPlot.HideBubble();
BottomBandPlot.AssignValueColor(
    if bothBands_Down then Color.dark_red
    else if bothBands_Up then color.Dark_green
    else if bottomBand_StepUp then Color.green
    else if bottomBand_StepDown then Color.red
    else if Bottomband_Above_Zero then Color.yellow
    else if warnphase then color.orange
    else if recphase then Color.magenta
    else if bullphase then Color.light_green
    else if bearphase then Color.light_red
    else  GlobalColor("Gray")
);
#####################################################
##                                                 ##
#####################################################
MasterEMADLinePlot.SetPaintingStrategy(PaintingStrategy.histogram);
MasterEMADLinePlot.HideBubble();
MasterEMADLinePlot.SetLineWeight(1);
MasterEMADLinePlot.AssignValueColor(
     if alert4 then color.cyan
else if HLBuy then color.light_green
else if LHSell then color.light_red
else if Madebad_CrossUp then Color.dark_green
else if Madebad_CrossDown then color.dark_red
else if Madebad_Condition_lookback > 1 and Band_Crossed_First then Color.dark_gray
else color.dark_red
);
#####################################################
##                                                 ##
#####################################################
RegularBuy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
ExtremeBuy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
RegularSell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ExtremeSell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#####################################################
##                                                 ##
#####################################################
RegularBuy.SetLineWeight((1));
ExtremeBuy.SetLineWeight((1));
RegularSell.SetLineWeight((1));
ExtremeSell.SetLineWeight((1));
#####################################################
##                                                 ##
#####################################################
ExtremeSell.SetDefaultColor((Color.WHITE));
ExtremeBuy.SetDefaultColor((Color.WHITE));
RegularBuy.SetDefaultColor((Color.white));
RegularSell.SetDefaultColor((Color.white));
#####################################################
##                                                 ##
#####################################################
UpperEMADLinePlot.HideTitle();
UpperEMADLinePlot.HideBubble();
UpperEMADLinePlot.AssignValueColor(
    if (lowerEMADLinePlot > upperEMADLinePlot) and (lowerEMADLinePlot[1] > upperEMADLinePlot[1]) then createcolor(153, 0, 0)
    else if (lowerEMADLinePlot < upperEMADLinePlot) and (lowerEMADLinePlot[1] < upperEMADLinePlot[1]) then createcolor(0, 167, 0)
    else GlobalColor("Gray")
);
#####################################################
##                                                 ##
#####################################################
LowerEMADLinePlot.HideTitle();
LowerEMADLinePlot.HideBubble();
LowerEMADLinePlot.AssignValueColor(
    if (lowerEMADLinePlot > upperEMADLinePlot) and (lowerEMADLinePlot[1] > upperEMADLinePlot[1]) then createcolor(153, 0, 0)
    else if (lowerEMADLinePlot < upperEMADLinePlot) and (lowerEMADLinePlot[1] < upperEMADLinePlot[1]) then createcolor(0, 167, 0)
    else GlobalColor("Gray")
);
#####################################################
##                                                 ##
#####################################################
MasterEMADLinePlot_2.HideTitle();
MasterEMADLinePlot_2.HideBubble();
MasterEMADLinePlot_2.SetLineWeight(EMAD_EMA_lineweight);
MasterEMADLinePlot_2.AssignValueColor(
    if masterEMADLinePlot > masterEMADLinePlot[2] then createColor(0, 186, 0)
    else if masterEMADLinePlot < masterEMADLinePlot[2] then createColor(174, 0, 0)
    else GlobalColor("Gray")
);
#####################################################
##                                                 ##
#####################################################
ZeroLine.HideTitle();
ZeroLine.HideBubble();
ZeroLine.AssignValueColor(
    if (upperEMADLinePlot > zeroLine) then  Color.green
    else if (upperEMADLinePlot < zeroLine) then  Color.red
    else GlobalColor("Yellow")
);
#####################################################
##                                                 ##
#####################################################
TopBandPlot_offset.HideTitle();
TopBandPlot_offset.HideBubble();
TopBandPlot_offset.SetDefaultColor(Color.black
);
#####################################################
##                                                 ##
#####################################################
BottomBandPlot_offset.HideTitle();
BottomBandPlot_offset.HideBubble();
BottomBandPlot_offset.SetDefaultColor(Color.black
);
#####################################################
## CANDLE COLOR                                    ##
#####################################################
AssignPriceColor(if Color_Candles then
     if ALERT4 THEN COLOR.cyan
else if BottomBand_PushDown THEN COLOR.red
else if TopBand_PushUp THEN COLOR.green
else if Madebad_CrossCondition == 1 then color.dark_green
else if Madebad_CrossCondition == -1 then color.dark_red
else if Madebad_Condition_lookback > 1 and Band_Crossed_First and EMAD_below_Zero and EMAD_EMA_up then color.gray
else if Madebad_Condition_lookback > 1 and Band_Crossed_First and EMAD_above_Zero and EMAD_EMA_down then color.gray
else if Madebad_Condition_lookback > 1 and Band_Crossed_First and EMAD_below_Zero then color.light_red
else if Madebad_Condition_lookback > 1 and Band_Crossed_First and EMAD_above_Zero then color.light_green
else Color.GRAY
else Color.CURRENT);
#####################################################
## VERTICAL CLOUD                                  ##
#####################################################
AddCloud(if TopBand_StepUp and TopBand_PushUp and !TopBand_below_Zero then topBandPlot else double.nan, bottomBandPlot, color.dark_green, color.dark_green);
AddCloud(if BottomBand_Above_Zero and !TopBand_PushUp then topBandPlot else double.nan, bottomBandPlot, color.dark_gray, color.dark_gray);
AddCloud(if BottomBand_StepDown and BottomBand_PushDown and !BottomBand_Above_Zero then topBandPlot else double.nan, bottomBandPlot, color.dark_red, color.dark_red);
AddCloud(if TopBand_below_Zero and !BottomBand_PushDown then topBandPlot else double.nan, bottomBandPlot, color.dark_gray, color.dark_gray);
AddCloud(if TopBand_StepDown and !EMAD_Above_Zero then topBandPlot else double.nan, bottomBandPlot, color.dark_red, color.dark_red);
AddCloud(if BottomBand_StepUp and !EMAD_Below_Zero then topBandPlot else double.nan, bottomBandPlot, color.dark_green, color.dark_green);
#####################################################
## EMA CLOUD                                       ##
#####################################################
AddCloud(if Show_EMA_Cloud and (made_1 > masterEMADLinePlot) then made_1 else Double.NaN, masterEMADLinePlot, Color.dark_red, Color.gray);
AddCloud(if Show_EMA_Cloud and (masterEMADLinePlot >= made) then masterEMADLinePlot else Double.NaN, made,  Color.dark_green, Color.gray);
#####################################################
##                                                 ##
#####################################################


Code:
   ##########################################################################################
   ##########################################################################################
   ###                                                                                    ###
   ###       #############################           #########               #########    ###
   ###     #################################         #########               #########    ###   
   ###    ####################################       #########               #########    ###
   ###   ########                     #########      #########               #########    ###        
   ###   ########  C3_BIG_SPARK_MAX   #########      #########               #########    ###
   ###   ########       +TSV9         #########      #########               #########    ###      
   ###   ########   @Christopher84    #########      #########               #########    ###    
   ###   ########                     #########      #########               #########    ###       
   ###   ######################################      ##################################   ###     
   ###    ####################################       ##################################   ###  
   ###     #################################         ##################################   ###
   ###    ####################################       ##################################   ###    
   ###   ########                     #########                              #########    ###
   ###   ########     @Horserider     #########                              #########    ###
   ###   ########  HORSERIDER VOLUME  #########                              #########    ###           
   ###   ########  TRIPLE EXHAUSTION  #########                              #########    ###       
   ###   ########      @chence27      #########                              #########    ###            
   ###   ########                     #########                              #########    ###        
   ###    ####################################                               #########    ###      
   ###     ##################################                                #########    ###       
   ###      ###############################                                  #########    ###     
   ###                                                                                    ###
   ##########################################################################################
   ##########################################################################################

declare upper;

input Filter = {default EMAD_Filter, Phase_Filter, No_Filter};
input x = 5;
input Ehlers_Length = 34;
input Big4_Confirmation_Factor = 4; #hint Big4
input ATR_Period_TSV9 = 11;
input ATR_Factor_TSV9 = 2.2;
input Color_Candles = yes;
input Show_EMA_Cloud = yes;
input Show_HL_LH = no;
input Show_EMAD_Arrows = no;
input Show_Labels = yes;
input TradeDaytimeOnly = yes; #hint tradeDaytimeOnly: (IntraDay Only) Only perform trades during hours stated
input OpenTime = 0730; #hint OpenTime: Opening time of market hides arrows
input CloseTime = 1800; #hint CloseTime: Closing time of market hides arrows
input FirstTrade_Squeeze = {default long, short};
input TrailType = {default modified, unmodified};
input FirstTrade = {default long, short};
input MACD_AverageType = {SMA, default EMA};
input DMI_averageType = AverageType.WILDERS;
input AvgType = AverageType.HULL;
########################################################
## 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));
########################################################
##                                                    ##
########################################################
def bn = barnumber();
def Begin = SecondsFromTime(OpenTime);
def End = SecondsTillTime(CloseTime);
def isIntraDay = if GetAggregationPeriod() > 14400000 or GetAggregationPeriod() == 0 then 0 else 1;
def MarketOpen = if !tradeDaytimeOnly or !isIntraDay then 1 else if tradeDaytimeOnly and isIntraDay and Begin > 0 and End > 0 then 1 else 0;
def LongTrades = yes; #hint LongTrades: perform long tradesvb  balanceOfMarketPower AbandonedBaby b           bbbbbbbbg
def ShortTrades = yes; #hint ShortTrades: perform short trades
def useStops = no;     #hint useStops: use stop orders
def useAlerts = no;    #hint useAlerts: use alerts on signals
########################################################
##                                                    ##
########################################################
def Cloud_Lookback = 20;
def HL_LH_Lookback = 20;
def Previous_Steps_Down = 2; #hint filter for hiding arrows
def Previous_Steps_Up = 2; #hint filter for hiding arrows
def Length_MAD = 4;
def Band_Length = 100;
def Smooth_Length = 12;
def Smooth_Length_2 = 14;
def Fast_Length = 10;
def Slow_Length = 35;
def Made_Avg_Length = 3;
def ATR_Period_Squeeze = 5;
def ATR_Factor_Squeeze = 2.0;
def Length_3x = 1000;
def AverageType = AverageType.WILDERS;
def AverageType_Squeeze = AverageType.SIMPLE;
def EMAD_EMA_lineweight = 2;
def Step_lookback = 10;
def FastExpAvg = ExpAverage(close, fast_Length);
def SlowExpAvg = ExpAverage(close, slow_Length);
def EMAD_Fast = (close - fastExpAvg);
def EMAD_Slow = (close - slowExpAvg);
def EMAD_Avg = (EMAD_Fast + EMAD_Slow) / 2;
#####################################################
## UPPER EMA                                       ##
#####################################################
def Upper_EMAD_EMA = ExpAverage(EMAD_Avg, smooth_Length);
def EMAD_Open = (Upper_EMAD_EMA + Upper_EMAD_EMA[1]) / 2;
def EMAD_High = Max(Upper_EMAD_EMA, Upper_EMAD_EMA[1]);
def EMAD_Low = Min(Upper_EMAD_EMA, Upper_EMAD_EMA[1]);
def EMAD_Close = Upper_EMAD_EMA;
def Bottom = Min(EMAD_Close[1], EMAD_Low);
def TR = TrueRange(EMAD_High, EMAD_Close, EMAD_Low);
def PTR = tr / (bottom + tr / 2);
def APTR = MovingAverage(averageType, ptr, smooth_Length_2);
def UpperBand = EMAD_Close[1] + (APTR * EMAD_Open);
def LowerBand = EMAD_Close[1] - (APTR * EMAD_Open);
#####################################################
## LOWER EMA                                       ##
#####################################################
def Lower_EMAD_EMA = (upperBand + lowerBand) / 2;
#####################################################
## TOP BAND                                        ##
#####################################################
def TopBand = Highest(Lower_EMAD_EMA, band_Length);
#####################################################
## BOTTOM BAND                                     ##
#####################################################
def BottomBand = Lowest(Lower_EMAD_EMA, band_Length);
#####################################################
## ZERO LINE                                       ##
#####################################################
def ZeroLineData = if IsNaN(close) then Double.NaN else 0;
def EMAD_Above_Zero = Upper_EMAD_EMA > zeroLineData;
def EMAD_Below_Zero = Upper_EMAD_EMA < zeroLineData;
def EMAD_EMA_down = (Lower_EMAD_EMA > Upper_EMAD_EMA);
def EMAD_EMA_up = (Upper_EMAD_EMA >= Lower_EMAD_EMA);
#####################################################
## MASTER EMA                                      ##
#####################################################
def Master_EMAD_EMA = (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2;
def TopBand_StepDown = if topBand < topBand[1] then 1 else 0;
def TopBand_StepUp = if topBand > topBand[1] then 1 else 0;
def BottomBand_StepDown = if bottomBand < bottomBand[1] then 1 else 0;
def BottomBand_StepUp = if bottomBand > bottomBand[1] then 1 else 0;
def BothBands_Down = bottomBand_StepDown and TopBand_StepDown;
def BothBands_Up = bottomBand_StepUp and TopBand_StepUp;
def BottomBand_Above_Zero = (bottomBand > zeroLineData);
def TopBand_below_Zero = (topBand < zeroLineData);
def TopBand_PushUp =  Master_EMAD_EMA >= TopBand;
def BottomBand_PushDown = Master_EMAD_EMA <= bottomBand;
def MidBand = (upperBand + lowerBand) / 2;
def EMA_CrossUp = if (midBand[1] > Upper_EMAD_EMA[1]) and (midBand < Upper_EMAD_EMA) then 1 else 0;
def EMA_CrossDown = if (Upper_EMAD_EMA[1] > midBand[1]) and (Upper_EMAD_EMA < midBand) then 1 else 0;
def Value_At_CrossUp = if EMA_CrossUp then midBand else 0;
def Value_At_CrossDown = if EMA_CrossDown then midBand else 0;
def CrossUp_BottomBand = if (Value_At_CrossUp - bottomBand) == 0 then 1 else 0;
def CrossDown_TopBand = if (Value_At_CrossDown - topBand) == 0 then 1 else 0;
def CrossUp_Zeroline = if (Value_At_CrossUp) == 0 then 1 else 0;
def CrossDown_Zeroline = if (Value_At_CrossDown) == 0 then 1 else 0;
def CrossUp_BottomBand_bn = if CrossUp_Bottomband and !CrossUp_Bottomband[1] then bn else CrossUp_Bottomband_bn[1];
def CrossDown_TopBand_bn = if CrossDown_topband and !CrossDown_topband[1] then bn else CrossDown_TopBand_bn[1];
def CrossUp_BottomBand_Within = if CrossUp_Bottomband_bn < bn then bn - CrossUp_Bottomband_bn else CrossUp_BottomBand_Within[1];
def CrossDown_TopBand_Within = if CrossDown_TopBand_bn < bn then bn - CrossDown_TopBand_bn else CrossDown_TopBand_Within[1];
def Band_Crossed_First = CrossUp_Bottomband_bn > CrossDown_TopBand_bn;
#####################################################
## EMAD CLOUD CONDITIONS                           ##
#####################################################
def EMAD_Green_Cloud_1 = if TopBand_StepUp and TopBand_PushUp and !TopBand_below_Zero then 1 else 0;
def EMAD_Green_Cloud_2 = if BottomBand_StepUp and !EMAD_Below_Zero then 1 else 0;
def EMAD_Red_Cloud_1 = if BottomBand_StepDown and BottomBand_PushDown and !BottomBand_Above_Zero then 1 else 0;
def EMAD_Red_Cloud_2 = if TopBand_StepDown and !EMAD_Above_Zero then 1 else 0;
def EMAD_Gray_Cloud_1 = if BottomBand_Above_Zero and !TopBand_PushUp then 1 else 0;
def EMAD_Gray_Cloud_2 = if TopBand_below_Zero and !BottomBand_PushDown then 1 else 0;
#####################################################
## HIGHER LOW - LOWER HIGH                         ##
#####################################################
def HL = CrossUp_Bottomband == 0 and CrossDown_topband == 0 and Upper_EMAD_EMA crosses above Lower_EMAD_EMA;  #Normal HL LH condition
def LH = CrossDown_topband == 0 and CrossUp_Bottomband == 0 and Upper_EMAD_EMA crosses below Lower_EMAD_EMA; #Normal HL LH condition
def bounceUpOnce = CrossUp_Bottomband <> CrossUp_Bottomband[1]; #XUpBtm happened previous bar or on current bar (<> = is NOT equal to)
def bounceDnOnce = CrossDown_topband <> CrossDown_topband[1];
def trackCrUp = if bounceUpOnce then 0 else if HL then 1 else trackCrUp[1];
def trackCrDn = if bounceDnOnce then 0 else if LH then 1 else trackCrDn[1];
def HLalready = if trackCrUp and !trackCrUp[1] then 1 else 0;
def LHalready = if trackCrDn and !trackCrDn[1] then 1 else 0;
def HL2_EMAD = HLalready == 0 and Upper_EMAD_EMA crosses above Lower_EMAD_EMA;
def LH2_EMAD  = LHalready == 0 and Upper_EMAD_EMA crosses below Lower_EMAD_EMA;
def crossedUpOnce = HLalready <> HLalready[1];
def crossedDnOnce = LHalready <> LHalready[1];
def trackCrUp2 = if crossedUpOnce then 0 else if HL2_EMAD then 1 else trackCrUp2[1];
def trackCrDn2 = if crossedDnOnce then 0 else if LH2_EMAD then 1 else trackCrDn2[1];
#####################################################
## SQUEEZE                                         ##
#####################################################
def HiLo_Squeeze = Min(high - low, 1.5 * Average(high - low, ATR_Period_Squeeze));
def HRef_Squeeze = if low <= high[1]
    then high - close[1]
    else (high - close[1]) - 0.5 * (low - high[1]);
def LRef_Squeeze = if high >= low[1]
    then close[1] - low
    else (close[1] - low) - 0.5 * (low[1] - high);
def trueRange_Squeeze;
switch (trailType) {
case modified:
    trueRange_Squeeze = Max(HiLo_Squeeze, Max(HRef_Squeeze, LRef_Squeeze));
case unmodified:
    trueRange_Squeeze = TrueRange(high, close, low);
}
def loss_Squeeze = ATR_Factor_Squeeze * MovingAverage(averageType_Squeeze, trueRange_Squeeze, ATR_Period_Squeeze);
def state_Squeeze = {default init, long, short};
def trail_Squeeze;
switch (state_Squeeze[1]) {
case init:
    if (!IsNaN(loss_Squeeze)) {
        switch (firstTrade) {
        case long:
            state_Squeeze = state_Squeeze.long;
            trail_Squeeze =  close - loss_Squeeze;
        case short:
            state_Squeeze = state_Squeeze.short;
            trail_Squeeze = close + loss_Squeeze;
    }
    } else {
        state_Squeeze = state_Squeeze.init;
        trail_Squeeze = Double.NaN;
    }
case long:
    if (close > trail_Squeeze[1]) {
        state_Squeeze = state_Squeeze.long;
        trail_Squeeze = Max(trail_Squeeze[1], close - loss_Squeeze);
    } else {
        state_Squeeze = state_Squeeze.short;
        trail_Squeeze = close + loss_Squeeze;
    }
case short:
    if (close < trail_Squeeze[1]) {
        state_Squeeze = state_Squeeze.short;
        trail_Squeeze = Min(trail_Squeeze[1], close + loss_Squeeze);
    } else {
        state_Squeeze = state_Squeeze.long;
        trail_Squeeze =  close - loss_Squeeze;
    }
}
def TrailingStop_Squeeze = trail_Squeeze;
def H = Highest(TrailingStop_Squeeze, 12);
def L = Lowest(TrailingStop_Squeeze, 12);
def Bandwidth_Squeeze = (H - L);
def IntermSupport2 = Lowest(Bandwidth_Squeeze, Band_Length);
def Squeeze_Trigger = Bandwidth_Squeeze <= IntermSupport2;
def Squeeze_Level = if !Squeeze_Trigger[1] and Squeeze_Trigger then hl2 else if !Squeeze_Trigger then Double.NaN else Squeeze_Level[1];
def Squeeze_Alert_1 = if Squeeze_Level then (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2 else (Upper_EMAD_EMA + Lower_EMAD_EMA) / 2 ;
#####################################################
## TS V9                                           ##
#####################################################
def HiLo_TSV9 = Min(high - low, 1.5 * Average(high - low, ATR_Period_TSV9));
def HRef_TSV9 = if low <= high[1]
    then high - close[1]
    else (high - close[1]) - 0.5 * (low - high[1]);
def LRef_TSV9 = if high >= low[1]
    then close[1] - low
    else (close[1] - low) - 0.5 * (low[1] - high);
def trueRange_TSV9;
switch (trailType) {
case modified:
    trueRange_TSV9 = Max(HiLo_TSV9, Max(HRef_TSV9, LRef_TSV9));
case unmodified:
    trueRange_TSV9 = TrueRange(high, close, low);
}
def loss_TSV9 = ATR_Factor_TSV9 * MovingAverage(averageType, trueRange_TSV9, ATR_Period_TSV9);
def state_TSV9 = {default init, long, short};
def trail_TSV9;
switch (state_TSV9[1]) {
case init:
    if (!IsNaN(loss_TSV9)) {
        switch (firstTrade) {
        case long:
            state_TSV9 = state_TSV9.long;
            trail_TSV9 =  close - loss_TSV9;
        case short:
            state_TSV9 = state_TSV9.short;
            trail_TSV9 = close + loss_TSV9;
    }
    } else {
        state_TSV9 = state_TSV9.init;
        trail_TSV9 = Double.NaN;
    }
case long:
    if (close > trail_TSV9[1]) {
        state_TSV9 = state_TSV9.long;
        trail_TSV9 = Max(trail_TSV9[1], close - loss_TSV9);
    } else {
        state_TSV9 = state_TSV9.short;
        trail_TSV9 = close + loss_TSV9;
    }
case short:
    if (close < trail_TSV9[1]) {
        state_TSV9 = state_TSV9.short;
        trail_TSV9 = Min(trail_TSV9[1], close + loss_TSV9);
    } else {
        state_TSV9 = state_TSV9.long;
        trail_TSV9 =  close - loss_TSV9;
    }
}
def Price = close;
def TrailingStop_TSV9 = trail_TSV9;
def LongEnter = (price crosses above TrailingStop_TSV9);
def LongExit = (price crosses below TrailingStop_TSV9);
def Upsignal =  (price crosses above TrailingStop_TSV9);
def Downsignal = (price crosses below TrailingStop_TSV9);
def PLBuyStop  = if !useStops then 0 else if  (0 > 0) then 1 else 0  ; # insert condition to stop in place of the 0<0
def PLSellStop = if !useStops then 0 else if (0 > 0) then 1 else 0  ; # insert condition to stop in place of the 0>0
def PLMktStop = if MarketOpen[-1] == 0 then 1 else 0; # If tradeDaytimeOnly is set, then stop at end of day
def PLBuySignal = if  MarketOpen and (upsignal) then 1 else 0 ; # insert condition to create long position in place of the 0>0
def PLSellSignal =  if MarketOpen and (downsignal) then 1 else 0; # insert condition to create short position in place 0>0
def CurrentPosition;  # holds whether flat = 0 long = 1 short = -1
if (BarNumber() == 1) or IsNaN(CurrentPosition[1]) {
    CurrentPosition = 0;
} else {
    if CurrentPosition[1] == 0 {            # FLAT
        if (PLBuySignal and LongTrades) {
            CurrentPosition = 1;
        } else if (PLSellSignal and ShortTrades) {
            CurrentPosition = -1;
        } else {
            CurrentPosition = CurrentPosition[1];
        }
    } else if CurrentPosition[1] == 1 {      # LONG
        if (PLSellSignal and ShortTrades) {
            CurrentPosition = -1;
        } else if ((PLBuyStop and useStops) or PLMktStop or (PLSellSignal and ShortTrades == 0)) {
            CurrentPosition = 0;
        } else {
            CurrentPosition = CurrentPosition[1];
        }
    } else if CurrentPosition[1] == -1 {     # SHORT
        if (PLBuySignal and LongTrades) {
            CurrentPosition = 1;
        } else if ((PLSellStop and useStops) or PLMktStop or (PLBuySignal and LongTrades == 0)) {
            CurrentPosition = 0;
        } else {
            CurrentPosition = CurrentPosition[1];
        }
    } else {
        CurrentPosition = CurrentPosition[1];
    }
}
def isLong  = if CurrentPosition == 1 then 1 else 0;
def isShort = if CurrentPosition == -1 then 1 else 0;
def isFlat  = if CurrentPosition == 0 then 1 else 0;
def BuySig = if marketopen and (((isShort[1] and LongTrades) or (isFlat[1] and LongTrades)) and PLBuySignal) then 1 else 0;
def SellSig = if marketopen and (((isLong[1] and ShortTrades) or (isFlat[1] and ShortTrades)) and PLSellSignal) then 1 else 0;
def BuySig_TSV9_bn = if upsignal[1] then BN else BuySig_TSV9_bn[1];  
def SellSig_TSV9_bn = if downsignal[1] then BN else SellSig_TSV9_bn[1];
def TS_Last = if BuySig_TSV9_bn > SellSig_TSV9_bn then 1 else 0;
def cond13_TS_Buy = Buysig;
def cond13_TS_Sell = Sellsig;
#####################################################
## TRIPLE EXHAUSTION                               ##
#####################################################
def OB_3x = 80;
def OS_3x = 20;
def KPeriod_3x = 10;
def DPeriod_3x = 10; 
def SlowK_3x = reference StochasticFull(OB_3x,  OS_3x,  KPeriod_3x,  DPeriod_3x,  EMAD_High, EMAD_Low, EMAD_Close,  3, if (averageType == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;
def MACD_3x = reference MACD()."Value";
def Price_Avg_3x = Average(MACD_3x, length_3x);
def MACD_stdev_3x =  (MACD_3x - Price_Avg_3x) / StDev(MACD_3x, length_3x);
def DPlus_3x = reference DMI()."DI+";
def DMinus_3x = reference DMI()."DI-";
def SellerRegular = SlowK_3x < 20 and MACD_stdev_3x < -1 and dPlus_3x < 15;
def SellerExtreme = SlowK_3x < 20 and MACD_stdev_3x < -2 and dPlus_3x < 15;
def BuyerRegular = SlowK_3x > 80 and MACD_stdev_3x > 1 and dMinus_3x < 15;
def BuyerExtreme = SlowK_3x > 80 and MACD_stdev_3x > 2 and dMinus_3x < 15;   
def Extreme_Buy = if sellerExtreme[1] and !sellerExtreme then 1 else 0;
def Extreme_Sell = if buyerExtreme[1] and !buyerExtreme then 1 else 0;
def Regular_Sell = if buyerRegular[1] and !buyerRegular then 1 else 0;
def Regular_Buy = if sellerRegular[1] and !sellerRegular then 1 else 0;
#####################################################
## C3 MAX YELLOW CANDLE                            ##
#####################################################
def Displace = 0;
def FactorK2 = 3.25;
def LengthK2 = 20;
def TrueRangeAverageType = AverageType.SIMPLE;
def ATR_length = 15;
def SMA_lengthS = 6;
def Multiplier_factor = 1.25;
def ValS = Average(price, SMA_lengthS);
def Average_true_range = Average(TrueRange(high, close, low), length = ATR_length);
def Upper_BandS = valS[-displace] + multiplier_factor * average_true_range[-displace];
def Middle_BandS = valS[-displace];
def Lower_BandS = valS[-displace] - multiplier_factor * average_true_range[-displace];
def shiftK2 = factorK2 * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), lengthK2);
def averageK2 = MovingAverage(averageType, price, lengthK2);
def AvgK2 = averageK2[-displace];
def Upper_BandK2 = averageK2[-displace] + shiftK2[-displace];
def Lower_BandK2 = averageK2[-displace] - shiftK2[-displace];
def Condition_BandRevDn = (Upper_BandS > Upper_BandK2);
def Condition_BandRevUp = (Lower_BandS < Lower_BandK2);
def FastLength = 12;
def SlowLength = 26;
def MACDLength = 9;
def FastEMA = ExpAverage(price, fastLength);
def SlowEMA = ExpAverage(price, slowLength);
def Value;
def Avg;
switch (MACD_AverageType) {
case SMA:
    Value = Average(price, fastLength) - Average(price, slowLength);
    Avg = Average(Value, MACDLength);
case EMA:
    Value = fastEMA - slowEMA;
    Avg = ExpAverage(Value, MACDLength);
}
def Diff = Value - Avg;
def MACDLevel = 0.0;
def Level = MACDLevel;
def condition1_Yellow_Candle = Value[1] <= Value;
def condition1D_Yellow_Candle = Value[1] > Value;
#####################################################
## RSI                                             ##
#####################################################
def RSI_length = 14;
def RSI_AverageType = AverageType.WILDERS;
def RSI_OB = 70;
def RSI_OS = 30;
def NetChgAvg = MovingAverage(RSI_AverageType, price - price[1], RSI_length);
def TotChgAvg = MovingAverage(RSI_AverageType, AbsValue(price - price[1]), RSI_length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);
def condition2_RSI = (RSI[3] < RSI) is true or (RSI >= 80) is true;
def condition2D_RSI = (RSI[3] > RSI) is true or (RSI < 20) is true;
def conditionOB1_RSI = RSI > RSI_OB;
def conditionOS1_RSI = RSI < RSI_OS;
#####################################################
## MONEY FLOW INDEX                                ##
#####################################################
def MFI_Length = 14;
def MFIover_Sold = 20;
def MFIover_Bought = 80;
def movingAvgLength = 1;
def MoneyFlowIndex = Average(MoneyFlow(high, close, low, volume, MFI_Length), movingAvgLength);
def MFIOverBought = MFIover_Bought;
def MFIOverSold = MFIover_Sold;
def condition3_MFI = (MoneyFlowIndex[2] < MoneyFlowIndex) is true or (MoneyFlowIndex > 85) is true;
def condition3D_MFI = (MoneyFlowIndex[2] > MoneyFlowIndex) is true or (MoneyFlowIndex < 20) is true;
def conditionOB2_MFI = MoneyFlowIndex > MFIover_Bought;
def conditionOS2_MFI = MoneyFlowIndex < MFIover_Sold;
#####################################################
## FORECAST                                        ##
#####################################################
def na = Double.NaN;
def MidLine = 50;
def Momentum = MarketForecast().Momentum;
def NearT =  MarketForecast().NearTerm;
def Intermed = MarketForecast().Intermediate;
def FOB = 80;
def FOS = 20;
def upperLine = 110;
def condition4_Forcast = (Intermed[1] <= Intermed) or (NearT >= MidLine);
def condition4D_Forcast = (Intermed[1] > Intermed) or (NearT < MidLine);
def conditionOB3_Forcast = Intermed > FOB;
def conditionOS3_Forcast = Intermed < FOS;
def conditionOB4_Forcast = NearT > FOB;
def conditionOS4_Forcast = NearT < FOS;
#####################################################
## CHANGE IN PRICE                                 ##
#####################################################
def lengthCIP = 5;
def CIP = (price - price[1]);
def AvgCIP = ExpAverage(CIP[-displace], lengthCIP);
def CIP_UP = AvgCIP > AvgCIP[1];
def CIP_DOWN = AvgCIP < AvgCIP[1];
def condition5_CIP = CIP_UP;
def condition5D_CIP = CIP_DOWN;
#####################################################
## EMA 1                                           ##
#####################################################
def EMA_length = 8;
def AvgExp = ExpAverage(price[-displace], EMA_length);
def condition6_EMA_1 = (price >= AvgExp) and (AvgExp[2] <= AvgExp);
def condition6D_EMA_1 = (price < AvgExp) and (AvgExp[2] > AvgExp);
#####################################################
## EMA 2                                           ##
#####################################################
def EMA_2length = 20;
def displace2 = 0;
def AvgExp2 = ExpAverage(price[-displace2], EMA_2length);
def condition7_EMA_2 = (price >= AvgExp2) and (AvgExp2[2] <= AvgExp);
def condition7D_EMA_2 = (price < AvgExp2) and (AvgExp2[2] > AvgExp);
#####################################################
## DMI OSCILLATOR                                  ##
#####################################################
def DMI_length = 5; #Typically set to 10
def diPlus = DMI(DMI_length, DMI_averageType)."DI+";
def diMinus = DMI(DMI_length, DMI_averageType)."DI-";
def Osc = diPlus - diMinus;
def Hist = Osc;
def ZeroLine = 0;
def condition8_DMI = Osc >= ZeroLine;
def condition8D_DMI = Osc < ZeroLine;
#####################################################
## TREND PERIODS                                   ##
#####################################################
def TP_fastLength = 3;#Typically 7
def TP_slowLength = 4;#Typically 15
def Periods = Sign(ExpAverage(close, TP_fastLength) - ExpAverage(close, TP_slowLength));
def condition9_Trend = Periods > 0;
def condition9D_Trend = Periods < 0;
#####################################################
## POLARIZED FRACTAL EFFICIENCY                    ##
#####################################################
def PFE_length = 5;#Typically 10
def smoothingLength = 2.5;#Typically 5
def PFE_diff = close - close[PFE_length - 1];
def val = 100 * Sqrt(Sqr(PFE_diff) + Sqr(PFE_length)) / Sum(Sqrt(1 + Sqr(close - close[1])), PFE_length - 1);
def PFE = ExpAverage(if PFE_diff > 0 then val else -val, smoothingLength);
def UpperLevel = 50;
def LowerLevel = -50;
def condition10_PFE = PFE > 0;
def condition10D_PFE = PFE < 0;
def conditionOB5_PFE = PFE > UpperLevel;
def conditionOS5_PFE = PFE < LowerLevel;
#####################################################
## BOLLINGER BANDS                                 ##
#####################################################
input AverageType_BB = AverageType.SIMPLE;
def BBPB_length = 20; #Typically 20
def Num_Dev_Dn = -2.0;
def Num_Dev_up = 2.0;
def BBPB_OB = 100;
def BBPB_OS = 0;
def upperBand_BB = BollingerBands(price, displace, BBPB_length, Num_Dev_Dn, Num_Dev_up, AverageType_BB).UpperBand;
def lowerBand_BB = BollingerBands(price, displace, BBPB_length, Num_Dev_Dn, Num_Dev_up, AverageType_BB).LowerBand;
def PercentB = (price - lowerBand_BB) / (upperBand_BB - lowerBand_BB) * 100;
def HalfLine = 50;
def UnitLine = 100;
def condition11_BB = PercentB > HalfLine;
def condition11D_BB = PercentB < HalfLine;
def conditionOB6_BB = PercentB > BBPB_OB;
def conditionOS6_BB = PercentB < BBPB_OS;
def condition12_BB = (Upper_BandS[1] <= Upper_BandS) and (Lower_BandS[1] <= Lower_BandS);
def condition12D_BB = (Upper_BandS[1] > Upper_BandS) and (Lower_BandS[1] > Lower_BandS);
#####################################################
## KLINGER HISTOGRAM                               ##
#####################################################
def Klinger_Length = 13;
def KVOsc = KlingerOscillator(Klinger_Length).KVOsc;
def KVOH = KVOsc - Average(KVOsc, Klinger_Length);
def condition13_KH = (KVOH > 0);
def condition13D_KH = (KVOH < 0);
#####################################################
## PROJECTION OSCILLATOR                           ##
#####################################################
def ProjectionOsc_length = 30;#Typically 10
def MaxBound = HighestWeighted(high, ProjectionOsc_length, LinearRegressionSlope(price = high, length = ProjectionOsc_length));
def MinBound = LowestWeighted(low, ProjectionOsc_length, LinearRegressionSlope(price = low, length = ProjectionOsc_length));
def ProjectionOsc_diff = MaxBound - MinBound;
def PROSC = if ProjectionOsc_diff != 0 then 100 * (close - MinBound) / ProjectionOsc_diff else 0;
def PROSC_OB = 80;
def PROSC_OS = 20;
def condition14_PO = PROSC > 50;
def condition14D_PO = PROSC < 50;
def conditionOB7_PO = PROSC > PROSC_OB;
def conditionOS7_PO = PROSC < PROSC_OS;
#####################################################
## AK TREND                                        ##
#####################################################
def aktrend_input1 = 3;
def aktrend_input2 = 8;
def aktrend_price = close;
def aktrend_fastmaa = MovAvgExponential(aktrend_price, aktrend_input1);
def aktrend_fastmab = MovAvgExponential(aktrend_price, aktrend_input2);
def aktrend_bspread = (aktrend_fastmaa - aktrend_fastmab) * 1.001;
def cond1_UP_AK = if aktrend_bspread > 0 then 1 else 0;
def cond1_DN_AK = if aktrend_bspread <= 0 then -1 else 0;
#####################################################
## ZSCORE                                          ##
#####################################################
def zscore_price = close;
def zscore_length = 20;
def zscore_ZavgLength = 20;
def zscore_oneSD = StDev(zscore_price, zscore_length);
def zscore_avgClose = SimpleMovingAvg(zscore_price, zscore_length);
def zscore_ofoneSD = zscore_oneSD * zscore_price[1];
def zscore_Zscorevalue = ((zscore_price - zscore_avgClose) / zscore_oneSD);
def zscore_avgZv = Average(zscore_Zscorevalue, 20);
def zscore_Zscore = ((zscore_price - zscore_avgClose) / zscore_oneSD);
def zscore_avgZscore = Average(zscore_Zscorevalue, zscore_ZavgLength);
def cond2_UP_ZSCORE = if zscore_Zscore > 0 then 1 else 0;
def cond2_DN_ZSCORE = if zscore_Zscore <= 0 then -1 else 0;
#####################################################
## EMAD EHLERS                                     ##
#####################################################
def Price_Ehlers_EMAD = (EMAD_High + EMAD_Low) / 2;
def Coeff = Ehlers_length * Price_Ehlers_EMAD * Price_Ehlers_EMAD - 2 * Price_Ehlers_EMAD * sum(Price_Ehlers_EMAD, Ehlers_length)[1] + sum(Price_Ehlers_EMAD * Price_Ehlers_EMAD, Ehlers_length)[1];
def Ehlers_EMAD =  sum(coeff * Price_Ehlers_EMAD, Ehlers_length) / sum(coeff, Ehlers_length);
#####################################################
## EHLERS                                          ##
#####################################################
def Price_Ehlers = (high + low) / 2;
def Ehlers_Coeff = ehlers_length * Price_Ehlers * Price_Ehlers - 2 * Price_Ehlers * Sum(Price_Ehlers, ehlers_length)[1] + Sum(Price_Ehlers * Price_Ehlers, ehlers_length)[1];
def Ehlers = Sum(ehlers_coeff * Price_Ehlers, ehlers_length) / Sum(ehlers_coeff, ehlers_length);
def Ehlers_Avg_Length = 9;
def Ehlers_Avg = average(Ehlers, Ehlers_Avg_Length);
def cond3_UP_Ehlers = if close > Ehlers_Avg then 1 else 0;
def cond3_DN_Ehlers = if close <= Ehlers_Avg then -1 else 0;
#####################################################
## ANCHORED MOMENTUM                               ##
#####################################################
def amom_src = close;
def amom_MomentumPeriod = 10;
def amom_SignalPeriod = 8;
def amom_SmoothMomentum = no;
def amom_SmoothingPeriod = 7;
def amom_p = 2 * amom_MomentumPeriod + 1;
def amom_t_amom = if amom_SmoothMomentum == yes then ExpAverage(amom_src, amom_SmoothingPeriod) else amom_src;
def amom_amom = 100 * ( (amom_t_amom / ( Average(amom_src, amom_p)) - 1));
def amom_amoms = Average(amom_amom, amom_SignalPeriod);
def cond4_UP_AM = if amom_amom > 0 then 1 else 0;
def cond4_DN_AM = if amom_amom <= 0 then -1 else 0;
#####################################################
## TMO                                             ##
#####################################################
def tmo_length = 30; #def 14
def tmo_calcLength = 6; #def 5
def tmo_smoothLength = 6; #def 3
def tmo_data = fold i = 0 to tmo_length with s do s + (if close > GetValue(open, i) then 1 else if close < GetValue(open, i) then - 1 else 0);
def tmo_EMA5 = ExpAverage(tmo_data, tmo_calcLength);
def tmo_Main = ExpAverage(tmo_EMA5, tmo_smoothLength);
def tmo_Signal = ExpAverage(tmo_Main, tmo_smoothLength);
def tmo_color = if tmo_Main > tmo_Signal then 1 else -1;
def cond5_UP_TMO = if tmo_Main <= 0 then 1 else 0;
def cond5_DN_TMO = if tmo_Main >= 0 then -1 else 0;
#####################################################
## MADE AVERAGE (EMAD)                             ##
#####################################################
def dataA = Master_EMAD_EMA;
def dataA1 =
    fold aD = 0
    to Length_MAD
    with k = 0  
    do k + dataA[aD];
def MAEMAD = dataA1 / LengtH_MAD;
def MADehlers = (MAEMAD - Ehlers_EMAD)/2;
def Madebad = madehlers + Ehlers_EMAD;
def Madebad_Avg = average(madebad, Made_Avg_Length);
def Madebad_CrossUp = if (madebad < Master_EMAD_EMA) and (madebad > madebad[1]) and (EMAD_Above_Zero > 0) then 1 else 0;
def Madebad_CrossDown = if (madebad > Master_EMAD_EMA) and (madebad < madebad[1]) and (EMAD_Below_Zero > 0) then 1 else 0;
def Madebad_CrossCondition = if (madebad < Master_EMAD_EMA) and (madebad > madebad[1]) and (EMAD_Above_Zero > 0) then 1
            else if (madebad > Master_EMAD_EMA) and (madebad < madebad[1]) and (EMAD_Below_Zero > 0) then -1
            else 0;
def Madebad_Condition = if (!Madebad_CrossCondition[1] or !Madebad_CrossCondition[2]) and (Madebad_CrossCondition == 0) then 1 else 0;
#####################################################
## ALERT4                                          ##
#####################################################
def PD = 22;
def BBL = 20;
def MULT = 2.0;
def LB = 50;
def PH = 0.85;
def LTLB = 40;
def MTLB = 14;
def STR = 3;
def WVF = ((Highest(close, pd) - low) / (Highest(close, pd))) * 100;
def StDev = mult * StDev(wvf, bbl);
def MidLine1 = SimpleMovingAvg(wvf, bbl);
def UpperBand1_Alert4 = midLine1 + StDev;
def Range_High = (Highest(wvf, lb)) * ph;
def UpRange = low > low[1] and close > high[1];
def UpRange_Aggr = close > close[1] and close > open[1];
def Filtered = ((wvf[1] >= UpperBand1_Alert4[1] or wvf[1] >= Range_High[1]) and (wvf < UpperBand1_Alert4 and wvf < Range_High));
def Filtered_Aggr = (wvf[1] >= UpperBand1_Alert4[1] or wvf[1] >= Range_High[1]);
def Alert4 = upRange_Aggr and close > close[str] and (close < close[ltLB] or close < close[mtLB]) and filtered_Aggr;
def Alert4close = if alert4 then close else 0;
def Made_Down = (madebad > Master_EMAD_EMA);
def Made_Up = (madebad < Master_EMAD_EMA);
def Made_Bottom_MA = if !Made_Down and (Madebad_Avg <= zerolinedata) then Madebad_Avg else double.nan;
def Made_Top_MA = if !Made_Up and (Madebad_Avg >= zerolinedata) then Madebad_Avg else double.nan;
def MadeAvg_Master_Cross_Up = if Madebad_Avg crosses below Master_EMAD_EMA then 1 else 0;
def MadeAvg_Master_Cross_Down = if Madebad_Avg crosses above Master_EMAD_EMA then 1 else 0;
def Cross_Up_MadeAvg_Master = if MadeAvg_Master_Cross_Up and !MadeAvg_Master_Cross_Up[1] then 1 else 0;
def Cross_Down_MadeAvg_Master = if MadeAvg_Master_Cross_Down and !MadeAvg_Master_Cross_Down[1] then 1 else 0;
def MadeAvg_Master_Up_Condition = if Cross_Up_MadeAvg_Master and Band_Crossed_First and EMAD_EMA_Up[1] then 1 else 0;
def MadeAvg_Master_Down_Condition = if Cross_Down_MadeAvg_Master and !Band_Crossed_First and EMAD_EMA_down[1] then 1 else 0;
def EMA_Push_StepUp = if topBand_StepUp and EMAD_EMA_Up and (((Upper_EMAD_EMA) - topband) == 0) then 1 else 0;;
def EMA_Push_StepDown = if bottomBand_StepDown and EMAD_EMA_down and (((Lower_EMAD_EMA) - bottomband) == 0) then 1 else 0;
def First_HL_1 = if Marketopen and Show_HL_LH and trackCrUp and !trackCrUp[1] then low else double.nan;
def First_LH_1 = if Marketopen and Show_HL_LH and trackCrDn and !trackCrDn[1] then high else double.nan;
def Second_HL_1 = if Marketopen and Show_HL_LH and trackCrUp2 and !trackCrUp2[1] and !CrossUp_Bottomband then low else double.nan;
def Second_LH_1 = if Marketopen and Show_HL_LH and trackCrDn2 and !trackCrDn2[1] and !CrossDown_Topband then high else double.nan;
#####################################################
## FOLD CONDITIONS (EMAD)                          ##
#####################################################
def TopBandStepDown_lookback = fold index1 = 1 to Step_lookback with x1 do x1 + topBand_StepDown[index1];
def TopBandStepUp_lookback = fold index2 = 1 to Step_lookback with x2 do x2 + topBand_StepUp[index2];
def BottomBandStepDown_lookback = fold index3 = 1 to Step_lookback with x3 do x3 + bottomBand_StepDown[index3];
def BottomBandStepUp_lookback = fold index4 = 1 to Step_lookback with x4 do x4 + bottomBand_StepUp[index4];
def Madebad_Condition_lookback = fold index5 = 1 to Cloud_lookback with x5 do x5 + Madebad_Condition[index5];
def HL_lookback = fold index6 = 1 to HL_LH_Lookback with x6 do x6 + First_HL_1[index6];
def LH_lookback = fold index7 = 1 to HL_LH_Lookback with x7 do x7 + First_LH_1[index7];
def HL2_lookback = fold index8 = 1 to HL_LH_Lookback with x8 do x8 + Second_HL_1[index8];
def LH2_lookback = fold index9 = 1 to HL_LH_Lookback with x9 do x9 + Second_LH_1[index9];
#####################################################
## HIDE ARROW CONDITIONS                           ##
#####################################################
def Hide_Arrow_TopBand_StepDown = topBandStepDown_lookback > Previous_Steps_Down;
def Hide_Arrow_TopBand_StepUp = topBandStepup_lookback > Previous_Steps_Up;
def Hide_Arrow_BottomBand_StepDown = bottomBandStepDown_lookback > Previous_Steps_Down;
def Hide_Arrow_BottomBand_StepUp = bottomBandStepup_lookback > Previous_Steps_Up;
def UP1 = if marketopen and Show_EMAD_Arrows and (!Hide_Arrow_TopBand_StepDown or !Hide_Arrow_BottomBand_StepDown) and !EMA_Push_StepDown and MadeAvg_Master_Up_Condition then 1 else 0;
def DOWN1 = if marketopen and Show_EMAD_Arrows and (!Hide_Arrow_TopBand_StepUp or !Hide_Arrow_BottomBand_StepUp) and !EMA_Push_StepUp and MadeAvg_Master_Down_Condition then 1 else 0;
def UP1_1 = if UP1 then lowest(low,2) else double.nan;
def DOWN1_1 = if DOWN1 then highest(high,2) else double.nan;
#####################################################
## MARKET PHASES                                   ##
#####################################################
def Fastavg_Phases = 50;
def Slowavg_Phases = 200;
def Fastsma_Phases = Average( close, Fastavg_Phases);
def Slowsma_Phases = Average(close, Slowavg_Phases);
def Bullphase = fastsma_Phases > slowsma_Phases && close > fastsma_Phases && close > slowsma_Phases; # Accumulation Phase : close > 50 SMA, close > 200 SMA, 50 SMA < 200 SMA
def Accphase = fastsma_Phases < slowsma_Phases && close > fastsma_Phases && close > slowsma_Phases;# Recovery Phase : close > 50 SMA, close < 200 SMA, 50 SMA < 200 SMA
def Recphase = fastsma_Phases < slowsma_Phases && close < slowsma_Phases && close > fastsma_Phases;# Bearish Phase : close < 50 SMA, close < 200 SMA, 50 SMA < 200 SMA
def Bearphase = fastsma_Phases < slowsma_Phases && close < fastsma_Phases && close < slowsma_Phases;# Distribution Phase : close < 50 SMA, close < 200 SMA, 50 SMA > 200 SMA
def Distphase = fastsma_Phases > slowsma_Phases && close < fastsma_Phases && close < slowsma_Phases;# Warning Phase : close < 50 SMA, close > 200 SMA, 50 SMA > 200 SMA
def Warnphase = fastsma_Phases> slowsma_Phases && close > slowsma_Phases && close < fastsma_Phases;
#####################################################
## BIG 4 DIRECTION                                 ##
#####################################################
def Strategy_FilterWithTMO = no;
def Strategy_FilterWithTMO_arrows = yes;
def Strategy_HoldTrend = no;
def Strategy_ColoredCandlesOn = yes;
def coloredCandlesOn = yes;
def cond_UP = cond1_UP_AK + cond2_UP_ZSCORE + cond3_UP_Ehlers + cond4_UP_AM;
def cond_DN = cond1_DN_AK + cond2_DN_ZSCORE + cond3_DN_Ehlers + cond4_DN_AM;
def direction = if cond_UP >= Big4_Confirmation_Factor and (!Strategy_FilterWithTMO or cond5_UP_TMO) then 1
else if cond_DN <= - Big4_Confirmation_Factor and (!Strategy_FilterWithTMO or cond5_DN_TMO) then -1
    else if !Strategy_HoldTrend and direction[1] == 1 and cond_UP < Big4_Confirmation_Factor and cond_DN > - Big4_Confirmation_Factor then 0
        else if !Strategy_HoldTrend and direction[1] == -1 and cond_DN > -Big4_Confirmation_Factor and cond_UP < Big4_Confirmation_Factor then 0
            else direction[1];
def direction2 = if cond_UP >= Big4_Confirmation_Factor and (!Strategy_FilterWithTMO_arrows or cond5_UP_TMO) then 1
    else if cond_DN <= - Big4_Confirmation_Factor and (!Strategy_FilterWithTMO_arrows or cond5_DN_TMO) then -1
        else if !Strategy_HoldTrend and direction2[1] == 1 and cond_UP < Big4_Confirmation_Factor and cond_DN > - Big4_Confirmation_Factor then 0
            else if !Strategy_HoldTrend and direction2[1] == -1 and cond_DN > - Big4_Confirmation_Factor and cond_UP < Big4_Confirmation_Factor then 0
                else direction2[1];
def direction_Equals_zero = direction == 0;
#####################################################
## TREND CONFIRMATION CALCULATOR                   ##
#####################################################
def Confirmation_Factor = 7;
def Agreement_LevelOB = 12;
def Agreement_LevelOS = 2;
def HideBoxLines = no;
def HideCloud = no;
def HideLabels = no;
def factorK = 2.0;
def lengthK = 20;
def shift = factorK * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), lengthK);
def averageK = MovingAverage(averageType, price, lengthK);
def AvgK = averageK[-displace];
def Upper_BandK = averageK[-displace] + shift[-displace];
def Lower_BandK = averageK[-displace] - shift[-displace];
def conditionK1UP = price >= Upper_BandK;
def conditionK2UP = (Upper_BandK[1] < Upper_BandK) and (Lower_BandK[1] < Lower_BandK);
def conditionK3DN = (Upper_BandK[1] > Upper_BandK) and (Lower_BandK[1] > Lower_BandK);
def conditionK4DN = price < Lower_BandK;
#######################################################
## AGREEMENT LEVEL                                   ##
#######################################################
def Agreement_Level = condition1_Yellow_Candle + condition2_RSI + condition3_MFI + condition4_Forcast
                    + condition5_CIP + condition6_EMA_1 + condition7_EMA_2 + condition8_DMI + condition9_Trend
                    + condition10_PFE + condition11_BB + condition12_BB + condition13_KH + condition14_PO
                    + conditionK1UP + conditionK2UP;
def Agreement_LevelD = (condition1D_Yellow_Candle + condition2D_RSI + condition3D_MFI + condition4D_Forcast + condition5D_CIP
                    + condition6D_EMA_1 + condition7D_EMA_2 + condition8D_DMI + condition9D_Trend + condition10D_PFE
                    + condition11D_BB + condition12D_BB + condition13D_KH + condition14D_PO + conditionK3DN
                    + conditionK4DN);
#######################################################
## CONSENSUS LEVEL                                   ##
#######################################################
def Consensus_Level = Agreement_Level - Agreement_LevelD;
def Consensus_UP = 6;
def Consensus_DOWN = -6;
def UP_CL = Consensus_Level >= Consensus_UP;
def DOWN_CL = Consensus_Level < Consensus_DOWN;
def priceColor = if UP_CL then 1 else if DOWN_CL then -1 else priceColor[1];
def Consensus_Level_OB = 14;
def Consensus_Level_OS = -12;
def OB_Level = conditionOB1_RSI + conditionOB2_MFI + conditionOB3_Forcast + conditionOB4_Forcast + conditionOB5_PFE + conditionOB6_BB + conditionOB7_PO;
def OS_Level = conditionOS1_RSI + conditionOS2_MFI + conditionOS3_Forcast + conditionOS4_Forcast + conditionOS5_PFE + conditionOS6_BB + conditionOS7_PO;
def Consensus_Line = OB_Level - OS_Level;
def MomentumUP = Consensus_Level[1] < Consensus_Level;
def MomentumDOWN = Consensus_Level[1] > Consensus_Level;
def conditionOB_CL = (Consensus_Level >= 12) and (Consensus_Line >= 4);
def conditionOS_CL = (Consensus_Level <= -12) and (Consensus_Line <= -3);
def Super_OB = 4;
def Super_OS = -4;
def DOWN_OB = (Agreement_Level > Agreement_LevelOB) and (Consensus_Line > Super_OB) and (Consensus_Level > Consensus_Level_OB);
def UP_OS = (Agreement_Level < Agreement_LevelOS) and (Consensus_Line < Super_OS) and (Consensus_Level < Consensus_Level_OS);
def YHOB = if ((open > Upper_BandS) and (condition_BandRevDn)) then high else Double.NaN;
def YHOS = if ((open < Lower_BandS) and (condition_BandRevUp)) then high else Double.NaN;
def YLOB = if ((open > Upper_BandS) and (condition_BandRevDn)) then low else Double.NaN;
def YLOS = if ((open < Lower_BandS) and (condition_BandRevUp)) then low else Double.NaN;
def YCOB = if !IsNaN(YHOB) then hl2 else Double.NaN;
def YHextOB = if IsNaN(YCOB) then YHextOB[1] else YCOB;
def YHextlineOB = YHextOB;
def YCOS = if !IsNaN(YHOS) then hl2 else Double.NaN;
def YHextOS = if IsNaN(YCOS) then YHextOS[1] else YCOS;
def YHextlineOS = YHextOS;
def BarsUsedForRange = 2;
def BarsRequiredToRemainInRange = 2;
def trig = 20;# for Blast-off candle color
def YC = coloredCandlesOn and priceColor == 1 and open > Upper_BandS and condition_BandRevDn;
def HH = Highest(high[1], BarsUsedForRange);
def LL = Lowest(low[1], BarsUsedForRange);
def maxH = Highest(HH, BarsRequiredToRemainInRange);
def maxL = Lowest(LL, BarsRequiredToRemainInRange);
def HHn = if maxH == maxH[1] or maxL == maxL then maxH else HHn[1];
def LLn = if maxH == maxH[1] or maxL == maxL then maxL else LLn[1];
def Bh = if high <= HHn and HHn == HHn[1] then HHn else Double.NaN;
def Bl = if low >= LLn and LLn == LLn[1] then LLn else Double.NaN;
def CountH = if IsNaN(Bh) or IsNaN(Bl) then 2 else CountH[1] + 1;
def CountL = if IsNaN(Bh) or IsNaN(Bl) then 2 else CountL[1] + 1;
def ExpH = if BarNumber() == 1 then Double.NaN else
           if CountH[-BarsRequiredToRemainInRange] >= BarsRequiredToRemainInRange then HHn[-BarsRequiredToRemainInRange] else
           if high <= ExpH[1] then ExpH[1] else Double.NaN;
def ExpL = if BarNumber() == 1 then Double.NaN else
           if CountL[-BarsRequiredToRemainInRange] >= BarsRequiredToRemainInRange then LLn[-BarsRequiredToRemainInRange] else
           if low >= ExpL[1] then ExpL[1] else Double.NaN;
def BoxHigh = if ((DOWN_OB) or (Upper_BandS crosses above Upper_BandK2) or (condition_BandRevDn) and (high > high[1]) and ((price > Upper_BandK2) or (price > Upper_BandS))) then Highest(ExpH) else Double.NaN;
def BoxLow = if (DOWN_OB) or ((Upper_BandS crosses above Upper_BandK2)) then Lowest(low) else Double.NaN;
def BoxHigh2 = if ((UP_OS) or ((Lower_BandS crosses below Lower_BandK2))) then Highest(ExpH) else Double.NaN;
def BH2 = if !IsNaN(BoxHigh2) then high else Double.NaN;
def BH2ext = if IsNaN(BH2) then BH2ext[1] else BH2;
def BH2extline = BH2ext;
def H_BH2extline2 = Lowest(BH2extline, 1);
def BoxLow2 = if ((UP_OS) or (Lower_BandS crosses below Lower_BandK2) or (condition_BandRevUp) and (low < low[1]) and ((price < Lower_BandK2) or (price < Lower_BandS))) or ((UP_OS[1]) and (low < low[1])) then Lowest(low) else Double.NaN;
def BH1 = if !IsNaN(BoxHigh) then high else Double.NaN;
def BH1ext = if IsNaN(BH1) then BH1ext[1] else BH1;
def BH1extline = BH1ext;
def BL1 = if !IsNaN(BoxLow) then low else Double.NaN;
def BL1ext = if IsNaN(BL1) then BL1ext[1] else BL1;
def BL1extline2 = BL1ext;
def BL2 = if !IsNaN(BoxLow2) then low else Double.NaN;
def BL2ext = if IsNaN(BL2) then BL2ext[1] else BL2;
def BL2extline2 = BL2ext;
#####################################################
## KEY LEVELS                                      ##
#####################################################
def KeylevelOB = if ((BH1ext >= YHextlineOB) and (BL1ext <= YHextlineOB)) then 1 else 0;
def KeylevelOS = if ((BH2ext >= YHextlineOS) and (BL2ext <= YHextlineOS)) then 1 else 0;
def keyobpaint = (hl2 < BH1ext) and (hl2 > BL1ext);
def keyospaint = (hl2 < BH2ext) and (hl2 > BL2ext);
def Keylevel2OB = (round(absvalue(close - yhextlineOB),1));
def Keylevel2OS = (round(absvalue(close - yhextlineOS),1));
def control = 13;
def Keylevel2cond = if KeylevelOS and (Keylevel2OS < Keylevel2OB) and (Keylevel2OS < control) then 1 else if KeylevelOB and (Keylevel2OB < Keylevel2OS) and (Keylevel2OB < control) then -1 else 0;
#####################################################
## TESTING                                         ##
#####################################################
#def OB_Cross_Below = if price crosses Below YHextlineOB then 1 else 0;
#def OS_Cross_Above = if price crosses Above YHextlineOS then 1 else 0;
#def Key_Cross_down = if marketopen and keylevelOB and OB_Cross_Below then 1 else 0;
#def Key_Cross_up = if marketopen and keylevelOS and OS_Cross_Above then 1 else 0;
#####################################################
##                                                 ##
#####################################################
#plot Key_UP = if Key_Cross_Up then low else double.nan;
#Key_UP.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#Key_UP.SetDefaultColor(Color.yellow);
#Key_UP.HideBubble();
#Key_UP.HideTitle();
#####################################################
##                                                 ##
#####################################################
#plot Key_down = if Key_Cross_Down then high else double.nan;;
#Key_down.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#Key_down.SetDefaultColor(Color.yellow);
#Key_down.HideBubble();
#Key_down.HideTitle();
#####################################################
## SPARK                                           ##
#####################################################
def Agperiod1 = GetAggregationPeriod();
def timeframe = if agperiod1 <= aggregationperiod.DAY then aggregationperiod.DAY else agperiod1;
def Length9 = 35;
def Length8 = 10;
def AvgExp8 = ExpAverage(price[-displace], length8);
def UPD = AvgExp8[1] < AvgExp8;
def AvgExp9 = ExpAverage(price[-displace], length9);
def UPW = AvgExp9[1] < AvgExp9;
def Below = AvgExp8 < AvgExp9;
def Spark = UPD + UPW + Below;
def UPEMA = AvgExp8[1] < AvgExp8;
def DOWNEMA = AvgExp8[1] > AvgExp8;
def BigUP = direction == 1;
def BigDN = direction == -1;
def BigNa = direction == 0;
def UPEMA2 = AvgExp9[1] < AvgExp9;
def DOWNEMA2 = AvgExp9[1] > AvgExp9;
def UP8 = UPEMA and UPEMA2;
def DOWN8 = DOWNEMA and DOWNEMA2;
def PriceColor8 = if UP8 then 1 else if DOWN8 then -1 else 0;
def UP11 = UPEMA;
def DOWN11 = DOWNEMA;
def PriceColor11 = if UP11 then 1 else if DOWN11 then -1 else 0;
def UP12 = UPEMA2;
def DOWN12 = DOWNEMA2;
def PriceColor12 = if UP12 then 1 else if DOWN12 then -1 else 0;
def CandleColor = if (priceColor == 1) and (priceColor12 == 1) and (Spark >= 2) then 1 else if (priceColor == -1) and (Spark < 1) then -1 else 0;
def SparkUP1 = (Spark == 3) and (CandleColor == 1);
def SparkDN1 = (Spark == 0) and (CandleColor == -1);
def Hide_SparkUP = if SparkUP1 and (SparkUP1[1] or SparkUP1[2] or SparkUP1[3] or SparkUP1[4] or SparkUP1[5]) or (bigdn) then 0 else 1;
def Hide_SparkDN = if SparkDN1 and (SparkDN1[1] or SparkDN1[2] or SparkDN1[3] or SparkDN1[4] or SparkDN1[5]) or (bigup) then 0 else 1;
def Vol = volume(period = timeframe);
def at_High = high(period = timeframe);
def at_Open = open(period = timeframe);
def at_Close = close(period = timeframe);
def at_Low = low(period = timeframe);
def Buy_Volume = RoundUp(Vol * (at_Close - at_Low) / (at_High - at_Low));
def Buy_percent = RoundUp((Buy_Volume / Vol) * 100);
def Sell_Volume = RoundDown(Vol * (at_High - at_Close) / (at_High - at_Low));
def Sell_percent = RoundUp((Sell_Volume / Vol) * 100);
def avg1 = ExpAverage(close(period = agperiod1), length8);
def height = avg1 - avg1[length8];
def avg2 = ExpAverage(close(period = agperiod1), length8);                                          
def Condition1UP = avg1 > avg2;
def Condition1DN = avg1 < avg2;
def Condition2UP = Buy_percent > 50;
def Condition2DN = Buy_percent < 50;
def Condition3UP = if buyerRegular then 1 else 0;
def Condition3DN =  if sellerRegular then 1 else 0;
def Condition4UP =  if buyerExtreme then 1 else 0;
def Condition4DN =  if sellerExtreme then 1 else 0;
#####################################################
## FILTER                                          ##
#####################################################
def Big4_up_1;
def Big4_dn_1;
def TS_UP_1;
def TS_DN_1;
def SparkUP_1;
def SparkDN_1;
switch (Filter) {
case EMAD_Filter:
    Big4_up_1 = marketopen and EMAD_Above_Zero and (direction2 == 1 and direction2[1] < 1);
    Big4_dn_1 = marketopen and EMAD_Below_Zero and (direction2 == -1 and direction2[1] > -1);
    TS_UP_1 = if EMAD_Above_Zero and (upsignal and !direction_Equals_zero) then 1 else 0;  
    TS_DN_1 = if EMAD_Below_Zero and (downsignal and !direction_Equals_zero) then 1 else 0;
    SparkUP_1 = marketopen and EMAD_Above_Zero and (SparkUP1 and hide_SparkUP);
    SparkDN_1 = marketopen and EMAD_Above_Zero and (SparkDN1 and hide_SparkDN);
case Phase_Filter:
    Big4_up_1 = marketopen and bullphase and (direction2 == 1 and direction2[1] < 1);
    Big4_dn_1 = marketopen and bearphase and (direction2 == -1 and direction2[1] > -1);
    TS_UP_1 = if (upsignal and bullphase and !direction_Equals_zero) then 1 else 0;  
    TS_DN_1 = if (downsignal and bearphase and !direction_Equals_zero) then 1 else 0;
    SparkUP_1 = marketopen and bullphase and (SparkUP1 and hide_SparkUP);
    SparkDN_1 = marketopen and bearphase and (SparkDN1 and hide_SparkDN);
case No_Filter:
    Big4_up_1 = marketopen and (direction2 == 1 and direction2[1] < 1);
    Big4_dn_1 = marketopen and  (direction2 == -1 and direction2[1] > -1);
    TS_UP_1 = if (upsignal and !direction_Equals_zero) then 1 else 0;  
    TS_DN_1 = if (downsignal and !direction_Equals_zero) then 1 else 0;
    SparkUP_1 = marketopen and (SparkUP1 and hide_SparkUP);
    SparkDN_1 = marketopen and (SparkDN1 and hide_SparkDN);}
#####################################################
## DIRECTION                                       ##
#####################################################
def Big4_up_bn = if Big4_up_1 then bn else Big4_up_bn[1];
def Big4_dn_bn = if Big4_dn_1 then bn else Big4_dn_bn[1];
def TS_Up_bn = if TS_Up_1 then bn else TS_Up_bn[1];
def TS_Dn_bn = if TS_Dn_1 then bn else TS_Dn_bn[1];
def SparkUp_bn = if SparkUP_1 then bn else SparkUP_bn[1];
def SparkDn_bn = if SparkDN_1 then bn else SparkDN_bn[1];
def last_up_bn = if  Big4_up_1 or TS_Up_1 or SparkUP_1 then bn else last_up_bn[1];
def last_dn_bn = if  Big4_dn_1 or TS_dn_1 or SparkDN_1 then bn else last_dn_bn[1];
def Last_Arrow_Up = if last_up_bn > last_dn_bn then 1 else 0;
def Last_Arrow_Down = if last_dn_bn > last_up_bn then 1 else 0;
#####################################################
## CANDLE COLOR                                    ##
#####################################################
AssignPriceColor(if Color_Candles then
     if ALERT4 THEN COLOR.green
else if buyerRegular or buyerExtreme then Color.green
else if buyerRegular[1] or buyerExtreme[1] then Color.red
else if direction == 1 then Color.light_green
else if sellerRegular or sellerExtreme then Color.dark_red
else if sellerRegular[1] or sellerExtreme[1] then Color.green
else if direction == -1 then color.red
else if direction == 0 then Color.gray
else Color.GRAY
else Color.CURRENT);
#####################################################
## LINE PLOTS                                      ##
#####################################################
plot Squeeze_Alert = Squeeze_Level;
plot YCOB_PLOT = if !IsNaN(YHOB) then hl2 else Double.NaN;
plot YHextlineOB_PLOT = YHextlineOB;
plot YCOS_PLOT = if !IsNaN(YHOS) then hl2 else Double.NaN;
plot YHextlineOS_PLOT = YHextlineOS;
plot H_BH2extline = Lowest(BH2extline, 1);
plot BL1extline = BL1ext;
plot BL2extline = BL2ext;
plot H_BH1extline = Highest(BH1extline, 1);
plot L_BL1extline = Highest(BL1extline, 1);
plot L_BL2extline = Lowest(BL2extline, 1);
plot Ehlers_1 = Ehlers_Avg;
#####################################################
## ARROW PLOTS                                     ##
#####################################################
plot UP = UP1_1;
plot DOWN = DOWN1_1;
plot First_HL = First_HL_1;
plot First_LH = First_LH_1;
plot Second_HL = Second_HL_1;
plot Second_LH = Second_LH_1;
plot TS_UP = TS_UP_1;
plot TS_DN = TS_DN_1;
plot SparkUP = SparkUP_1;
plot SparkDN = SparkDN_1;
plot Big4_up = Big4_up_1;
plot Big4_dn = Big4_dn_1;
#####################################################
##                                                 ##
#####################################################
Ehlers_1.SetStyle(Curve.SHORT_DASH);
Ehlers_1.SetLineWeight(1);
Ehlers_1.AssignValueColor(color.gray);
#####################################################
##                                                 ##
#####################################################
Squeeze_Alert.SetPaintingStrategy(PaintingStrategy.POINTS);
Squeeze_Alert.SetLineWeight(3);
Squeeze_Alert.AssignValueColor(if(direction2 == 1) then color.yellow
                          else if direction2 == -1 then color.yellow
                          else if direction2 == 0 then color.light_Gray
                          else color.yellow);
#####################################################
##                                                 ##
#####################################################
YCOB_PLOT.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
YCOB_PLOT.SetDefaultColor(Color.GREEN);
YCOB_PLOT.SetLineWeight(1);
YCOB_PLOT.HideTitle();
#####################################################
##                                                 ##
#####################################################
YHextlineOB_PLOT.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
YHextlineOB_PLOT.SetDefaultColor(Color.ORANGE);
YHextlineOB_PLOT.SetLineWeight(2);
YHextlineOB_PLOT.HideTitle();
#####################################################
##                                                 ##
#####################################################
YCOS_PLOT.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
YCOS_PLOT.SetDefaultColor(Color.GREEN);
YCOS_PLOT.SetLineWeight(1);
YCOS_PLOT.HideTitle();
#####################################################
##                                                 ##
#####################################################
YHextlineOS_PLOT.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
YHextlineOS_PLOT.SetDefaultColor(Color.LIGHT_GREEN);
YHextlineOS_PLOT.SetLineWeight(2);
YHextlineOS_PLOT.HideTitle();
#####################################################
##                                                 ##
#####################################################
H_BH2extline.SetDefaultColor(Color.dark_GREEN);
H_BH2extline.SetLineWeight(1);
H_BH2extline.HideBubble();
H_BH2extline.HideTitle();
#####################################################
##                                                 ##
#####################################################
BL1extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BL1extline.SetDefaultColor(Color.dark_RED);
BL1extline.SetLineWeight(1);
BL1extline.HideTitle();
#####################################################
##                                                 ##
#####################################################
BL2extline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BL2extline.SetDefaultColor(Color.dark_GREEN);
BL2extline.SetLineWeight(1);
BL2extline.HideTitle();
#####################################################
##                                                 ##
#####################################################
H_BH1extline.SetDefaultColor(Color.dark_RED);
H_BH1extline.SetLineWeight(1);
H_BH1extline.HideBubble();
H_BH1extline.HideTitle();
#####################################################
##                                                 ##
#####################################################
L_BL1extline.SetDefaultColor(Color.dark_RED);
L_BL1extline.SetLineWeight(1);
L_BL1extline.HideBubble();
L_BL1extline.HideTitle();
#####################################################
##                                                 ##
#####################################################
L_BL2extline.SetDefaultColor(Color.dark_GREEN);
L_BL2extline.SetLineWeight(1);
L_BL2extline.HideBubble();
L_BL2extline.HideTitle();
#####################################################
##                                                 ##
#####################################################
UP.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UP.assignvaluecolor(if alert4 then color.CYAN else Color.white);
UP.HideBubble();
UP.HideTitle();
#####################################################
##                                                 ##
#####################################################
DOWN.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DOWN.SetDefaultColor(Color.white);
DOWN.HideBubble();
DOWN.HideTitle();
#####################################################
##                                                 ##
#####################################################
First_HL.SetPaintingStrategy(PaintingStrategy.ARROW_Up);
First_HL.SetDefaultColor(Color.magenta);
First_HL.HideBubble();
First_HL.HideTitle();
#####################################################
##                                                 ##
#####################################################
First_LH.SetPaintingStrategy(PaintingStrategy.ARROW_down);
First_LH.SetDefaultColor(Color.magenta);
First_LH.HideBubble();
First_LH.HideTitle();
#####################################################
##                                                 ##
#####################################################
Second_HL.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
Second_HL.SetDefaultColor(Color.orange);
Second_HL.HideBubble();
Second_HL.HideTitle();
#####################################################
##                                                 ##
#####################################################
Second_LH.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
Second_LH.SetDefaultColor(Color.orange);
Second_LH.HideBubble();
Second_LH.HideTitle();
#####################################################
##                                                 ##
#####################################################
Big4_up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Big4_up.AssignValueColor(if(direction2 == 1 and direction2[1] < 1)then color.blue else color.gray);
Big4_up.HideBubble();
Big4_up.HideTitle();
#####################################################
##                                                 ##
#####################################################
Big4_dn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Big4_dn.AssignValueColor(if(direction2 == -1 and direction2[1] > -1) then color.blue else color.gray);
Big4_dn.HideBubble();
Big4_dn.HideTitle();
#####################################################
##                                                 ##
#####################################################
TS_UP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
TS_UP.AssignValueColor(color.cyan);
TS_UP.HideBubble();
TS_UP.HideTitle();
#####################################################
##                                                 ##
#####################################################
TS_DN.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
TS_DN.AssignValueColor(color.cyan);
TS_DN.HideBubble();
TS_DN.HideTitle();
#####################################################
##                                                 ##
#####################################################
SparkUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
SparkUP.AssignValueColor(Color.green);
SparkUP.HideBubble();
SparkUP.HideTitle();
#####################################################
##                                                 ##
#####################################################
SparkDN.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SparkDN.AssignValueColor(Color.Light_red); 
SparkDN.HideBubble();
SparkDN.HideTitle();
#####################################################
##                                                 ##
#####################################################
AddCloud(if !HideCloud then BH1extline else Double.NaN, BL1extline, Color.DARK_RED, Color.GRAY);
AddCloud(if !HideCloud then BH2extline else Double.NaN, BL2extline, Color.DARK_GREEN, Color.GRAY);
#####################################################
##                                                 ##
#####################################################
#AddCloud(if (EMAD_Green_Cloud_1 or EMAD_Green_Cloud_2) then H_BH1extline else double.nan, L_BL2extline, color.dark_green, color.dark_green);
AddCloud(if (EMAD_Gray_Cloud_1 or EMAD_Gray_Cloud_2) then H_BH1extline else double.nan, L_BL2extline, color.dark_gray, color.dark_gray);
#AddCloud(if (EMAD_Red_Cloud_1 or EMAD_Red_Cloud_2) then H_BH1extline else double.nan, L_BL2extline, color.dark_red, color.dark_red);
#####################################################
## lABELS                                          ##
#####################################################
def Buy = UP_OS;
def Sell = DOWN_OB;
def conditionLTB = (conditionK2UP and (Consensus_Level < 0));
def conditionLTS = (conditionK3DN and (Consensus_Level > 0));
def conditionBO = ((Upper_BandS[1] < Upper_BandS))
              and ((Lower_BandS[1] < Lower_BandS))
              and ((Upper_BandK[1] < Upper_BandK))
              and ((Lower_BandK[1] < Lower_BandK));
def conditionBD = ((Upper_BandS[1] > Upper_BandS))
              and ((Lower_BandS[1] > Lower_BandS))
              and ((Upper_BandK[1] > Upper_BandK))
              and ((Lower_BandK[1] > Lower_BandK)); 
AddLabel(yes, if conditionLTB then "LOOK TO BUY"
         else if conditionLTS then "LOOK TO SELL"
         else if conditionK2UP then "TREND: BULL"
         else if conditionK3DN then "TREND: BEAR" else "TREND: CONSOLIDATION",
              if conditionLTB then Color.YELLOW
         else if conditionLTS then Color.YELLOW
         else if conditionK2UP then Color.LIGHT_GREEN
         else if conditionK3DN then Color.RED else Color.GRAY);
AddLabel(yes, if conditionBD then "BREAKDOWN"
         else if conditionBO then "BREAKOUT" else "NO BREAK",
              if conditionBD then Color.RED
         else if conditionBO then Color.GREEN else Color.GRAY);
AddLabel(show_Labels, if (Spark == 3) then "SPARK: " + Round(Spark, 1)
         else if (Spark == 0) then  "SPARK: " + Round(Spark, 1) else "SPARK: " + Round(Spark, 1),
              if (Spark == 3) then Color.orange
         else if (Spark == 2) then Color.light_GREEN
         else if (Spark == 0) then Color.RED else Color.GRAY);
AddLabel(show_labels, if  Condition1UP==1 and Condition2UP == 1 and (Condition3UP == 1 or Condition4UP == 1) then "**CALLS ONLY!**"
else if Condition1UP == 1 and Condition2UP == 1 then "VERY BULLISH" 
else if direction == 1 then "BULLISH"              
else if Condition1DN == 1 and Condition2DN == 1 and (Condition3DN == 1 or Condition4DN == 1) then "**PUTS ONLY!**"
else if Condition1DN == 1 and Condition2DN == 1 then "VERY BEARISH"
else if direction == -1 then "BEARISH"            
else if ((avg[1] > avg) and (avg > avg2) and (Buy_percent > 50)) then "BULLISH RETRACEMENT"
else if ((avg[1] < avg) and (avg < avg2) and (Buy_percent < 50)) then "BEARISH RETRACEMENT" else "CHOP",
     if Condition1UP == 1 and Condition2UP == 1 and (Condition3UP == 1 or Condition4UP == 1) then Color.cyan
else if Condition1DN == 1 and Condition2DN == 1 and (Condition3DN == 1 or Condition4DN == 1) then Color.Magenta
else if Condition1UP == 1 and Condition2UP == 1 then Color.light_GREEN
else if direction == 1 then Color.light_green                        
else if Condition1DN == 1 and Condition2DN == 1 then Color.RED
else if direction == -1 then Color.red         
else Color.orange);
AddLabel(yes, if MomentumUP then "Consensus Increasing = " + Round(Consensus_Level, 1)
else if MomentumUP or MomentumDOWN and conditionOB_CL then "Consensus OVERBOUGHT = " + Round(Consensus_Level, 1)
else if MomentumDOWN then  "Consensus Decreasing = " + Round(Consensus_Level, 1)
else if MomentumUP or MomentumDOWN and conditionOS_CL then "Consensus OVERSOLD = " + Round(Consensus_Level, 1)
else "Consensus = " + Round(Consensus_Level, 1), if conditionOB_CL then Color.light_green
else if conditionOS_CL then Color.red else Color.GRAY);
AddLabel(squeeze_Alert, "SQUEEZE ALERT", if Squeeze_Alert then Color.YELLOW else Color.GRAY);
AddLabel(KeylevelOS and keyospaint," Near Key Support Level: $" + round(yhextlineOS,0), color.light_green);
AddLabel(KeylevelOB and keyobpaint," Near Key Resistance Level: $" + round(yhextlineOB,0), color.orange);
AddLabel(show_labels,
     if TS_Last == 1 then " TS Last Signal: BUY "
else if TS_Last == 0 and ((alert4 or regular_Buy or extreme_buy) within 5 bars) then " TS Last Signal: SELL "
else if TS_Last == 0 then " TS Last Signal: SELL "
else "",
     if TS_Last == 1 then color.light_gray
else if TS_Last == 0 and((alert4 or regular_Buy or extreme_buy) within 5 bars) then color.orange
else if TS_Last == 0 then color.light_gray
else color.white);
addlabel(yes, " Filter In Use: " + Filter, color.white);
#####################################################
## END                                             ##
#####################################################

hey dude, hate to keep throwing questions at you but I really appreciate all your work and help so far.

Is there a way to make the dashed line color green or red based like in the previous versions?
 
I assume you are referring to the ehlers dashed line... the line is not colored in the study from that post because I took a step back to clean up the code re-think some things. That being said, I will update that shortly.
 
Hi, Im new to these charts that are made available. Can anyone recommend what setup of these charts to use Daytrading /scalping index futures. They look very promising.
 
Hi, Im new to these charts that are made available. Can anyone recommend what setup of these charts to use Daytrading /scalping index futures. They look very promising.
Use the most recent (posted today) is my recommendation... entry methods on page one still apply.
 
Ok this is awesome. Still lots to learn and test myself. Is there a way to make the Red and Green Cloud colors not so bright? Maybe change the opacity down like 50%
 
RwRzaKz.png


EMAD Variant Mobile: http://tos.mx/UhGOjQf (updated link... removed some unnecessary code and changed some default settings 9:15am 1/11/24)

Added the line coloring like the desktop version for the following conditions
:
1. Zeroline turns green or red when EMAs cross above or below the zeroline
2. Top and bottom bands turn green or red when stepping up or down individually or at the same time
3. EMAs colored based on being crossed up or down (whereas before in mobile there were two EMAs one green and the other red)
4. The other moving average (called "made" in settings) is colored also however the gray portions of the line are not working quite as they should but good enough for now
5. Top and bottom bands turn yellow when the zeroline is below the bottom band or when the zeroline is above the top band.

Notes concerning settings:
  • The "UP" and "DOWN" plots will need to be changed from lines to arrows in settings
  • The "Squeeze" plot can remain a solid line (or dots) but you will need to increase the line weight to 3 or 4 in settings

unJRHwt.jpg


Code:
   ##########################################################################################
   ##########################################################################################
   ###                                                                                    ###
   ###       #############################           #########               #########    ###
   ###     #################################         #########               #########    ###    
   ###    ####################################       #########               #########    ###
   ###   ########                     #########      #########               #########    ###         
   ###   ########   EMAD for MOBILE   #########      #########               #########    ###
   ###   ########   @Christopher84    #########      #########               #########    ###       
   ###   ########       @HODL         #########      #########               #########    ###     
   ###   ########                     #########      #########               #########    ###        
   ###   ######################################      ##################################   ###      
   ###    ####################################       ##################################   ###   
   ###     #################################         ##################################   ###
   ###    ####################################       ##################################   ###     
   ###   ########                     #########                              #########    ###
   ###   ########     @Horserider     #########                              #########    ###
   ###   ########  HORSERIDER VOLUME  #########                              #########    ###            
   ###   ########  TRIPLE EXHAUSTION  #########                              #########    ###        
   ###   ########      @chence27      #########                              #########    ###             
   ###   ########                     #########                              #########    ###         
   ###    ####################################                               #########    ###       
   ###     ##################################                                #########    ###        
   ###      ###############################                                  #########    ###      
   ###                                                                                    ###
   ##########################################################################################
   ##########################################################################################

#DECLARATIONS
declare lower;


#USER INPUTS
input use_alert4_filter = yes; #hint show alert4 arrow only when it appears with an UP arrow
input showverticalline = no;
input Ehlers_length = 34;
input Length_MAD = 4;
input emadLineWeight = 2;
input filterBounceArrows = 1; #hint lookback period for bothbandsup/down etc. fold
input cloudlinelookback = 20;  #hint lookback period for "made" average crossover fold used for arrows
input Steplookback = 10; #hint lookback period for top/bottomstepup/down etc. fold
input filterZeroline = 20; #hint lookback period for crosses up/down at zeroline etc. fold
input PreviousStepsDown = 2; #hint filter for hiding arrows
input PreviousStepsUp = 2; #hint filter for hiding arrows
input tradeDaytimeOnly = yes; #hint tradeDaytimeOnly: used to hide arrows
input OpenTime = 0730; #hint OpenTime: Opening time of market hides arrows
input CloseTime = 1800; #hint CloseTime: Closing time of market hides arrows
input firstTrade_Sq = {default long, short};
input trailType = {default modified, unmodified};
input firstTrade = {default long, short};

def bn = barnumber();
def Begin = SecondsFromTime(OpenTime);
def End = SecondsTillTime(CloseTime);
def isIntraDay = if GetAggregationPeriod() > 14400000 or GetAggregationPeriod() == 0 then 0 else 1;
def MarketOpen = if !tradeDaytimeOnly or !isIntraDay then 1 else if tradeDaytimeOnly and isIntraDay and Begin > 0 and End > 0 then 1 else 0;
def fastLength = 10;
def slowLength = 35;
def lengthBAD = 3;
def ATRPeriod = 5;
def ATRFactor = 2.0;
def length_3x = 1000;
def priceType = close;
def showTripleExh = yes;
def showHLLH = no;
def showtestlabels = no;
def bandLength = 100;
def bandLength2 = 100;
def smoothLength = 12;
def smoothLength2 = 14;
def averageType = AverageType.WILDERS;
def averageType_Sq = AverageType.SIMPLE;

#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));

## 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);
def zerolinedatacondition = if (zerolinedata > topBand) then topBand else if (zerolinedata < bottomBand) then bottomBand else 0;
## Master
def masteremadline = (upperEMADLine + lowerEMADLine) / 2;
## 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;
###############################################################
def crossesUpZeroline = if (valueUp) == 0 then 1 else 0;
def crossesDownZeroline = if (valueDown) == 0 then 1 else 0;
###############################################################
def crossesUpBottomlinebn = if crossesUpline and !crossesUpline[1] then bn else crossesUpBottomlinebn[1];
def crossesDownToplinebn = if crossesDownline and !crossesDownline[1] then bn else crossesDownToplinebn[1];
def crossuplinewithin = if crossesUpBottomlinebn < bn then bn - crossesUpBottomlinebn else crossuplinewithin[1];
def crossdownlinewithin = if crossesDownToplinebn < bn then bn - crossesDownToplinebn else crossdownlinewithin[1];
#def crosslinefirst = if crossuplinewithin < crossdownlinewithin then 1 else 0;
def crosslinelogic = crossesUpBottomlinebn > crossesDownToplinebn;
def HL = crossesUpline == 0 and crossesDownline == 0 and upperEMADLine crosses above lowerEMADLine;  #Normal HL LH condition
def LH = crossesDownline == 0 and upperEMADLine crosses below lowerEMADLine; #Normal HL LH condition
def bounceUpOnce = crossesUpline <> crossesUpline[1]; #XUpBtm happened previous bar or on current bar
def bounceDnOnce = crossesDownline <> crossesDownline[1];
def trackCrUp = if bounceUpOnce then 0 else if HL then 1 else trackCrUp[1];
def trackCrDn = if bounceDnOnce then 0 else if LH then 1 else trackCrDn[1];
## SECOND HL AND LH
def HLalready = if trackCrUp and !trackCrUp[1] then 1 else 0;
def LHalready = if trackCrDn and !trackCrDn[1] then 1 else 0;
def HL2 = HLalready == 0 and upperEMADLine crosses above lowerEMADLine;
def LH2 = LHalready == 0 and upperEMADLine crosses below lowerEMADLine;
def crossedUpOnce = HLalready <> HLalready[1];
def crossedDnOnce = LHalready <> LHalready[1];
def trackCrUp2 = if crossedUpOnce then 0 else if HL2 then 1 else trackCrUp2[1];
def trackCrDn2 = if crossedDnOnce then 0 else if LH2 then 1 else trackCrDn2[1];
#####################################################
##                                                 ##
#####################################################
def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
def HRef = if low <= high[1]
    then high - close[1]
    else (high - close[1]) - 0.5 * (low - high[1]);
def LRef = if high >= low[1]
    then close[1] - low
    else (close[1] - low) - 0.5 * (low[1] - high);
def trueRange_Sq;
switch (trailType) {
case modified:
    trueRange_Sq = Max(HiLo, Max(HRef, LRef));
case unmodified:
    trueRange_Sq = TrueRange(high, close, low);
}
def loss_Sq = ATRFactor * MovingAverage(averageType_Sq, trueRange_Sq, ATRPeriod);
def state_Sq = {default init, long, short};
def trail_Sq;
switch (state_Sq[1]) {
case init:
    if (!IsNaN(loss_Sq)) {
        switch (firstTrade) {
        case long:
            state_Sq = state_Sq.long;
            trail_Sq =  close - loss_Sq;
        case short:
            state_Sq = state_Sq.short;
            trail_Sq = close + loss_Sq;
    }
    } else {
        state_Sq = state_Sq.init;
        trail_Sq = Double.NaN;
    }
case long:
    if (close > trail_Sq[1]) {
        state_Sq = state_Sq.long;
        trail_Sq = Max(trail_Sq[1], close - loss_Sq);
    } else {
        state_Sq = state_Sq.short;
        trail_Sq = close + loss_Sq;
    }
case short:
    if (close < trail_Sq[1]) {
        state_Sq = state_Sq.short;
        trail_Sq = Min(trail_Sq[1], close + loss_Sq);
    } else {
        state_Sq = state_Sq.long;
        trail_Sq =  close - loss_Sq;
    }
}
def TrailingStop_Sq = trail_Sq;
def H = Highest(TrailingStop_Sq, 12);
def L = Lowest(TrailingStop_Sq, 12);
def BulgeLengthPrice = 100;
def SqueezeLengthPrice = 100;
def BandwidthC3 = (H - L);
def IntermResistance2 = Highest(BandwidthC3, BulgeLengthPrice);
def IntermSupport2 = Lowest(BandwidthC3, SqueezeLengthPrice);
def sqzTrigger = BandwidthC3 <= IntermSupport2;
def sqzLevel = if !sqzTrigger[1] and sqzTrigger then hl2 else if !sqzTrigger then Double.NaN else sqzLevel[1];
def Squeeze_Alert_1 = if sqzLevel then (upperEMADLine + lowerEMADLine) / 2 else (upperEMADLine + lowerEMADLine) / 2 ;
#####################################################
##                                                 ##
#####################################################
def over_bought_3x = 80;
def over_sold_3x = 20;
def KPeriod_3x = 10;
def DPeriod_3x = 10;
def priceH1 = emadhigh;
def priceL1 =  emadlow;
def priceC1 =  emadclose;
def priceO1 =  emadclose;
def priceH_3x =  emadhigh;
def priceL_3x =  emadlow;
def priceC_3x =  emadclose;   
def SlowK_3x = reference StochasticFull(over_bought_3x,  over_sold_3x,  KPeriod_3x,  DPeriod_3x,  priceH_3x,  priceL_3x,  priceC_3x,  3, if (averageType == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;
def MACD_3x = reference MACD()."Value";
def priceMean_3x = Average(MACD_3x, length_3x);
def MACD_stdev_3x =  (MACD_3x - priceMean_3x) / StDev(MACD_3x, length_3x);
def dPlus_3x = reference DMI()."DI+";
def dMinus_3x = reference DMI()."DI-";
def sellerRegular = SlowK_3x < 20 and MACD_stdev_3x < -1 and dPlus_3x < 15;
def sellerExtreme = SlowK_3x < 20 and MACD_stdev_3x < -2 and dPlus_3x < 15;
def buyerRegular = SlowK_3x > 80 and MACD_stdev_3x > 1 and dMinus_3x < 15;
def buyerExtreme = SlowK_3x > 80 and MACD_stdev_3x > 2 and dMinus_3x < 15;   
def ExtremeBuy1 = if sellerExtreme[1] and !sellerExtreme then 1 else 0;
def ExtremeSell1 = if buyerExtreme[1] and !buyerExtreme then 1 else 0;
def RegularSell1 = if buyerRegular[1] and !buyerRegular then 1 else 0;
def RegularBuy1= if sellerRegular[1] and !sellerRegular then 1 else 0;
#####################################################
##                                                 ##
#####################################################
def priceE = (emadhigh + emadlow) / 2;
def coeff = Ehlers_length * priceE * priceE - 2 * priceE * sum(priceE, Ehlers_length)[1] + sum(priceE * priceE, Ehlers_length)[1];
def Ehlers =  sum(coeff * priceE, Ehlers_length) / sum(coeff, Ehlers_length);
#####################################################
##                                                 ##
#####################################################
def dataA = masterEMADLine;
def dataA1 =
    fold aD = 0
    to LengtH_MAD
    with k = 0
    do k + dataA[aD];
def MAEMAD = dataA1 / LengtH_MAD;
def MAD = MAEMAD;
def ElCrossDN = ehlers crosses above masterEMADline;
def ElCrossUP = ehlers crosses below masterEMADline;
def bullBias2 = (emadsup);
def bearBias2 = (emadsdown);
def MADehlers = (MAD - Ehlers)/2;
def Madebad = madehlers + ehlers;
def madebadavg = average(madebad, lengthBAD);
def crossmadeup = madebad > madebad[1];
def crossmadedn = madebad < madebad[1];
def crossmade2up = if (madebad < masterEMADLine) and crossmadeup and (bullBias2 > 0) then 1 else 0;
def crossmade2dn = if (madebad > masterEMADLine) and crossmadedn and (bearBias2 > 0) then 1 else 0;
def crossmade2 = if (madebad < masterEMADLine) and crossmadeup and (bullBias2 > 0) then 1 else if (madebad > masterEMADLine) and crossmadedn and (bearBias2 > 0) then -1 else 0;
def crossmade0 = if (!crossmade2[1] or !crossmade2[1]) and (crossmade2 == 0) then 1 else 0;
#####################################################
##                                                 ##
#####################################################
def pd = 22;
def bbl = 20;
def mult = 2.0;
def lb = 50;
def ph = 0.85;
def ltLB = 40;
def mtLB = 14;
def str = 3;
def wvf = ((Highest(close, pd) - low) / (Highest(close, pd))) * 100;
def sDev = mult * StDev(wvf, bbl);
def midLine1 = SimpleMovingAvg(wvf, bbl);
def lowerBand1 = midLine1 - sDev;
def upperBand1 = midLine1 + sDev;
def rangeHigh = (Highest(wvf, lb)) * ph;
def upRange = low > low[1] and close > high[1];
def upRange_Aggr = close > close[1] and close > open[1];
def filtered = ((wvf[1] >= upperBand1[1] or wvf[1] >= rangeHigh[1]) and (wvf < upperBand1 and wvf < rangeHigh));
def filtered_Aggr = (wvf[1] >= upperBand1[1] or wvf[1] >= rangeHigh[1]);
def alert4 = upRange_Aggr and close > close[str] and (close < close[ltLB] or close < close[mtLB]) and filtered_Aggr;
def alert4close = if alert4 then close else 0;
def crossmade = if (madebad < masterEMADLine) and crossmadeup then 1 else if (madebad > masterEMADLine) and crossmadedn then -1 else 0;
def madedn = (madebad > masterEMADLine);
def madeup = (madebad < masterEMADLine);
def made2 = madebadavg;
def made2_abovezero = (made2 > zerolinedata);
def made2_belowzero = (made2 < zerolinedata);
def made3 = if !madedn and made2_belowzero then madebadavg else double.nan;
def made4 = if !madeup and made2_abovezero then madebadavg else double.nan;
def made5 = if (made2 == zerolinedata) then madebadavg else double.nan;
def madecrossupavg = if made2 crosses below masterEMADLine then 1 else 0;
def madecrossdownavg = if made2 crosses above masterEMADLine then 1 else 0;
def crossmadelogicup = if madecrossupavg and !madecrossupavg[1] then 1 else 0;
def crossmadelogicdown = if madecrossdownavg and !madecrossdownavg[1] then 1 else 0;
def crossmademorelogicup = if crossmadelogicup and crosslinelogic and emadup[1] then 1 else 0;
def crossmademorelogicdown = if crossmadelogicdown and !crosslinelogic and emaddown[1] then 1 else 0;
def EmaPushStepUp = if topBandStepUp and EmadUp and (((upperEMADLine) - bottomband) == 0) then 1 else 0;;
def EmaPushStepDown = if bottomBandStepDown and EmadDown and (((lowerEMADLine) - bottomband) == 0) then 1 else 0;
def HLFilter = if HL[1] < HL then 1 else 0;
def LHFilter = if LH[1] > LH then 1 else 0;
def HLFilter2 = if HL[2] < HL[1] then 1 else 0;
def LHFilter2 = if LH[2] > LH[1] then 1 else 0;
#####################################################
##                                                 ##
#####################################################
def topBandStepDown_lookback = fold index3 = 1 to Steplookback with x3 do x3 + topBandStepDown[index3];
def topBandStepUp_lookback = fold index4 = 1 to Steplookback with x4 do x4 + topBandStepUp[index4];
def bottomBandStepDown_lookback = fold index5 = 1 to Steplookback with x5 do x5 + bottomBandStepDown[index5];
def bottomBandStepUp_lookback = fold index6 = 1 to Steplookback with x6 do x6 + bottomBandStepUp[index6];
def bothBandsDown_lookback = fold index7 = 1 to filterBounceArrows with x7 do x7 + bothBandsDown[index7];
def bothBandsUp_lookback = fold index8 = 1 to filterBounceArrows with x8 do x8 + bothBandsUp[index8];
def bullBias_lookback = fold index9 = 1 to filterBounceArrows with x9 do x9 + bullBias[index9];
def bearBias_lookback = fold index10 = 1 to filterBounceArrows with x10 do x10 + bearBias[index10];
######################################################################################################################
def crossesUpZeroline_lookback = fold index11 = 1 to filterZeroline with x11 do x11 + crossesUpZeroline[index11];
def crossesDownZeroline_lookback = fold index12 = 1 to filterZeroline with x12 do x12 + crossesDownZeroline[index12];
######################################################################################################################
def crossesUpBottomline_lookback = fold index13 = 1 to filterBounceArrows with x13 do x13 + crossesUpline[index13];
def crossesDownTopline_lookback = fold index14 = 1 to filterBounceArrows with x14 do x14 + crossesDownline[index14];
def crossmade0_lookback = fold index33 = 1 to cloudlinelookback with x33 do x33 + crossmade0[index33];
#####################################################
##                                                 ##
#####################################################
def bubblehidertopdown = topBandStepDown_lookback > PreviousStepsDown;
def bubblehidertopup = topBandStepup_lookback > PreviousStepsUp;
def bubblehiderbottomdown = bottomBandStepDown_lookback > PreviousStepsDown;
def bubblehiderbottomup = bottomBandStepup_lookback > PreviousStepsUp;
def UP1 = if marketopen and (!bubblehidertopdown or !bubblehiderbottomdown) and !EmaPushStepDown and crossmademorelogicup then midband else double.nan;
def DOWN1 = if marketopen and (!bubblehidertopup or !bubblehiderbottomup) and !EmaPushStepUp and crossmademorelogicdown then midband else double.nan;
def made1 = made4;
def made = made3;
def madecolor_1 = if crossmade2 == 1 then made3 else double.nan;
def madecolor_2 = if crossmade2 == -1 then made3 else double.nan;
def madecolor_3 = if crossmade0_lookback > 1 and crosslinelogic then made3 else double.nan;
def madecolor = if crossmade2 == 0 then made3 else double.nan;
def madecolor1_1 = if crossmade2 == 1 then made4 else double.nan;
def madecolor1_2 = if crossmade2 == -1 then made4 else double.nan;
def madecolor1_3 = if crossmade0_lookback > 1 and crosslinelogic then made4 else double.nan;
def madecolor1 = if  crossmade2 == 0 then made4 else double.nan;
def UP_Alert4_1 = if (alert4) then midband else double.nan;
def masterEMADLinePlot2_1 = (upperEMADLine + lowerEMADLine) / 2;
def masterEMADLinePlot2_Up = if (masterEMADLinePlot2_1 >= masterEMADLinePlot2_1[1]) then masterEMADLinePlot2_1 else double.nan;
def masterEMADLinePlot2_Down = if (masterEMADLinePlot2_1 < masterEMADLinePlot2_1[1]) then masterEMADLinePlot2_1 else double.nan;
def TopBottomSpan = absvalue(topband) + absvalue(bottomband);
def TopBottomOffset_2 = absvalue(topband)/5;
#####################################################
##                                                 ##
#####################################################
plot zeroLine = if (upperEMADLine > zerolinedata) then zerolinedata else double.nan;
plot zeroLine_2 = if (upperEMADLine < zerolinedata) then zerolinedata else double.nan;
plot zeroLine_3 = if (!zeroline and !zeroline_2) then zerolinedata else double.nan;
plot Squeeze_Alert = if sqzLevel then (upperEMADLine + lowerEMADLine) / 2 else (upperEMADLine + lowerEMADLine) / 2 ;
plot ExtremeBuy = if showTripleExh and sellerExtreme[1] and !sellerExtreme then midband else Double.NaN;
plot ExtremeSell = if showTripleExh and buyerExtreme[1] and !buyerExtreme then midband else Double.NaN;
plot RegularSell = if showTripleExh and buyerRegular[1] and !buyerRegular then midband else Double.NaN;
plot RegularBuy = if showTripleExh and sellerRegular[1] and !sellerRegular then midband else Double.NaN;
plot topBandPlot_offset = topBand +  TopBottomOffset_2;
plot topBandPlot = if !bothBandsDown and !bothBandsUp and !topBandStepUp and !topBandStepDown then topBand else double.nan;
plot topBandPlot_1 = if bothBandsDown then topBand else double.nan;
plot topBandPlot_2 = if bothBandsup then topBand else double.nan;
plot topBandPlot_3 = if topBandStepUp then topBand else double.nan;
plot topBandPlot_4 = if topBandStepDown then topBand else double.nan;
plot topBandPlot_5 = if bearbias then topBand else double.nan;
plot bottomBandPlot_offset = bottomBand - TopBottomOffset_2;
plot bottomBandPlot = if (!bothBandsDown and !bothBandsUp and !bottomBandStepUp and !bottomBandStepDown) or (bottomband == bottomband[1]) then bottomBand else double.nan;
plot bottomBandPlot_1 = if bothBandsDown then bottomBand else double.nan;
plot bottomBandPlot_2 = if bothBandsUp then bottomBand else double.nan;
plot bottomBandPlot_3 = if bottomBandStepUp then bottomBand else double.nan;
plot bottomBandPlot_4 = if bottomBandStepDown then bottomBand else double.nan;
plot bottomBandPlot_5 = if bullbias then bottomBand else double.nan;
plot upperEMADLinePlot_3 = upperEMADLine;
plot lowerEMADLinePlot_3 = lowerEMADline;
plot masterEMADLinePlot =  masterEMADLinePlot2_1;
plot masterEMADLinePlot_Up =  masterEMADLinePlot2_Up;
plot masterEMADLinePlot_Down =  masterEMADLinePlot2_Down;
plot made55 = made5;
plot made1_1 = madecolor_1;
plot made1_2 = madecolor_2;
plot made1_3 = madecolor_3;
plot made1_0 = madecolor;
plot made_1 = madecolor1_1;
plot made_2 = madecolor1_2;
plot made_3 = madecolor1_3;
plot made_0 = madecolor1;
plot UP_Alert4 = UP_Alert4_1;
plot UP = UP1;
plot DOWN = DOWN1;
#####################################################
##                                                 ##
#####################################################
UP_Alert4.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UP_Alert4.SetLineWeight(1);
UP_Alert4.SetDefaultColor(Color.cyan);
UP_Alert4.HideBubble();
UP_Alert4.HideTitle();
#####################################################
##                                                 ##
#####################################################
UP.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UP.SetLineWeight(1);
UP.SetDefaultColor(Color.white);
UP.HideBubble();
UP.HideTitle();
#####################################################
##                                                 ##
#####################################################
DOWN.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DOWN.SetLineWeight(1);
DOWN.SetDefaultColor(Color.white);
DOWN.HideBubble();
DOWN.HideTitle();
#####################################################
##                                                 ##
#####################################################
made55.SetDefaultColor(color.gray);
#####################################################
##                                                 ##
#####################################################
made1_1.SetDefaultColor(color.dark_green);
#####################################################
##                                                 ##
#####################################################
made1_2.SetDefaultColor(color.dark_red);
#####################################################
##                                                 ##
#####################################################
made1_3.SetDefaultColor(color.gray);
#####################################################
##                                                 ##
#####################################################
made1_0.SetDefaultColor(color.dark_green);
#####################################################
##                                                 ##
#####################################################
made_1.SetDefaultColor(color.dark_green);
#####################################################
##                                                 ##
#####################################################
made_2.SetDefaultColor(color.dark_red);
#####################################################
##                                                 ##
#####################################################
made_3.SetDefaultColor(color.gray);
#####################################################
##                                                 ##
#####################################################
made_0.SetDefaultColor(color.dark_red);
#####################################################
##                                                 ##
#####################################################
Squeeze_Alert.SetPaintingStrategy(PaintingStrategy.line);
Squeeze_Alert.SetLineWeight(3);
Squeeze_Alert.SetDefaultColor(Color.YELLOW);
#####################################################
##                                                 ##
#####################################################
RegularBuy.SetPaintingStrategy(PaintingStrategy.Histogram);
ExtremeBuy.SetPaintingStrategy(PaintingStrategy.Histogram);
RegularSell.SetPaintingStrategy(PaintingStrategy.Histogram);
ExtremeSell.SetPaintingStrategy(PaintingStrategy.Histogram);
#####################################################
##                                                 ##
#####################################################
RegularBuy.SetLineWeight((1));
ExtremeBuy.SetLineWeight((1));
RegularSell.SetLineWeight((1));
ExtremeSell.SetLineWeight((1));
#####################################################
##                                                 ##
#####################################################
ExtremeSell.SetDefaultColor((Color.red));
ExtremeBuy.SetDefaultColor((Color.green));
RegularBuy.SetDefaultColor((Color.green));
RegularSell.SetDefaultColor((Color.red));
#####################################################
##                                                 ##
#####################################################
upperEMADLinePlot_3.HideTitle();
upperEMADLinePlot_3.HideBubble();
upperEMADLinePlot_3.SetLineWeight((1));
upperEMADLinePlot_3.SetDefaultColor(color.dark_gray
);
lowerEMADLinePlot_3.HideTitle();
lowerEMADLinePlot_3.HideBubble();
lowerEMADLinePlot_3.SetLineWeight((1));
lowerEMADLinePlot_3.SetDefaultColor(color.dark_gray
);
masterEMADLinePlot_Up.HideTitle();
masterEMADLinePlot_Up.HideBubble();
masterEMADLinePlot_Up.SetLineWeight((2));
masterEMADLinePlot_Up.SetDefaultColor(color.green
);
masterEMADLinePlot_Down.HideTitle();
masterEMADLinePlot_Down.HideBubble();
masterEMADLinePlot_Down.SetLineWeight((1));
masterEMADLinePlot_Down.SetDefaultColor(color.red
);
masterEMADLinePlot.HideTitle();
masterEMADLinePlot.HideBubble();
masterEMADLinePlot.SetLineWeight((1));
masterEMADLinePlot.SetDefaultColor(color.gray
);
zeroLine.HideTitle();
zeroLine.HideBubble();
zeroLine.SetDefaultColor(Color.green
);
zeroLine_2.HideTitle();
zeroLine_2.HideBubble();
zeroLine_2.SetDefaultColor(color.red
);
zeroLine_3.HideTitle();
zeroLine_3.HideBubble();
zeroLine_3.SetDefaultColor(color.gray
);
topBandPlot_offset.SetPaintingStrategy(PaintingStrategy.liNE_VS_POINTS);
topBandPlot_offset.HideTitle();
topBandPlot_offset.HideBubble();
topBandPlot_offset.SetDefaultColor(Color.black
);
topBandPlot.HideTitle();
topBandPlot.HideBubble();
topBandPlot.SetDefaultColor(Color.gray
);
topBandPlot_1.HideTitle();
topBandPlot_1.HideBubble();
topBandPlot_1.SetdefaultColor(Color.red
);
topBandPlot_2.HideTitle();
topBandPlot_2.HideBubble();
topBandPlot_2.SetDefaultColor(Color.green
);
topBandPlot_3.HideTitle();
topBandPlot_3.HideBubble();
topBandPlot_3.SetDefaultColor(Color.green
);
topBandPlot_4.HideTitle();
topBandPlot_4.HideBubble();
topBandPlot_4.SetDefaultColor(Color.red
);
topBandPlot_5.HideTitle();
topBandPlot_5.HideBubble();
topBandPlot_5.SetDefaultColor(Color.yellow
);
BottomBandPlot_offset.SetPaintingStrategy(PaintingStrategy.liNE_VS_POINTS);
BottomBandPlot_offset.HideTitle();
BottomBandPlot_offset.HideBubble();
BottomBandPlot_offset.SetDefaultColor(Color.black
);
BottomBandPlot.HideTitle();
BottomBandPlot.HideBubble();
BottomBandPlot.SetDefaultColor(Color.gray
);
BottomBandPlot_1.HideTitle();
BottomBandPlot_1.HideBubble();
BottomBandPlot_1.SetDefaultColor(Color.red
);
BottomBandPlot_2.HideTitle();
BottomBandPlot_2.HideBubble();
BottomBandPlot_2.SetDefaultColor(Color.green
);
BottomBandPlot_3.HideTitle();
BottomBandPlot_3.HideBubble();
BottomBandPlot_3.SetDefaultColor(Color.green
);
BottomBandPlot_4.HideTitle();
BottomBandPlot_4.HideBubble();
BottomBandPlot_4.SetDefaultColor(Color.red
);
BottomBandPlot_5.HideTitle();
BottomBandPlot_5.HideBubble();
BottomBandPlot_5.SetDefaultColor(Color.yellow
);
#####################################################
##                                                 ##
#####################################################
AddVerticalLine(showVerticalline and crossesUpline and crossesUpline_filter, "", GlobalColor("Green"), no);
AddVerticalLine(showVerticalline and crossesDownline and crossesDownline_filter,"" , GlobalColor("Red"), yes);

Hate to annoy you with all these questions: What study is shown in the upper chart? Thanks!
 
@HODL-Lay-HE-hoo! Do the signal arrows n the EMAD repaint?

Check out EMAD on upper chart.
View attachment 20816
nope no repaint... the emad indication does show divergence pretty well... Also im working on something now that is going surprisingly well well... will post soon...
As for the bad signal... none of the indications are not meant to show divergence as the entry method described on page one does not require divergence. Really they are an attempt to show the HL and LH.

There is a study that @BenTen added divergence tracking with pivot points i think? been considering adding something like that also.
 
Last edited:
After testing it out since last Christmas! Based on my experience I look at how all the indicators perform on consolidating markets.

I usually see the same thing. Low volume with little price action. Indicators can only do their best in these situations but I know OB and OS on RSI and MFI performs well in these types of markets.

I think a study of volume as an aggregate would be a game changer. Not an easy task but may clear away signals in choppy markets etc.

Really great work. It looks amazing with less signals. If I remember correctly its unable to be scanned with. Is there a way to setup alerts?
 
Status
Not open for further replies.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
198 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top