MACD+RSI+BB

AmirZainol · study · 69 行 · 点赞 3,611 · TradingView 原页

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

Pine Script

//@version=5  
indicator(title="MACD+RSI+BB", shorttitle="MACD+RSI+BB", timeframe="", timeframe_gaps=true)  
 
// Getting inputs  
fast_length = input(title = "MACD Fast Length", defval = 12, group="MACD Settings")  
slow_length = input(title = "MACD Slow Length", defval = 26, group="MACD Settings")  
src = input(title = "MACD Source", defval = close, group="MACD Settings")  
signal_length = input.int(title = "MACD Signal Smoothing",  minval = 1, maxval = 50, defval = 9, display = display.data_window, group="MACD Settings")  
sma_source = input.string(title = "MACD Oscillator MA Type",  defval = "EMA", options = ["SMA", "EMA"], display = display.data_window, group="MACD Settings")  
sma_signal = input.string(title = "MACD Signal Line MA Type", defval = "EMA", options = ["SMA", "EMA"], display = display.data_window, group="MACD Settings")  
 
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")  
rsiUpper = input.int(70, minval=1, title="RSI Upper", group="RSI Settings")
rsiLower = input.int(30, minval=1, title="RSI Lower", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")  
 
lengthBB = input.int(20, minval=1, title="BB Length", group="BB Settings")
srcBB = input(close, title="BB Source", group="BB Settings")
multBB = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="BB Settings")
maTypeBB = input.string("EMA", "Basis MA Type", options = ["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="BB Settings")
 
ma(source, length, _type) =>
    switch _type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)
 
basisBB = ma(srcBB, lengthBB, maTypeBB)
devBB = multBB * ta.stdev(srcBB, lengthBB)
upperBB = basisBB + devBB
lowerBB = basisBB - devBB
 
change = ta.change(rsiSourceInput)  
up = ta.rma(math.max(change, 0), rsiLengthInput)  
down = ta.rma(-math.min(change, 0), rsiLengthInput)  
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))  
 
// Calculating  
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)  
hist = macd - signal  
 
alertcondition(hist[1] >= 0 and hist < 0, title = 'Rising to falling', message = 'The MACD histogram switched from a rising to falling state')  
alertcondition(hist[1] <= 0 and hist > 0, title = 'Falling to rising', message = 'The MACD histogram switched from a falling to rising state')  
 
hline(0, "Zero Line", color = color.new(#787B86, 50))  
 
histColor = if hist >= 0  
    if rsi > rsiUpper and close > upperBB
        color.black
    else if rsi > rsiUpper
        #4caf50  
    else  
        #a5d6a7  
else
    if rsi < rsiLower and close < lowerBB
        color.black
    else if rsi < rsiLower
        #f23645
    else  
        #faa1a4
 
plot(hist, title = "Histogram", style = plot.style_columns, color = histColor)  
//plot(macd,   title = "MACD",   color = #2962FF)  
//plot(signal, title = "Signal", color = #FF6D00)

← 返回列表