이 전략은 가격의 변동 범위를 기반으로 구매와 판매의 시간을 판단한다. 그것은 일정 주기 동안의 가격 변동 범위를 계산하고, 그 범위를 필터 조건으로 거래 신호를 생성한다. 가격이 변동 범위를 초과 할 때 구매 또는 판매 신호를 생성한다. 가격 돌파구를 기반으로 한 단선 거래 전략에 속한다.
이 전략의 핵심 지표는 가격의 변동 범위이다. 구체적인 계산 과정은 다음과 같다:
지난 N주기의 최고 가격과 최저 가격의 차이를 가격 상승으로 계산한다.
가격 상승을 평평하게 처리하여 범주 필터를 얻습니다.
가격 상승이 범위 필터를 초과하면 구매 신호가 발생
가격 하락이 범위를 넘어서면 판매 신호가 발생
이런 식으로, 가격의 변동 범위를 깨는 것은 트렌드 방향을 판단하고, 거래의 소음을 필터링하여 보다 명확한 거래 신호를 얻을 수 있습니다.
위험은 다음과 같은 방법으로 줄일 수 있습니다.
이 전략은 다음과 같은 부분에서 최적화될 수 있습니다.
테스트 계산 범위의 다른 주기 변수
최적화 범위 필터 변동수
MACD와 같은 지표에 추가하여 2차 확인
이동 중지 또는 추적 중지 사용
특정 품종에 따라 다른 조정 매개 변수
포지션 관리 시스템을 최적화하는 것을 고려하십시오.
이 전략은 가격의 돌파구 범위를 사용하여 단선 거래 신호를 생성한다. 이것은 단기 트렌드 기회를 효과적으로 식별할 수 있다. 그러나 오파크 반전의 위험도 쉽게 발생한다. 우리는 매개 변수 최적화, 중지 손실 규칙을 설정, 지표 필터링을 추가하는 방법과 같은 방법을 통해 전략 시스템을 개선할 수 있으며, 돌파구 식별 능력을 유지하면서 위험을 제어하고, 반전을 줄일 수 있다. 또한, 다양한 품종의 특성에 대한 매개 변수 조정이 중요합니다.
/*backtest
start: 2023-08-21 00:00:00
end: 2023-09-20 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
strategy(title="Range Filter Buy and Sell 5min [Strategy]", overlay=true, commission_type=strategy.commission.percent, commission_value=0.025, default_qty_type=strategy.cash, default_qty_value=10000, initial_capital=10000, slippage=0)
// === INPUT BACKTEST RANGE ===
useDate = input(true, title='---------------- Use Date ----------------', type=bool)
FromMonth = input(defval = 7, title = "From Month", minval = 1, maxval = 12)
FromDay = input(defval = 25, title = "From Day", minval = 1, maxval = 31)
FromYear = input(defval = 2019, title = "From Year", minval = 2017)
ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear = input(defval = 9999, title = "To Year", minval = 2017)
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() => true // create function "within window of time"
// === INPUT BACKTEST RANGE ===
sources = input(defval=close, title="Source")
isHA = input(false, "Use HA Candles", bool)
src = isHA ? security(heikenashi(tickerid), period, sources) : sources
// Sampling Period
// Settings for 5min chart, BTCUSDC. For Other coin, change the paremeters
per = input(defval=50, minval=1, title="Sampling Period")
// Range Multiplier
mult = input(defval=3.0, minval=0.1, title="Range Multiplier")
// Smooth Average Range
smoothrng(x, t, m)=>
wper = (t*2) - 1
avrng = ema(abs(x - x[1]), t)
smoothrng = ema(avrng, wper)*m
smoothrng
smrng = smoothrng(src, per, mult)
// Range Filter
rngfilt(x, r)=>
rngfilt = x
rngfilt := x > nz(rngfilt[1]) ? ((x - r) < nz(rngfilt[1]) ? nz(rngfilt[1]) : (x - r)) : ((x + r) > nz(rngfilt[1]) ? nz(rngfilt[1]) : (x + r))
rngfilt
filt = rngfilt(src, smrng)
// Filter Direction
upward = 0.0
upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
// Target Bands
hband = filt + smrng
lband = filt - smrng
// Colors
filtcolor = upward > 0 ? lime : downward > 0 ? red : orange
barcolor = (src > filt) and (src > src[1]) and (upward > 0) ? lime : (src > filt) and (src < src[1]) and (upward > 0) ? green :
(src < filt) and (src < src[1]) and (downward > 0) ? red : (src < filt) and (src > src[1]) and (downward > 0) ? maroon : orange
filtplot = plot(filt, color=filtcolor, linewidth=3, title="Range Filter")
// Target
hbandplot = plot(hband, color=aqua, transp=100, title="High Target")
lbandplot = plot(lband, color=fuchsia, transp=100, title="Low Target")
// Fills
fill(hbandplot, filtplot, color=aqua, title="High Target Range")
fill(lbandplot, filtplot, color=fuchsia, title="Low Target Range")
// Bar Color
//barcolor(barcolor)
// Break Outs
longCond = na
shortCond = na
longCond := ((src > filt) and (src > src[1]) and (upward > 0)) or ((src > filt) and (src < src[1]) and (upward > 0))
shortCond := ((src < filt) and (src < src[1]) and (downward > 0)) or ((src < filt) and (src > 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
//Alerts
plotshape(longCondition, title = "Buy Signal", text ="BUY", textcolor = white, style=shape.labelup, size = size.normal, location=location.belowbar, color = green, transp = 0)
plotshape(shortCondition, title = "Sell Signal", text ="SELL", textcolor = white, style=shape.labeldown, size = size.normal, location=location.abovebar, color = red, transp = 0)
//strategy.entry("Long", strategy.long, stop = hband, when = window() , comment="Long")
//strategy.entry("Short", strategy.short, stop = lband, when = window() , comment="Short")
strategy.entry("Long", strategy.long, when = longCondition and window() , comment="Long")
strategy.entry("Short", strategy.short, when = shortCondition and window() , comment="Short")
// === Stop LOSS ===
useStopLoss = input(false, title='----- Use Stop Loss / Take profit -----', type=bool)
sl_inp = input(100, title='Stop Loss %', type=float, step=0.25)/100
tp_inp = input(1.5, title='Take Profit %', type=float, step=0.25)/100
stop_level = strategy.position_avg_price * (1 - sl_inp)
take_level = strategy.position_avg_price * (1 + tp_inp)
stop_level_short = strategy.position_avg_price * (1 + sl_inp)
take_level_short = strategy.position_avg_price * (1 - tp_inp)
// === Stop LOSS ===
if useStopLoss
strategy.exit("Stop Loss/Profit Long","Long", stop=stop_level, limit=take_level)
strategy.exit("Stop Loss/Profit Short","Short", stop=stop_level_short, limit=take_level_short)