이동 평균선과 일중 패턴을 기반으로 한 지능형 트레일링 손절매 전략

SMA MA18 ATR
생성 날짜: 2025-01-17 16:04:09 마지막으로 수정됨: 2025-01-17 16:04:09
복사: 4 클릭수: 361
avatar of ChaoZhang ChaoZhang
1
집중하다
1617
수행원

이동 평균선과 일중 패턴을 기반으로 한 지능형 트레일링 손절매 전략

개요

이는 18일 이동평균(SMA18)을 기반으로 한 전략으로, 일중 거래 패턴 인식과 스마트 트레일링 스톱 메커니즘을 결합한 것입니다. 이 전략은 주로 가격과 SMA18 간의 관계를 관찰하고, 일중 고점과 저점을 결합하며, 적절한 시기에 롱 포지션에 진입합니다. 이 전략은 고정 손절매 지점이나 2일 최저가 지점을 추적 손절매 벤치마크로 사용할 수 있는 유연한 손절매 계획을 채택합니다.

전략 원칙

전략의 핵심 논리에는 다음과 같은 핵심 요소가 포함됩니다.

  1. 진입 조건은 18일 이동 평균에 대한 가격의 상대적 위치에 따라 달라집니다. 이동 평균을 돌파할 때 시장에 진입하거나 이동 평균 위에 진입할 수 있습니다.
  2. 일중 K-라인 패턴 분석, 특히 내부 K-라인(Inside Bar) 패턴에 주목하여 진입 정확도를 높입니다.
  3. 주중의 다양한 거래일의 성과 특성을 기반으로 특정 요일을 선택적으로 거래할 수 있습니다.
  4. 진입 가격은 제한 주문으로 설정되며, 최저점 위에 약간의 프리미엄이 추가되어 거래 확률이 높아집니다.
  5. 손절매 메커니즘은 두 가지 모드를 지원합니다. 하나는 진입 가격을 기반으로 하는 고정 손절매이고, 다른 하나는 이전 2개 거래일의 최저가를 기반으로 하는 추적 손절매입니다.

전략적 이점

  1. 기술 지표와 가격 패턴을 결합하면 진입 신호가 더욱 안정적입니다.
  2. 다양한 시장 특성에 따라 최적화 가능한 유연한 거래 시간 선택 메커니즘
  3. 지능형 손절매 솔루션은 가격에 변동을 허용하면서도 수익을 보호합니다.
  4. 전략 매개변수는 매우 조정 가능하며 다양한 시장 환경에 적응할 수 있습니다.
  5. 내부 K-라인 패턴을 스크리닝하여 거짓 신호를 효과적으로 줄입니다.

전략적 위험

  1. 변동성이 큰 시장에서는 고정된 정지가 조기 종료로 이어질 수 있습니다.
  2. 빠른 반전을 위해 추적 손절매는 적은 이익을 확보할 수 있습니다.
  3. 횡보 국면에서는 내부 캔들스틱이 자주 나타나 과도한 거래로 이어질 수 있습니다. 대책:
  • 시장 변동성에 따라 손절매 거리를 동적으로 조정합니다.
  • 추세 확인 지표 추가
  • 저품질 거래를 걸러내기 위해 최소 이익 목표를 설정하세요

전략 최적화 방향

  1. 변동성 지표(ATR 등)를 도입하여 손절매 거리를 동적으로 조정합니다.
  2. 볼륨 분석의 차원을 확대하고 신호 신뢰성을 향상시킵니다.
  3. 과거 실적을 기반으로 거래 시간을 자동으로 최적화하기 위해 보다 스마트한 날짜 선택 알고리즘을 개발합니다.
  4. 약한 추세에서 거래를 방지하기 위해 추세 강도 필터 추가
  5. 패턴 인식의 정확도를 높이기 위해 내부 K-라인 인식 알고리즘을 최적화합니다.

요약하다

이 전략은 여러 차원의 분석 방법을 결합하여 비교적 완전한 거래 시스템을 구축합니다. 이 전략의 핵심적인 장점은 유연한 매개변수 설정과 지능형 손절매 메커니즘에 있으며, 이를 통해 다양한 시장 환경에 적응할 수 있습니다. 지속적인 최적화와 개선을 통해 이 전략은 다양한 시장 상황에서도 안정적인 성과를 유지할 것으로 기대됩니다.

전략 소스 코드
/*backtest
start: 2019-12-23 08:00:00
end: 2025-01-16 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT","balance":49999}]
*/

//@version=5
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zweiprozent

strategy('Buy Low over 18 SMA Strategy', overlay=true, default_qty_value=1)
xing = input(false, title='crossing 18 sma?')
sib = input(false, title='trade inside Bars?')
shortinside = input(false, title='trade inside range bars?')
offset = input(title='offset', defval=0.001)
belowlow = input(title='stop below low minus', defval=0.001)
alsobelow = input(false, title='Trade only above 18 sma?')
tradeabove = input(false, title='Trade with stop above order?')
trailingtwo = input(false, title='exit with two days low trailing?')


insideBar() =>  //and high <= high[1] and low >= low[1] ? 1 : 0
    open <= close[1] and close >= open[1] and close <= close[1] or open >= close[1] and open <= open[1] and close <= open[1] and close >= close[1] ? 1 : 0

inside() =>
    high <= high[1] and low >= low[1] ? 1 : 0
enterIndex = 0.0
enterIndex := enterIndex[1]

inPosition = not na(strategy.position_size) and strategy.position_size > 0
if inPosition and na(enterIndex)
    enterIndex := bar_index
    enterIndex



//if strategy.position_size <= 0 

//    strategy.exit("Long", stop=low[0]-stop_loss,comment="stop loss")


//if not na(enterIndex) and bar_index - enterIndex + 0 >= 0 

//    strategy.exit("Long", stop=low[0]-belowlow,comment="exit")

//    enterIndex := na

T_Low = request.security(syminfo.tickerid, 'D', low[0])
D_High = request.security(syminfo.tickerid, 'D', high[1])
D_Low = request.security(syminfo.tickerid, 'D', low[1])
D_Close = request.security(syminfo.tickerid, 'D', close[1])
D_Open = request.security(syminfo.tickerid, 'D', open[1])

W_High2 = request.security(syminfo.tickerid, 'W', high[1])
W_High = request.security(syminfo.tickerid, 'W', high[0])
W_Low = request.security(syminfo.tickerid, 'W', low[0])
W_Low2 = request.security(syminfo.tickerid, 'W', low[1])
W_Close = request.security(syminfo.tickerid, 'W', close[1])
W_Open = request.security(syminfo.tickerid, 'W', open[1])

//longStopPrice  = strategy.position_avg_price * (1 - stopl)
// Go Long - if prev day low is broken and stop loss prev day low
entryprice = ta.sma(close, 18)

//(high[0]<=high[1]or close[0]<open[0]) and low[0]>vwma(close,30) and time>timestamp(2020,12,0,0,0)

showMon = input(true, title='trade tuesdays?')
showTue = input(true, title='trade wednesdayy?')
showWed = input(true, title='trade thursday?')
showThu = input(true, title='trade friday?')
showFri = input(true, title='trade saturday?')
showSat = input(true, title='trade sunday?')
showSun = input(true, title='trade monday?')

isMon() =>
    dayofweek(time('D')) == dayofweek.monday and showMon
isTue() =>
    dayofweek(time('D')) == dayofweek.tuesday and showTue
isWed() =>
    dayofweek(time('D')) == dayofweek.wednesday and showWed
isThu() =>
    dayofweek(time('D')) == dayofweek.thursday and showThu
isFri() =>
    dayofweek(time('D')) == dayofweek.friday and showFri
isSat() =>
    dayofweek(time('D')) == dayofweek.saturday and showSat
isSun() =>
    dayofweek(time('D')) == dayofweek.sunday and showSun


clprior = close[0]
entryline = ta.sma(close, 18)[1]
//(isMon() or isTue()or isTue()or  isWed() 
noathigh = high < high[1] or high[2] < high[3] or high[1] < high[2] or low[1] < ta.sma(close, 18)[0] and close > ta.sma(close, 18)[0]

if noathigh and time > timestamp(2020, 12, 0, 0, 0) and (alsobelow == false or high >= ta.sma(close, 18)[0]) and (isMon() or isTue() or isWed() or isThu() or isFri() or isSat() or isSun()) and (high >= high[1] or sib or low <= low[1])  //((sib == false and inside()==true) or inside()==false) and (insideBar()==true or shortinside==false)
    if tradeabove == false
        strategy.entry('Long', strategy.long, limit=low + offset * syminfo.mintick, comment='long')
    if tradeabove == true and (xing == false or clprior < entryline)  // and high<high[1] 
        strategy.entry('Long', strategy.long, stop=high + offset * syminfo.mintick, comment='long')


//if time>timestamp(2020,12,0,0,0) and isSat()  
//    strategy.entry("Long", strategy.long, limit=0, comment="long")


//strategy.exit("Long", stop=low-400*syminfo.mintick)

//strategy.exit("Long", stop=strategy.position_avg_price-10*syminfo.mintick,comment="exit")
//strategy.exit("Long", stop=low[1]-belowlow*syminfo.mintick, comment="stop")

if strategy.position_avg_price > 0 and trailingtwo == false and close > strategy.position_avg_price
    strategy.exit('Long', stop=strategy.position_avg_price, comment='stop')

if strategy.position_avg_price > 0 and trailingtwo == false and (low > strategy.position_avg_price or close < strategy.position_avg_price)
    strategy.exit('Long', stop=low[0] - belowlow * syminfo.mintick, comment='stop')

if strategy.position_avg_price > 0 and trailingtwo
    strategy.exit('Long', stop=ta.lowest(low, 2)[0] - belowlow * syminfo.mintick, comment='stop')