Skip to content
Pinescripter

Pine Script Multi Time Frame Strategy: Build and Test

Learn how to combine a confirmed higher-timeframe trend filter with lower-timeframe entries in Pine Script v6, then check the strategy for repainting and weak test assumptions.

A higher-timeframe trend chart aligned with a lower-timeframe entry chart

A Pine Script multi time frame strategy combines signals from one chart timeframe with context from another, often using a confirmed higher-timeframe filter to gate lower-timeframe entries. Request the prior closed higher-timeframe value to avoid acting on a candle that is still forming.

How does a multi-timeframe strategy fit together?

A multi-timeframe strategy uses data from more than one timeframe to make a single trading decision. A common design puts the slower timeframe in charge of context and the faster timeframe in charge of timing. For example, a daily trend filter can allow long entries on an hourly chart, while a short-term signal decides when to enter.

Think of the two parts as separate rules:

  • Context: What market conditions allow a trade? A trend filter might permit longs only when the confirmed higher-timeframe close is above its moving average.

  • Trigger: What event creates an entry? It could be a crossover, breakout, or oscillator signal on the chart timeframe.

  • Exit and risk: What closes the position, and how are order size and costs handled?

The filter does not predict what the market will do next. It simply restricts which entries your strategy is allowed to take. That restriction can reduce trades as well as change results, so compare the filtered and unfiltered versions rather than assuming the filter improves the system.

If you are still turning an idea into explicit rules, our guide to turning a trading idea into testable Pine Script can help you define the entry, exit, filter, and sizing before coding.

How do you request a confirmed higher-timeframe value?

Use request.security() to evaluate an expression in another symbol-and-timeframe context and bring its result onto the chart. For a strategy, the important part is not just which timeframe you request, but whether the requested bar has finished.

A higher-timeframe candle can still be forming while several lower-timeframe bars appear. Its moving average, RSI, or close can change during that period. If your strategy uses that unfinished value, a signal may look different after the higher-timeframe candle closes. The [1] offset inside the requested expression selects the previous higher-timeframe bar, which is closed. State lookahead_off explicitly so the intent is visible in the code.

//@version=6
strategy("Confirmed MTF trend filter", overlay = true)

htf = input.timeframe("D", "Trend timeframe")
trendLength = input.int(50, "Trend EMA length", minval = 2)

// Request the EMA from the previous, closed higher-timeframe bar.
confirmedHtfEma = request.security(
syminfo.tickerid,
htf,
ta.ema(close, trendLength)[1],
lookahead = barmerge.lookahead_off
)

allowLong = close > confirmedHtfEma
plot(confirmedHtfEma, "Confirmed HTF EMA", color = color.orange)

Here, close in allowLong is from the chart timeframe, while confirmedHtfEma is the prior closed higher-timeframe EMA. The example only defines a filter; it is not a complete trading system. Add an entry trigger and exit rules that match your own hypothesis.

The offset belongs inside the request.security() expression. Moving it outside changes which series is offset and can produce a different result. For more detail, see our explanation of how request.security works and the confirmed-bar pattern for multi-timeframe indicators.

A completed higher-timeframe candle provides context for smaller chart bars

How do you add lower-timeframe entries?

Once the higher-timeframe filter is stable, combine it with a clearly defined entry condition on the chart timeframe. For instance, an RSI crossover can act as the trigger, while the confirmed EMA decides whether a long is allowed. The entry then reads as a rule you can inspect: take the crossover only when the trend gate is open.

rsiLength = input.int(14, "RSI length", minval = 2)
rsiValue = ta.rsi(close, rsiLength)

longTrigger = ta.crossover(rsiValue, 30)
longEntry = longTrigger and allowLong

if longEntry
strategy.entry("Long", strategy.long)

This snippet illustrates the relationship between filter and trigger, not a recommended RSI system. Choose thresholds, direction rules, and exits for the hypothesis you want to test. If you want both long and short entries, define their filters and triggers separately; do not assume the short side should simply mirror the long side.

Keep each condition readable while developing. Plot the higher-timeframe filter, and mark the trigger and actual entry separately. That makes it easier to see whether a missed trade came from the filter, the trigger, or the order logic. A 200 EMA trend filter guide shows how to treat a filter as a gate on an existing entry rather than an entry signal by itself.

What can make an MTF strategy repaint?

The main hazard is using a higher-timeframe value before that candle has closed. A daily RSI or EMA can move while the daily bar is live. Historical bars show the final settled value, but a live strategy encountered the unfinished value as it changed. That mismatch can make past entries appear cleaner than the decisions available in real time.

Do not treat barmerge.lookahead_off alone as a full repainting fix. It prevents the particular future leak associated with lookahead_on, but a request without a prior-bar offset can still use the current, unfinished higher-timeframe bar. Conversely, using lookahead_on with a current higher-timeframe value can expose the final value on historical lower-timeframe bars before it would have been known.

For signals that must use completed higher-timeframe data, request the prior value inside the expression and use lookahead_off, as in the earlier code. This introduces a delay: you are choosing confirmation over using a moving value. That delay is part of the rule, not a coding defect.

Also check how your strategy handles the chart’s live bar. If an entry condition is allowed to change before that bar closes, it may appear and disappear intrabar. Decide whether your system acts on intrabar changes or only confirmed chart bars, and test that behavior deliberately. Our guide to Pine Script lookahead explains why the setting matters; our article on repainting and its different causes covers related pitfalls.

When should you use lower-timeframe data requests?

A strategy that requests a higher timeframe is different from one that needs the individual bars inside a chart bar. request.security() returns a value merged onto the chart’s bars. When you need the collection of intrabars within each chart bar, investigate request.security_lower_tf() and its array-based results instead.

That distinction matters for questions such as whether a stop or target was reached first inside a larger chart candle. A single merged value may not provide the intrabar sequence your rule needs. Lower-timeframe requests can offer more detail, but they also make the data structure and logic more involved. Use them only when the strategy’s decision genuinely depends on information at that finer resolution.

Before changing functions, write down the exact question your strategy is asking. Is it “Was the prior daily trend positive?” or “What happened inside this five-minute bar?” The first is a higher-timeframe context request; the second is an intrabar-data problem. Our guide to request.security_lower_tf() walks through lower-timeframe arrays and their use.

A trader checks a strategy bar by bar before the higher-timeframe candle closes

How should you test the strategy fairly?

A clean compile only tells you that Pine accepted the script. It does not establish that the rules behave as intended or that the backtest is useful. Review the strategy in stages:

  1. Check the logic on the chart. Confirm that the higher-timeframe filter changes only when expected and that entries match the written conditions.

  2. Inspect individual trades. Look at entry and exit bars, not just the headline result. Check whether the strategy uses the intended timeframe values.

  3. Compare like with like. Run the same entry and exit logic with the filter on and off. Note changes in trade count as well as performance measures.

  4. Include trading costs. Set commission and slippage to assumptions you consider realistic. Results without costs can misrepresent a strategy.

  5. Check for future dependence. Replay the script bar by bar and inspect whether signals that looked present historically were actually available at the time.

PineScripter can simulate strategies on past bars, and its strategy simulations use starting capital, commission, and slippage specified by the user. The simulation describes historical behavior; it is not a forecast. Our strategy commission and slippage guide explains how costs enter a Pine Script strategy.

For an MTF strategy, bar replay is especially useful: stop where a higher-timeframe candle is still forming and ask whether your rule uses only information available then. A backtest can be a way to investigate a hypothesis, not a promise about future performance.

How can you build and debug the strategy efficiently?

Start with a small, testable specification rather than asking for a complete strategy in one vague sentence. State the chart timeframe, requested timeframe, exact higher-timeframe series, whether it must be confirmed, entry trigger, exits, and cost assumptions. If a choice is open—such as whether a short entry should be included—decide it before treating the code as finished.

When debugging, isolate one question at a time. First confirm the requested series on the chart. Then check the filter. Then add the entry condition and inspect its markers. Finally, add exits and compare the strategy’s trade list with the rules. This makes a logic problem easier to distinguish from a compile error.

In PineScripter, we turn a request into a requirements checklist, ask clarifying questions when details are ambiguous, write Pine Script v6, and run draft scripts through a built-in lint engine to check and repair compile errors. We explain the resulting script in plain English. For follow-up changes, we edit affected lines and show the changes side by side, with saved versions in the library. These checks help with code and iteration; they do not prove that a strategy is profitable or suitable for live trading.

You can also inspect the script on a chart and use replay to see when conditions trigger. If you want to try that workflow, see our Pine Script AI editor and bar replay tools.

Frequently asked questions

Can a Pine Script strategy use more than two timeframes?
Yes. A strategy can request data from multiple timeframes, but each added series should answer a defined part of the rules. Keep the conditions understandable and confirm that each requested value is available when the strategy acts.
Does `lookahead_off` alone prevent MTF repainting?
Not necessarily. It avoids the future leak from `lookahead_on`, but an unoffset request may still use a higher-timeframe candle that is forming. For a confirmed signal, request the prior higher-timeframe value inside the expression.
What is the difference between `request.security()` and `request.security_lower_tf()`?
`request.security()` merges a requested series onto the chart. `request.security_lower_tf()` is for accessing lower-timeframe intrabars within a chart bar, typically as arrays. Choose based on whether you need a timeframe value or intrabar detail.
Should an MTF strategy wait for the higher-timeframe candle to close?
If your rule is meant to use confirmed higher-timeframe information, yes: use a value from the previous closed higher-timeframe bar. That adds delay, but avoids making decisions from a value that can still change.

This article was drafted with AI assistance from Pinescripter's own information and current sources.

  • A chart alert being delivered to a chat channel

    Send TradingView Alerts to Discord With Pine Script

    Connect a Pine Script alert to a Discord channel using an incoming webhook. Learn the JSON format Discord expects, how to include live chart values, and how to test the setup.

    Siddhant Chaudhary ·

  • Candlestick chart showing price approaching a limit order level

    Pine Script Strategy Limit Order Backtest

    A limit order backtest depends on more than the limit price in your code. Learn how to place Pine Script limit entries, interpret fills, and test assumptions carefully.

    Siddhant Chaudhary ·

  • Illustration of a trading position being built through successive entries

    Pine Script Strategy Pyramiding: Entries and Limits

    Pyramiding lets a Pine Script strategy add entries in the same direction. Learn how to set the limit, what strategy.entry() does, and how to check the resulting trades and exposure.

    Siddhant Chaudhary ·