_CM_MacD_Ult_MTF_V2.1

ChrisMoody · study · 95 行 · 点赞 18,032 · TradingView 原页

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

Pine Script

//------New V2 Update 07-28-2021----------
//Thanks to @SKTennis for help in Updating code to V2
//Added Groups to Settings Pane.
//Added Color Plots to Settings Pane
//Switched MTF Logic to turn ON/OFF automatically w/ TradingView's Built in Feature
//Updated Color Transparency plots to work in future update
//Added Ability to Turn ON/OFF Show MacD & Signal Line
//Added Ability to Turn ON/OFF Show Histogram
//Added Ability to Change MACD Line Colors Based on Trend
//Added Ability to Highlight Price Bars Based on Trend
//Added Alerts to Settings Pane.
//Customized how Alerts work.  Must keep Checked in Settings Pane, and...
//When you go to Alerts Panel, Change Symbol to Indicator (CM_Ult_MacD_MTF_V2)
//Customized Alerts to Show Symbol, TimeFrame, Closing Price, MACD Crosses Up & MACD Crosses Down Signals in Alert 
//Alerts are Pre-Set to only Alert on Bar Close
//See Video for Detailed Overview
 
//@version=4
study(title="CM_MacD_Ult_MTF_V2", shorttitle="CM_Ult_MacD_MTF_V2")
//Plot Inputs
res           = input("",  "Indicator TimeFrame", type = input.resolution)
fast_length   = input(title="Fast Length", type=input.integer, defval=12)
slow_length   = input(title="Slow Length", type=input.integer, defval=26)
src           = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_source    = input(title="Oscillator MA Type", type=input.string, defval="EMA", options=["SMA", "EMA"])
sma_signal    = input(title="Signal Line MA Type", type=input.string, defval="EMA", options=["SMA", "EMA"])
// Show Plots T/F
show_macd     = input(true, title="Show MACD Lines", group="Show Plots?", inline="SP1")
show_Hist     = input(true, title="Show Histogram", group="Show Plots?", inline="SP2")
show_trend    = input(true, title = "Colors MACD Lines w/ Trend Color", group="Show Plots?", inline="SP3")
show_HB       = input(false, title="Highlight Price Bars", group="Show Plots?", inline="SP4")
show_cross    = input(false, title = "Colors BackGround when MACD Crosses", group="Show Plots?", inline="SP5")
//show_trend    = input(true, title = "Colors MACD Lines w/ Trend Color", group="Show Plots?", inline="SP5")
// MACD Lines colors
col_macd      = input(#FF6D00, "MACD Line  ", input.color, group="Color Settings", inline="CS1")
col_signal    = input(#2962FF, "Signal Line  ", input.color, group="Color Settings", inline="CS1")
col_trnd_Up   = input(#4BAF4F, "Trend Up      ", input.color, group="Color Settings", inline="CS2")
col_trnd_Dn   = input(#B71D1C, "Trend Down    ", input.color, group="Color Settings", inline="CS2")
// Histogram Colors
col_grow_above = input(#26A69A, "Above   Grow", input.color, group="Histogram Colors", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", input.color, group="Histogram Colors", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", input.color, group="Histogram Colors", inline="Below")
col_fall_below = input(#FF5252, "Fall", input.color, group="Histogram Colors", inline="Below")
// Alerts T/F Inputs
alert_Long    = input(true, title = "MACD Cross Up", group = "Alerts", inline="Alert1")
alert_Short   = input(true, title = "MACD Cross Dn", group = "Alerts", inline="Alert1")
 
// Calculating
fast_ma = security(syminfo.tickerid, res, sma_source == "SMA" ? sma(src, fast_length) : ema(src, fast_length))
slow_ma = security(syminfo.tickerid, res, sma_source == "SMA" ? sma(src, slow_length) : ema(src, slow_length))
macd = fast_ma - slow_ma
signal = security(syminfo.tickerid, res, sma_signal == "SMA" ? sma(macd, signal_length) : ema(macd, signal_length))
hist = macd - signal
// MACD Trend and Cross Up/Down conditions
trend_up = macd > signal
trend_dn = macd < signal
cross_UP = signal[1] >= macd[1] and signal < macd
cross_DN = signal[1] <= macd[1] and signal > macd
// Condition that changes Color of MACD Line if Show Trend is turned on..
trend_col = show_trend  and trend_up ? col_trnd_Up : trend_up ? col_macd : show_trend  and trend_dn ? col_trnd_Dn: trend_dn ? col_macd : na 
 
//Var Statements for Histogram Color Change
var bool histA_IsUp = false
var bool histA_IsDown = false
var bool histB_IsDown = false
var bool histB_IsUp = false
histA_IsUp   := hist == hist[1] ? histA_IsUp[1] : hist > hist[1] and hist > 0
histA_IsDown := hist == hist[1] ? histA_IsDown[1] : hist < hist[1] and hist > 0
histB_IsDown := hist == hist[1] ? histB_IsDown[1] : hist < hist[1] and hist <= 0
histB_IsUp   := hist == hist[1] ? histB_IsUp[1] : hist > hist[1] and hist <= 0
 
hist_col =  histA_IsUp ? col_grow_above : histA_IsDown ? col_fall_above : histB_IsDown ? col_fall_below : histB_IsUp ? col_grow_below :color.silver 
 
// Plot Statements
bgcolor(show_cross and cross_UP ? col_trnd_Up : na, editable=false)
bgcolor(show_cross and cross_DN ? col_trnd_Dn : na, editable=false)
 
barcolor(show_HB and trend_up ? col_trnd_Up : na, title="Trend Up", offset = 0, editable=false)
barcolor(show_HB and trend_dn ? col_trnd_Dn : na, title="Trend Dn", offset = 0, editable=false)
 
plot(show_Hist and hist ? hist : na, title="Histogram", style=plot.style_columns, color=color.new(hist_col ,0))
plot(show_macd  and macd ? macd : na, title="MACD", color=color.new(trend_col, 0),  style=plot.style_line ,linewidth=3)
 
plot(show_macd  and signal ? signal : na, title="Signal", color=color.new(col_signal, 0),  style=plot.style_line ,linewidth=2)
hline(0, title="0 Line", color=color.new(color.gray, 0), linestyle=hline.style_dashed, linewidth=1, editable=false)
 
//Alerts
if alert_Long and cross_UP
    alert("Symbol = (" + syminfo.tickerid + ") TimeFrame = (" + timeframe.period + ") Current Price (" + tostring(close) + ") MACD Crosses Up.", alert.freq_once_per_bar_close)
 
if alert_Short and cross_DN
    alert("Symbol = (" + syminfo.tickerid + ") TimeFrame = (" + timeframe.period + ") Current Price (" + tostring(close) + ") MACD Crosses Down.", alert.freq_once_per_bar_close)
 
//End Code

← 返回列表