가격이 이동평균선을 돌파할 때의 거래 전략


생성 날짜: 2023-12-15 16:28:12 마지막으로 수정됨: 2023-12-15 16:28:12
복사: 0 클릭수: 575
avatar of ChaoZhang ChaoZhang
1
집중하다
1621
수행원

가격이 이동평균선을 돌파할 때의 거래 전략

개요

쌍방향 가격 돌파 평균 이동 타이밍 트레이딩 전략 (Dual Direction Price Breakthrough Moving Average Timing Trading Strategy) 은 가격 돌파 평균을 사용하여 매매 시기를 판단하는 양적 거래 전략이다. 이 전략은 가격을 지정된 주기 이동 평균과 비교하여 가격 상위 또는 하위 평균을 통과하는 것에 따라 거래를 생성합니다. 신호

전략 원칙

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

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

  2. 클로즈 가격과 EMA의 크기와 관계를 비교하여 가격이 EMA를 뚫었는지 여부를 판단한다. 구체적으로, 하루 클로즈 가격이 EMA보다 큰 경우, 가격이 EMA를 밟았다고 간주하고, 하루 클로즈 가격이 EMA보다 작은 경우, 가격이 EMA를 밟았다고 간주한다.

  3. 상위와 하위 파동에 따라 매매 시기를 판단한다. 가격이 상위 EMA를 파동할 때, 매매 신호가 발생한다. 가격이 하위 EMA를 파동할 때, 매매 신호가 발생한다.

  4. 신호가 생성될 때, 일정 비율로 (예: 전체 매장) 주문을 하고, 그 다음에는 스톱로스 및 스톱 스톱 가격을 설정한다.

  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)