이동평균 기반 다이버전스 전략


생성 날짜: 2024-01-24 11:43:41 마지막으로 수정됨: 2024-01-24 11:43:41
복사: 1 클릭수: 654
avatar of ChaoZhang ChaoZhang
1
집중하다
1621
수행원

이동평균 기반 다이버전스 전략

개요

이 전략은 이동 평균과 그 축점들을 계산하여 가격과 이동 평균 사이의 오차를 발견하여 구매 및 판매 신호로 사용한다. 이 전략은 오차를 찾기 위해 모든 진동 지표에 적용할 수 있다. 이것은 재검토 및 실전 거래에 사용할 수 있는 가치 있는 도구이다.

전략 원칙

  1. Len의 이동 평균 (MA) 로 계산
  2. MA의 중심축 낮은 점 ((PL) 과 중심축 높은 점 ((PH) 을 검출
  3. 정방향의 존재 여부를 판단한다: 가격 혁신 하위점과 MA는 혁신 하위점 또는 가격 혁신 하위점과 MA는 혁신 하위점
  4. 역방향이 존재하는지 판단하기 위해: 가격 혁신이 높은 반면 MA는 혁신이 높지 않거나 가격 혁신이 높지 않은 반면 MA는 혁신이 높습니다.
  5. 사거나 파는 것은 상황에 따라 결정됩니다.

우위 분석

  1. 가격과 MA 사이의 오차를 자동으로 감지하여 인적 판단 오류를 방지합니다.
  2. 모든 진동 지표에 적용 가능, 확장성이 강
  3. 전략의 수익성을 재검토하고 검증하는 데 사용할 수 있습니다.
  4. 설정 가능한 매개 변수가 감수성을 조정하여 잘못된 신호를 방지합니다.
  5. 다양한 종류의 탈선, 정확하고 포괄적인 판단을 제공합니다

위험 분석

  1. 진동 지표가 잘못 설정되면 많은 잘못된 신호가 발생할 수 있습니다.
  2. 발현하기 전에 유효한 축점으로부터 멀어지면 신호 부족이 발생할 수 있습니다.
  3. 감수성과 필터링 오류 신호를 균형을 맞추기 위해 파라미터를 적절하게 조정해야 합니다.
  4. 다른 요소와 결합하여 더 잘 사용되며, 단독으로 사용해도 신뢰도가 낮습니다.

최적화 방향

  1. 이동 평균 변수를 최적화하여 최적의 변수 조합을 찾습니다.
  2. 다른 지표와 같이 수량 가격 지표와 함께 잘못된 신호를 피하십시오.
  3. 더 많은 기계 학습 모델은 신뢰성이 떨어집니다.
  4. 위험 관리 제도를 강화하고 단독 손실을 통제합니다.

요약하다

이 전략은 가격과 이동 평균 사이의 오차를 발견하여 거래 신호로 판단을 자동화하여 주관적 오류를 방지할 수 있다. 모든 진동 지표에 광범위하게 적용할 수 있으며, 강력한 확장성을 가지고 있다. 다른 지표와 함께 파라미터 최적화를 사용하여 거래 신호의 신뢰성과 시스템의 안정성을 크게 향상시킬 수 있다.

전략 소스 코드
/*backtest
start: 2023-12-24 00:00:00
end: 2024-01-12 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tista 
//https://www.tradingview.com/u/tista/#published-scripts

//@version=4

strategy(title="MA Divergences", format=format.price)

//* Backtesting Period Selector | Component *//
//* https://www.tradingview.com/script/eCC1cvxQ-Backtesting-Period-Selector-Component *//
//* https://www.tradingview.com/u/pbergden/ *//
//* Modifications made *//
testStartYear = input(2021, "Backtest Start Year") 
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)

testStopYear = input(999999, "Backtest Stop Year")
testStopMonth = input(9, "Backtest Stop Month")
testStopDay = input(26, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)

testPeriod() => true
/////////////// END - Backtesting Period Selector | Component ///////////////
len = input(title="MA Period", minval=1, defval=14)
src = input(title="MA Source", defval=close)
lbR = input(title="Pivot Lookback Right", defval=5)
lbL = input(title="Pivot Lookback Left", defval=5)
rangeUpper = input(title="Max of Lookback Range", defval=600)
rangeLower = input(title="Min of Lookback Range", defval=2)
plotBull = input(title="Plot Bullish", defval=true)
plotHiddenBull = input(title="Plot Hidden Bullish", defval=true)
plotBear = input(title="Plot Bearish", defval=true)
plotHiddenBear = input(title="Plot Hidden Bearish", defval=true)

bearColor = color.red
bullColor = color.green
hiddenBullColor = color.green
hiddenBearColor = color.red
textColor = color.white
noneColor = color.new(color.white, 100)

osc = wma(src, len)

plot(osc, title="MA", linewidth=2, color=color.yellow)

plFound = na(pivotlow(osc, lbL, lbR)) ? false : true
phFound = na(pivothigh(osc, lbL, lbR)) ? false : true

_inRange(cond) =>
    bars = barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper

alertcondition(osc[1] > 100.0 and osc[2] < 100.0, title="MA value crosses over 100.0", message="Check charts for a MA cross over 100.0")
alertcondition(osc[1] < 100.0 and osc[2] > 100.0, title="MA value crosses under 100.0", message="Check charts for a MA cross under 100.0")
alertcondition(osc[1] > -100. and osc[2] < -100.0, title="MA value crosses over -100.0", message="Check charts for a MA cross over -100.0")
alertcondition(osc[1] < -100.0 and osc[2] > -100.0, title="MA value crosses under -100.0", message="Check charts for a MA cross under -100.0")

//------------------------------------------------------------------------------
// Regular Bullish

// Osc: Higher Low
oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Lower Low
priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)

bullCond = plotBull and priceLL and oscHL and plFound

plot(
	 plFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bullish",
	 linewidth=2,
	 color=(bullCond ? bullColor : noneColor),
	 transp=0
	 )

plotshape(
	 bullCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bullish Label",
	 text=" Bull ",
	 style=shape.labelup,
	 location=location.absolute,
	 color=bullColor,
	 textcolor=textColor,
	 transp=0
	 )

alertcondition(bullCond, title="Regular bullish divergence in MA found", message="Check charts for a regular bullish divergence found with MA")

//------------------------------------------------------------------------------
// Hidden Bullish

// Osc: Lower Low
oscLL = osc[lbR] < valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Higher Low
priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)

hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound

plot(
	 plFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bullish",
	 linewidth=2,
	 color=(hiddenBullCond ? hiddenBullColor : noneColor),
	 transp=0
	 )

plotshape(
	 hiddenBullCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bullish Label",
	 text=" H Bull ",
	 style=shape.labelup,
	 location=location.absolute,
	 color=bullColor,
	 textcolor=textColor,
	 transp=0
	 )

alertcondition(hiddenBullCond, title="Hidden bullish divergence in MA found", message="Check charts for a hidden bullish divergence found with MA")

//------------------------------------------------------------------------------
// Regular Bearish

// Osc: Lower High
oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// Price: Higher High
priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)

bearCond = plotBear and priceHH and oscLH and phFound

plot(
	 phFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bearish",
	 linewidth=2,
	 color=(bearCond ? bearColor : noneColor),
	 transp=0
	 )

plotshape(
	 bearCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bearish Label",
	 text=" Bear ",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=bearColor,
	 textcolor=textColor,
	 transp=0
	 )

alertcondition(bearCond, title="Regular bearish divergence in MA found", message="Check charts for a regular bearish divergence found with MA")

//------------------------------------------------------------------------------
// Hidden Bearish

// Osc: Higher High
oscHH = osc[lbR] > valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// Price: Lower High
priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)

hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound

plot(
	 phFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bearish",
	 linewidth=2,
	 color=(hiddenBearCond ? hiddenBearColor : noneColor),
	 transp=0
	 )

plotshape(
	 hiddenBearCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bearish Label",
	 text=" H Bear ",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=bearColor,
	 textcolor=textColor,
	 transp=0
	 )

// Alerts
//alertcondition(bearCond or hiddenBearCond, title='Bear div', message='Bear div')
//alertcondition(bullCond or hiddenBullCond, title='Bull div', message='Bull div')
//alertcondition(bearCond or bullCond, title='Bull or beal div', message='Bull or bear div') 
//alertcondition(hiddenBearCond or hiddenBullCond, title='Bull or beal div', message='Hidden Bull or bear div') 
//alertcondition(hiddenBearCond or hiddenBullCond or bearCond or bullCond, title='Bull or beal div', message='Any Bull or bear div') 

if testPeriod()
    if bullCond or hiddenBullCond
        strategy.entry("Buy", strategy.long)
    if bearCond or hiddenBearCond
        strategy.entry("Sell", strategy.short)