Chiến lược xu hướng BB KC dần dần

Tác giả:ChaoZhang, Ngày: 2024-01-26 15:10:41
Tags:

img

Tổng quan

Chiến lược này sử dụng sự kết hợp của Bollinger Bands và Keltner Channel tín hiệu để xác định xu hướng thị trường. Bollinger Bands là một công cụ phân tích kỹ thuật xác định các kênh dựa trên phạm vi biến động giá.

Nguyên tắc chiến lược

  1. Tính toán dải Bollinger giữa, trên và dưới trong 20 giai đoạn.
  2. Tính toán các kênh Keltner giữa, trên và dưới trong 20 giai đoạn.
  3. Khi đường trên kênh Keltner vượt qua đường trên dải Bollinger và khối lượng lớn hơn trung bình động 10 giai đoạn của nó, mua dài.
  4. Khi đường dưới Keltner Channel vượt qua dưới đường dưới Bollinger Band và khối lượng lớn hơn trung bình động 10 giai đoạn của nó, đi ngắn.
  5. Đóng tất cả các vị trí nếu không có tín hiệu ra ngoài kích hoạt sau 20 bar kể từ khi nhập.
  6. Thiết lập stop loss 1,5% cho các giao dịch dài và -1,5% stop loss cho các giao dịch ngắn.

Chiến lược này chủ yếu dựa trên các dải Bollinger để đánh giá phạm vi biến động và động lực. Keltner Channel phục vụ như một công cụ xác minh do các đặc điểm tương tự nhưng các tham số khác nhau. Sử dụng hai chỉ số này cùng nhau cải thiện độ chính xác tín hiệu.

Phân tích sức mạnh

  1. Sử dụng các lợi thế kết hợp của Bollinger Bands và Keltner Channels để cải thiện độ chính xác tín hiệu.
  2. Việc lọc theo khối lượng giao dịch làm giảm các tín hiệu không hợp lệ từ việc chạm vào dòng thường xuyên.
  3. Kiểm soát rủi ro hiệu quả từ các cơ chế dừng lỗ được lập trình và ngăn chặn kéo dài.
  4. Rõ ràng là các khoản đầu tư sẽ bị hạn chế khi có tín hiệu không hợp lệ.

Phân tích rủi ro

  1. Cả Bollinger Bands và Keltner Channels đều dựa trên đường trung bình động và biến động. Chúng có thể tạo ra tín hiệu sai trong các thị trường dao động.
  2. Không có cơ chế hợp nhất có nghĩa là nhiều lần dừng lại có thể dẫn đến tổn thất quá lớn.
  3. Các tín hiệu đảo ngược xảy ra thường xuyên. Các điều chỉnh tham số có thể khiến các cơ hội xu hướng bị bỏ lỡ.

Mở rộng phạm vi dừng lỗ hoặc thêm các chỉ số xác nhận như MACD có thể làm giảm rủi ro từ các tín hiệu sai.

Hướng dẫn tối ưu hóa

  1. Tác động của các tham số thử nghiệm đối với lợi nhuận chiến lược, chẳng hạn như chiều dài, số nhân độ lệch chuẩn v.v.
  2. Thêm các chỉ số khác để xác nhận tín hiệu, ví dụ: KDJ, MACD.
  3. Sử dụng máy học để tối ưu hóa tham số tự động.

Tóm lại

Chiến lược này kết hợp Bollinger Bands và Keltner Channels để xác định xu hướng, được xác minh bởi khối lượng giao dịch. Các cải tiến thêm như tối ưu hóa tham số và thêm các chỉ số sẽ tăng cường nó cho nhiều chế độ thị trường hơn. Nó có khả năng thực hiện mạnh mẽ như một chiến lược giao dịch dễ hiểu và tùy chỉnh.


/*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/
// © jensenvilhelm

//@version=5
strategy("BB and KC Strategy", overlay=true)

// Define the input parameters for the strategy, these can be changed by the user to adjust the strategy
kcLength = input.int(20, "KC Length", minval=1) // Length for Keltner Channel calculation
kcStdDev = input.float(2.2, "KC StdDev") // Standard Deviation for Keltner Channel calculation
bbLength = input.int(20, "BB Length", minval=1) // Length for Bollinger Bands calculation
bbStdDev = input.float(2, "BB StdDev") // Standard Deviation for Bollinger Bands calculation
volumeLength = input.int(10, "Volume MA Length", minval=1) // Length for moving average of volume calculation
stopLossPercent = input.float(1.5, "Stop Loss (%)") // Percent of price for Stop loss 
trailStopPercent = input.float(2, "Trail Stop (%)") // Percent of price for Trailing Stop
barsInTrade = input.int(20, "Bars in trade before exit", minval = 1) // Minimum number of bars in trade before considering exit

// Calculate Bollinger Bands and Keltner Channel
[bb_middle, bb_upper, bb_lower] = ta.bb(close, bbLength, bbStdDev) // Bollinger Bands calculation
[kc_middle, kc_upper, kc_lower] = ta.kc(close, kcLength, kcStdDev) // Keltner Channel calculation

// Calculate moving average of volume
vol_ma = ta.sma(volume, volumeLength) // Moving average of volume calculation

// Plotting Bollinger Bands and Keltner Channels on the chart
plot(bb_upper, color=color.red) // Bollinger Bands upper line
plot(bb_middle, color=color.blue) // Bollinger Bands middle line
plot(bb_lower, color=color.red) // Bollinger Bands lower line
plot(kc_upper, color=color.rgb(105, 255, 82)) // Keltner Channel upper line
plot(kc_middle, color=color.blue) // Keltner Channel middle line
plot(kc_lower, color=color.rgb(105, 255, 82)) // Keltner Channel lower line

// Define entry conditions: long position if upper KC line crosses above upper BB line and volume is above MA of volume
// and short position if lower KC line crosses below lower BB line and volume is above MA of volume
longCond = ta.crossover(kc_upper, bb_upper) and volume > vol_ma // Entry condition for long position
shortCond = ta.crossunder(kc_lower, bb_lower) and volume > vol_ma // Entry condition for short position

// Define variables to store entry price and bar counter at entry point
var float entry_price = na // variable to store entry price
var int bar_counter = na // variable to store bar counter at entry point

// Check entry conditions and if met, open long or short position
if (longCond)
    strategy.entry("Buy", strategy.long) // Open long position
    entry_price := close // Store entry price
    bar_counter := 1 // Start bar counter
if (shortCond)
    strategy.entry("Sell", strategy.short) // Open short position
    entry_price := close // Store entry price
    bar_counter := 1 // Start bar counter

// If in a position and bar counter is not na, increment bar counter
if (strategy.position_size != 0 and na(bar_counter) == false)
    bar_counter := bar_counter + 1 // Increment bar counter

// Define exit conditions: close position if been in trade for more than specified bars
// or if price drops by more than specified percent for long or rises by more than specified percent for short
if (bar_counter > barsInTrade) // Only consider exit after minimum bars in trade
    if (bar_counter >= barsInTrade)
        strategy.close_all() // Close all positions
    // Stop loss and trailing stop
    if (strategy.position_size > 0)
        strategy.exit("Sell", "Buy", stop=entry_price * (1 - stopLossPercent/100), trail_points=entry_price * trailStopPercent/100) // Set stop loss and trailing stop for long position
    else if (strategy.position_size < 0)
        strategy.exit("Buy", "Sell", stop=entry_price * (1 + stopLossPercent/100), trail_points=entry_price * trailStopPercent/100) // Set stop loss and trailing stop for short position


Thêm nữa