本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// © Dreadblitz
//@version=4
study(title="Normalized smoothed MACD", shorttitle = "NSM", overlay=false)
//
inpFastPeriod = input(defval=12, title="MACD fast period", minval=1, type=input.integer)
inpSlowPeriod = input(defval=26, title="MACD slow period", minval=1, type=input.integer)
inpMacdSignal = input(defval=9, title="Signal period", minval=1, type=input.integer)
inpSmoothPeriod = input(defval=5, title="Smoothing period", minval=1, type=input.integer)
inpNormPeriod = input(defval=20, title="Normalization period", minval=1, type=input.integer)
price = input(close, title="Price Source",type=input.source)
//
emaf = 0.0
emas = 0.0
val = 0.0
nval = 0.0
sig = 0.0
//
red =color.new(#FF0000, 0)
green=color.new(#32CD32, 0)
black=color.new(#000000, 0)
//
if bar_index > inpSlowPeriod
alphaf = 2.0/(1.0+max(inpFastPeriod,1))
alphas = 2.0/(1.0+max(inpSlowPeriod,1))
alphasig = 2.0/(1.0+max(inpMacdSignal,1))
alphasm = 2.0/(1.0+max(inpSmoothPeriod,1))
emaf := emaf[1]+alphaf*(price-emaf[1])
emas := emas[1]+alphas*(price-emas[1])
imacd = emaf-emas
mmax = highest(imacd,inpNormPeriod)
mmin = lowest(imacd,inpNormPeriod)
if mmin != mmax
nval := 2.0*(imacd-mmin)/(mmax-mmin)-1.0
else
nval := 0
val := val[1] + alphasm*(nval-val[1])
sig := sig[1] + alphasig*(val-sig[1])
//
plot(val, color=val>val[1]?green:red, style=plot.style_line, linewidth=2, title="Reg smooth MACD")
plot(sig, color=black, style=plot.style_cross, linewidth=1, title="Signal line")
hline(0, title='0', color=color.gray, linestyle=hline.style_dotted, linewidth=1)
//
alertcondition(crossunder(val,sig),title="Sell",message="Sell")
alertcondition(crossover(val,sig),title="Buy",message="Buy")
alertcondition(crossunder(val,sig) or crossover(val,sig) ,title="Sell/Buy",message="Sell/Buy")