이동 평균 회귀 거래 전략


생성 날짜: 2023-10-26 15:38:14 마지막으로 수정됨: 2023-10-26 15:38:14
복사: 0 클릭수: 628
avatar of ChaoZhang ChaoZhang
1
집중하다
1617
수행원

이동 평균 회귀 거래 전략

개요

평균선 회귀 거래 전략은 가격의 평균선에서 벗어난 정도를 기반으로 거래하는 전략이다. 이 전략은 가격의 단기간의 평균선에서 벗어난 특징을 활용하여 가격이 평균선보다 크게 낮거나 높을 때 포지션을 설정하고 가격이 평균선으로 돌아가는 경우 경매를 조정한다.

전략 원칙

이 전략은 우선 일정 주기의 이동 평균을 계산하여 장기적인 가격 추세를 나타냅니다. 그리고 가격의 이동 평균에 대한 편차 정도에 따라 포지션을 설정하는 시간과 포지션 규모를 판단합니다.

가격이 이동 평균보다 일정 비율로 낮아지면, 장기적인 추세에서 벗어난다는 것을 나타냅니다. 이 경우 특정 포지션 비율에 따라 순차적으로 다중을 구축합니다. 가격이 멀리 떨어져 갈수록, 구축 된 포지션은 더 커집니다. 가격이 이동 평균보다 다시 상승하면, 장기적인 추세로 돌아온다는 것을 나타냅니다.

마찬가지로, 가격이 이동 평균보다 일정 비율로 높을 때 공표를 설정한다. 가격이 이동 평균으로 돌아가는 경우, 비율로 평형한다.

우위 분석

  1. 평균선의 트렌드 식별 능력을 활용하여 주식 가격의 장기적인 균형 트렌드를 따라 주요 트렌드 방향을 파악한다.

  2. 포지션을 배치하여 창고 건설 비용을 절감하고 우수한 비용 가격을 얻습니다.

  3. 단계적 정지, 각기 다른 수준의 회귀 평균선으로 서로 다른 정지 기회를 사용하여 위험을 줄인다.

  4. 포지션 컨트롤은 일정한 지분을 사용해서 단일 손실이 너무 크지 않도록 한다.

  5. 매개 변수 설정은 다양한 품종에 따라 이동 평균 주기 또는 포지션 비율을 조정할 수 있습니다.

위험 분석

  1. 가격 변동이 있을 때, 빈번하게 멈출 수 있다. 적절히 느슨하게 멈출 수 있다, 또는 다른 필터링 조건을 적용한다.

  2. 강한 주식은 직접 평균선을 뚫고 계속 상승하거나 하락할 수 있으며, 평균선 정지점으로 돌아갈 수 없다. 강한 경향을 판단하기 위해 트렌드 지표와 결합하여 포지션을 줄일 수 있다.

  3. 매개 변수 설정이 잘못되면 너무 급진적인 포지션 구축이나 상쇄로 이어질 수 있습니다. 매개 변수를 신중하게 테스트하고 시장에 따라 조정해야합니다.

  4. 거래 빈도가 높으면 거래 비용이 더 높을 수 있으며, 비용 요소 최적화 매개 변수를 고려해야 한다.

최적화 방향

  1. 이동평선주기를 최적화하여 다양한 품종 특성에 적응한다.

  2. 포지션 비율을 최적화하고 리스크/이익을 균형 잡는다.

  3. 불필요한 거래를 방지하기 위해 다른 기술 지표 필터를 추가하십시오.

  4. 변동률 지표와 함께 시장의 변동 정도에 따라 포지션 비율을 조정한다.

  5. 리스크를 줄이고 수익률을 높이기 위해 제약 확대 메커니즘에 가입하십시오.

요약하다

평평선 회귀 전략은 주식의 균형 회귀 특성을 활용하여 가격이 이동 평균선에서 벗어날 때 포지션을 구축하고, 가격이 돌아오는 경우 정지할 수 있으며, 주식의 장기 단기 추세를 효과적으로 파악할 수 있다. 매개 변수 최적화 및 지표 필터링을 통해 시장의 변화에 적응할 수 있으며, 위험을 통제하는 전제하에서 더 나은 수익을 얻을 수 있다. 이 전략은 추세 추적을 고려하고 위험 통제에 중점을 두는 동시에 투자자의 연구와 응용에 가치가 있다.

전략 소스 코드
/*backtest
start: 2022-10-19 00:00:00
end: 2023-10-25 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy("YJ Mean Reversion", overlay=true)
//Was designed firstly to work on an index like the S&P 500 , which over time tends to go up in value. 
//Avoid trading too frequently (e.g. Daily, Weekly), to avoid getting eaten by fees. 
//If you change the underlying asset, or time frame, tweaking the moving average may be necessary. 
//Can work with a starting capital of just $1000, optimise the settings as necessary. 
//Accepts floating point values for the amount of units to purchase (e.g. Bitcoin ). 
//If price of units exceeds available capital, script will cancel the buy. 
//Adjusted the input parameters to be more intuitive.

//input variables
movingAverage = input(title="Moving Average (bars)", type=input.integer, defval=28, minval=1, maxval=1000)
//riskPercentage = input(title="Amount to Risk (%)", type=input.integer, defval=1, minval=1, maxval=50)
deviation = input(title="Deviation Increment (%)", type=input.float, defval=5, minval=0.01, maxval=100) / 100
unitsLevel1 = input(title="Level 1 (units)", type=input.float, defval=1, minval=0.0001, maxval=10000)
unitsLevel2 = input(title="Level 2 (units)", type=input.float, defval=2, minval=0.0001, maxval=10000)
unitsLevel3 = input(title="Level 3 (units)", type=input.float, defval=4, minval=0.0001, maxval=10000)
unitsLevel4 = input(title="Level 4 (units)", type=input.float, defval=8, minval=0.0001, maxval=10000)
unitsLevel5 = input(title="Level 5 (units)", type=input.float, defval=16, minval=0.0001, maxval=10000)

//moving average and ma slope (use on weekly chart)
ma = sma(close, movingAverage)
//sl = ma > ma[4]

//units to buy
//amount = riskPercentage / 100 * (strategy.initial_capital + strategy.netprofit)
//units = floor(amount / close)

//mode 1
//strategy.order("buy", true, 1, when = (close < 0.95 * ma) and (strategy.position_size < 10))
//strategy.order("sell", false, strategy.position_size, when = (close > 1.05 * ma) and (strategy.position_size > 0))

//mode 2
//strategy.order("buy", true, 1, when = close < 0.8 * ma)
//strategy.order("sell", false, strategy.position_size, when = (close > 310) and (strategy.position_size > 0))

//mode 3
//strategy.order("buy", true, 1, when = (close < 0.95 * ma) and (close > 0.9 * ma))
//strategy.order("buy", true, 2, when = (close < 0.9 * ma) and (close > 0.85 * ma))
//strategy.order("buy", true, 4, when = (close < 0.85 * ma) and (close > 0.8 * ma))
//strategy.order("buy", true, 8, when = (close < 0.8 * ma) and (close > 0.75 * ma))
//strategy.order("buy", true, 16, when = (close < 0.75 * ma))
//strategy.order("sell", false, strategy.position_size, when = (close > 310) and (strategy.position_size > 0))

//mode 4
//strategy.order("buy", true, 1, when = (close < 0.98 * ma) and (close > 0.96 * ma) and (sl))
//strategy.order("buy", true, 2, when = (close < 0.96 * ma) and (close > 0.94 * ma) and (sl))
//strategy.order("buy", true, 4, when = (close < 0.94 * ma) and (close > 0.92 * ma) and (sl))
//strategy.order("buy", true, 8, when = (close < 0.92 * ma) and (close > 0.90 * ma) and (sl))
//strategy.order("buy", true, 16, when = (close < 0.90 * ma) and (sl))
//strategy.order("sell", false, strategy.position_size, when = (close > 310) and (strategy.position_size > 0))

//mode 5
//strategy.order("buy", true, 1, when = (close < 0.95 * ma) and (close > 0.9 * ma))
//strategy.order("buy", true, 2, when = (close < 0.9 * ma) and (close > 0.85 * ma))
//strategy.order("buy", true, 4, when = (close < 0.85 * ma) and (close > 0.8 * ma))
//strategy.order("buy", true, 8, when = (close < 0.8 * ma) and (close > 0.75 * ma))
//strategy.order("buy", true, 16, when = (close < 0.75 * ma))

//strategy.order("sell", false, 1, when = (close > 1.05 * ma) and (close < 1.1 * ma) and (strategy.position_size > 0))
//strategy.order("sell", false, 2, when = (close > 1.1 * ma) and (close < 1.15 * ma) and (strategy.position_size > 0))
//strategy.order("sell", false, 4, when = (close > 1.15 * ma) and (close < 1.2 * ma) and (strategy.position_size > 0))
//strategy.order("sell", false, 8, when = (close > 1.2 * ma) and (close < 1.25 * ma) and (strategy.position_size > 0))
//strategy.order("sell", false, 16, when = (close > 1.25 * ma) and (close < 1.3 * ma) and (strategy.position_size > 0))

//mode 6
//strategy.order("B1", true, unitsLevel1 * units, when = (close < 0.95 * ma) and (close > 0.9 * ma))
//strategy.order("B2", true, unitsLevel2 * units, when = (close < 0.9 * ma) and (close > 0.85 * ma))
//strategy.order("B3", true, unitsLevel3 * units, when = (close < 0.85 * ma) and (close > 0.8 * ma))
//strategy.order("B4", true, unitsLevel4 * units, when = (close < 0.8 * ma) and (close > 0.75 * ma))
//strategy.order("B5", true, unitsLevel5 * units, when = (close < 0.75 * ma))

//strategy.order("S1", false, unitsLevel1 * units, when = (close > 1.05 * ma) and (close < 1.1 * ma) and (strategy.position_size > 0))
//strategy.order("S2", false, unitsLevel2 * units, when = (close > 1.1 * ma) and (close < 1.15 * ma) and (strategy.position_size > 0))
//strategy.order("S3", false, unitsLevel3 * units, when = (close > 1.15 * ma) and (close < 1.2 * ma) and (strategy.position_size > 0))
//strategy.order("S4", false, unitsLevel4 * units, when = (close > 1.2 * ma) and (close < 1.25 * ma) and (strategy.position_size > 0))
//strategy.order("S5", false, unitsLevel5 * units, when = (close > 1.25 * ma) and (close < 1.3 * ma) and (strategy.position_size > 0))

//mode 7
//strategy.order("B1", true, units, when = (close < 0.95 * ma) and (close > 0.9 * ma))
//strategy.order("B2", true, units, when = (close < 0.9 * ma) and (close > 0.85 * ma))
//strategy.order("B3", true, units, when = (close < 0.85 * ma) and (close > 0.8 * ma))
//strategy.order("B4", true, units, when = (close < 0.8 * ma) and (close > 0.75 * ma))
//strategy.order("B5", true, units, when = (close < 0.75 * ma))

//strategy.order("S1", false, units, when = (close > 1.05 * ma) and (close < 1.1 * ma) and (strategy.position_size > 0))
//strategy.order("S2", false, units, when = (close > 1.1 * ma) and (close < 1.15 * ma) and (strategy.position_size > 0))
//strategy.order("S3", false, units, when = (close > 1.15 * ma) and (close < 1.2 * ma) and (strategy.position_size > 0))
//strategy.order("S4", false, units, when = (close > 1.2 * ma) and (close < 1.25 * ma) and (strategy.position_size > 0))
//strategy.order("S5", false, units, when = (close > 1.25 * ma) and (close < 1.3 * ma) and (strategy.position_size > 0))

//banding calculations
aH = 1.0 - deviation
aL = aH - deviation
bH = aL
bL = bH - deviation
cH = bL
cL = cH - deviation
dH = cL
dL = dH - deviation
eH = dL
strategy.initial_capital = 50000
//mode 8
strategy.order("B1", true, unitsLevel1, when = (close < aH * ma) and (close > aL * ma) and (unitsLevel1 * close < (strategy.initial_capital + strategy.netprofit)))
strategy.order("B2", true, unitsLevel2, when = (close < bH * ma) and (close > bL * ma) and (unitsLevel2 * close < (strategy.initial_capital + strategy.netprofit)))
strategy.order("B3", true, unitsLevel3, when = (close < cH * ma) and (close > cL * ma) and (unitsLevel3 * close < (strategy.initial_capital + strategy.netprofit)))
strategy.order("B4", true, unitsLevel4, when = (close < dH * ma) and (close > dL * ma) and (unitsLevel4 * close < (strategy.initial_capital + strategy.netprofit)))
strategy.order("B5", true, unitsLevel5, when = (close < eH * ma) and (unitsLevel5 * close < (strategy.initial_capital + strategy.netprofit)))

//banding calculations
fL = 1.0 + deviation
fH = fL + deviation
gL = fH
gH = gL + deviation
hL = gH
hH = hL + deviation
iL = hH
iH = iL + deviation
jL = iH

strategy.order("S1", false, unitsLevel1, when = (close > fL * ma) and (close < fH * ma) and (strategy.position_size > 0))
strategy.order("S2", false, unitsLevel2, when = (close > gL * ma) and (close < gH * ma) and (strategy.position_size > 0))
strategy.order("S3", false, unitsLevel3, when = (close > hL * ma) and (close < hH * ma) and (strategy.position_size > 0))
strategy.order("S4", false, unitsLevel4, when = (close > iL * ma) and (close < iH * ma) and (strategy.position_size > 0))
strategy.order("S5", false, unitsLevel5, when = (close > jL * ma) and (strategy.position_size > 0))

plot(ma, color=#666666, linewidth=5)