
Ý tưởng cốt lõi của chiến lược này là kết hợp nhiều khung thời gian để xác định xu hướng thị trường, sử dụng các chỉ số vượt quá trên khung thời gian cao hơn làm bộ lọc, phát tín hiệu mua và bán trên khung thời gian thấp hơn. Chiến lược này nhằm mục đích sử dụng thông tin cấu trúc thị trường cung cấp bởi khung thời gian cao để nâng cao chất lượng quyết định giao dịch.
Chiến lược này lấy giá trị chỉ số vượt quá của khung thời gian cao hơn (chính thức là 4 lần khung thời gian hiện tại) bằng cách gọi hàm security. Các chỉ số vượt quá bao gồm hai đường: đường vượt quá và đường xu hướng.
Chiến lược này sử dụng chiều hướng của xu hướng vượt quá khung thời gian cao như một điều kiện lọc và chỉ phát ra tín hiệu giao dịch khi chiều hướng của xu hướng vượt quá khung thời gian thấp phù hợp với khung thời gian cao. Nói cách khác, chiến lược này sẽ chỉ làm nhiều hoặc thiếu khi chỉ số vượt quá xu hướng trên cả hai khung thời gian phát ra tín hiệu đồng hướng.
Điều này có thể tránh được sự nhiễu loạn của tiếng ồn thị trường khung thời gian thấp, tăng độ tin cậy của tín hiệu. Đồng thời sử dụng khung thời gian cao để đánh giá cấu trúc thị trường và đưa ra phán đoán tổng thể chính xác.
Giải pháp:
Chiến lược này có thể được tối ưu hóa theo các khía cạnh sau:
Các phương pháp như tối ưu hóa tham số, kết hợp các chỉ số, cải thiện dừng lỗ và giới thiệu học máy có thể nâng cao đáng kể hiệu quả của chiến lược theo dõi xu hướng đa khung thời gian.
Chiến lược này khéo léo sử dụng sự phán đoán xu hướng của khung thời gian cao để hướng dẫn thực hiện giao dịch của khung thời gian thấp. Thiết kế khung thời gian đa dạng này có thể lọc hiệu quả tiếng ồn thị trường và nhận ra hướng xu hướng rõ ràng hơn.
/*backtest
start: 2023-02-14 00:00:00
end: 2024-02-20 00:00:00
period: 1d
basePeriod: 1h
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/
// © HeWhoMustNotBeNamed
//@version=4
strategy("Higher TF - Repainting", overlay=true, initial_capital = 100000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01, calc_on_order_fills = true)
HTFMultiplier = input(4, minval=1, step=1)
SupertrendMult = input(1)
SupertrendPd = input(4, minval=4, step=4)
backtestBars = input(title="Backtest from ", defval=10, minval=1, maxval=30)
backtestFrom = input(title="Timeframe", defval="years", options=["days", "months", "years"])
repaintOption = input(title="Repaint", defval="Yes", options=["Yes", "No - set lookahead false", "No - do not use security"])
f_multiple_resolution(HTFMultiplier) =>
target_Res_In_Min = timeframe.multiplier * HTFMultiplier * (
timeframe.isseconds ? 1. / 60. :
timeframe.isminutes ? 1. :
timeframe.isdaily ? 1440. :
timeframe.isweekly ? 7. * 24. * 60. :
timeframe.ismonthly ? 30.417 * 24. * 60. : na)
target_Res_In_Min <= 0.0417 ? "1S" :
target_Res_In_Min <= 0.167 ? "5S" :
target_Res_In_Min <= 0.376 ? "15S" :
target_Res_In_Min <= 0.751 ? "30S" :
target_Res_In_Min <= 1440 ? tostring(round(target_Res_In_Min)) :
tostring(round(min(target_Res_In_Min / 1440, 365))) + "D"
f_getBackTestTimeFrom(backtestFrom, backtestBars)=>
byDate = backtestFrom == "days"
byMonth = backtestFrom == "months"
byYear = backtestFrom == "years"
date = dayofmonth(timenow)
mth = month(timenow)
yr = year(timenow)
leapYearDaysInMonth = array.new_int(12,0)
array.set(leapYearDaysInMonth,0,31)
array.set(leapYearDaysInMonth,1,29)
nonleapYearDaysInMonth = array.new_int(12,0)
array.set(leapYearDaysInMonth,0,31)
array.set(leapYearDaysInMonth,1,28)
restMonths = array.new_int(10,0)
array.set(leapYearDaysInMonth,0,31)
array.set(leapYearDaysInMonth,1,30)
array.set(leapYearDaysInMonth,2,31)
array.set(leapYearDaysInMonth,3,30)
array.set(leapYearDaysInMonth,4,31)
array.set(leapYearDaysInMonth,5,31)
array.set(leapYearDaysInMonth,6,30)
array.set(leapYearDaysInMonth,7,31)
array.set(leapYearDaysInMonth,8,30)
array.set(leapYearDaysInMonth,9,31)
array.concat(leapYearDaysInMonth,restMonths)
array.concat(nonleapYearDaysInMonth,restMonths)
isLeapYear = yr % 4 == 0 and (year%100 != 0 or year%400 == 0)
numberOfDaysInCurrentMonth = isLeapYear ? array.get(leapYearDaysInMonth, mth-2) : array.get(nonleapYearDaysInMonth, mth-2)
if(byDate)
mth := (date - backtestBars) < 0 ? mth - 1 : mth
yr := mth < 1 ? yr - 1 : yr
mth := mth < 1 ? 1 : mth
date := (date - backtestBars) < 0 ? numberOfDaysInCurrentMonth - backtestBars + date + 1 : date - backtestBars + 1
if(byMonth)
date := 1
yr := (mth - (backtestBars%12)) < 0 ? yr - int(backtestBars/12) - 1 : yr - int(backtestBars/12)
mth := mth - (backtestBars%12) + 1
if(byYear)
date := 1
mth := 1
yr := yr - backtestBars
[date, mth, yr]
repaint = repaintOption == "Yes"
useSecurityLookahead = repaintOption == "No - set lookahead false"
[SupertrendRepaint, DirRepaint] = security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), supertrend(SupertrendMult, SupertrendPd), lookahead = true, gaps=true)
[SupertrendNoLookahead, DirNoLookahead] = security(syminfo.tickerid, f_multiple_resolution(HTFMultiplier), supertrend(SupertrendMult, SupertrendPd), lookahead = false, gaps=false)
[SupertrendRegular, DirRegular] = supertrend(SupertrendMult, SupertrendPd)
[date, mth, yr] = f_getBackTestTimeFrom(backtestFrom, backtestBars)
inDateRange = time >= timestamp(syminfo.timezone, yr, mth, date, 0, 0)
longCondition = repaint ? DirRepaint == -1 : useSecurityLookahead? DirNoLookahead == -1 : DirRegular == -1
shortCondition = repaint ? DirRepaint == 1 : useSecurityLookahead? DirNoLookahead == 1 : DirRegular == 1
strategy.entry("Buy", strategy.long, when=longCondition and inDateRange)
strategy.entry("Sell", strategy.short, when=shortCondition and inDateRange)