
La idea central de esta estrategia es identificar tendencias en el mercado en combinación con varios marcos de tiempo, y usar los indicadores de superación en los marcos de tiempo superiores como filtros para emitir señales de compra y venta en los marcos de tiempo inferiores. La estrategia tiene como objetivo aprovechar la información sobre la estructura del mercado proporcionada por los marcos de tiempo superiores para mejorar la calidad de las decisiones de negociación.
La estrategia obtiene el valor del indicador de sobrepasado en el marco de tiempo superior (default 4 veces el marco de tiempo actual) mediante la invocación de la función de seguridad. El indicador de sobrepasado incluye dos líneas: la línea de sobrepasado y la línea de tendencia. Cuando la línea de sobrepasado está por encima de la línea de tendencia, es una señal de pesimismo, y cuando está por debajo, es una señal de descenso.
La estrategia utiliza como condición de filtro la dirección de la tendencia de la tendencia de la franja de tiempo alta y emite una señal de negociación solo cuando la dirección de la tendencia de la franja de tiempo baja coincide con la del marco de tiempo alto. Es decir, la estrategia solo hace más o menos cuando los indicadores de tendencia de la franja de tiempo en los dos marcos de tiempo emiten señales homogéneas.
Esto evita la interferencia con el ruido del mercado en el marco de tiempo bajo y mejora la fiabilidad de la señal. Al mismo tiempo, utiliza el marco de tiempo alto para juzgar la estructura del mercado y hacer un juicio general correcto.
La solución:
La estrategia puede ser optimizada en los siguientes aspectos:
La optimización de parámetros, la combinación de indicadores, la mejora del stop loss y la introducción de aprendizaje automático pueden mejorar significativamente la eficacia de la estrategia de seguimiento de tendencias de múltiples marcos de tiempo.
Esta estrategia utiliza hábilmente el juicio de tendencias en los marcos de tiempo altos para guiar la ejecución de operaciones en los marcos de tiempo bajos. Este diseño de marcos de tiempo múltiples puede filtrar eficazmente el ruido del mercado y identificar la dirección de la tendencia con mayor claridad. Al mismo tiempo, la función de configuración de fechas incorporada hace que la retroalimentación sea más flexible.
/*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)