MACD 지표를 기반으로 한 양방향 거래 전략


생성 날짜: 2023-12-07 17:11:52 마지막으로 수정됨: 2023-12-07 17:11:52
복사: 0 클릭수: 829
avatar of ChaoZhang ChaoZhang
1
집중하다
1619
수행원

MACD 지표를 기반으로 한 양방향 거래 전략

개요

이 전략은 MACD 지표에 기반한 양방향 거래 전략을 구현한다. MACD 지표의 금포크와 사다리 시에는 각각 오이닝과 오피닝을 할 수 있으며, 다른 지표 판단과 함께 일부 신호를 필터링 할 수 있다.

전략 원칙

이 전략은 주로 MACD 지표를 사용하여 양방향 거래를 수행한다. 구체적으로, 그것은 빠른 이동 평균, 느린 이동 평균 및 MACD 신호선을 계산한다. 빠른 이동 평균 상에서 느린 이동 평균을 통과 할 때 골드 포크 신호를 생성한다. 빠른 이동 평균 아래에서 느린 이동 평균을 통과 할 때 죽은 포크 신호를 생성한다.

일부 무효 신호를 필터링하기 위해, 이 전략은 또한 ± 30 범위를 필터로 설정하여 MACD 기둥이 이 범위를 초과 할 때만 거래 신호를 유발합니다. 또한, 평소 상태에서 MACD 기둥이 방향을 판단하여 연속적으로 두 기둥의 방향이 바뀌었을 때만 평소됩니다.

전략적 이점

  • MACD 지표를 주요 거래 신호로 사용한다. 이 지표는 양방향 주식 시장의 상황에 상대적으로 민감하다.
  • 필터를 추가하여 일부 무효 신호를 필터링할 수 있습니다.
  • 연속적으로 두 기둥의 방향을 판단하는 평지 논리를 사용하여 가짜 돌파구를 어느 정도 피할 수 있습니다.

전략적 위험

  • 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)