本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ChartPrime
//@version=6
indicator(title="MACD Support and Resistance [ChartPrime]", shorttitle="MACD S/R [ChartPrime]")
// --------------------------------------------------------------------------------------------------------------------}
// 📌 𝙐𝙎𝙀𝙍 𝙄𝙉𝙋𝙐𝙏𝙎
// --------------------------------------------------------------------------------------------------------------------{
fast_length = input(title = "Fast Length", defval = 12)
slow_length = input(title = "Slow Length", defval = 26)
src = input(title = "Source", defval = close)
signal_length = input.int(title = "Signal Smoothing", minval = 1, maxval = 50, defval = 9, display = display.data_window)
sma_source = input.string(title = "Oscillator MA Type", defval = "EMA", options = ["SMA", "EMA"], display = display.data_window)
sma_signal = input.string(title = "Signal Line MA Type", defval = "EMA", options = ["SMA", "EMA"], display = display.data_window)
col_up = input.color(#2962FF, "", inline = "color")
col_dn = input.color(#FF6D00, "", inline = "color")
// --------------------------------------------------------------------------------------------------------------------}
// 📌 𝙄𝙉𝘿𝙄𝘾𝘼𝙏𝙊𝙍 𝘾𝘼𝙇𝘾𝙐𝙇𝘼𝙏𝙄𝙊𝙉𝙎
// --------------------------------------------------------------------------------------------------------------------{
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
macd_col = macd > signal ? col_up : col_dn
type level
bool issupport
float lvl
int start
var s_r_levels = array.new<level>()
if ta.crossunder(macd, signal)
max = array.new<float>()
for i = 0 to 5
h = high[i]
max.push(h)
for i = 0 to 5
h = high[i]
if h == max.max()
sr = level.new(false, max.max(), bar_index-i)
s_r_levels.push(sr)
if ta.crossover(macd, signal)
min = array.new<float>()
for i = 0 to 5
l = low[i]
min.push(l)
for i = 0 to 5
l = low[i]
if l == min.min()
sr = level.new(true, min.min(), bar_index-i)
s_r_levels.push(sr)
// Max amount of Levels 20
if s_r_levels.size() > 20
s_r_levels.shift()
// Remove crossed levels
if s_r_levels.size() > 0
for i = 0 to s_r_levels.size() - 1
sr = s_r_levels.get(i)
start = sr.start
is_sup = sr.issupport
level_ = sr.lvl
if bar_index > start and is_sup and low < level_
s_r_levels.remove(s_r_levels.indexof(sr))
if bar_index > start and not is_sup and high > level_
s_r_levels.remove(s_r_levels.indexof(sr))
// --------------------------------------------------------------------------------------------------------------------}
// 📌 𝙑𝙄𝙎𝙐𝘼𝙇𝙄𝙕𝘼𝙏𝙄𝙊𝙉
// --------------------------------------------------------------------------------------------------------------------{
for sr in s_r_levels
start = sr.start
is_sup = sr.issupport
level_ = sr.lvl
col = is_sup ? col_up : col_dn
line.new(start, level_, bar_index, level_, color = col, force_overlay = true)
label.new(start, level_, "", style = label.style_diamond, color = col, force_overlay = true, size = size.tiny)
label.delete(label.new(bar_index, level_, str.tostring(level_), style = label.style_label_left, color = col, force_overlay = true, size = size.small)[1])
hline(0, "Zero Line", color = color.new(#787B86, 30))
pm = plot(macd, title = "MACD", color = macd_col, display = display.none)
ps = plot(signal, title = "Signal", color = macd_col, linewidth = 2)
fill(pm, ps, color.new(macd_col, 70))
plotshape(ta.crossover(macd, signal) ? signal : float(na), "Macd UP", style = shape.diamond, location = location.absolute, size = size.tiny, color = macd_col)
plotshape(ta.crossunder(macd, signal) ? signal : float(na), "Macd DN", style = shape.diamond, location = location.absolute, size = size.tiny, color = macd_col)
plot(ta.highest(macd, 100), "Local high", color = bar_index % 5 == 0 or bar_index % 4 == 0 or bar_index % 3 == 0 ? na : color.new(#787B86, 30))
plot(ta.lowest(macd, 100), "Local Low", color = bar_index % 5 == 0 or bar_index % 4 == 0 or bar_index % 3 == 0 ? na : color.new(#787B86, 30))
// --------------------------------------------------------------------------------------------------------------------}