행동 영역 ATR 역 순서 양성 전략

저자:차오장날짜: 2023-11-24 15:55:36
태그:

img

전반적인 설명

이 전략의 주요 아이디어는 액션 존과 ATR 지표를 결합하여 골든 크로스가 있을 때 긴 거리와 죽은 크로스가 있을 때 짧은 거리로 이동하는 것입니다. 또한 스톱 로스를 설정하고 수익을 취합니다. 가격 반전 신호가 발생하면 역 순서 기능을 달성하기 위해 역 포지션을 열 것입니다.

원칙

  1. 긴 신호와 짧은 신호를 계산하기 위해 빠른 EMA와 느린 EMA를 사용하십시오. 느린 EMA 이상의 빠른 EMA는 상승하고 그렇지 않으면 하락합니다.
  2. 위치가 없을 때, 골든 크로스 상에서 장점을 취하고, 죽은 크로스 상에서 단점을 취합니다.
  3. 이미 포지션을 가지고 있을 때, 반전 신호가 발생하면, 먼저 현재의 포지션을 닫고, 그 다음 반대 방향으로 새로운 포지션을 열게 됩니다.
  4. ATR 지표를 사용하여 스톱 로스 및 노프스 가격을 계산합니다. 작은 스톱 로스 위험을 보장하기 위해 스톱 로스 가격은 ATR 채널에 따라 조정됩니다.
  5. 가격이 과잉 구매 또는 과잉 판매 구역에 들어갈 때, 중지 손실 가격은 갇히지 않도록 마지막 바의 가장 높거나 가장 낮은 가격으로 조정됩니다.

장점

  1. 액션 존과 ATR을 결합하면 트렌드 중에 트렌드를 따라 포지션을 열 수 있고, 손해를 멈추고 적시에 이익을 얻을 수 있습니다.
  2. 리버스 오더 기능을 구현하면 가격이 뒤집어지면 빠르게 방향을 전환할 수 있으며, 더 높은 수익을 위해 양방향 가격 변동을 완전히 활용할 수 있습니다.
  3. ATR 스톱 손실 메커니즘은 단일 스톱 손실 위험을 효과적으로 제어하고 전반적으로 높은 승률을 달성 할 수 있습니다.
  4. 갑작스러운 사건에 걸리지 않도록 과소득과 과소득 판단과 결합합니다.

위험 과 해결책

  1. 리버스 오더는 범위에 제한된 시장에서 거래의 과도한 빈도를 유발하여 거래 비용을 증가시키고 스톱 로스 확률을 증가시킬 수 있습니다.
    • 솔루션: 범위 제한 시장에서 반전을 줄이기 위해 최소 보유 기간을 늘립니다.
  2. ATR 값의 변화는 스톱 손실 범위가 너무 크거나 너무 작을 수 있습니다.
    • 해결책: 실시간 ATR 값에 따라 스톱 손실 거리를 조정합니다.
  3. 부적절한 매개 변수 설정은 과도하게 높은 거래 주파수 또는 약한 신호 효과로 이어질 수 있습니다.
    • 해결책: 서로 다른 품종에 따라 합리적으로 매개 변수 조합을 선택하십시오.

최적화 방향

  1. 가장 좋은 매개 변수 조합을 찾기 위해 매개 변수 설정을 최적화합니다.
  2. 신호 품질을 향상시키기 위해 필터에 보조 기술 지표를 추가합니다.
  3. 자본 관리 모듈을 추가하여 포지션 크기를 전체 계좌 자산과 연결합니다.
  4. 더 많은 정보로 전략 성과를 향상시키기 위해 시간 프레임 분석을 추가하십시오.

요약

이 전략은 효율적인 양방향 거래를 달성하기 위해 액션 존 및 ATR 지표의 장점을 통합합니다. 역 순서 메커니즘과 지능형 ATR 스톱 손실은 가격 변동을 완전히 활용 할 수 있습니다. 매개 변수 설정을 최적화하고 더 많은 지표를 통합하면 전략 성능을 더욱 향상시킬 수 있습니다. 이 전략은 고 빈도 양방향 거래에 적합하며 보조 의사 결정 도구로도 사용될 수 있습니다.


/*backtest
start: 2023-10-24 00:00:00
end: 2023-11-23 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/
// © fenirlix

//@version=5
// Strategy parameter incl. position size, commission and initial capital
strategy("ACTIONZONE-ATR REVERSEORDER STRATEGY", "ACTIONZONEATR-REVERSEORDER", overlay=true
     )

// User Input Variable
fastMaInput     = input.int(12, "Fast MA Period", minval=2, step=1)
slowMaInput     = input.int(26, "Fast MA Period", minval=2, step=1)

atrLengthInput  = input.int(14, "ATR length", minval=2,step=1)
atrInnerMultInput = input.float(1, "atr inner multiplier", minval=0.1, step=0.1)
atrMidMultInput = input.float(2, "atr inner multiplier", minval=0.1, step=0.1) //***** MOST OF RISK MANAGEMENT LOGIC BASE ON THIS INPUT *****//
atrOuterMultInput = input.float(3, "atr inner multiplier", minval=0.1, step=0.1)

// Backtesting Date range
startYearInput      = input.int(2021, "Start Year", minval=1900, maxval=2100, step=1)
startMonthInput     = input.int(12, "Start Month", minval=1, maxval=12, step=1)
startDateInput      = input.int(1, "Start Day", minval=1, maxval=31, step=1)
setEndRangeInput    = input.bool(false, "Using Specific End Test Date") //Set specific End date or use present(end of candle) data
endYearInput        = input.int(2022, "End Year", minval=1900, maxval=2100, step=1)
endMonthInput       = input.int(1, "End Month", minval=1, maxval=12, step=1)
endDateInput        = input.int(31, "End Day", minval=1, maxval=31, step=1)

startDate = timestamp(syminfo.timezone, startYearInput, startMonthInput, startDateInput)
endDate = timestamp(syminfo.timezone, endYearInput, endMonthInput, endDateInput)
inDateRange = time >= startDate //Set backtest date range to present data
if setEndRangeInput
    inDateRange and time <= endDate //set backtest date range to specific date

// minimum position hold period (to get rid of false signal in sideway trend)
minHoldInput = input.int(8, 'Minimum position Hold Limit', minval=1, maxval=365, step=1) // Set Minimum Position Hold

var bool reverseToLong = false // Assign reverse order operator
var bool reverseToShort = false // Assign reverse order operator

// Indicator Declaration
fastEma = ta.ema(close, fastMaInput)
slowEma = ta.ema(close, slowMaInput)
atr = ta.atr(atrLengthInput)

// Declare trend of asset
isBullish = fastEma > slowEma
isBearish = fastEma <= slowEma

// Record position hold length, to limit minimum hold period(candle)
var int hold_length = 0
if strategy.opentrades > 0 or strategy.opentrades < 0
    hold_length := hold_length + 1
else
    hold_length := 0

// create permanent variable of stop price
var float longStopPrice = na
var float shortStopPrice = na
    
// Chart-Indicator COLOR declaration
REDBEAR     = color.new(color.red, 80)
GREENBULL   = color.new(color.green, 80)

greenLong = isBullish and close > fastEma
yellowLong = isBullish and close < fastEma
blueShort = isBearish and close > fastEma
redShort = isBearish and close < fastEma

// assign oversold, overbought condition(in this case, price over middle atr plus/minus fastEma)
overBand = high[1] > fastEma + (2*atr)
underBand = low[1] < fastEma - (2*atr)

// Strategy

// Main Entry Condition
goLong = isBullish and isBullish[1] == 0
goShort = isBearish and isBearish[1] == 0

inPosition = strategy.position_size != 0
minHoldPeriod = hold_length > minHoldInput ? true : false

// Entry Condition
if not inPosition and inDateRange and barstate.isconfirmed == true //compute after close of the bar to avoid repainting
    if goLong or reverseToLong // Long if longcondition or reverse order receive.
        strategy.entry('long', strategy.long)
        longStopPrice := fastEma - (atr * 2) // Set stop loss price
        reverseToLong := false // Reset reverse order status
    
    else if goShort or reverseToShort
        strategy.entry('short', strategy.short)
        shortStopPrice := fastEma + (atr * 2)
        reverseToShort := false
// Take profit and Set Higher Stop 
if inPosition and minHoldPeriod and barstate.isconfirmed == true // check if we're in position and pass minimum hold period, confirm no repainting
    if strategy.position_size > 0
        // if exit position by Sellcondition(which is the same as ShortCondition), Exit Long position and make Short order(by set reverse order to true)
        strategy.close('long', when=goShort, comment='exitLong(' + str.tostring(hold_length) + ')')
        reverseToShort := true
        if overBand //If overbought condition met, set Stop price to LAST LOW, and not reverse any position
            longStopPrice := low[1]
            reverseToShort := false
    else if strategy.position_size < 0
        strategy.close('short', when=goLong, comment='exitShort(' + str.tostring(hold_length) + ')')
        reverseToLong := true
        if underBand
            shortStopPrice := high[1]
            reverseToLong := false
// Stop Loss and Set calculate stop loss using Atr Channel
if inPosition 
    if strategy.position_size > 0
        if fastEma - (atr * atrMidMultInput) > longStopPrice // set long stop price to the higher of latest long stop price and ATR lower channel
            longStopPrice := fastEma - (atr * atrMidMultInput)
        strategy.exit('Long Stop atr ', 'long', stop=longStopPrice)
    else if strategy.position_size < 0
        if fastEma + (atr * atrMidMultInput) < shortStopPrice
            shortStopPrice := fastEma + (atr * atrMidMultInput)
        strategy.exit('Short Stop atr ', 'short', stop=shortStopPrice)

// Plotting
fastLine = plot(fastEma, title='fast ema line', linewidth=1, color=isBullish ? color.green : color.red)
slowLine = plot(slowEma, title='slow ema line', linewidth=2, color= isBullish? color.green : color.red)
atrUpperLine1 = plot(fastEma + (atr * atrInnerMultInput), title='ATR Upperline1', color=color.new(color.black,85))
atrLowerLine1 = plot(fastEma - (atr * atrInnerMultInput), title='ATR Lowerline1', color=color.new(color.black,85))
atrUpperLine2 = plot(fastEma + (atr * atrMidMultInput), title='ATR Upperline2', color=color.new(color.black,75))
atrLowerLine2 = plot(fastEma - (atr * atrMidMultInput), title='ATR Lowerline2', color=color.new(color.black,75))
atrUpperLine3 = plot(fastEma + (atr * atrOuterMultInput), title='ATR Upperline3', color=color.new(color.black,50))
atrLowerLine3 = plot(fastEma - (atr * atrOuterMultInput), title='ATR Lowerline3', color=color.new(color.black,50))

plot(longStopPrice, color=strategy.position_size > 0 ? color.red : na, linewidth=2)
plot(shortStopPrice, color=strategy.position_size < 0 ? color.red : na, linewidth=2)

//  Filling
fill(fastLine, slowLine, color=isBullish ? GREENBULL : REDBEAR)
fill(atrUpperLine3, atrLowerLine3, color=inPosition and (minHoldInput - hold_length > 0) ? color.new(color.blue,90): na)

barColor = switch
    greenLong => color.green
    yellowLong =>  color.yellow
    blueShort => color.blue
    redShort => color.red
    => color.black
barcolor(color=barColor)

// Fill background to distinguish inactive time(Zulu time)
nightTime = time(timeframe.period, "1500-0100") ? color.new(color.black, 95): na
bgcolor(nightTime)


더 많은