Simulating TradingView Bar Replay with Pine Script
One of TradingView’s most useful features for practice, strategy testing, and analyzing historical price action is Bar Replay. It allows traders to move the chart back to a specific point in the past and then watch the market unfold from that point onward, one bar at a time.
This makes it possible to recreate conditions similar to a live market, without knowing what happened next in the chart.
In this tutorial, we’ll explore how to use Pine Script to create a simple version of this functionality. Although this approach does not offer all the features of TradingView’s native Bar Replay, it can still be very useful for practicing and testing trading ideas.
What Is Bar Replay and What Is It Used For?
Bar Replay is a tool for reviewing and analyzing historical market behavior. You can select a specific point on the chart as the starting point and then reveal subsequent bars step by step.
For example, suppose you want to practice a trading strategy. Instead of looking at the entire chart from start to finish, you can go back to a specific date in the past and hide everything that happened afterward.
This way, you only have access to the information that would have been available at that point in time. You can then decide whether you would have entered a trade, set your stop-loss and take-profit levels, and continue advancing through the chart to see how the trade would have played out.
This makes Bar Replay particularly useful for practicing technical analysis, testing trading ideas, and improving decision-making skills.
TradingView also describes Bar Replay as a tool for simulating historical market conditions and practicing trading. You can select a starting point and replay historical price action at different speeds. (TradingView)
Limitations of Bar Replay and the Pine Script Approach
TradingView’s native Bar Replay offers many more features than the approach we’re going to build in this tutorial. For example, you can control the replay, change the playback speed, and, depending on your plan, access a greater amount of historical data.
The features available in Bar Replay and the amount of historical data you can access also depend on your TradingView plan and timeframe. Currently, TradingView provides different levels of historical Intraday data across its plans, with Premium and Professional plans offering access to deeper Intraday history. (TradingView)
But here’s an interesting question:
Can we create a simple version of Replay using Pine Script?
The answer is yes.
Of course, this approach is not a full replacement for Bar Replay. Instead, it is a simple technique for hiding part of the chart and gradually revealing it.
The basic idea is straightforward:
Define a specific point in time and display only the bars whose timestamps are earlier than that point.
Whenever we move this time marker, the number of visible bars will change accordingly.
Using plotcandle to Recreate Candles
To implement this idea in Pine Script, we can use the plotcandle function.
This function allows us to draw candles on the chart using custom Open, High, Low, and Close values.
For example, if we want to recreate exactly the same candles shown on the main chart, we can pass the OHLC values directly to plotcandle:
plotcandle(open, high, low, close)
An important advantage of this approach is that the OHLC values do not necessarily have to match those of the original candles.
For example, we can create candles using completely different calculations, similar to the way alternative chart types such as Heikin Ashi calculate their Open, High, Low, and Close values.
In this tutorial, however, our goal is simply to recreate the original candles without changing their values.
We can also define the candle colors based on the relationship between Open and Close.
If Close is greater than Open, we consider the candle bullish; if Close is lower than Open, we consider it bearish:
COLOR = close > open ? color.green : close < open ? color.red : color.black
We can then pass this color to plotcandle.
In addition to the candle body, we can also control the color of the wick and border:
plotcandle(open, high, low, close, color = COLOR, wickcolor = COLOR, bordercolor = COLOR)
As a result, we’ll have candles that match the original chart in both their OHLC values and appearance.
Setting the Replay Point
Now we get to the main part of the idea.
We need a time variable that the user can change from the indicator’s settings.
In Pine Script, we can use input.time to let the user select a date and time:
TIME = input.time(defval = timestamp("23 Nov 2021"))
We can also specify the hour, minute, and other details when defining the timestamp.
Now we need to answer an important question:
How do we determine which candles should be displayed?
All we need to do is compare the timestamp of each bar with the time selected by the user.
For example:
RP = time < TIME
Here, RP will be a Boolean value.
If the bar’s timestamp is earlier than the selected time:
RP = true
And if the bar’s timestamp is later:
RP = false
We can now use this condition to determine whether a candle should be displayed.
Hiding Candles After a Specific Point in Time
To do this, we can define the OHLC values conditionally.
For example:
OPEN = RP ? open : na
HIGH = RP ? high : na
LOW = RP ? low : na
CLOSE = RP ? close : na
The na value is important here.
When the RP condition is true, the actual Open, High, Low, and Close values are assigned to the variables.
When the condition is false, the variables receive na, so no candle will be plotted for that bar.
Finally, we can pass these values to plotcandle:
plotcandle(OPEN, HIGH, LOW, CLOSE, color = COLOR, wickcolor = COLOR, bordercolor = COLOR)
Now, whenever we change the date in the indicator settings, the point after which the candles are hidden will also move.
Turning the Indicator into a Simple Replay Tool
Once we place the indicator on the main chart, we can use the time marker to move through historical price action.
For example, suppose we set the Replay date to a specific day.
Only the candles before that point will be displayed.
If we move the time marker forward, more candles will appear.
If we move it backward, more of the future will be hidden.
This allows us to go back to a point in the market, perform our analysis, and then gradually move the Replay point forward to see whether our analysis matches what actually happened afterward.
Conceptually, this is similar to Bar Replay, but instead of using TradingView’s native Replay controls, we control the process through the time parameter of our indicator.
An Important Limitation When Using Other Indicators
There is an important limitation to this approach.
Suppose we add a Moving Average to the chart separately.
You might expect the Moving Average to stop at the same point because part of the chart has been hidden.
However, that is not what happens.
The original candles still exist on the chart; we are simply hiding them using plotcandle.
As a result, the Moving Average continues to be calculated using the actual, complete chart data, including candles that are supposed to remain hidden during the Replay.
This means the Moving Average can still have access to future price data, which can compromise the results of the exercise.
For example:
Visible candles
█████████████████
Hidden candles
████████████████████████
Moving Average
████████████████████████
In other words, the Moving Average continues to use information from the future, even though those candles are no longer visible.
The Solution: Calculate the Indicator Within the Same Script
If your analytical approach is well-defined and you are using indicators whose code is available to you, this problem can be largely addressed.
For example, if you know exactly how your Moving Average is calculated, you can include its calculation directly inside the same Replay script.
Suppose the Moving Average is calculated as follows:
MA = ta.sma(close, 20)
We can apply the same Replay condition to it:
MA_REPLAY = RP ? MA : na
And then plot it:
plot(MA_REPLAY)
Now the Moving Average will only be displayed while the Replay condition is true.
As a result, when we move the Replay point, both the candles and the Moving Average will move together.
This creates a much more realistic environment for practicing a trading strategy.
Which Indicators Can Be Used with This Method?
This technique is particularly useful when the indicator you are using can be recreated or calculated within the same script.
For example, indicators such as:
- Moving Average
- EMA
- SMA
- Bollinger Bands
- Some trend-following indicators
- Many price-based calculations
can be incorporated into this type of setup.
However, things are somewhat different with indicators that are displayed in a separate pane, such as RSI, MACD, and other oscillators. In these cases, the force_overlay feature can be used.
The logic for the separate-pane indicator still needs to be handled appropriately. Otherwise, the indicator may continue to display information from bars beyond the Replay point.
Another Consideration When Changing Timeframes
If you set the Replay point on a higher timeframe and then switch to a much lower timeframe, the selected point may no longer fall within the visible range of the chart.
In such cases, you might think that the indicator has disappeared, but that is not necessarily the case.
You can usually select the indicator and locate the Replay line or marker, then move it back into the appropriate area of the chart.
It is also worth noting that TradingView does not provide the same amount of historical data across all timeframes. For example, daily data for a symbol may be available much further back in history, while one-minute data for the same symbol may only be available from a much more recent date. (TradingView)
Conclusion
Bar Replay is a useful TradingView tool for practicing and analyzing historical market behavior. However, in some situations, you may want a simpler way to hide future price action and gradually reveal it as you analyze the market.
With Pine Script, we can create a basic version of this functionality by combining a few simple concepts:
- Define a date and time using
input.time - Compare each bar’s timestamp with the selected time
- Use a Boolean condition such as
RP - Hide candles after the Replay point by assigning
na - Recreate the candles using
plotcandle - Apply the same condition to the indicators being used
Of course, this method is not a replacement for TradingView’s native Bar Replay and does not provide features such as playback speed control, standard step-by-step replay, and some of the advanced Replay functionality. TradingView’s native Bar Replay offers a much broader set of features, including the ability to synchronize Replay across multiple charts. (TradingView)
From an educational perspective, however, this technique demonstrates something important: Pine Script gives us considerable control over how market data is displayed, allowing us to build custom tools tailored to our specific needs.
If your goal is to practice a specific trading method, you can take this idea one step further by incorporating the logic of the indicators you use directly into the same script. This allows them to be calculated and displayed only within the portion of the market that is currently visible.
In this way, you can create a simple practice environment where only part of the market is initially available, perform your analysis, and then gradually move the Replay point forward to reveal what happened next—just as you would in a live market, where you have no knowledge of the candles that have yet to form.
This approach can be a useful tool for practicing technical analysis, testing trading ideas, and evaluating the quality of your trading decisions using historical market data.