In a nutshell:
I’m developing a multi-timeframe trading strategy in Pine Script.
The strategy uses two moving averages (44-period and 777-period) calculated on the 1-hour timeframe to determine a “market regime” (long or short bias) from this higher timeframe.
This regime dictates if I take only long or only short trades on the 1 minute timeframe.
Problem:
The regime detection and execution works as expected on the visual chart.
However, when I run a deep backtest over a longer period (1 month), the strategy doesn’t switch regimes at all, as if the market regime did not exist at all, leading to incorrect results.
I initially suspected a bar misalignment issue due to the different number of bars on the 1 minute and 1-hour timeframes. However, even when limiting the backtest to a 3-day period where the regime should have consistently signaled long trades, the strategy still disregards the regime.
I’m now suspecting issues related to request.security and data limitations or state initialization problems. The 777-period moving average on the 1-hour timeframe requires a large amount of historical data, which might be causing problems in the backtest.
–> Effectively I am just looking for an approach that would enable my strategy to switch trade mode based on the higher timeframe market conditions, that works with the backtester. <–
In this dummy code we are placing long signals only when the higher timeframe market regime is bullish, and short only when the higher timeframe market regime is bearish. This is so we can easily test the backtester. We have 2 main states, when marketRegime == 1 (bull) when == -1 (bear) also when == 0 neutral. These switch the strategy correctly into the corresponding states, but the switching is fully disregarded by the backtester.
// Input for higher timeframe selection
HigherTimeFrame = input.timeframe("60", "Higher Time Frame")
// SMA lengths
maLength1 = 44
maLength2 = 777
// Moving Average Calculations using request.security
ma1 = request.security(syminfo.tickerid, HigherTimeFrame, ta.sma(close, maLength1), lookahead=barmerge.lookahead_off)
ma2 = request.security(syminfo.tickerid, HigherTimeFrame, ta.sma(close, maLength2), lookahead=barmerge.lookahead_off)
// Market Regime Conditions MA's set on 44 and 777 fetched from the 1 Hour timeframe with request.sequrity
upward_slope = ma1 > ma2
downward_slope = ma1 < ma2
noslope = not upward_slope and not downward_slope
// Translate to single int variable, with debugging
if upward_slope
marketRegime := 1
log.info("Bullish Regime, bar_index: {0}", bar_index)
else if downward_slope
marketRegime := -1
log.info("Bearish Regime, bar_index: {0}", bar_index)
else if noslope
marketRegime := 0
log.info("Neutral Regime, bar_index: {0}", bar_index)
//Strategy entry and exit
// Moving Averages for entry and exit conditions.
fastMA = ta.sma(close, 555)
slowMA = ta.sma(close, 777)
// Conditions for Long and Short Signals
longCondition = ta.crossover(fastMA, slowMA) and (marketRegime == 1)
shortCondition = ta.crossunder(fastMA, slowMA) and (marketRegime == -1)
// Conditions for Long and Short exit
shortConditionExit = ta.crossover(fastMA, slowMA)
longConditionExit = ta.crossunder(fastMA, slowMA)
// Entry Orders
if (longCondition)
strategy.entry("Long Entry", strategy.long)
if (shortCondition)
strategy.entry("Short Entry", strategy.short)
// Exit Orders
if (strategy.position_size > 0) // Check for existing long position
if (longConditionExit) // Exit if the market regime changes
strategy.close("Long Entry", comment = "Exit Long")
if (strategy.position_size < 0) // Check for existing short position
if (shortConditionExit) // Exit if the market regime changes
strategy.close("Short Entry", comment = "Exit Short")
// Background Color visual
bgcolor(enable_background ? (upward_slope ? color_up : downward_slope ? color_down : na) : na)
Questions:
Data Limitations: Could the issue be related to request.security’s data limits when dealing with long lookback periods from higher timeframes? If so, what are the best workarounds?
Alternative Approaches: Are there alternative ways to calculate and use long-period moving averages from higher timeframes in a 1 minute strategy without hitting data limits?
Issue with the code: maybe there is an issue with the code I am not seeing ?
Additional Notes:
I’ve verified the historical data quality for both timeframes. The regime detection logic seems to work correctly on the 1-hour chart and on the lower timeframe visible chart. Any insights or suggestions would be greatly appreciated!