
Esta estratégia é um sistema de negociação baseado no indicador MACD e na relação entre volume e preço. Ela determina o ponto de virada da tendência do mercado observando as mudanças no formato do histograma MACD. A estratégia adota um mecanismo dinâmico de stop-profit e stop-loss e usa o indicador ATR para se adaptar às flutuações do mercado e controlar riscos de forma eficaz.
A lógica central da estratégia é baseada nas mudanças colunares profundas e superficiais do indicador MACD, combinadas com o sistema de média móvel dupla EMA e SMA. Quando o histograma MACD muda de escuro para claro, isso indica uma mudança no momentum e o sistema negociará neste momento. Especificamente:
Esta é uma estratégia abrangente que combina o indicador clássico de análise técnica MACD com métodos modernos de controle de risco. Capture a mudança no momento do mercado observando as mudanças no formato do histograma MACD e use o ATR para controle dinâmico de risco. A estratégia é razoavelmente elaborada, a lógica da operação é clara e tem bom valor prático. Por meio de otimização e melhoria contínuas, espera-se que essa estratégia alcance melhor desempenho em 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)