Nothing Special   »   [go: up one dir, main page]

Advanced MACD

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

//@version=4

study(title="Advanced MACD", shorttitle="MACD", resolution="")


tema(s, p) =>
3 * ema(s, p) - 3 * ema(ema(s, p), p) + ema(ema(ema(s, p), p), p)
ma(t, s, p) =>
ema_1 = ema(s, p)
rma_1 = rma(s, p)
vwma_1 = vwma(s, p)
wma_1 = wma(s, p)
tema_1 = tema(s, p)
sma_1 = sma(s, p)
t == "ema" ? ema_1 : t == "rma" ? rma_1 :
t == "vwma" ? vwma_1 : t == "wma" ? wma_1 : t == "tema" ? tema_1 : sma_1
src = input(close, title="Source")
fastLength = input(12, title="Fast Length", type=input.integer, minval=1)
slowLength = input(26, title="Slow Length", type=input.integer, minval=1)
signalLength = input(9, title="Signal Length", type=input.integer, minval=1)
volumeWeighted = input(true, title="Volume Weighted", type=input.bool)
volumeLength = input(3, title="Volume Length", type=input.integer, minval=1)
volumePower = input(1, title="Volume Power", type=input.integer, minval=1)
ppo = input(false, title="Percentage Price Oscillator", type=input.bool)
fastType = input(defval="ema", options=["ema", "sma", "rma", "vwma", "wma",
"tema"], title="Fast Moving Average Type")
slowType = input(defval="ema", options=["ema", "sma", "rma", "vwma", "wma",
"tema"], title="Slow Moving Average Type")
signalType = input(defval="sma", options=["ema", "sma", "rma", "vwma", "wma",
"tema"], title="Signal Moving Average Type")
vwType = input(defval="ema", options=["ema", "sma", "rma", "vwma", "wma", "tema"],
title="Volume Weighted Moving Average Type")
useTrueRange = input(defval="Auto", options=["Always", "Auto", "Never"], title="Use
True Range instead of Volume")
isSMACD = input(false, title="Stochastic MACD", type=input.bool)
stochLength = input(45, title="Stochastic Length", type=input.integer, minval=1)
stochType = input(defval="ema", options=["ema", "sma", "rma", "vwma", "wma",
"tema"], title="Stochastic Moving Average Type")
vol = useTrueRange == "Always" or useTrueRange == "Auto" and na(volume) ? tr :
volume
slowMA = float(na)
fastMA = float(na)
if volumeWeighted
volMA = ma(vwType, pow(vol, volumePower), volumeLength)
fastMA := ma(fastType, volMA * src, fastLength) / ma(fastType, volMA,
fastLength)
slowMA := ma(slowType, volMA * src, slowLength) / ma(slowType, volMA,
slowLength)
else
fastMA := ma(fastType, src, fastLength)
slowMA := ma(slowType, src, slowLength)

macd = ppo ? (fastMA - slowMA) / slowMA * 100 : fastMA - slowMA

if isSMACD
fastStoch = (fastMA - lowest(low, stochLength)) /
(highest(high, stochLength) - lowest(low, stochLength))
slowStoch = (slowMA - lowest(low, stochLength)) /
(highest(high, stochLength) - lowest(low, stochLength))
macd := (fastStoch - slowStoch) * 100
macd := ma(stochType, macd, signalLength)
signal = ma(signalType, macd, signalLength)
hist = macd - signal
plot(macd, title='MACD', color=#000099, linewidth=1)
plot(signal, title='Signal', color=#990000, linewidth=1)
plot(hist, title='Histogram', color=#009900, linewidth=3, transp=70,
style=plot.style_histogram)
hline(0, title="Zero", color=color.gray, linestyle=hline.style_dashed)

//This is a more advanced version of the standard moving average


convergence/divergence indicator ( MACD ). It allows you to change the type of all
moving averages (Simple, Exponential, Weighted, Volume-weighted, Triple EMA or a
moving average that uses RSI ). By for example setting the period to 3/10/16 and
use simple moving averages instead of exponential moving averages you can turn it
into the modified version of the MACD oscillator (mMACD) described in detail in
Appendix B in the book "The Art and Science of Technical Analysis: Market
Structure, Price Action and Trading Strategies" by Adam Grimes.

The indicator also allows you to volume weight the indicator (turned on by
default), which will turn it into a Volume-Weighted Moving Average Convergence
Divergence (VW-MACD) first used by Buff Pelz Dormeier in 2002 and described in
detail in his book "Investing with Volume Analysis: Identify, Follow, and Profit
from Trends". If you want to weight the oscillator against the true range instead
of volume this is also possible. By default, this will be done automatically for
assets that do not support volume .

You might also like