MACD-X, More Than MACD by DGT

dgtrd · study · 86 行 · 点赞 5,213 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=4
study("MACD-X, More Than MACD by DGT", shorttitle="MACD-X by DGT")
 
// -Input ======================================================================================= //
 
macdType      = input("MACD-SOURCE", "MACD Calculation Method", 
                         options=["MACD-TRADITIONAL", 
                                  "MACD-HISTOGRAM",
                                  "MACD-LEADER", 
                                  "MACD-SOURCE"])
 
sourceType    = input("Current Close Price (close)", "Source", 
                         options=["Current Close Price (close)", 
                                  "Average of High, Low, and Close Price (hlc3)", 
                                  "On Balance Volume (obv)", 
                                  "Accumulation Distribution (accdist)", 
                                  "Price Volume Trend (pvt)"])
 
fast_length   = input(12, "Fast Length", type=input.integer)
slow_length   = input(26, "Slow Length", type=input.integer)
signal_length = input(9 , "Signal Smoothing", type=input.integer, minval = 1, maxval = 50)
 
exponential   = input(true , "Use Exponential Moving Average", type=input.bool)
macdTrdDisp   = input(false, "Traditional MACD (MACD-X will be auto Highlighted)")
var highlight = input(false, "Highlight MACD-X/Signal-X Area")
dispLabel     = input(false, "Label of MACD-X, displays Calculation Method and Source")
 
// -Calculation ================================================================================= //
ma(s,l) => exponential ? ema(s,l) : sma(s,l)
 
source = sourceType == "Current Close Price (close)" ? close : 
     sourceType == "Average of High, Low, and Close Price (hlc3)" ? hlc3 : 
     nz(volume) and sourceType == "On Balance Volume (obv)" ? obv : 
     nz(volume) and sourceType == "Accumulation Distribution (accdist)" ? accdist : 
     nz(volume) and sourceType == "Price Volume Trend (pvt)" ? pvt : na
 
fast_ma = ma(source, fast_length)
slow_ma = ma(source, slow_length)
 
macd  = fast_ma - slow_ma
 
macdx = if macdType == "MACD-TRADITIONAL"
    macd
else if macdType == "MACD-HISTOGRAM"
    macd - ma(macd, signal_length)
else if macdType == "MACD-LEADER"
    macd + ma(source - fast_ma, fast_length) - ma(source - slow_ma, slow_length)
else if macdType == "MACD-SOURCE"
    ma(source - avg(fast_ma, slow_ma), 5)
 
signal  = ma(macd , signal_length)
signalx = ma(macdx, signal_length)
histx   = macdx - signalx
 
highlight := macdTrdDisp ? true : highlight
 
// -Color ======================================================================================= //
histColor = histx >=0 ? 
     (histx[1] < histx ? #006400 : color.green) : 
     (histx[1] < histx ? color.red : #910000)
 
// -Label ======================================================================================= //
sourceText = sourceType == "Current Close Price (close)" ? "close" : 
     sourceType == "Average of High, Low, and Close Price (hlc3)" ? "hlc3" : 
     sourceType == "On Balance Volume (obv)" ? "obv" : 
     sourceType == "Accumulation Distribution (accdist)" ? "accdist" : "pvt"
 
macdText = macdType == "MACD-TRADITIONAL" ? "MACD-T" : 
     macdType == "MACD-HISTOGRAM" ? "MACD-H" :
     macdType == "MACD-LEADER" ? "MACD-L" : "MACD-S"
 
// -Plot ======================================================================================== //
plot(histx, title="Histogram-X", color=histColor, style=plot.style_columns )
p1 = plot(macdx  , title="MACD-X"  , color=#0094ff)
p2 = plot(signalx, title="Signal-X", color=#ff6a00)
 
plot(macdTrdDisp ? macd   : na, title="MACD"  , color=#0094ff)
plot(macdTrdDisp ? signal : na, title="Signal", color=#ff6a00)
 
fill(p1, p2, color = highlight ? macdx > signalx ? #0094ff : #ff6a00 : na, transp = 55)
 
if dispLabel
    label macdLabel = label.new(bar_index, 0, text=macdText + "(" + sourceText + ", " + tostring(fast_length) + ", " + tostring(slow_length) + ", " + tostring(signal_length) + ")"
     ,tooltip="MACD Calculation Method = " + macdType + "\nMACD Source = " + sourceType
     ,color=#0094ff, style=label.style_label_left, textcolor=color.white)
    label.delete(macdLabel[1])

← 返回列表