Also I created a watchlist based on this with added squeeze. Can @barbaros @cos251 @Trader Raider @Chuck you have a look and refine.May be I was using different settings on macd length that made the difference. which one to use? 25 0r 9? I see fw__dpo_mobo is more sensitive with dpo moving up and down although its above bb_upper. macd stays less sensitive which can be better.
Code:
#################################################################
############ SCAN Variables #########
#################################################################
# MACD_BB
input MACDBB_BandLength = 10;
input MACDBB_FastLength = 12;
input MACDBB_SlowLength = 26;
input MACDBB_Length = 5;
input MACDBB_NumDev = 1.0;
input showBreakoutSignals = no;
def MACDBB_Data = MACD(fastLength = MACDBB_FastLength, slowLength = MACDBB_SlowLength, MACDLength = MACDBB_length);
plot MACDBB_Upper = reference BollingerBands(price = MACDBB_data, length = MACDBB_BandLength, Num_Dev_Dn = -MACDBB_NumDev, Num_Dev_Up = MACDBB_NumDev).UpperBand;
plot MACDBB_Lower = reference BollingerBands(price = MACDBB_data, length = MACDBB_BandLength,Num_Dev_Dn = -MACDBB_NumDev, Num_Dev_Up = MACDBB_NumDev).Lowerband;
plot MACDBB_Midline = reference BollingerBands(price = MACDBB_data, length = MACDBB_BandLength,
Num_Dev_Dn = -MACDBB_NumDev, Num_Dev_Up = MACDBB_NumDev).MidLine;
MACDBB_Midline.SetDefaultColor(Color.Gray);
MACDBB_Midline.SetStyle(Curve.FIRM);
MACDBB_MidLine.HideTitle();
MACDBB_MidLine.HideBubble();
plot MACDBB_Line = MACDBB_Data;
MACDBB_Line.SetLineWeight(1);
plot MACDBB_Dots = MACDBB_Data;
# Keltner Channels
input klength = 10;
input factormid = 1.5;
input factorhigh = 1.0;
input factorlow = 2.0;
input averageType = AverageType.EXPONENTIAL;
input trueRangeAverageType = AverageType.EXPONENTIAL;
def shifthigh = factorhigh * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), klength);
def shiftMid = factormid * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), klength);
def shiftlow = factorlow * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), klength);
def average = MovingAverage(averageType, close, klength);
def Upper_Band = average + shiftmid;
def Lower_Band = average - shiftmid;
# Bollinger Bands
input BBLength2 = 10;
input Num_Dev_Dn = -2.4;
input Num_Dev_up = 2.4;
input bb_averageType = AverageType.SIMPLE;
def sDev = StDev(data = close, length = BBLength2);
def MidLine = MovingAverage(bb_averageType, data = close, length = BBLength2);
def LowerBandbb = MidLine + Num_Dev_Dn * sDev;
def UpperBandbb = MidLine + Num_Dev_up * sDev;
def UpperBandKCLow = average + shiftlow;
def LowerBandKCLow = average - shiftlow;
def UpperBandKCMid = average + shiftMid;
def LowerBandKCMid = average - shiftMid;
def UpperBandKCHigh = average + shifthigh;
def LowerBandKCHigh = average - shifthigh;
def K = (Highest(high, klength) + Lowest(low,klength)) /
2 + ExpAverage(close, klength);
def momo = Inertia(close - K / 2, klength);
def pos = momo >= 0;
def neg = momo < 0;
def up = momo >= momo[1];
def dn = momo < momo[1];
def presqueeze = LowerBandBB > LowerBandKCLow and UpperBandBB < UpperBandKCLow;
def presqueezein = LowerBandBB > LowerBandKCLow and UpperBandBB < UpperBandKCLow and lowerBandBB > lowerBandBB[1];
def presqueezeout = LowerBandBB > LowerBandKCLow and UpperBandBB < UpperBandKCLow and lowerbandbb < lowerbandbb[1];
def originalSqueezein = LowerBandBB > LowerBandKCMid and UpperBandBB < UpperBandKCMid and lowerbandBB > lowerbandbb[1];
def originalSqueezeout = LowerBandBB > LowerBandKCMid and UpperBandBB < UpperBandKCMid and lowerbandbb < lowerbandbb[1];
def ExtrSqueeze = LowerBandBB > LowerBandKCHigh and UpperBandBB < UpperBandKCHigh;
def Squeeze = if Extrsqueeze then 0 else if originalsqueezein then 0 else if originalsqueezeout then 0 else 3 ;
# RSI/STOCASTIC/MACD CONFLUENCE COMBO
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageTypeMACD = AverageType.WEIGHTED;
def Value = MovingAverage(averageTypeMACD, close, fastLength) - MovingAverage(averageTypeMACD, close, slowLength);
def Avg = MovingAverage(averageTypeMACD, Value, MACDLength);
plot RSM_MACD_Diff = Value - Avg;
#####
input lengthRSI = 7;
input averageTypeRSI = AverageType.EXPONENTIAL;
def NetChgAvg = MovingAverage(averageTypeRSI, close - close[1], lengthRSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(close - close[1]), lengthRSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSM_RSI = 50 * (ChgRatio + 1);
#####
def RSM_Stoch_Val = 100 * (close - lowest(low, 14)) / (highest(high, 14) - lowest(low, 14));
def RSM_StochSlowK = SimpleMovingAvg(SimpleMovingAvg(RSM_Stoch_Val,3),3);
def RSM_rsiGreen = RSM_RSI >= 50;
def RSM_rsiRed = RSM_RSI < 50;
def RSM_stochGreen = RSM_StochSlowK >= 50;
def RSM_stochRed = RSM_StochSlowK < 50;
def RSM_macdGreen = RSM_MACD_Diff >= 0;
def RSM_macdRed = RSM_MACD_Diff < 0;
def RSM_Buy = RSM_rsiGreen and RSM_stochGreen and RSM_macdGreen;
def RSM_Sell = RSM_rsiRed and RSM_stochRed and RSM_macdRed;
input price= close;
def avgexpo = MovingAverage(averageType, price, klength);
def RSM_EMAGreen = close > avgexpo;
def RSM_EMARed = close < avgexpo;
# Shade areas based on criteria; adjust as needed
###################################################
# If you want to scan for stocks that are not in either trend you can add two filters and scan for false for both conditions
# The UpTrend and DownTrend plots can be used to scan for stocks that are currently in that trend
def UPP = MACDbb_Line >0 and MACDbb_Line > MACDbb_Line[1] and MACDbb_Line >macdbb_upper and RSM_Buy and squeeze ==3 ;
def DOWN = MACDbb_Line <0 and MACDbb_Line < MACDbb_Line[1] and MACDbb_Line < macdbb_lower and RSM_Sell and squeeze ==3;;
def UpTrend = MACDbb_Line > MACDbb_Line[1] and MACDbb_Line > macdbb_upper and macdbb_line < 0 and RSM_Buy and squeeze ==3;
def DownTrend = MACDbb_Line < MACDbb_Line[1] and MACDbb_Line < macdbb_lower and macdbb_line > 0 and RSM_Sell and squeeze ==3;;
# The UpTrendJustStarted and DownTrendJustStarted plots can be used to find stocks that have just started
# a trend in either direction
def UpTrendJustStarted = MACDbb_Line crosses above macdbb_upper and RSM_Buy and squeeze ==3;
def DownTrendJustStarted = MACDbb_line crosses below macdbb_lower and RSM_Sell and squeeze ==3;;
def Downtrendended = MACDbb_Line > MACDbb_Line[1] and macdbb_line <macdbb_upper and RSM_Buy and squeeze ==3;;
def Uptrendended = MACDbb_Line < MACDbb_Line[1] and macdbb_line > macdbb_lower and RSM_Sell and squeeze ==3;
###################################################
def GetTrend = if UptrendJustStarted then 8 else if Uptrend then 9 else if UPP then 3 else if DownTrendJustStarted then -8 else if DownTrend then -9 else if DOWN then -3 else if Downtrendended then 7 else if Uptrendended then -7 else 0;
###################################################
def count = if macdbb_line>= macdbb_upper then if macdbb_dots >macdbb_dots[1] then count[1]+1 else 0 else if macdbb_line<= macdbb_lower then if macdbb_dots <macdbb_dots[1] then count[1]+1 else 0 else 0;
def barnumber = if count == 0 then barNumber[1]+1 else 0;
def macdsar = macdbb_dots - macdbb_dots[1];
###################################################
def MBullish = gettrend == 3 and squeeze ==3;
def MBearish = gettrend == -3 and squeeze ==3;
def BBullish = gettrend == 8 and squeeze ==3;
def BBearish = gettrend == -8 and squeeze ==3;
def SBullish = gettrend == 9 and squeeze ==3;
def SBearish = gettrend == -9 and squeeze ==3;
def ABullish = gettrend == 7 and squeeze ==3;
def ABearish = gettrend == -7 and squeeze ==3;
assignbackgroundcolor( if MBullish then color.lime else if MBearish then color.magenta else if BBullish then color.dark_green else if BBearish then color.dark_red else if SBullish then color.green else if SBearish then color.red else if Abullish then color.cyan else if Abearish then color.yellow else color.white);
addlabel(yes, if MBullish then "0UP" + count else if MBearish then "Fdown" + count else if BBullish then "ABull" + count else if BBearish then "GBear" + count else if SBullish then "BBull" + count else if SBearish then "HBear" + count else if Abullish then "Dbull" else if Abearish then "Jbear" else "ynos", color.black);
Last edited: