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