Chiến lược giao dịch Bollinger Band Breakout

Tác giả:ChaoZhang, Ngày: 2023-10-07 09:59:11
Tags:

Tổng quan

Chiến lược này giao dịch việc phá vỡ Bollinger Bands, lấy các vị trí chống xu hướng khi giá hoàn toàn xuyên qua dải trên hoặc dưới. Nó nhằm mục đích nắm bắt sự đảo ngược trung bình sau sự biến động bất thường. Nó phù hợp với các nhà giao dịch tích cực tìm kiếm lợi nhuận nhanh chóng.

Nguyên tắc

Chiến lược này sử dụng Bollinger Bands để xác định phạm vi biến động hiện tại. Khi giá hình thành một ngọn nến đầy đủ trên dải trên hoặc dưới dải dưới, nó cho thấy trạng thái biến động cao và khả năng đảo ngược hướng tới mức trung bình.

Cụ thể, các dải giữa, trên và dưới được tính bằng cách sử dụng giá đóng 20 giai đoạn. Một tín hiệu dài được tạo ra khi giá đóng dưới dải dưới sau khi mở cao hơn. Một tín hiệu ngắn được kích hoạt khi giá đóng trên dải trên sau khi mở thấp hơn. Điểm đột phá phục vụ như là mức dừng lỗ và dải giữa là mục tiêu lợi nhuận ban đầu.

Ưu điểm

Những lợi thế chính là:

  1. Bollinger Bands đánh giá sự biến động của thị trường một cách hiệu quả để xác định sự bất thường.

  2. Điểm thoát là mức dừng lỗ rõ ràng để kiểm soát rủi ro.

  3. Dải giữa cung cấp một mục tiêu hợp lý cho sự đảo ngược trung bình.

  4. Các ngọn nến đầy đủ lọc các sự phá vỡ sai với độ tin cậy tín hiệu cao hơn.

  5. Các tham số đơn giản làm cho việc thực hiện và tối ưu hóa dễ dàng.

  6. Logic rõ ràng được thể hiện một cách ngắn gọn trong mã.

Rủi ro

Một số rủi ro bao gồm:

  1. Các thông số BB kém có thể làm vô hiệu hóa chiến lược.

  2. Sự đột phá có thể báo hiệu sự khởi đầu của xu hướng, có nguy cơ thoát khỏi quá sớm.

  3. Các mục tiêu trong dải trung gian có thể quá bảo thủ, giới hạn lợi nhuận.

  4. Các vết nứt rộng có thể không lấp đầy đầy đủ, gây trượt.

  5. Whipsaws có thể dẫn đến giao dịch vô nghĩa quá mức trong các thị trường khác nhau.

Những cải tiến

Một số cân nhắc cải thiện:

  1. Đo cường độ xu hướng để điều chỉnh cài đặt hoặc tần số.

  2. Thêm các chỉ số khác để tinh chỉnh thời gian nhập cảnh.

  3. Điều chỉnh stop loss dựa trên biến động.

  4. Tối ưu hóa các mục tiêu ban đầu cho lợi nhuận trơn tru.

  5. Thực hiện các cơ chế tái tham gia vào lợi nhuận tổng hợp.

  6. Đánh giá giá trị thoát để tránh giao dịch xấu.

Kết luận

Chiến lược này giao dịch BB breakout cho lợi nhuận ngắn hạn phù hợp với các nhà giao dịch tích cực.


/*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)

Thêm nữa