MACD Pro + ADX Filter + Smart Histogram TP + RSI

Mahdibas · study · 128 行 · 点赞 1,335 · TradingView 原页

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

Pine Script

//@version=5
indicator("MACD Pro + ADX Filter + Smart Histogram TP + RSI", shorttitle="MACD Pro+", overlay=true)
 
// ── Inputs
fast_length   = input.int(12, "Fast Length")
slow_length   = input.int(26, "Slow Length")
signal_length = input.int(9, "Signal Smoothing", minval=1)
src           = input.source(close, "Source")
 
adx_len  = input.int(14, "ADX Length")
adx_min  = input.int(25, "Min ADX for Signals (strictly >)", minval=1, maxval=50)
 
// ── RSI Inputs
rsi_length = input.int(14, "RSI Length")
rsi_ob     = input.int(70, "RSI Overbought")
rsi_os     = input.int(30, "RSI Oversold")
 
// ── MACD
fast_ma = ta.ema(src, fast_length)
slow_ma = ta.ema(src, slow_length)
macd    = fast_ma - slow_ma
signal  = ta.ema(macd, signal_length)
hist    = macd - signal
 
// ── ADX
[di_plus, di_minus, adx] = ta.dmi(adx_len, adx_len)
strong_trend = adx > adx_min
plot(adx, "ADX")   // allows {{plot("ADX")}} compatibility
 
// ── RSI (independent)
rsi = ta.rsi(close, rsi_length)
rsi_overbought = rsi > rsi_ob
rsi_oversold   = rsi < rsi_os
 
// Plot RSI in a separate pane
rsi_plot = plot(rsi, "RSI", color=color.new(color.blue, 0))
hline(rsi_ob, "RSI OB", color=color.red)
hline(rsi_os, "RSI OS", color=color.green)
 
// Optional RSI markers (does NOT affect MACD logic)
plotshape(rsi_overbought, title="RSI Overbought", location=location.abovebar, color=color.red, style=shape.circle, size=size.tiny, text="RSI OB")
plotshape(rsi_oversold,   title="RSI Oversold",   location=location.belowbar, color=color.green, style=shape.circle, size=size.tiny, text="RSI OS")
 
// ── Histogram States
hist_growing_bull   = hist >= 0 and hist > hist[1]
hist_weakening_bull = hist >= 0 and hist < hist[1]
hist_growing_bear   = hist <  0 and hist < hist[1]
hist_weakening_bear = hist <  0 and hist > hist[1]
 
// ── Entry Confirmation
buy_entry_ok  = hist_growing_bull and hist[1] <= 0
sell_entry_ok = hist_growing_bear and hist[1] >= 0
 
raw_buy  = strong_trend and ta.crossover(macd, signal) and buy_entry_ok
raw_sell = strong_trend and ta.crossunder(macd, signal) and sell_entry_ok
 
// ── One TP per trade
var int  trade_state     = 0
var bool tp_long_fired   = false
var bool tp_short_fired  = false
 
if raw_buy and trade_state != 1
    trade_state     := 1
    tp_long_fired   := false
    tp_short_fired  := false
 
if raw_sell and trade_state != -1
    trade_state     := -1
    tp_long_fired   := false
    tp_short_fired  := false
 
tp_long_raw  = trade_state == 1 and hist_weakening_bull and hist_growing_bull[1]
tp_short_raw = trade_state == -1 and hist_weakening_bear and hist_growing_bear[1]
 
tp_long  = tp_long_raw  and not tp_long_fired
tp_short = tp_short_raw and not tp_short_fired
 
if tp_long
    tp_long_fired := true
if tp_short
    tp_short_fired := true
 
buy_signal  = raw_buy  and trade_state == 1
sell_signal = raw_sell and trade_state == -1
 
// ── Plotting
hist_color = hist >= 0 ? (hist > hist[1] ? #26A69A : #B2DFDB) :
                         (hist < hist[1] ? #EF5350 : #FFCDD2)
 
plot(hist,   "Histogram", style=plot.style_columns, color=hist_color, linewidth=4)
plot(macd,   "MACD",      color=#2962FF, linewidth=2)
plot(signal, "Signal",    color=#FF6D00, linewidth=2)
hline(0, "Zero Line", color=color.gray)
 
// ── Visual MACD Signals
plotshape(buy_signal,  title="MACD BUY",  location=location.belowbar, color=#26A69A, style=shape.labelup,    text="MACD BUY",  textcolor=color.white, size=size.small)
plotshape(sell_signal, title="MACD SELL", location=location.abovebar, color=#EF5350, style=shape.labeldown, text="MACD SELL", textcolor=color.white, size=size.small)
 
plotshape(tp_long,  title="TP Long",  location=location.belowbar, color=color.lime, style=shape.triangleup,   text="TP", size=size.tiny)
plotshape(tp_short, title="TP Short", location=location.abovebar, color=color.red,  style=shape.triangledown, text="TP", size=size.tiny)
 
// ── ALERTS
buy_alert_cond  = buy_signal  and barstate.isconfirmed
sell_alert_cond = sell_signal and barstate.isconfirmed
tp_long_cond    = tp_long     and barstate.isconfirmed
tp_short_cond   = tp_short    and barstate.isconfirmed
 
fmt(x) => str.tostring(x, "#.##")
 
if buy_alert_cond
    msg = "MACD Pro — BUY\nTicker: " + syminfo.ticker + "\nPrice: " + fmt(close) + "\nADX: " + fmt(adx)
    alert(msg, alert.freq_once_per_bar_close)
 
if sell_alert_cond
    msg = "MACD Pro — SELL\nTicker: " + syminfo.ticker + "\nPrice: " + fmt(close) + "\nADX: " + fmt(adx)
    alert(msg, alert.freq_once_per_bar_close)
 
if tp_long_cond
    msg = "MACD Pro — TP (Long) hit\nTicker: " + syminfo.ticker + "\nPrice: " + fmt(close) + "\nADX: " + fmt(adx)
    alert(msg, alert.freq_once_per_bar_close)
 
if tp_short_cond
    msg = "MACD Pro — TP (Short) hit\nTicker: " + syminfo.ticker + "\nPrice: " + fmt(close) + "\nADX: " + fmt(adx)
    alert(msg, alert.freq_once_per_bar_close)
 
// optional combined alert
combined = buy_alert_cond or sell_alert_cond or tp_long_cond or tp_short_cond
alertcondition(combined, title="MACD Pro — Any Signal", message="MACD Pro fired a signal.")

← 返回列表