Breakout Trend Follower V2

Tác giả:ChaoZhang, Ngày: 2023-11-01 17:24:08
Tags:

img

Tổng quan

Chiến lược này là một biến thể của chiến lược Breakout Trend Follower khác của tôi. Trong chiến lược khác, bạn có thể sử dụng đường trung bình động để hoạt động như một bộ lọc cho các giao dịch của mình (tức là nếu giá thấp hơn đường trung bình động, nó sẽ không kéo dài). Sau khi tạo ra công cụ phát hiện xu hướng trên các khung thời gian cao hơn, tôi muốn xem liệu đó có thể là một bộ lọc tốt hơn so với đường trung bình động.

Vì vậy, kịch bản này cho phép bạn xem xu hướng khung thời gian cao hơn (tức là có mức cao hơn và mức thấp hơn không? Nếu có, đây là xu hướng tăng). Bạn chỉ giao dịch khi bạn đang theo xu hướng. Bạn có khả năng chọn tối đa hai xu hướng để hoạt động như một bộ lọc. Mỗi hướng xu hướng được hiển thị trên bảng trên biểu đồ để tham khảo dễ dàng.

Những gì tôi thấy là nói chung đây không hoạt động tốt như các chiến lược khác, nhưng nó dường như là nhiều hơn chọn lọc với các giao dịch.

Chiến lược logic

Lý thuyết cốt lõi của chiến lược này là xác định xu hướng bằng cách phá vỡ mức hỗ trợ và kháng cự trên các khung thời gian cao hơn và thực hiện giao dịch theo hướng xu hướng.

Cụ thể, nó thực hiện các bước sau:

  1. Tính toán mức hỗ trợ và kháng cự trục trên khung thời gian hiện tại (ví dụ: 1 giờ).

  2. Tính toán mức hỗ trợ và kháng cự trục trên một hoặc nhiều khung thời gian cao hơn (ví dụ: 4 giờ và hàng ngày).

  3. Chụp các mức hỗ trợ và kháng cự này dưới dạng đường ngang trên biểu đồ.

  4. Xác định hướng xu hướng dựa trên việc liệu giá có phá vỡ điểm cao hoặc thấp trước đó hay không. Bước qua mức cao trước đó cho thấy xu hướng tăng. Bước qua mức thấp trước đó cho thấy xu hướng giảm.

  5. Cho phép người dùng chọn một hoặc nhiều xu hướng khung thời gian cao hơn như một điều kiện lọc. tức là chỉ xem xét giao dịch khi xu hướng khung thời gian hiện tại phù hợp với xu hướng khung thời gian cao hơn.

  6. Khi điều kiện lọc xu hướng được đáp ứng và giá hiện tại phá vỡ mức chính, nhập dài hoặc ngắn.

  7. Khi giá chuyển động, điều chỉnh dừng lỗ đến các điểm thấp mới để khóa lợi nhuận và theo xu hướng.

  8. Rút khi stop loss được kích hoạt hoặc mức hỗ trợ / kháng cự chính bị phá vỡ.

Bằng cách phân tích xu hướng trên nhiều khung thời gian, chiến lược này cố gắng chỉ giao dịch theo hướng xu hướng mạnh hơn để cải thiện tỷ lệ thắng.

Ưu điểm của Chiến lược

  • Sử dụng nhiều khung thời gian để đánh giá xu hướng có thể xác định chính xác hơn hướng xu hướng mạnh hơn, tránh tiếng ồn.

  • Chỉ giao dịch với các xu hướng chính cải thiện đáng kể tỷ lệ thắng. So với bộ lọc trung bình động đơn giản, chiến lược này cho thấy tỷ lệ thắng và tỷ lệ rủi ro-lợi nhuận cao hơn.

  • Mức hỗ trợ và kháng cự cung cấp mức nhập và dừng lỗ rõ ràng.

  • Điều chỉnh dừng theo xu hướng để tối đa hóa lợi nhuận khóa.

  • Đơn giản và rõ ràng chiến lược logic, dễ hiểu và tối ưu hóa.

Rủi ro của chiến lược

  • Dựa trên xu hướng dài hạn, dễ bị mắc kẹt trong các sự đảo ngược xu hướng.

  • Không xem xét tác động cơ bản, có thể lệch so với giá trên các sự kiện lớn.

  • Không có điều khiển kích thước vị trí được thiết lập. nên tối ưu hóa kích thước theo kích thước tài khoản, biến động vv

  • Thời gian kiểm tra hậu quả hạn chế.

  • Không tính đến chi phí giao dịch nên điều chỉnh các tham số dựa trên chi phí thực tế.

  • Chỉ xem xét giao dịch dài hạn. Có thể phát triển tín hiệu cho các giao dịch ngắn hạn để thực hiện các chiến lược nhiều khung thời gian.

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

  • Thêm điều kiện bộ lọc:

    • Các yếu tố cơ bản như thu nhập, các sự kiện tin tức

    • Các chỉ số như khối lượng, ATR dừng lại

  • Tối ưu hóa các thông số:

    • Thời gian tính toán hỗ trợ/kháng cự

    • Các khung thời gian để xác định xu hướng

  • Mở rộng phạm vi chiến lược:

    • Phát triển các chiến lược giao dịch ngắn hạn

    • Xem xét các cơ hội bán ngắn

    • Phân biệt giữa các thị trường

  • Cải thiện quản lý rủi ro:

    • Tối ưu hóa kích thước vị trí theo biến động và kích thước

    • Cải thiện các chiến lược dừng lỗ như dừng di chuyển / đệm

    • Thiết lập các chỉ số điều chỉnh rủi ro

  • Cải thiện logic thực thi:

    • Lựa chọn thời gian nhập cảnh

    • Các mục có kích thước một phần

    • Tối ưu hóa chuyển động dừng lỗ

Kết luận

Chiến lược này thiết kế một hệ thống đột phá tương đối mạnh mẽ bằng cách phân tích xu hướng trên nhiều khung thời gian. So với các bộ lọc đơn giản như trung bình động, nó cho thấy tỷ lệ thắng cao hơn và tỷ lệ rủi ro-lợi nhuận cao hơn. Nhưng có một số lĩnh vực có thể được cải thiện, chẳng hạn như thiếu cơ chế quản lý rủi ro vững chắc và xem xét các nguyên tắc cơ bản. Với tối ưu hóa hơn nữa, nó có thể trở thành một chiến lược theo xu hướng rất thực tế. Nhìn chung, thiết kế chiến lược là âm thanh, cải thiện độ chính xác thông qua phân tích nhiều khung thời gian, và đáng để nghiên cứu và áp dụng thêm.


/*backtest
start: 2023-10-24 00:00:00
end: 2023-10-26 00:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4

// Revision:        1
// Author:          @millerrh
// Strategy:  Enter long when recent swing high breaks out, using recent swing low as stop level.  Move stops up as higher lows print to act
// as trailing stops.  Ride trend as long as it is there and the higher lows aren't breached.  
// The difference between this one and the previous Breakout Trend Follower is that this one uses higher timeframe higher highs/higher lows as a filter instead 
// of an arbitrary Moving Average.  I wanted to test out whether waiting for longer term actual trend changes produced better stats than just the moving average.
// Conditions/Variables 
//    1. Manually configure which dates to back test
//    2. Can add a filter to only take setups that are above (or below for shorts) user-defined larger timeframe trends (helps avoid trading counter trend) 

// === CALL STRATEGY/STUDY, PROGRAMATICALLY ENTER STRATEGY PARAMETERS HERE SO YOU DON'T HAVE TO CHANGE THEM EVERY TIME YOU RUN A TEST ===
// (STRATEGY ONLY) - Comment out srategy() when in a study() 
strategy("Breakout Trend Follower V2", overlay=true, initial_capital=10000, currency='USD', 
   default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1)
// (STUDY ONLY) - Comment out study() when in a strategy() 
//study("Breakout Trend Follower V2", overlay=true)

// === BACKTEST RANGE ===
Start = input(defval = timestamp("01 Jan 2019 06:00 +0000"), title = "Backtest Start Date", type = input.time, group = "Backtest Range")
Finish = input(defval = timestamp("01 Jan 2100 00:00 +0000"), title = "Backtest End Date", type = input.time, group = "Backtest Range")

// == USER INPUTS ==
tableLocation = input(defval="Top", options=["Top", "Bottom"], title = "Info Table Location", group = "Display",
  tooltip = "Place information table on the top of the pane or the bottom of the pane.")
lookback = input(defval = 3, title = "Pivot Lookback Period", group = "Pivot Points",
  tooltip = "Looks for pivot points within this number of bars both left and right.")
showPivotPoints = input(title = "Show Historical Pivot Points?", type = input.bool, defval = false, group = "Pivot Points",
  tooltip = "Toggle this on to see the historical pivot points that were used.  Change the Lookback Period to adjust the frequency of these points.
  The pivot points are only shown for the current chart timeframe - to see the Daily pivot pionts, use the Daily timeframe, etc.")
trendFilter = input(defval="1st Timeframe", options=["1st Timeframe", "Both Timeframes", "None"], title = "Use HTF Trend for Filtering?", group = "Higher Timeframe Levels",
  tooltip = "Signals will be ignored when price is not aligned with the higher timeframe trend(s).  The intent is to keep you out of bear periods and only buying when 
  price is showing strength and you are trading with the trend.")
twoSet = input(defval="D", title="1st High Timeframe", type=input.resolution, group = "Higher Timeframe Levels",
  tooltip = "Allows you to set two different time frames for looking at the trend.")
threeSet = input(defval="W", title="2nd High Timeframe", type=input.resolution, group = "Higher Timeframe Levels")
showMTFLevels = input(title = "Show Multiple Timeframe S/R Levels?", type = input.bool, defval = true, group = "Higher Timeframe Levels",
  tooltip = "Displays the pivot highs and lows of higher timeframes to use as support/resistance levels. When these levels break, the trend
  will change on these higher timeframes.")
currentColorS = input(color.new(color.orange,50), title = "Current Timeframe    Support", type = input.color, group = "Higher Timeframe Levels", inline = "MTF1")
currentColorR = input(color.new(color.blue,50), title = " Resistance", type = input.color, group = "Higher Timeframe Levels", inline = "MTF1")
oneColorS = input(color.new(color.yellow,50), title = "1st High Timeframe   Support", type = input.color, group = "Higher Timeframe Levels", inline = "MTF2")
oneColorR = input(color.new(color.yellow,50), title = " Resistance", type = input.color, group = "Higher Timeframe Levels", inline = "MTF2")
twoColorS = input(color.new(color.white,50), title = "2nd High Timeframe    Support", type = input.color, group = "Higher Timeframe Levels", inline = "MTF3")
twoColorR = input(color.new(color.white,50), title = " Resistance", type = input.color, group = "Higher Timeframe Levels", inline = "MTF3")

//  == DEFINE FUNCTIONS FOR USE IN MULTIPLE TIMEFRAMES (USING A TUPLE TO AVOID SO MANY SECURITY CALLS) ==  
f_getHTF() =>
    ph = pivothigh(high, lookback, lookback)
    pl = pivotlow(low, lookback, lookback)
    highLevel = valuewhen(ph, high[lookback], 0)
    lowLevel = valuewhen(pl, low[lookback], 0)
    barsSinceHigh = barssince(ph) + lookback
    barsSinceLow = barssince(pl) + lookback
    timeSinceHigh = time[barsSinceHigh]
    timeSinceLow = time[barsSinceLow]
    [ph, pl, highLevel, lowLevel, barsSinceHigh, barsSinceLow, timeSinceHigh, timeSinceLow]
    
[ph_01, pl_01, hL_01, lL_01, bsSH_01, bsSL_01, tSH_01, tSL_01] = security(syminfo.tickerid, "", f_getHTF())
[ph_02, pl_02, hL_02, lL_02, bsSH_02, bsSL_02, tSH_02, tSL_02] = security(syminfo.tickerid, twoSet, f_getHTF())
[ph_03, pl_03, hL_03, lL_03, bsSH_03, bsSL_03, tSH_03, tSL_03] = security(syminfo.tickerid, threeSet, f_getHTF())

// Plot historical pivot points for debugging and configuring the lookback period.
plot(showPivotPoints ? ph_01 : na, style=plot.style_cross, linewidth=3, color=color.new(color.yellow,50), offset=-lookback)
plot(showPivotPoints ? pl_01 : na, style=plot.style_cross, linewidth=3, color=color.new(color.yellow,50), offset=-lookback)

// == PLOT SUPPORT/RESISTANCE LINES ON THE HIGHER TIMEFRAMES ==
// Use a function to define the lines
f_line(x1, y1, y2, _color) =>
    var line id = na
    // line.delete(id)
    // id := line.new(x1, y1, time, y2, xloc.bar_time, extend.right, _color)

// 1st Timeframe
highLine1 = showMTFLevels ? f_line(tSH_01, hL_01, hL_01, currentColorR) : na
lowLine1 = showMTFLevels ? f_line(tSL_01, lL_01, lL_01, currentColorS) : na 
// 2nd Timeframe
highLine2 = showMTFLevels ? f_line(tSH_02, hL_02, hL_02, oneColorR) : na
lowLine2 = showMTFLevels ? f_line(tSL_02, lL_02, lL_02, oneColorS) : na
// 3rd Timeframe
highLine3 = showMTFLevels ? f_line(tSH_03, hL_03, hL_03, twoColorR) : na
lowLine3 = showMTFLevels ? f_line(tSL_03, lL_03, lL_03, twoColorS) : na

// == TREND CALCULATIONS (USING A TUPLE TO CONSOLIDATE REPETATIVE CODE AND GENERATE MULTIPE VARIABLES WITH ONE FUNCTION ==
f_signal(highLevel, lowLevel) =>
    uptrendSignal    = high > highLevel
    downtrendSignal  = low < lowLevel
    inUptrend        = bool(na)
    inDowntrend      = bool(na) 
    inUptrend       := uptrendSignal[1] ? true : downtrendSignal[1] ? false : inUptrend[1]
    inDowntrend     := not inUptrend
    [uptrendSignal, downtrendSignal, inUptrend, inDowntrend]

[uptrendSignal1, downtrendSignal1, inUptrend1, inDowntrend1] = f_signal(hL_01, lL_01)  // 1st Timeframe
[uptrendSignal2, downtrendSignal2, inUptrend2, inDowntrend2] = f_signal(hL_02, lL_02)  // 2nd Timeframe
[uptrendSignal3, downtrendSignal3, inUptrend3, inDowntrend3] = f_signal(hL_03, lL_03)  // 3rd Timeframe

// == TREND TABLE PLOTTING ==
tablePos = tableLocation == "Top" ? position.top_right : position.bottom_right
var table trendTable = table.new(tablePos, 3, 1, border_width = 3)
upColor = color.rgb(38, 166, 154)
downColor = color.rgb(240, 83, 80)

f_fillCell(_column, _row, _cellText, _c_color) =>
    table.cell(trendTable, _column, _row, _cellText, bgcolor = color.new(_c_color, 70), text_color = _c_color, width = 6)

if barstate.islast or barstate.islastconfirmedhistory
    f_fillCell(0, 0, inUptrend1 ? "▲" : "▼", inUptrend1 ? upColor : downColor)
    f_fillCell(1, 0, inUptrend2 ? "▲ " + twoSet : "▼ " + twoSet, inUptrend2 ? upColor : downColor)
    f_fillCell(2, 0, inUptrend3 ? "▲ " + threeSet : "▼ " + threeSet, inUptrend3 ? upColor : downColor)

// Conditions for entry and exit
buyConditions =  true
buySignal = high > hL_01 and buyConditions // Code to act like a stop-buy for the Study
sellSignal = low < lL_01 // Code to act like a stop-loss for the Study

// (STRATEGY ONLY) Comment out for Study
strategy.entry("Long", strategy.long, stop = hL_01, when = buyConditions)
// strategy.entry("Long", strategy.long, stop = buyLevel2, when = time > Start and time < Finish and high > maFilterCheck)
strategy.exit("Exit Long", from_entry = "Long", stop=lL_01)



Thêm nữa