Pine Script request.security higher timeframe issue with Backtesting results

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!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật