브레이크오웃 볼링거 밴드 거래 전략

저자:차오장, 날짜: 2023-10-07 09:59:11
태그:

전반적인 설명

이 전략은 볼링거 밴드의 브레이크아웃을 거래하며 가격이 상위 또는 하위 밴드를 완전히 뚫었을 때 역 트렌드 포지션을 취합니다. 비정상적인 변동성 이후 평균 반전을 포착하는 것을 목표로합니다. 빠른 이익을 추구하는 적극적인 거래자에게 적합합니다.

원칙

이 전략은 현재 변동성 범위를 정의하기 위해 볼링거 밴드를 사용합니다. 가격이 상위 밴드 이상 또는 하위 밴드 아래에 완전한 촛불을 형성하면 매우 변동적인 상태와 평균으로 역전될 가능성을 나타냅니다.

특히, 중간, 상위 및 하위 대역은 20 기간 폐쇄 가격을 사용하여 계산됩니다. 가격이 더 높게 열린 후 하위 대역 아래에 닫을 때 긴 신호가 생성됩니다. 가격이 더 낮게 열린 후 상위 대역 위에 닫을 때 짧은 신호가 유발됩니다. 브레이크아웃 포인트는 스톱 로스로 작용하며 중간 대역은 초기 이익 목표입니다.

장점

주요 장점은 다음과 같습니다.

  1. 볼링거 밴드는 시장의 변동성을 효과적으로 측정하여 부조리를 식별합니다.

  2. 브레이크포인트 (breakout point) 는 위험을 통제하기 위한 명확한 스톱 로스 레벨입니다.

  3. 중간에 있는 띠는 평균 회귀를 위한 합리적인 표적을 제공합니다.

  4. 촛불은 신호의 신뢰성을 높일 수 있습니다.

  5. 간단한 매개 변수는 구현과 최적화를 쉽게 합니다.

  6. 순수한 논리가 코드에서 간결하게 표현되었습니다.

위험성

몇 가지 위험 요소는 다음과 같습니다.

  1. 나쁜 BB 매개 변수는 전략을 무효화 할 수 있습니다.

  2. 파업은 트렌드 시작을 알릴 수 있고, 조기퇴출의 위험이 있습니다.

  3. 중위층의 목표가 너무 보수적이어서 수익을 제한할 수도 있습니다.

  4. 넓은 파편은 완전히 채워지지 않아 미끄러질 수 있습니다.

  5. 윙사 (Whipsaws) 는 다양한 시장에서 과도한 무의미한 거래를 유도할 수 있습니다.

개선

몇 가지 개선 사항:

  1. 트렌드 강도를 측정하여 설정 또는 주파수를 조정합니다.

  2. 다른 지표들을 추가해서 입력 시기를 정밀하게 조정하세요.

  3. 변동성에 따라 스톱 로드를 조정합니다.

  4. 초기 목표물을 최적화하여 순조로운 수익을 얻습니다.

  5. 복합 수익에 대한 재입구 메커니즘을 구현합니다.

  6. 나쁜 거래를 피하기 위해 브레이크의 유효성을 평가합니다.

결론

이 전략은 적극적인 거래자에게 적합한 단기 수익을 위해 BB 브레이크오트를 거래합니다. 장점은 명확한 위험 통제이며 단점은 초기 출구 및 이익 제한입니다. 세밀한 조정 매개 변수, 필터 등을 추가하면 성능을 향상시킬 수 있습니다.


/*backtest
start: 2023-09-06 00:00:00
end: 2023-10-06 00:00:00
period: 3h
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/
// © Bishnu103

//@version=4
strategy(title="Full Candle Outside BB [v1.0][Bishnu103]",shorttitle="OUTSIDE BB",overlay=true,calc_on_every_tick=true,backtest_fill_limits_assumption=2)

// ***********************************************************************************************************************
// input variables
buy_session         = input(title="Buy Session", type=input.session, defval="0915-1430")
exit_inraday        = input(title="Exit Intraday?", type=input.bool, defval=true)
entry_distance      = input(title="Entry distance from alert", minval=1, maxval=10, defval=3)
show_bb_switch      = input(title="Show BB", type=input.bool, defval=true)
//
bbLength            = input(title="BB Length", minval=1, defval=20)
bbStdDev            = input(title="BB StdDev", minval=1, defval=2)

// ***********************************************************************************************************************
// global variables
long_entry          = false
short_entry         = false
long_exit           = false
short_exit          = false

// variable values available across candles
var entry_price     = 0.0  
var sl_price        = 0.0
var exit_price      = 0.0
var candle_count    = 0

// ***********************************************************************************************************************
// function to return bollinger band values based on candle poition passed 
getBB(pos) => [mBB, uBB, lBB] = bb(close[pos], bbLength, bbStdDev)

// function returns true if current time is within intraday byuing session set in input
BarInSession(sess) => time(timeframe.period, sess) != 0

// ***********************************************************************************************************************
// strategy
//
// get current bb value
[mBB_0,uBB_0,lBB_0] = getBB(0)

// check if full candle outside upper BB
outside_uBB = low > uBB_0 and close <= open
outside_lBB = high < lBB_0 and close >= open

// ***********************************************************************************************************************
// entry conditions 
long_entry   := outside_lBB
short_entry  := outside_uBB

// keep candle count since the alert generated so that order can be cancelled after N number of candle calling it out as invalid alert
candle_count := candle_count + 1
if long_entry or short_entry
    candle_count    := 0

// ***********************************************************************************************************************
// risk management
//
// decide entry and sl price
if long_entry
    entry_price := high
    
if short_entry
    entry_price := low
    
if long_entry
    sl_price    := low
    
if short_entry
    sl_price    := high

// first exit is when price hits middle BB, gets updated for each candle based on it's middle BB value
exit_price  := mBB_0

// ***********************************************************************************************************************
// position sizing
price = if close[0] > 25000
    25000
else
    price = close[0]

qty = 25000/price

// ***********************************************************************************************************************
// entry
//if long_entry and strategy.position_size == 0
//    strategy.entry("BUY", strategy.long, qty, stop=entry_price, comment="BUY @ "+ tostring(entry_price)) 
if long_entry and strategy.position_size == 0
    strategy.order("BUY", strategy.long, qty, stop=entry_price, comment="BUY @ "+ tostring(entry_price))

//if short_entry and strategy.position_size == 0
//    strategy.entry("SELL", strategy.short, qty, stop=entry_price, comment="SELL @ "+ tostring(entry_price))
if short_entry and strategy.position_size == 0
    strategy.order("SELL", strategy.short, qty, stop=entry_price, comment="SELL @ "+ tostring(entry_price))

// cancel an order if N number of candles are completed after alert candle
strategy.cancel_all(candle_count > entry_distance)

// if current time is outside byuing session then do not enter intraday trade
strategy.cancel_all(timeframe.isintraday and not BarInSession(buy_session))

// ***********************************************************************************************************************
// exit
if strategy.position_size > 0
    strategy.cancel("EXIT at MBB", true)
    strategy.cancel("EXIT at SL", true)
    strategy.order("EXIT at MBB", strategy.short, abs(strategy.position_size), limit=exit_price, comment="EXIT TG @ "+ tostring(exit_price))
    strategy.order("EXIT at SL", strategy.short, abs(strategy.position_size), stop=sl_price, comment="EXIT SL @ "+ tostring(sl_price))

if strategy.position_size < 0
    strategy.cancel("EXIT at MBB", true)
    strategy.cancel("EXIT at SL", true)
    strategy.order("EXIT at MBB", strategy.long, abs(strategy.position_size), limit=exit_price, comment="EXIT TG @ "+ tostring(exit_price))
    strategy.order("EXIT at SL", strategy.long, abs(strategy.position_size), stop=sl_price, comment="EXIT SL @ "+ tostring(sl_price))

// if intraday trade, close the trade at open of 15:15 candle //!!!!!!!!!!!!!!!!!!!!! TO BE CORRECTED !!!!!!!!!!!!!!!!!!!!!!!
if timeframe.isintraday and exit_inraday and hour == 15 and minute == 00
    strategy.close("BUY",  when=strategy.position_size > 0, qty=strategy.position_size, comment="EXIT @ "+ tostring(close))
    strategy.close("SELL", when=strategy.position_size < 0, qty=strategy.position_size, comment="EXIT @ "+ tostring(close))

// ***********************************************************************************************************************
// plots    
//
// plot BB
[mBBp,uBBp,lBBp] = getBB(0)
p_mBB = plot(show_bb_switch ? mBBp : na, color=color.teal)
p_uBB = plot(show_bb_switch ? uBBp : na, color=color.teal)
p_lBB = plot(show_bb_switch ? lBBp : na, color=color.teal)
fill(p_uBB,p_lBB,color=color.teal,transp=95)

더 많은