MACD指標に基づく双方向取引戦略


作成日: 2023-12-07 17:11:52 最終変更日: 2023-12-07 17:11:52
コピー: 0 クリック数: 829
1
フォロー
1619
フォロワー

MACD指標に基づく双方向取引戦略

概要

この戦略は,MACD指数に基づく二方向取引戦略を実現する.これは,MACD指数上での金叉と死叉の時に分別的に高値と空値を打つことができ,他の指標判断と組み合わせていくつかの信号をフィルターすることができます.

戦略原則

この戦略は,主にMACD指標を利用して双方向取引を行う.具体的には,高速移動平均,遅い移動平均およびMACD信号ラインを計算する.高速移動平均の上を通過すると,金叉信号が多く発生し,高速移動平均の下を通過すると,死叉信号が空になる.

部分的に無効な信号をフィルタリングするために,この戦略は±30の範囲をフィルターとして設定し,MACD柱状線がこの範囲を超えた場合にのみ取引信号をトリガーする.さらに,平仓時にMACD柱状線の方向を判断し,連続した2本の柱の方向が変化したときにのみ平仓する.

戦略的優位性

  • MACD指標を主要取引シグナルとして使用し,この指標は両方向の株式市場の動きに比較的敏感である
  • フィルターが追加され,無効信号の一部をフィルターします.
  • 連続した2本の柱の方向を判断する平仓論理を採用し,偽突破を一定程度に回避できる.

戦略リスク

  • MACD指標は,頻繁に取引するシグナルを生じやすく,過剰な取引頻度をもたらす可能性があります.
  • シングル指標戦略で,信号の遅延は損失につながる可能性があります.
  • 柱状線方向判断の平仓論理は厳格ではないため,信号漏れのリスクがある.

戦略最適化の方向性

  • KDJ指標,ブリン帯指標など,他の指標と組み合わせて信号を確認することを考慮することができる.
  • KD指数などのMACD指数に代わる他のより高度な指標を研究することができます.
  • 単一損失を制御するために,平仓ロジックを最適化し,ストップとストップを設定できます.

要約する

この戦略は,全体として基本的には利用可能な二方向取引戦略である.それはMACD指標の優位性を利用し,同時に,信号の質を制御するためのフィルターを追加している.しかし,MACD指標自体にもいくつかの問題があり,その戦略をより信頼性のあるものにするために,現場でさらなるテストと最適化が必要である.全体的に,この戦略は,二方向取引戦略の基礎を築き,その基礎を継続的に最適化し,強力な量化取引戦略にすることができる.

]

ストラテジーソースコード
/*backtest
start: 2022-11-30 00:00:00
end: 2023-12-06 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=3

//Created by user ChrisMoody updated 4-10-2014
//Regular MACD Indicator with Histogram that plots 4 Colors Based on Direction Above and Below the Zero Line
//Update allows Check Box Options, Show MacD & Signal Line, Show Change In color of MacD Line based on cross of Signal Line.
//Show Dots at Cross of MacD and Signal Line, Histogram can show 4 colors or 1, Turn on and off Histogram.
//Special Thanks to that incredible person in Tech Support whoem I won't say you r name so you don't get bombarded with emails
//Note the feature Tech Support showed me on how to set the default timeframe of the indicator to the chart Timeframe, but also allow you to choose a different timeframe.
//By the way I fully disclose that I completely STOLE the Dots at the MAcd Cross from "TheLark"

strategy("MACD Strategy", overlay=false)
// study(title="CM_MacD_Ult_MTF", shorttitle="CM_Ult_MacD_MTF")
source = close
useCurrentRes = input(true, title="Use Current Chart Resolution?")
resCustom = input(title="Use Different Timeframe? Uncheck Box Above", defval="60")
smd = input(true, title="Show MacD & Signal Line? Also Turn Off Dots Below")
sd = input(true, title="Show Dots When MacD Crosses Signal Line?")
sh = input(true, title="Show Histogram?")
macd_colorChange = input(true,title="Change MacD Line Color-Signal Line Cross?")
hist_colorChange = input(true,title="MacD Histogram 4 Colors?")

res = useCurrentRes ? timeframe.period : resCustom

fastLength = input(12, minval=1), slowLength=input(26,minval=1)
signalLength=input(9,minval=1)

fastMA = ema(source, fastLength)
slowMA = ema(source, slowLength)

macd = fastMA - slowMA
signal = sma(macd, signalLength)
hist = macd - signal

outMacD = request.security(syminfo.tickerid, res, macd)
outSignal = request.security(syminfo.tickerid, res, signal)
outHist = request.security(syminfo.tickerid, res, hist)

histA_IsUp = outHist > outHist[1] and outHist > 0
histA_IsDown = outHist < outHist[1] and outHist > 0
histB_IsDown = outHist < outHist[1] and outHist <= 0
histB_IsUp = outHist > outHist[1] and outHist <= 0

//MacD Color Definitions
macd_IsAbove = outMacD >= outSignal
macd_IsBelow = outMacD < outSignal



// strategy.entry("Long", strategy.long, 1, when = shouldPlaceLong) 
       
// strategy.close("Long", shouldExitLong)
    

// strategy.entry("Short", strategy.short, 1, when = shouldPlaceShort) 
       
// strategy.close("Short", shouldExitShort)
    
    
isWithinZeroMacd = outHist < 30 and outHist > -30 

delta = hist
// shouldExitShort = false//crossover(delta, 0)    
// shouldExitLong = false//crossunder(delta, 0)

// if(crossover(delta, 0))// and not isWithinZeroMacd)
//     strategy.entry("Long", strategy.long, comment="Long")

// if (crossunder(delta, 0))// and not isWithinZeroMacd)
//     strategy.entry("Short", strategy.short, comment="Short")
    
shouldPlaceLong = crossover(delta, 0)
    
strategy.entry("Long", strategy.long, 1, when = shouldPlaceLong) 

shouldExitLong = not histA_IsUp and histA_IsDown

shouldExitShort = not histA_IsUp and not histA_IsDown and not histB_IsDown and histB_IsUp

shouldPlaceShort = crossunder(delta, 0)
strategy.entry("Short", strategy.short, 1, when = shouldPlaceShort) 
       
// plot_color = gray
plot_color = if(hist_colorChange)
	if(histA_IsUp)
	    aqua
	else
		if(histA_IsDown)
			//need to sell
// 			if(not isWithinZeroMacd)
// 			shouldExitLong = true
			    //   strategy.entry("Short", strategy.short, comment="Short")
			
			blue
		else
			if(histB_IsDown)
				red 
			else
				if(histB_IsUp)
					//need to buy
				// 	if(not isWithinZeroMacd)
				// 	shouldExitShort = true
					   // strategy.entry("Long", strategy.long, comment="Long")
					    
					    
					maroon
				else
					yellow
else
	gray


// plot_color = hist_colorChange ? histA_IsUp ? aqua : histA_IsDown ? blue : histB_IsDown ? red : histB_IsUp ? maroon :yellow :gray
macd_color = macd_colorChange ? macd_IsAbove ? lime : red : red
signal_color = macd_colorChange ? macd_IsAbove ? orange : orange : lime

circleYPosition = outSignal

plot(smd and outMacD ? outMacD : na, title="MACD", color=macd_color, linewidth=4)
plot(smd and outSignal ? outSignal : na, title="Signal Line", color=signal_color, style=line ,linewidth=2)
plot(sh and outHist ? outHist : na, title="Histogram", color=plot_color, style=histogram, linewidth=4)
plot(sd and cross(outMacD, outSignal) ? circleYPosition : na, title="Cross", style=circles, linewidth=4, color=macd_color)

// plot( isWithinZeroMacd ? outHist : na, title="CheckSmallHistBars", style=circles, linewidth=4, color=black)

hline(0, '0 Line',  linewidth=2, color=white)




strategy.close("Short", shouldExitShort)
strategy.close("Long", shouldExitLong)

// fastLength = input(12)
// slowlength = input(26)
// MACDLength = input(9)

// MACD = ema(close, fastLength) - ema(close, slowlength)
// aMACD = ema(MACD, MACDLength)
// delta = MACD - aMACD


// if (crossover(delta, 0))
   // strategy.entry("MacdLE", strategy.long, comment="MacdLE")

//if last two macd bars are higher than current, close long position

// if (crossunder(delta, 0))
   // strategy.entry("MacdSE", strategy.short, comment="MacdSE")

//if last two macd bars are higher than current, close long position

// plot(strategy.equity, title="equity", color=red, linewidth=2, style=areabr)