
Esta estrategia es un sistema de trading basado en el indicador MACD y la relación entre el volumen y el precio. Determina el punto de inflexión de la tendencia del mercado observando los cambios en la forma del histograma MACD. La estrategia adopta un mecanismo dinámico de stop-profit y stop-loss, y utiliza el indicador ATR para adaptarse a las fluctuaciones del mercado y controlar eficazmente los riesgos.
La lógica central de la estrategia se basa en los cambios columnares profundos y superficiales del indicador MACD, combinados con el sistema de promedio móvil dual EMA y SMA. Cuando el histograma MACD cambia de oscuro a claro, indica un cambio en el impulso y el sistema operará en ese momento. Específicamente:
Se trata de una estrategia integral que combina el clásico indicador de análisis técnico MACD con métodos modernos de control de riesgos. Capture el cambio en el impulso del mercado observando los cambios en la forma del histograma MACD y utilice ATR para el control dinámico del riesgo. La estrategia está diseñada razonablemente, la lógica de operación es clara y tiene un buen valor práctico. Se espera que mediante la optimización y mejora continuas, esta estrategia logre un mejor rendimiento en el combate real.
/*backtest
start: 2019-12-23 08:00:00
end: 2025-01-08 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT","balance":49999}]
*/
//@version=5
strategy(title="軒割MACD 空心量能不足策略", shorttitle="軒割MACD 空心量能不足策略", overlay=true)
//=== 1) 參數 ===//
fast_length = input.int(title="Fast Length", defval=12)
slow_length = input.int(title="Slow Length", defval=26)
src = input.source(title="MACD Source", defval=close)
signal_length = input.int(title="Signal Smoothing", defval=9, minval=1, maxval=50)
sma_source = input.string(title="Oscillator MA Type", defval="EMA", options=["SMA","EMA"])
sma_signal = input.string(title="Signal MA Type", defval="EMA", options=["SMA","EMA"])
// 啟用多單 / 空單
useLong = input.bool(title="啟用多單?(底部紅色)", defval=true)
useShort = input.bool(title="啟用空單?(頂部綠色)", defval=true)
// 止盈倍數 (1~10倍 ATR)
tpATRmult = input.int(title="止盈 ATR 倍數 (1~10)", defval=10, minval=1, maxval=500)
// 止損倍數 (1~10倍 ATR)
slATRmult = input.int(title="止損 ATR 倍數 (1~10)", defval=3, minval=1, maxval=500)
//=== 2) MACD 計算 ===//
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
//=== 3) 判斷深色/淺色(用於變化訊號)===//
darkGreen = hist >= 0 and hist <= hist[1] // 上方,柱子縮小或持平
lightGreen = hist >= 0 and hist > hist[1] // 上方,柱子變大
darkRed = hist < 0 and hist <= hist[1] // 下方,柱子(絕對值)變大或持平
lightRed = hist < 0 and hist > hist[1] // 下方,柱子(絕對值)變小
// 由「深 → 淺」是否發生在上一根
colorChangeToLightGreen = darkGreen[1] and lightGreen
colorChangeToLightRed = darkRed[1] and lightRed
//=== 4) ATR 計算 (用於止盈止損) ===//
atrPeriod = 14
atrValue = ta.atr(atrPeriod)
//=== 5) 多單策略:深紅 → 淺紅 (底部紅色) ===//
if useLong and colorChangeToLightRed
// 以當前 K 線 low - ATR倍數 作為多單止損
longStopLoss = low - (slATRmult * atrValue)
// 以當前 close + ATR倍數 作為多單止盈
longTakeProfit = close + (tpATRmult * atrValue)
// 進多單
strategy.entry("Long Entry", strategy.long, comment="多", qty=1)
strategy.exit("平多", "Long Entry", stop=longStopLoss, limit=longTakeProfit)
//=== 6) 空單策略:深綠 → 淺綠 (頂部綠色) ===//
if useShort and colorChangeToLightGreen
// 以當前 K 線 high + ATR倍數 作為空單止損
shortStopLoss = high + (slATRmult * atrValue)
// 以當前 close - ATR倍數 作為空單止盈
shortTakeProfit = close - (tpATRmult * atrValue)
// 進空單
strategy.entry("Short Entry", strategy.short, comment="空", qty=1)
strategy.exit("平空", "Short Entry", stop=shortStopLoss, limit=shortTakeProfit)
//=== 7) 繪製 MACD 與直方圖 ===//
hline(0, "Zero Line", color=color.new(#787B86, 50))
// 長條圖顏色:
// - 上方 (hist >= 0) 時:hist 比前一根大 (淺綠) 或小 (深綠)
// - 下方 (hist < 0) 時:hist 比前一根大 (淺紅) 或小 (深紅)
plot(hist,title="Histogram",style=plot.style_columns,color = hist >= 0? (hist > hist[1] ? #26A69A : #B2DFDB) : (hist > hist[1] ? #FFCDD2 : #FF5252) )
// 繪製 MACD 與 Signal
plot(macd, title="MACD", color=#2962FF)
plot(signal, title="Signal", color=#FF6D00)