Chande Momentum Oscillator Pro

Maximilianned · study · 199 行 · 点赞 24 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Maximilianned
 
//@version=6
indicator("Chande Momentum Oscillator Pro", shorttitle = "CMO+", overlay = false, max_lines_count = 500, max_labels_count = 500)
 
// ═══════════════════════════════════════════════════════════════════
// INPUTS — CORE
// ═══════════════════════════════════════════════════════════════════
grpC      = "CMO"
src       = input.source(close, "Source",            group = grpC)
cmoLen    = input.int(9,  "CMO Length",              minval = 1, group = grpC)
smoothLen = input.int(4,  "EMA Smoothing (CMO)",     minval = 1, group = grpC, tooltip = "EMA applied to the raw CMO. 1 = no smoothing.")
wmaLen    = input.int(12, "WMA Signal Length",       minval = 1, group = grpC, tooltip = "WMA of the smoothed CMO. LONG/SHORT = CMO crossing this line.")
halfScale = input.bool(true, "Scale to ±50",         group = grpC, tooltip = "CMO natively runs ±100. When on, it is halved so +50 / -50 are the extremes.")
 
// ═══════════════════════════════════════════════════════════════════
// INPUTS — LEVELS
// ═══════════════════════════════════════════════════════════════════
grpL       = "Levels"
upLvl      = input.float(33.0,  "Upper Level", step = 0.5, group = grpL)
dnLvl      = input.float(-33.0, "Lower Level", step = 0.5, group = grpL)
showZero   = input.bool(true,   "Show Zero Line",        group = grpL)
showBounds = input.bool(true,   "Show Max (±50) Lines",  group = grpL)
showFill   = input.bool(true,   "Shade Extreme Zones",   group = grpL)
 
// ═══════════════════════════════════════════════════════════════════
// INPUTS — STYLE
// ═══════════════════════════════════════════════════════════════════
grpS      = "Style — CMO & WMA"
cmoCol    = input.color(#ffffff, "CMO",                          group = grpS, inline = "cmo")
cmoW      = input.int(1, "Width", minval = 1, maxval = 5,        group = grpS, inline = "cmo")
cmoStyIn  = input.string("Solid", "Style", options = ["Solid", "Dashed", "Dotted"], group = grpS, inline = "cmo")
dirColor  = input.bool(true, "Colour CMO by direction vs WMA",  group = grpS, tooltip = "Off = CMO uses the single colour above.")
cmoUpCol  = input.color(#83ddff, "  Above WMA",                  group = grpS, inline = "dir")
cmoDnCol  = input.color(#ff004d, "Below WMA",                    group = grpS, inline = "dir")
wmaCol    = input.color(#ffffff, "WMA",                          group = grpS, inline = "wma")
wmaW      = input.int(1, "Width", minval = 1, maxval = 5,        group = grpS, inline = "wma")
wmaStyIn  = input.string("Solid", "Style", options = ["Solid", "Dashed", "Dotted"], group = grpS, inline = "wma")
 
grpLS     = "Style — Levels"
upCol     = input.color(color.new(#EF5350, 0), "Upper Level",    group = grpLS, inline = "up")
upW       = input.int(1, "Width", minval = 1, maxval = 5,        group = grpLS, inline = "up")
upStyIn   = input.string("Dashed", "Style", options = ["Solid", "Dashed", "Dotted"], group = grpLS, inline = "up")
dnCol     = input.color(color.new(#26A69A, 0), "Lower Level",    group = grpLS, inline = "dn")
dnW       = input.int(1, "Width", minval = 1, maxval = 5,        group = grpLS, inline = "dn")
dnStyIn   = input.string("Dashed", "Style", options = ["Solid", "Dashed", "Dotted"], group = grpLS, inline = "dn")
zeroCol   = input.color(color.new(#787B86, 0), "Zero Line",      group = grpLS, inline = "zr")
zeroW     = input.int(1, "Width", minval = 1, maxval = 5,        group = grpLS, inline = "zr")
zeroStyIn = input.string("Dotted", "Style", options = ["Solid", "Dashed", "Dotted"], group = grpLS, inline = "zr")
bndCol    = input.color(color.new(#787B86, 50), "Max Lines",     group = grpLS, inline = "bd")
bndW      = input.int(1, "Width", minval = 1, maxval = 5,        group = grpLS, inline = "bd")
bndStyIn  = input.string("Solid", "Style", options = ["Solid", "Dashed", "Dotted"], group = grpLS, inline = "bd")
upFillCol = input.color(color.new(#EF5350, 90), "Upper Zone Fill", group = grpLS, inline = "fl")
dnFillCol = input.color(color.new(#26A69A, 90), "Lower Zone Fill", group = grpLS, inline = "fl")
 
grpSig    = "Cross Signals"
useSigFilt = input.bool(true, "Require cross outside mid zone",  group = grpSig, tooltip = "Ignores WMA crosses in the choppy middle. LONG only counts when the CMO crosses up at or below the LONG threshold; SHORT only when it crosses down at or above the SHORT threshold.")
longMax   = input.float(-10.0, "LONG: cross at or below",  step = 0.5, group = grpSig)
shortMin  = input.float(10.0,  "SHORT: cross at or above", step = 0.5, group = grpSig)
showSig   = input.bool(true, "Show LONG/SHORT Markers",          group = grpSig)
longCol   = input.color(color.new(#26A69A, 0), "LONG",           group = grpSig, inline = "sg")
shortCol  = input.color(color.new(#EF5350, 0), "SHORT",          group = grpSig, inline = "sg")
 
// ═══════════════════════════════════════════════════════════════════
// INPUTS — DIVERGENCES (regular)
// ═══════════════════════════════════════════════════════════════════
grpD      = "Divergences (Regular)"
showBull  = input.bool(true, "Show Bullish",                     group = grpD, inline = "dshow")
showBear  = input.bool(true, "Show Bearish",                     group = grpD, inline = "dshow")
lbL       = input.int(5,  "Pivot Lookback Left",  minval = 1,    group = grpD)
lbR       = input.int(5,  "Pivot Lookback Right", minval = 1,    group = grpD, tooltip = "Divergences confirm this many bars after the pivot.")
rngMin    = input.int(5,  "Min Bars Between Pivots", minval = 1, group = grpD)
rngMax    = input.int(60, "Max Bars Between Pivots", minval = 2, group = grpD)
bullCol   = input.color(#3179f5, "Bullish",                      group = grpD, inline = "dcol")
bearCol   = input.color(#d4396d, "Bearish",                      group = grpD, inline = "dcol")
divW      = input.int(1, "Line Width", minval = 1, maxval = 5,   group = grpD, inline = "dln")
divStyIn  = input.string("Solid", "Style", options = ["Solid", "Dashed", "Dotted"], group = grpD, inline = "dln")
showDivLb = input.bool(true, "Show Labels",                      group = grpD, inline = "dlb")
divTxtCol = input.color(color.white, "Label Text",               group = grpD, inline = "dlb")
 
// ═══════════════════════════════════════════════════════════════════
// INPUTS — ALERTS
// ═══════════════════════════════════════════════════════════════════
grpA      = "Alerts (single 'Any alert() call' alert)"
useAlertFn = input.bool(true, "Enable alert() messages", group = grpA, tooltip = "Create ONE alert with condition 'Any alert() function call' to receive every type ticked below. Individual alertcondition() entries are also available.")
alLong    = input.bool(true, "LONG (CMO crosses above WMA)",  group = grpA)
alShort   = input.bool(true, "SHORT (CMO crosses below WMA)", group = grpA)
alUp      = input.bool(true, "Upper Level Hit",               group = grpA)
alDn      = input.bool(true, "Lower Level Hit",               group = grpA)
alBull    = input.bool(true, "Bullish Divergence",            group = grpA)
alBear    = input.bool(true, "Bearish Divergence",            group = grpA)
 
// ═══════════════════════════════════════════════════════════════════
// STYLE MAPPING
// ═══════════════════════════════════════════════════════════════════
cmoSty  = cmoStyIn  == "Solid" ? plot.linestyle_solid : cmoStyIn == "Dashed" ? plot.linestyle_dashed : plot.linestyle_dotted
wmaSty  = wmaStyIn  == "Solid" ? plot.linestyle_solid : wmaStyIn == "Dashed" ? plot.linestyle_dashed : plot.linestyle_dotted
upSty   = upStyIn   == "Solid" ? hline.style_solid : upStyIn   == "Dashed" ? hline.style_dashed : hline.style_dotted
dnSty   = dnStyIn   == "Solid" ? hline.style_solid : dnStyIn   == "Dashed" ? hline.style_dashed : hline.style_dotted
zeroSty = zeroStyIn == "Solid" ? hline.style_solid : zeroStyIn == "Dashed" ? hline.style_dashed : hline.style_dotted
bndSty  = bndStyIn  == "Solid" ? hline.style_solid : bndStyIn  == "Dashed" ? hline.style_dashed : hline.style_dotted
divSty  = divStyIn  == "Solid" ? line.style_solid  : divStyIn  == "Dashed" ? line.style_dashed  : line.style_dotted
 
// ═══════════════════════════════════════════════════════════════════
// CALCULATION
// ═══════════════════════════════════════════════════════════════════
scaleK = halfScale ? 0.5 : 1.0
bound  = halfScale ? 50.0 : 100.0
 
cmoRaw = ta.cmo(src, cmoLen)
osc    = ta.ema(cmoRaw, smoothLen) * scaleK
sig    = ta.wma(osc, wmaLen)
 
longRaw  = ta.crossover(osc, sig)
shortRaw = ta.crossunder(osc, sig)
longX    = longRaw  and (not useSigFilt or osc <= longMax)
shortX   = shortRaw and (not useSigFilt or osc >= shortMin)
upHit  = ta.crossover(osc, upLvl)
dnHit  = ta.crossunder(osc, dnLvl)
 
// ═══════════════════════════════════════════════════════════════════
// PLOTS
// ═══════════════════════════════════════════════════════════════════
hBndUp = hline(bound,  "Max +",       color = bndCol,  linestyle = bndSty,  linewidth = bndW,  display = showBounds ? display.all : display.none)
hUp    = hline(upLvl,  "Upper Level", color = upCol,   linestyle = upSty,   linewidth = upW)
hZero  = hline(0,      "Zero",        color = zeroCol, linestyle = zeroSty, linewidth = zeroW, display = showZero ? display.all : display.none)
hDn    = hline(dnLvl,  "Lower Level", color = dnCol,   linestyle = dnSty,   linewidth = dnW)
hBndDn = hline(-bound, "Max -",       color = bndCol,  linestyle = bndSty,  linewidth = bndW,  display = showBounds ? display.all : display.none)
 
fill(hUp, hBndUp, color = upFillCol, title = "Upper Zone", display = showFill ? display.all : display.none)
fill(hDn, hBndDn, color = dnFillCol, title = "Lower Zone", display = showFill ? display.all : display.none)
 
oscCol = dirColor ? (osc >= sig ? cmoUpCol : cmoDnCol) : cmoCol
plot(osc, "CMO", color = oscCol, linewidth = cmoW, linestyle = cmoSty)
plot(sig, "WMA", color = wmaCol, linewidth = wmaW, linestyle = wmaSty)
 
plotshape(showSig and longX  ? osc : na, "LONG",  shape.triangleup,   location.absolute, longCol,  size = size.tiny)
plotshape(showSig and shortX ? osc : na, "SHORT", shape.triangledown, location.absolute, shortCol, size = size.tiny)
 
// ═══════════════════════════════════════════════════════════════════
// REGULAR DIVERGENCES
// Bullish: price Lower Low  + CMO Higher Low
// Bearish: price Higher High + CMO Lower High
// ═══════════════════════════════════════════════════════════════════
plFound = not na(ta.pivotlow(osc, lbL, lbR))
phFound = not na(ta.pivothigh(osc, lbL, lbR))
 
pivBar  = bar_index - lbR
 
prevPlOsc  = ta.valuewhen(plFound, osc[lbR], 1)
prevPlLow  = ta.valuewhen(plFound, low[lbR], 1)
prevPlBar  = ta.valuewhen(plFound, pivBar, 1)
 
prevPhOsc  = ta.valuewhen(phFound, osc[lbR], 1)
prevPhHigh = ta.valuewhen(phFound, high[lbR], 1)
prevPhBar  = ta.valuewhen(phFound, pivBar, 1)
 
plGap = pivBar - prevPlBar
phGap = pivBar - prevPhBar
 
bullDiv = plFound and not na(prevPlOsc) and osc[lbR] > prevPlOsc and low[lbR]  < prevPlLow  and plGap >= rngMin and plGap <= rngMax
bearDiv = phFound and not na(prevPhOsc) and osc[lbR] < prevPhOsc and high[lbR] > prevPhHigh and phGap >= rngMin and phGap <= rngMax
 
if bullDiv and showBull
    line.new(prevPlBar, prevPlOsc, pivBar, osc[lbR], color = bullCol, width = divW, style = divSty)
    if showDivLb
        label.new(pivBar, osc[lbR], "Bull", style = label.style_label_up, color = bullCol, textcolor = divTxtCol, size = size.tiny)
 
if bearDiv and showBear
    line.new(prevPhBar, prevPhOsc, pivBar, osc[lbR], color = bearCol, width = divW, style = divSty)
    if showDivLb
        label.new(pivBar, osc[lbR], "Bear", style = label.style_label_down, color = bearCol, textcolor = divTxtCol, size = size.tiny)
 
// ═══════════════════════════════════════════════════════════════════
// ALERTS — individual conditions
// ═══════════════════════════════════════════════════════════════════
alertcondition(longX,   "CMO LONG",               "CMO LONG — crossed above WMA | {{ticker}} {{interval}} @ {{close}}")
alertcondition(shortX,  "CMO SHORT",              "CMO SHORT — crossed below WMA | {{ticker}} {{interval}} @ {{close}}")
alertcondition(upHit,   "CMO Upper Level Hit",    "CMO hit Upper Level | {{ticker}} {{interval}} @ {{close}}")
alertcondition(dnHit,   "CMO Lower Level Hit",    "CMO hit Lower Level | {{ticker}} {{interval}} @ {{close}}")
alertcondition(bullDiv, "CMO Bullish Divergence", "CMO Bullish Divergence | {{ticker}} {{interval}} @ {{close}}")
alertcondition(bearDiv, "CMO Bearish Divergence", "CMO Bearish Divergence | {{ticker}} {{interval}} @ {{close}}")
 
// ALERTS — single alert() route (bar close)
if useAlertFn
    tag = " | " + syminfo.ticker + " " + timeframe.period + " @ " + str.tostring(close)
    if alLong and longX
        alert("CMO LONG — crossed above WMA" + tag, alert.freq_once_per_bar_close)
    if alShort and shortX
        alert("CMO SHORT — crossed below WMA" + tag, alert.freq_once_per_bar_close)
    if alUp and upHit
        alert("CMO hit Upper Level (" + str.tostring(upLvl) + ")" + tag, alert.freq_once_per_bar_close)
    if alDn and dnHit
        alert("CMO hit Lower Level (" + str.tostring(dnLvl) + ")" + tag, alert.freq_once_per_bar_close)
    if alBull and bullDiv
        alert("CMO Bullish Divergence" + tag, alert.freq_once_per_bar_close)
    if alBear and bearDiv
        alert("CMO Bearish Divergence" + tag, alert.freq_once_per_bar_close)

← 返回列表