Normalized MACD Stochastic

Chuck

Moderator
Staff member
EfBsV9U.png
Code:
#Normalized MACD, Stochastic
# Conditions and Squeeze indicator on the Zero Line
# by Mobius at MyTrade. Labels & clarifications by StanL

declare lower;

script normalizePlot {
  input data = close;
  input newRngMin =  -1;
  input newRngMax = 1;

  def hhData = HighestAll( data );
  def llData = LowestAll( data );

  plot nr = ((( newRngMax - newRngMin ) * ( data - llData )) / ( hhData - llData )) + newRngMin;
}

input fastEMA = 13;
input slowEMA = 21;
input Smooth  =  8;
input MacdWaveC = 50;
input nK = 10;
input nD = 3;
input nVP = 21;
input UpperLine = 30;
input LowerLine = -30;

def data   = close;

#======== Coloring the info header text ===========
plot WhiteLabel = Double.NaN;
WhiteLabel.SetDefaultColor(Color.White);

#==============================================
# VWAP Calculations
def v = volume;
def p = close;
def n = 5;
def vwap = Sum((p * v), n) / Sum(v, n);

#MACD Normalization script
script MACD{
  input fastEMA = 13;
  input slowEMA = 21;
  input Smooth  =  8;
  input value = close;

  def FSignal = (value * .15) +  (.85  * ExpAverage(value, fastEMA)[1]);
  def SSignal = (value * .075) + (.925 * ExpAverage(value, slowEMA)[1]);
  def MACD    = FSignal - SSignal;

  plot MACDSL  = ExpAverage(MACD, Smooth);
}

#Stochastic normalization script
script Stochastic{
  input nK = 10;
  input nD = 3;
  input value = close;

  def l = low;
  def h = high;
  def Data = 100 * ((value - Lowest(l, nK)) / (Highest(h, nK) - Lowest(l, nK)));
  def K = Inertia(Data, nK);

  plot D = ExpAverage(K, nD);
}

#========== the normalized MACD & Stochastic =============
def newMACD = normalizePlot( MACD("fastEMA" = fastEMA, "slowEMA" = slowEMA, "Smooth" = Smooth, "Value" = vwap), -100, 100 );
def newStochastic = normalizePlot( Stochastic("value" = vwap, "nK" = nK, "nD" = nD ), -50, 50 );

plot MACD = newMACD;
plot Stoch = newStochastic;
plot os = if isNaN(close) then Double.NaN else LowerLine;
os.SetPaintingStrategy(PaintingStrategy.LINE);
os.SetLineWeight(1);
os.SetDefaultColor(Color.WHITE);

plot ob = If IsNaN(close) then Double.NaN else UpperLine;
ob.SetPaintingStrategy(PaintingStrategy.LINE);
ob.SetLineWeight(1);
ob.SetDefaultColor(Color.WHITE);

plot ml = If  isNaN(close) then Double.NaN else 0;
ml.SetPaintingStrategy(PaintingStrategy.DASHES);
ml.SetLineWeight(1);
ml.SetDefaultColor(Color.WHITE);

#BB KC Squeeze
def Avg = Average(close, 20);
def stDeviation = stDev(close, 20);
def ATR = Average(TrueRange(high, low, close), 20);
def upperBB = Avg + (2 * stDeviation);
def upperKC = Avg + (1.5 * ATR);
def Squeeze = If upperBB < upperKC then yes else Double.NaN;

#Chart Management
#Plots

MACD.SetPaintingStrategy(PaintingStrategy.Histogram);
MACD.AssignValueColor(if MACD >= 50 and MACD > MACD[1] then Color.GREEN
else if MACD >= 50 and MACD < MACD[1]
then Color.RED
else if MACD <= 50 and MACD < MACD[1]
then Color.RED
else Color.DARK_GREEN);
MACD.SetLineWeight(3);

#======== lining the histogram =========
plot MACD2 = newMACD;
MACD2.SetPaintingStrategy(PaintingStrategy.line);
MACD2.SetLineWeight(2);
MACD2.SetDefaultColor(Color.WHITE);

#=======================================
Stoch.SetPaintingStrategy(PaintingStrategy.Line);
Stoch.AssignValueColor(if Stoch >= 50 and Stoch > Stoch[1] then Color.CYAN else if Stoch >= 50 and Stoch < Stoch[1] then Color.BLUE
else if Stoch <= 50 and Stoch < Stoch[1]
then Color.LIGHT_RED else Color.YELLOW);
Stoch.SetLineWeight(2);

 #MACD Code
def cond1 =  if close > open and close > close[1] and MACD > MACD[1]  and Stoch > Stoch[1] then yes else Double.NaN;

plot zeroLineSqueeze = if Squeeze then 0 else Double.NaN;
zeroLineSqueeze.SetPaintingStrategy(PaintingStrategy.Points);
zeroLineSqueeze.SetLineWeight(2);
zeroLineSqueeze.SetDefaultColor(Color.BLUE);

plot zeroLineCond2 = if Cond1 then 0 else Double.NaN;
zeroLineCond2.SetPaintingStrategy(PaintingStrategy.Points);
zeroLineCond2.SetLineWeight(2);
zeroLineCond2.SetDefaultColor(Color.DarK_Green);

plot zeroLine = if IsNaN(close)
then Double.NaN
else if isNaN(Squeeze) and isNaN(Cond1)
then 0
else Double.NaN;
zeroLine.SetPaintingStrategy(PaintingStrategy.Points);
zeroLine.SetLineWeight(2);
zeroLine.SetDefaultColor(Color.Yellow);

AddLabel(1,"MACD is Histogram",Color.PINK);
AddLabel(1, "Stochastic is the line plot", color.PINK);
AddLabel(1,if squeeze then "A Squeeze is ON now" else "A squeeze is OFF now",color.PINK);

#======= Draw a line at 100 when a squeeze exists ===========
Plot D_squeeze = If squeeze then 100 else Double.NaN;
D_squeeze.SetDefaultColor(Color.MAGENTA);

#======Push label upper so squeeze line is visable ========
Plot Label_pushUP = 120;
Label_pushUP.SetDefaultColor(Color.BLUE);#insert your background color so line is invisible
 
Top