이중 이동 평균 크로스오버 지속 상승 추세 전략

저자:차오장, 날짜: 2023-11-13 10:47:48
태그:

img

전반적인 설명

이 거래 전략은 상거래에 진입하기 위해 지속적인 상승 추세와 함께 움직이는 평균 크로스오버 패턴을 활용하는 거래 전략입니다. 빠른 MA가 느린 MA보다 넘을 때 황금 크로스오버 신호가 생성됩니다. 크로스오버 이후 상승 추세가 지속되면 긴 포지션을 열 수 있습니다. 가격이 스톱 로스로 상승하거나 수익을 취할 때 포지션은 스톱 로스로 닫을 수 있습니다.

전략 논리

이 전략은 주로 입력 신호의 이동 평균 크로스오버에 기반합니다. 구체적으로, 빠른 MA (MA1) 와 느린 MA (MA2) 가 정의됩니다. MA1가 MA2를 넘을 때, 그것은 긴 신호입니다.

단기 크로스오버로부터의 잘못된 신호를 피하기 위해 각의 임계값이 추가되므로 MA2 각이 설정된 임계값을 초과할 때만 구매 신호가 발생한다. 이것은 트렌드가 아닌 일부 단기 릴리를 필터링한다.

이 전략은 또한 스톱 로스를 설정하고 이윤을 취합니다. 스톱 로스는 갑작스러운 시장 전환의 경우 손실을 피하며, 이윤에 이윤을 취합니다. 그들은 엔트리 가격의 비율 범위로 설정됩니다.

이윤을 취하기 위해 가격이 상승할 때, 전략은 이윤을 취하기 위해 긴 문을 닫을 것입니다. 또한, 이승만이 강하다면, 전략은 평균 반전을위한 짧은 위치를 열 것입니다.

이점 분석

이것은 간단하고 직관적인 트렌드 다음 전략입니다. 이점은 다음과 같습니다.

  1. MA 조합은 시장 소음을 필터링하고 트렌드 방향으로 잠겨 있습니다.
  2. 앵글 문턱은 단기 변동에 의해 오인되는 것을 피합니다
  3. 쌍방향 거래는 범위 제한 시장에서 이익을 얻을 수 있습니다.
  4. 스톱 손실 및 수익 통제 위험

위험 분석

주의해야 할 몇 가지 위험 요소가 있습니다.

  1. MAs는 지연이 있고 전환점을 놓칠 수 있습니다.
  2. 스톱 손실은 보장되지 않습니다. 빠른 시장에서 격차 될 수 있습니다.
  3. 이중 거래는 위험을 두 배로 증가시킵니다. 짧은 진입의 타이밍이 중요합니다.
  4. MA 기간과 같은 매개 변수 조정이 좋지 않으면 결과에 영향을 줄 수 있습니다.

개선 할 수 있는 분야

전략을 더 최적화 할 수있는 몇 가지 방법:

  1. 정확성을 향상시키기 위해 MACD, 볼링거와 같은 트렌드 필터를 추가합니다.
  2. 기계 학습을 사용하여 MA 기간을 동적으로 최적화하십시오.
  3. 스톱 로스 및 수익 설정, 예를 들어 트레일링 스톱을 최적화합니다.
  4. 손실을 제한하기 위해 포지션 크기를 추가합니다.
  5. ADX와 크기의 평균 반전 지점으로 추세 강도를 측정합니다.

결론

전체적으로 이것은 전략에 따른 간단하고 실용적인 추세입니다. 이 방법에는 장점이 있지만 위험도 있습니다. 매개 변수 조정, 최적 지표, 스톱 로스 설정 등과 같은 추가적인 정교화가 개선 될 수 있습니다. 그러나 어떤 전략도 시스템 위험을 완전히 제거하지 않습니다. 위험 관리는 신중한 거래의 핵심입니다.


/*backtest
start: 2023-11-05 00:00:00
end: 2023-11-12 00:00:00
period: 15m
basePeriod: 5m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//written by markjames12210@gmail.com
//@version=5
strategy(title="MJ-Dual Moving Average",initial_capital=10000,overlay=false)

// import TradingView/ZigZag/6 as ZigZagLib 

// // Create Zig Zag instance from user settings.
// var zigZag = ZigZagLib.newInstance(
//   ZigZagLib.Settings.new(
//       input.float(5.0, "Price deviation for reversals (%)", 0.00001, 100.0, 0.5, "0.00001 - 100"),
//       input.int(10, "Pivot legs", 2),
//       input(#2962FF, "Line color"),
//       input(true, "Extend to last bar"),
//       input(true, "Display reversal price"),
//       input(true, "Display cumulative volume"),
//       input(true, "Display reversal price change", inline = "priceRev"),
//       input.string("Absolute", "", ["Absolute", "Percent"], inline = "priceRev"),
//       true)
//  )

// // Update 'zigZag' object on each bar with new ​pivots, ​volume, lines, labels.
// zigZag.update()
// // plot(zigZag.pivots, "zigZag")

ma1= ta.sma(close,8)
ma2= ta.sma(close,21)

angleCriteria = input.int(title="Angle", defval=7, minval=1, maxval=13)

i_lookback   = input.int(2,     "Angle Period", minval = 1)
i_atrPeriod  = input.int(10,    "ATR Period",   minval = 1)
i_angleLevel = input.int(6,     "Angle Level",  minval = 1)
i_maSource   = input.source(close, "MA Source")
TP = input.float(1, "TP", minval = 0.1)
SL = input.float(1, "SL", minval = 0.1)

f_angle(_src, _lookback, _atrPeriod) =>
    rad2degree = 180 / 3.141592653589793238462643  //pi 
    ang = rad2degree * math.atan((_src[0] - _src[_lookback]) / ta.atr(_atrPeriod)/_lookback)
    ang
_angle = f_angle(ma2, i_lookback, i_atrPeriod)
plot(ta.atr(i_atrPeriod), "atr")
// plot(ma1,color=#FF0000)
// plot(ma2,color=#00FF00)

crosso=ta.crossover(ma1,ma2) 
crossu=ta.crossunder(ma1,ma2)

_lookback = 15

f_somethingHappened(_cond, _lookback) =>
    bool _crossed = false
    for i = 1 to _lookback
        if _cond[i]
            _crossed := true
    _crossed
    
longcrossed = f_somethingHappened(crosso,_lookback)
shortcrossed = f_somethingHappened(crossu,_lookback)

atr_factor = 1
atr = ta.atr(i_atrPeriod)
e = atr * atr_factor 

afr = close 
afr := nz(afr[1], afr)

atr_factoryHigh = close + e
atr_factoryLow = close - e 

if atr_factoryLow > afr 
    afr := atr_factoryLow
if atr_factoryHigh < afr 
    afr := atr_factoryHigh

// plot(afr, "afr", display = display.data_window)
// plot(atr_factoryHigh, "afr", color = color.yellow, display = display.all)
// plot(atr_factoryLow, "afr", color = color.green, display = display.all)


inLong() => strategy.position_size > 0
inShort() => strategy.position_size < 0
inZero() => not inLong() and not inShort()

long = longcrossed and _angle > angleCriteria
short= shortcrossed and _angle < -(angleCriteria)

plotshape(long, "Buy", shape.arrowup, location.belowbar, color = #FF0000)
plotshape(short, "Sell", shape.arrowdown, location.abovebar, color = #00FF00)

var longTp = 0.0
var longSl = 0.0
var shortTp = 0.0
var shortSl = 0.0
[b_middle, b_high, b_low] = ta.bb(close, 20, 2)
entry_price = strategy.opentrades.entry_price(0)

if inZero()
    if short
        longTp := close * (1 + TP/100)
        longSl := close * (1 - SL/100)
        strategy.entry("LONG",strategy.long, comment = "tp:" + str.tostring(longTp) + " sl:" + str.tostring(longSl))
    if long
        shortTp := close * (1 - TP/100)
        shortSl := close * (1 + SL/100)
        strategy.entry("SHORT",strategy.short, comment = "tp:" + str.tostring(shortTp) + " sl:" + str.tostring(shortSl))

if inLong()
    // if close - entry_price > close * 0.005
    //     longSl := entry_price + close * 0.001
    if high > longTp
        strategy.close("LONG")
        if (close - open) > close * 0.014
            shortTp := close * (1 - TP/100)
            shortSl := close * (1 + SL/100)
            strategy.entry("SHORT",strategy.short, comment = "tp:" + str.tostring(shortTp) + " sl:" + str.tostring(shortSl))

    if close < longSl
        strategy.close("LONG")
    if open >= b_high and close >= b_high
        strategy.close("LONG")
    // if high > b_high and entry_price < high
    //     strategy.close("LONG")


if inShort()
    // if entry_price - close > close * 0.005
    //     shortSl := entry_price - close * 0.001
    if low < shortTp
        strategy.close("SHORT")
        if (open - close) > close * 0.014
            longTp := close * (1 + TP/100)
            longSl := close * (1 - SL/100)
            strategy.entry("LONG",strategy.long, comment = "tp:" + str.tostring(longTp) + " sl:" + str.tostring(longSl))


    if close > shortSl
        strategy.close("SHORT")
    if open < b_low and close < b_low
        strategy.close("SHORT")
    // if low < b_low and entry_price > low
    //     strategy.close("SHORT")

더 많은