이중 내부 바 & 트렌드 전략

저자:차오장, 날짜: 2024-01-30 15:11:48
태그:

img

이중 내부 바 & 트렌드 전략

전반적인 설명

이중 내부 바와 트렌드 전략은 트렌드를 결정하기 위해 이동 평균과 결합한 이중 내부 바 패턴을 활용하는 양적 거래 전략이다. 이중 내부 바와 함께 높은 확률의 거래 신호를 제공하며 이동 평균 라인에 의해 판단되는 트렌드에 따라 길거나 짧게 간다.

전략 논리

  1. 추세 판단을 위한 지표로 Hull Moving Average (HMA) 를 사용한다.
  2. 이중 내부 바 패턴이 발생하면 높은 확률의 거래 신호로 간주됩니다. 내부 바는 마지막 바의 높고 낮은 부분이 이전 바에 포함되는 패턴입니다.
  3. 클로즈 가격이 MA보다 높고 올리는 바가 형성되면, 인더 바의 높이에 구매 중지 명령을 배치하십시오. 클로즈가 MA보다 낮고 하락하는 바가 형성되면, 인더 바의 낮위에 판매 중지 명령을 배치하십시오.
  4. 스톱 오더가 트리거되면 미리 정의된 스톱 로스 비율과 스톱 로스 비율을 기준으로 스톱 로스를 설정하고 이윤을 취합니다.

이점 분석

  1. 인더 바는 높은 확률의 반전 신호를 제공합니다. 이중 인더 바의 발생은 단기 가격 반전을 나타낼 수 있습니다.
  2. 주요 트렌드 방향을 따르기 위해 이동 평균과 함께 사용하면 이윤의 확률을 향상시킵니다.
  3. 트렌드의 돌파점을 중심으로 스톱 오더를 사용하는 것은 좋은 진입 기회를 가지고 있습니다.

위험 분석

  1. 범위에 있는 시장에서, 바 내부에서 나오는 거래 신호는 종종 손실로 이어질 수 있습니다.
  2. 트렌드 지표로서의 이동 평균은 또한 잘못된 신호를 줄 수 있으며, 이는 역 트렌드 거래에서 손실을 초래할 수 있습니다.
  3. 만약 스톱 로스가 너무 좁게 설정되면 작은 가격 하락으로 인해 발생될 수 있습니다.

최적화 방향

  1. 트렌드 판단 지표로 이동 평균의 다른 매개 변수를 테스트합니다.
  2. 다른 지표를 결합하여 시장 범위를 필터링하고 명확한 추세가 없는 맹목적 거래를 피합니다.
  3. 이동 평균 기간, 스톱 로스 멀티플리커, 이윤 비율 등과 같은 빅 데이터 분석을 통해 더 최적의 매개 변수 조합을 얻으십시오.
  4. 다른 시간 프레임과 제품 특성에 적응하기 위해 거래 세션과 제품에 필터를 추가합니다.

요약

이중 내부 바 및 트렌드 전략은 이중 내부 바에서 나오는 높은 확률 거래 신호를 활용하여 이동 평균에 의해 장기 또는 단행 주요 트렌드 방향을 결정하여 비교적 안정적인 브레이크아웃 전략으로 만들어집니다. 매개 변수 최적화 및 논리 최적화를 통해이 전략의 적응성과 수익성이 향상 될 수 있습니다.


/*backtest
start: 2023-12-01 00:00:00
end: 2023-12-31 23:59:59
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/
// © Kaspricci

//@version=5
strategy(
     title = "Double Inside Bar & Trend Strategy - Kaspricci", 
     shorttitle = "Double Inside Bar & Trend", 
     overlay=true, 
     initial_capital = 100000, 
     currency = currency.USD, 
     default_qty_type = strategy.percent_of_equity, 
     default_qty_value = 100, 
     calc_on_every_tick = true, 
     close_entries_rule = "ANY")

// ================================================ Entry Inputs ======================================================================
headlineEntry   = "Entry Seettings"

maSource        = input.source(defval = close,             group = headlineEntry, title = "MA Source")
maType          = input.string(defval = "HMA",             group = headlineEntry, title = "MA Type", options = ["EMA", "HMA", "SMA", "SWMA", "VWMA", "WMA"])
maLength        = input.int(   defval = 45,    minval = 1, group = headlineEntry, title = "HMA Length")

float ma = switch maType 
    "EMA"  => ta.ema(maSource,  maLength)
    "HMA"  => ta.hma(maSource,  maLength)
    "SMA"  => ta.sma(maSource,  maLength)
    "SWMA" => ta.swma(maSource)
    "VWMA" => ta.vwma(maSource, maLength)
    "WMA"  => ta.wma(maSource,  maLength)

plot(ma, "Trend MA", color.purple)

// ================================================ Trade Inputs ======================================================================
headlineTrade   = "Trade Seettings"

stopLossType    = input.string(defval = "ATR",                         group = headlineTrade,                 title = "Stop Loss Type",            options = ["ATR", "FIX"])
atrLength       = input.int(   defval = 50,   minval = 1,              group = headlineTrade, inline = "ATR", title = "   ATR: Length                 ")
atrFactor       = input.float( defval =  2.5, minval = 0, step = 0.05, group = headlineTrade, inline = "ATR", title = "Factor       ",             tooltip = "multiplier for ATR value")
takeProfitRatio = input.float( defval =  2.0, minval = 0, step = 0.05, group = headlineTrade,                 title = "            TP Ration",     tooltip = "Multiplier for Take Profit calculation")
fixStopLoss     = input.float( defval = 10.0, minval = 0, step = 0.5,  group = headlineTrade, inline = "FIX", title = "   FIX: Stop Loss             ") * 10 // need this in ticks
fixTakeProfit   = input.float( defval = 20.0, minval = 0, step = 0.5,  group = headlineTrade, inline = "FIX", title = "Take Profit",               tooltip = "in pips") * 10 // need this in ticks
useRiskMagmt    = input.bool(  defval = true,                          group = headlineTrade, inline = "RM",  title = "")
riskPercent     = input.float( defval = 1.0,  minval = 0., step = 0.5, group = headlineTrade, inline = "RM",  title = "Risk in %                ", tooltip = "This will overwrite quantity from startegy settings and calculate the trade size based on stop loss and risk percent") / 100

// ================================================ Filter Inputs =====================================================================
headlineFilter  = "Filter Setings"

// date filter
filterDates     = input.bool(defval = false,                                 group = headlineFilter, title = "Filter trades by dates")
startDateTime   = input(defval = timestamp("2022-01-01T00:00:00+0000"), group = headlineFilter, title = "       Start Date & Time")
endDateTime     = input(defval = timestamp("2099-12-31T23:59:00+0000"), group = headlineFilter, title = "       End Date & Time  ")

dateFilter      = not filterDates or (time >= startDateTime and time <= endDateTime)

// session filter
filterSession   = input.bool(title = "Filter trades by session", defval = false, group = headlineFilter)
session         = input(title = "       Session", defval = "0045-2245", group = headlineFilter)

sessionFilter   = not filterSession or time(timeframe.period, session, timezone = "CET")

// ================================================ Trade Entries and Exits =====================================================================

// calculate stop loss
stopLoss        = switch stopLossType
    "ATR" => nz(math.round(ta.atr(atrLength) * atrFactor / syminfo.mintick, 0), 0)
    "FIX" => fixStopLoss

// calculate take profit
takeProfit      = switch stopLossType
    "ATR" => math.round(stopLoss * takeProfitRatio, 0)
    "FIX" => fixTakeProfit


doubleInsideBar = high[2] > high[1] and high[2] > high[0] and low[2] < low[1] and low[2] < low[0]

// highlight mother candel and inside bar candles
bgcolor(doubleInsideBar ? color.rgb(33, 149, 243, 80) : na)
bgcolor(doubleInsideBar ? color.rgb(33, 149, 243, 80) : na, offset = -1)
bgcolor(doubleInsideBar ? color.rgb(33, 149, 243, 80) : na, offset = -2)

var float buyStopPrice  = na
var float sellStopPrice = na

if (strategy.opentrades == 0 and doubleInsideBar and barstate.isconfirmed)
    buyStopPrice  := high[0] // high of recent candle (second inside bar)
    sellStopPrice := low[0] // low of recent candle (second inside bar)

    tradeID = str.tostring(strategy.closedtrades + strategy.opentrades + 1)

    quantity = useRiskMagmt ? math.round(strategy.equity * riskPercent / stopLoss, 2) / syminfo.mintick : na

    commentTemplate = "{0} QTY: {1,number,#.##} SL: {2} TP: {3}"

    if (close > ma)
        longComment = str.format(commentTemplate, tradeID + "L", quantity, stopLoss / 10, takeProfit / 10)
        strategy.entry(tradeID + "L", strategy.long, qty = quantity, stop = buyStopPrice, comment = longComment)
        strategy.exit(tradeID + "SL", tradeID + "L", profit = takeProfit, loss = stopLoss, comment_loss = "SL", comment_profit = "TP")

    if (close < ma)
        shortComment = str.format(commentTemplate, tradeID + "S", quantity, stopLoss / 10, takeProfit / 10)
        strategy.entry(tradeID + "S", strategy.short, qty = quantity, stop = sellStopPrice, comment = shortComment)
        strategy.exit(tradeID + "SL", tradeID + "S", profit = takeProfit, loss = stopLoss, comment_loss = "SL", comment_profit = "TP")

// as soon as the first pending order has been entered the remaing pending order shall be cancelled 
if strategy.opentrades > 0
    currentTradeID = str.tostring(strategy.closedtrades + strategy.opentrades)
    strategy.cancel(currentTradeID + "S")
    strategy.cancel(currentTradeID + "L")


더 많은