쌍방향 가격 돌파 이동 평균 시점 거래 전략

저자:차오장, 날짜: 2023-12-15 16:28:12
태그:

img

전반적인 설명

이중 방향 가격 돌파 이동 평균 시점 거래 전략은 이동 평균의 가격 돌파를 사용하여 거래 신호를 결정하는 양적 거래 전략입니다. 특정 기간의 이동 평균과 가격을 비교하고 가격이 이동 평균을 통과 할 때 거래 신호를 생성합니다.

전략 논리

이 전략의 핵심 논리는 다음과 같습니다.

  1. EMA 함수를 사용하여 지정된 기간 (200 일) 의 이동 평균 (EMA) 을 계산합니다.

  2. 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스 마이너스

  3. 돌파구에 따라 긴 신호와 짧은 신호를 결정합니다. 가격이 EMA를 통과하면 긴 신호를 생성합니다. 가격이 EMA를 통과하면 짧은 신호를 생성합니다.

  4. 신호가 발사되면 특정 비율 (예: 100%) 로 주문을 하고 스톱 로스 및 영업 가격을 설정합니다.

  5. 스톱 로스 또는 영업 가격에 도달하면 포지션을 닫습니다.

  6. 이 과정을 반복해서 이동평균을 통과하는 가격의 타이밍에서 이익을 얻습니다.

이 전략은 간단하고 이해하기 쉽고 실행하기 쉽습니다. 이동 평균을 깨는 신호에 의해 단기 추진력을 포착하는 것을 목표로합니다. 그러나 또한 특정 지연 및 윙사 리스크가 있습니다.

장점

  • 단순하고 명확한 논리, 이해하기 쉽고 검증하기 쉽습니다.
  • 이동평균의 특성을 이용한 부드러운 추적 능력
  • 높은 거래 빈도, 단기 거래에 적합합니다.
  • 가격 변화에 대한 신속한 반응, 좋은 타이밍을 잡습니다.

위험성

  • 일정 수준의 차질은 초기 가격의 돌파구를 놓칠 수 있습니다.
  • 여러 번 때 자주 거래
  • 급격한 반전으로 멈출 위험이 있습니다.

최적화 방법에는 매개 변수 조정, 더 효과적인 지표 사용, 거래 빈도를 줄이는 등이 포함됩니다. 적응적 인 정지 및 필터링 조건 또한 위험을 제어 할 수 있습니다.

최적화 방향

  • 더 나은 해법을 위해 다른 유형의 이동 평균과 매개 변수를 테스트하십시오. 예를 들어 EMA, SMA, LWMA.
  • 윙사 트레이드를 피하기 위해 필터링 조건을 추가합니다. 예를 들어, 볼링거 밴드, ATR 등
  • 스톱 로스를 최적화하고 테스트하고 리스크를 줄이기 위해 수익 전략을 취하십시오.
  • 트렌드 추종, 평균 역전 및 다른 전략을 결합하여 견고한 거래 시스템을 만듭니다.
  • 더 넓은 적응력을 위해 매개 변수를 추가합니다.

결론

이 전략은 단기 동력을 포착하기 위해 이동 평균을 추적하는 비교적 간단한 논리를 가지고 있습니다. 장점에는 반응성과 사용 편의성; 단점에는 지연 및 관성 등이 있습니다. 더 많은 최적화 표시자 선택, 중지 손실 메커니즘, 필터링 기술을 수행하여 전략을 더욱 견고하고 포괄적으로 만들 수 있습니다.


/*backtest
start: 2022-12-08 00:00:00
end: 2023-12-14 00:00:00
period: 1d
basePeriod: 1h
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/

// Credits to the original Script - Range Filter DonovanWall https://www.tradingview.com/script/lut7sBgG-Range-Filter-DW/
// This version is the old version of the Range Filter with less settings to tinker with

//@version=5
strategy(title='Range Filter - B&S Signals', shorttitle='RF - B&S Signals', initial_capital=1000, currency=currency.GBP, default_qty_value=100, default_qty_type=strategy.percent_of_equity, commission_type=strategy.commission.percent, commission_value=0.075, overlay=true)


i_startTime = input(defval=timestamp('01 Jan 2020 12:00 +0000'), title='Backtest Start')
i_endTime = input(defval=timestamp('01 Jan 2024 12:00 +0000'), title='Backtest End')

inDateRange     = true
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Functions
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
longLossPerc = input.float(title='Long Stop Loss (%)', minval=0.0, step=0.1, defval=1) * 0.01
shortLossPerc = input.float(title='Short Stop Loss (%)', minval=0.0, step=0.1, defval=1) * 0.01

longTakePerc = input.float(title='Long Take(%)', minval=0.0, step=0.1, defval=1) * 0.01
shortTakePerc = input.float(title='Short Take (%)', minval=0.0, step=0.1, defval=1) * 0.01

emaLength = input.int(200, title="EMA Length")

    // Determine stop loss price

//Range Size Function
rng_size(x, qty, n) =>
//    AC       = Cond_EMA(abs(x - x[1]), 1, n)
    wper = n * 2 - 1
    avrng = ta.ema(math.abs(x - x[1]), n)
    AC = ta.ema(avrng, wper) * qty
    rng_size = AC
    rng_size

//Range Filter Function
rng_filt(x, rng_, n) =>
    r = rng_
    var rfilt = array.new_float(2, x)
    array.set(rfilt, 1, array.get(rfilt, 0))
    if x - r > array.get(rfilt, 1)
        array.set(rfilt, 0, x - r)
    if x + r < array.get(rfilt, 1)
        array.set(rfilt, 0, x + r)
    rng_filt1 = array.get(rfilt, 0)

    hi_band = rng_filt1 + r
    lo_band = rng_filt1 - r
    rng_filt = rng_filt1
    [hi_band, lo_band, rng_filt]

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Inputs
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------

//Range Source
rng_src = input(defval=close, title='Swing Source')

//Range Period
rng_per = input.int(defval=20, minval=1, title='Swing Period')

//Range Size Inputs
rng_qty = input.float(defval=3.5, minval=0.0000001, title='Swing Multiplier')

//Bar Colors
use_barcolor = input(defval=false, title='Bar Colors On/Off')

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Definitions
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------

//Range Filter Values
[h_band, l_band, filt] = rng_filt(rng_src, rng_size(rng_src, rng_qty, rng_per), rng_per)

//Direction Conditions
var fdir = 0.0
fdir := filt > filt[1] ? 1 : filt < filt[1] ? -1 : fdir
upward = fdir == 1 ? 1 : 0
downward = fdir == -1 ? 1 : 0

//Trading Condition
longCond = rng_src > filt and rng_src > rng_src[1] and upward > 0 or rng_src > filt and rng_src < rng_src[1] and upward > 0
shortCond = rng_src < filt and rng_src < rng_src[1] and downward > 0 or rng_src < filt and rng_src > rng_src[1] and downward > 0

CondIni = 0
CondIni := longCond ? 1 : shortCond ? -1 : CondIni[1]
longCondition = longCond and CondIni[1] == -1
shortCondition = shortCond and CondIni[1] == 1

//Colors
filt_color = upward ? #05ff9b : downward ? #ff0583 : #cccccc
bar_color = upward and rng_src > filt ? rng_src > rng_src[1] ? #05ff9b : #00b36b : downward and rng_src < filt ? rng_src < rng_src[1] ? #ff0583 : #b8005d : #cccccc


ema = ta.ema(close,emaLength)

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Outputs
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
longStopPrice = strategy.position_avg_price * (1 - longLossPerc)
shortStopPrice = strategy.position_avg_price * (1 + shortLossPerc)

longTakePrice = strategy.position_avg_price * (1 + longTakePerc)
shortTakePrice = strategy.position_avg_price * (1 - shortTakePerc)

//Filter Plot
filt_plot = plot(filt, color=filt_color, linewidth=3, title='Filter', transp=67)

//Band Plots
h_band_plot = plot(h_band, color=color.new(#05ff9b, 100), title='High Band')
l_band_plot = plot(l_band, color=color.new(#ff0583, 100), title='Low Band')

//Band Fills
fill(h_band_plot, filt_plot, color=color.new(#00b36b, 92), title='High Band Fill')
fill(l_band_plot, filt_plot, color=color.new(#b8005d, 92), title='Low Band Fill')

//Bar Color
barcolor(use_barcolor ? bar_color : na)

if  inDateRange and close>ema
    strategy.entry("Long", strategy.long, when=longCondition)
    
if   inDateRange and close<ema
    strategy.entry("Short", strategy.short, when=shortCondition)


plot(ema)




//Plot Buy and Sell Labels
plotshape(longCondition, title='Buy Signal', text='BUY', textcolor=color.white, style=shape.labelup, size=size.normal, location=location.belowbar, color=color.new(color.green, 0))
plotshape(shortCondition, title='Sell Signal', text='SELL', textcolor=color.white, style=shape.labeldown, size=size.normal, location=location.abovebar, color=color.new(color.red, 0))

//Alerts
alertcondition(longCondition, title='Buy Alert', message='BUY')
alertcondition(shortCondition, title='Sell Alert', message='SELL')

if strategy.position_size > 0
    strategy.exit(id='Long', stop=longStopPrice, limit=longTakePrice)

if strategy.position_size < 0
    strategy.exit(id='Short', stop=shortStopPrice, limit=shortTakePrice)




더 많은