양성 바 % 브레이크업 전략

저자:차오장, 날짜: 2024-01-08 10:32:25
태그:

img

전반적인 설명

양성 바스 비율 브레이크아웃 전략 (Positive Bars Percentage Breakout Strategy) 은 가격 행동 판단에 기반한 양적 거래 전략이다. 시장이 현재 상승 추세 상태에 있는지 여부를 결정하기 위해 특정 기간 동안 상승 추세 촛불의 비율을 계산합니다. 상승 추세 촛불의 비율이 사용자가 정의한 상위 한계보다 높을 때 전략은 시장이 현재 상승 추세에 있으며 길게 갈 것이라고 판단합니다. 비율이 사용자가 정의한 하위 한계보다 낮을 때 전략은 시장이 현재 하락 추세에 있으며 짧게 갈 것이라고 판단합니다.

전략 논리

이 전략의 핵심 지표는 상승 추세 촛불의 비율이다. 상승 추세 촛불은 이전 최저보다 낮게 열리고 열기보다 높게 닫혀 그 기간 동안 가격이 상승한 것을 나타냅니다. 전략은 사용자가 정의한 룩백 기간 동안 상승 추세 촛불의 수를 계산하고 모든 촛불 중 상승 추세 촛불의 비율을 계산합니다. 비율이 상위 경계보다 높을 때 전략은 시장이 지속적인 상승 추세에 있으며 길게 갈 것이라고 판단합니다. 비율이 하위 경계보다 낮을 때 전략은 시장이 하락 추세에 있고 짧게 갈 것이라고 판단합니다. 스톱 손실 및 수익 주문은 사용자가 정의한 스톱 손실 방법에 따라 설정됩니다.

예를 들어, 사용자가 룩백 기간을 20로 설정하면, 상위 한도는 70으로, 하위 한도는 30으로 설정하면, 전략은 최신 20개의 촛불을 추적합니다. 그 중 16개가 상승 추세 촛불이라면, 비율은 16/20=80입니다. 80%가 70% 상위 한도보다 높기 때문에, 전략은 긴 주문을 실행합니다. 최신 20개의 촛불 중 5개만 상승 추세 촛불이라면, 비율은 5/20=25%입니다. 이것은 30% 하위 한도보다 낮기 때문에, 전략은 짧은 주문을 실행합니다.

이점 분석

이 전략의 주요 장점은 다음과 같습니다.

  1. 전략 논리는 간단하고 직관적이며 이해하기 쉽습니다.
  2. 그것은 단지 하나의 지표에 의존하여, 과잉 장착의 위험을 줄입니다.
  3. 사용자들은 다른 제품에 대한 매개 변수를 사용자 정의 할 수 있습니다.
  4. 큰 손실을 방지하기 위해 스톱 로스/프로프트 취업 기능이 내장되어 있습니다.
  5. 먼저 포지션을 종료하지 않고 역거래를 허용하고, 트렌드를 더 빠르게 추적합니다.

위험 분석

이 전략의 주요 위험은 다음과 같습니다.

  1. 하나의 지표에만 의존하면 잘못된 신호가 발생할 수 있습니다.
  2. 매개 변수는 과도하게 부착될 수 있고, 실시간 성능은 크게 다를 수 있습니다.
  3. 스톱 로즈는 변동적인 가격 변동으로 인해 손실이 발생할 수 있습니다.
  4. 리버스 트레이드는 손실을 증폭시킬 수 있습니다.
  5. 성능은 상징에 크게 의존하고 별도의 테스트가 필요합니다.

위험은 다음과 같이 감소 할 수 있습니다.

  1. 거짓 신호를 피하기 위해 필터를 추가합니다.
  2. 손실을 제한하기 위해 스톱 로스 논리를 최적화합니다.
  3. 최대 손실 규모를 평가하고 통제하는 것
  4. 각기 다른 기호를 개별적으로 테스트합니다.

최적화 방향

이 전략을 최적화하는 주요 방향은 다음과 같습니다.

  1. 잘못된 신호를 피하기 위해 볼륨과 같은 보조 지표를 추가합니다.
  2. 스톱 로스 방법의 최적화, 예를 들어 트래일 스톱 로스
  3. 볼링거 밴드의 브레이크오웃과 같은 입력 필터를 추가합니다.
  4. 각기 다른 기호에 대한 상승 트렌드 촛불의 최적 매개 변수 테스트
  5. 최대 적립을 평가하고 손실 규모를 제어합니다.

결론

양성 바스 비율 브레이크아웃 전략은 상승 추세/하락 추세의 지속성을 통계적으로 판단하여 추세를 포착하는 간단하고 직설적인 논리를 가지고 있습니다. 이해하기 쉽고 사용자 친화적이며 초보자 양에 적합합니다. 그러나 단일 지표와 매개 변수 최적화에 의존하는 것은 다른 시장에서 안정적인 수익성을 위해 위험 통제에 대한 추가 개선이 필요합니다.


/*backtest
start: 2023-12-31 00:00:00
end: 2024-01-04 00:00:00
period: 1m
basePeriod: 1m
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/
// © ZenAndTheArtOfTrading 
// © TweakerID

// Based on the calculations by ZenAndTheArtOfTrading, I added stop loss, take profit and reverse line codes.
// The Positive Bars % calculates the number of green (positive) bars, relative to a lookback period, defined 
// by the user. If the percentage is low, it means that there was a bigger number of red candles in the 
// lookback period. The strategy goes long when the percentage is high and short when it's low, although
// this logic can be reversed with positive results on different time frames.

//@version=4
strategy("Positive Bars % Strat", 
     overlay=true, 
     default_qty_type=strategy.percent_of_equity, 
     default_qty_value=100, 
     initial_capital=10000, 
     commission_value=0.04, 
     calc_on_every_tick=false, 
     slippage=0)

direction = input(0, title = "Strategy Direction", type=input.integer, minval=-1, maxval=1)
strategy.risk.allow_entry_in(direction == 0 ? strategy.direction.all : (direction < 0 ? strategy.direction.short : strategy.direction.long))

/////////////////////// STRATEGY INPUTS ////////////////////////////////////////
title1=input(true, "-----------------Strategy Inputs-------------------")  

lookback = input(title="Lookback", type=input.integer, defval=13)
upperLimit = input(title="Upper Limit", type=input.integer, defval=70)
lowerLimit = input(title="Lower Limit", type=input.integer, defval=30)

/////////////////////// BACKTESTER /////////////////////////////////////////////
title2=input(true, "-----------------General Inputs-------------------")  

// Backtester General Inputs
i_SL=input(true, title="Use Stop Loss and Take Profit")
i_SLType=input(defval="ATR Stop", title="Type Of Stop", options=["Strategy Stop", "Swing Lo/Hi", "ATR Stop"])
i_SPL=input(defval=10, title="Swing Point Lookback")
i_PercIncrement=input(defval=2, step=.1, title="Swing Point SL Perc Increment")*0.01
i_ATR = input(14, title="ATR Length")
i_ATRMult = input(10, step=.1, title="ATR Multiple")
i_TPRRR = input(1.6, step=.1, title="Take Profit Risk Reward Ratio")

// Bought and Sold Boolean Signal
bought = strategy.position_size > strategy.position_size[1] 
 or strategy.position_size < strategy.position_size[1]

// Price Action Stop and Take Profit
LL=(lowest(i_SPL))*(1-i_PercIncrement)
HH=(highest(i_SPL))*(1+i_PercIncrement)
LL_price = valuewhen(bought, LL, 0)
HH_price = valuewhen(bought, HH, 0)
entry_LL_price = strategy.position_size > 0 ? LL_price : na 
entry_HH_price = strategy.position_size < 0 ? HH_price : na 
tp=strategy.position_avg_price + (strategy.position_avg_price - entry_LL_price)*i_TPRRR
stp=strategy.position_avg_price - (entry_HH_price - strategy.position_avg_price)*i_TPRRR

// ATR Stop
ATR=atr(i_ATR)*i_ATRMult
ATRLong = ohlc4 - ATR
ATRShort = ohlc4 + ATR
ATRLongStop = valuewhen(bought, ATRLong, 0)
ATRShortStop = valuewhen(bought, ATRShort, 0)
LongSL_ATR_price = strategy.position_size > 0 ? ATRLongStop : na 
ShortSL_ATR_price = strategy.position_size < 0 ? ATRShortStop : na 
ATRtp=strategy.position_avg_price + (strategy.position_avg_price - LongSL_ATR_price)*i_TPRRR
ATRstp=strategy.position_avg_price - (ShortSL_ATR_price - strategy.position_avg_price)*i_TPRRR

// Strategy Stop
float LongStop = na
float ShortStop = na
float StratTP = na
float StratSTP = na

/////////////////////// STRATEGY LOGIC /////////////////////////////////////////

//Calculations
positiveBars = 0
for i = (lookback - 1) to 0
    if close[i] > open[i]
        positiveBars := positiveBars + 1
positiveBarsPercent = (positiveBars / lookback) * 100

BUY=positiveBarsPercent >= upperLimit
SELL=positiveBarsPercent <= lowerLimit

//Trading Inputs
DPR=input(true, "Allow Direct Position Reverse")
reverse=input(false, "Reverse Trades")

// Entries
if reverse
    if not DPR
        strategy.entry("long", strategy.long, when=SELL and strategy.position_size == 0)
        strategy.entry("short", strategy.short, when=BUY and strategy.position_size == 0)
    else     
        strategy.entry("long", strategy.long, when=SELL)
        strategy.entry("short", strategy.short, when=BUY)
else
    if not DPR 
        strategy.entry("long", strategy.long, when=BUY and strategy.position_size == 0)
        strategy.entry("short", strategy.short, when=SELL and strategy.position_size == 0)
    else
        strategy.entry("long", strategy.long, when=BUY)
        strategy.entry("short", strategy.short, when=SELL)


SL= i_SLType == "Swing Lo/Hi" ? entry_LL_price : i_SLType == "ATR Stop" ? LongSL_ATR_price : LongStop
SSL= i_SLType == "Swing Lo/Hi" ? entry_HH_price : i_SLType == "ATR Stop" ? ShortSL_ATR_price : ShortStop
TP= i_SLType == "Swing Lo/Hi" ? tp : i_SLType == "ATR Stop" ? ATRtp : StratTP
STP= i_SLType == "Swing Lo/Hi" ? stp : i_SLType == "ATR Stop" ? ATRstp : StratSTP

strategy.exit("TP & SL", "long", limit=TP, stop=SL, when=i_SL)
strategy.exit("TP & SL", "short", limit=STP, stop=SSL, when=i_SL)

/////////////////////// PLOTS //////////////////////////////////////////////////

plot(i_SL and strategy.position_size > 0 ? SL : na , title='SL', style=plot.style_cross, color=color.red)
plot(i_SL and strategy.position_size < 0 ? SSL : na , title='SSL', style=plot.style_cross, color=color.red)
plot(i_SL and strategy.position_size > 0 ? TP : na, title='TP', style=plot.style_cross, color=color.green)
plot(i_SL and strategy.position_size < 0 ? STP : na, title='STP', style=plot.style_cross, color=color.green)
// Draw price action setup arrows
plotshape(BUY ? 1 : na, style=shape.triangleup, location=location.belowbar, 
 color=color.green, title="Bullish Setup", size=size.auto)
plotshape(SELL ? 1 : na, style=shape.triangledown, location=location.abovebar, 
 color=color.red, title="Bearish Setup", size=size.auto)

더 많은