Since starting this blog, I have posted quite a few articles specifically aimed at helping beginners get started. Said articles have always focused on “how to” do something on either Backtrader or Tradingview. However, tinkering around in a specific language or framework is just one part of the equation. There are also backtesting ideas, concepts, fundamentals and best practices that are equally important for the beginner to understand and apply to any language or framework. One item, in particular, has come up a few times with clients who are interested in outsourcing their development work. As such, I think the topic is a good place to start in what will hopefully be a series of posts covering backtesting topics in general.
Why are my trades delayed?
Can you help me tweak my strategy to enter trades earlier? These are two questions that could be asked for a variety of reasons but I have found that there is a common misunderstanding of how a strategy is tested. This can then lead new users to believe that there is a problem with their code. This is often not the case.
If you are reading this and had a similar question, let me start by saying there is absolutely nothing wrong with asking it. In fact, it is always good to be sceptical of the results presented to you. When you look at the charts, it is easy to understand why beginners could believe that their backtest is not running in an optimal fashion. It also touches on why indicators can often look so good “on paper” but when it comes to real-world performance we find them lacking. Let’s take a look at some simple examples that demonstrate why people might ask themselves “why are my trades delayed?”.
Example 1
The following screenshots are taken from a simple moving average crossover strategy.
In the first screenshot, we can see that the moving averages crossover before the strategy makes a long entry. The strategy does not enter until the opening of candle following the crossover. Some people may be trying to enter the moment the crossover happens and they see this as late. However, this is to be expected for a couple of reasons.
- The signal is not confirmed until the candle is closed. Therefore, in the real world, the absolute soonest you would be able to enter would be the opening of the following bar.
- When backtesting on a single time-frame, we do not have the data of how price moved between the open and close of the bar. Did it hit the highs first or lows first? Did it bounce between the two multiple times? Unless we are working on multiple time-frames or replaying data for the whole day we do not have this information. Therefore, it is impossible to know exactly when a trade would be opened between the bars and a such, what price you could have entered at.
Example 2
In the second screenshot below, the moving averages crossover between the bars.
Again this is expected. The lines of the moving average are drawn from point to point. Depending on the angle of the lines, they can easily cross between the bars rather than right on them. As such, the crossover you see is not related to time. For example, if a crossover happens halfway between daily bars, it does not mean the MA’s crossed over after exactly 12 hours.
Example 3: A 2 bar delay – What is going on?
In the third screenshot, the problem appears to get worse! We are now delayed by 2 candles! This one also caused me to start debugging the first time I saw it. There must be something wrong with the script right?
However, if you take a closer look, you will see that although it appears that the lines crossed over 2 bars before the entry, the indicator values show that they actually did not cross over until the following bar.
Hopefully, that will save your scalp from a bit of head scratching.
Is it possible to enter earlier?
Yes, you can set “Recalculate On Every Tick” in the properties tab of the strategy. This allows you to enter as soon as a condition is met on the tick! Note: That this only affects real-time data. I.e. Forward tests, where data is delivered tick by tick. By comparison, historical data is delivered bar by bar. In other words, one bar equals one tick for historical data. Having said all that, you should carefully consider if you want to enter on the tick in real-time. If you enter and exit trades before a candle has closed, the signal you think you are entering on has not been confirmed yet. Price could easily turn around and invalidate the signal. In addition, you are no longer trading on the same time frame. You are actually trading the price action on a lower time frame. If you want to get into trades earlier it is better to trade on a lower time frame. You can still do this while referencing values on the higher time frame for confirmation.

Conclusion
I used the word “delayed” a lot in this article and perhaps for no good reason in the end but I think it touches on the first thing that comes to mind when you see the differences in the entries on the chart. We can see that in the end, there are no delays, but rather a logical explanation to each case. Hopefully, this article can save you some debugging time and pain. If you are seeing something similar, all is good!
Can I user “Recalculate on every tick” in Pine? If yes could you give me an example?
Hi Andre,
Sure – If you click the cog icon on your strategy and then select “properties” you will see the option.
Here is an example
https://i.imgur.com/mU9tkn7.jpg
Cheers!
So with backtesting, the strategy bases this on the next bar and not the actual bar where the signal is true (this would signify that it acts as if you selected alerts for once per bar close, or once per bar?) only- and having looked at where the backtest executes the trades, this seems to be the case. If you set it to once only _ recalculate on every tick does this mean that the live trade executes on the bar where the signal is true so long as you select it based on once only or once per bar? When the backtest is set to recalculate for every tick= true it seems to improve backtest results without moving the position the trade takes place which is mystifying.
However someone is testing our script and has found that even with recalculate for every tick=true ticked on, the strategy executes after the bar where the signal is true (howver he has not tried once per minute, or once only, he has been using once per bar close and once per bar).
Can you explain how to get the trade to enter on the same bar as the strategy? Also can you reccomend how to reduce slippage- if you take a market order obviously there’s more slippage, but with a limit order it faces the chances of not being filled.
Hi Steve,
Sorry for the late reply.
I guess you are trying to setup for use with Autoview. This article was more focused on Backtesting with historical data rather then live trading with extensions.
If you have your alerts set to “once per bar”, then you should get a single alert the moment the script is true.
Note that recalculate every tick only works on live data (i.e Forwardtesting) in a strategy.
Without seeing your indicator it is hard to say why your live trades might be delayed. Having said that, the alerts system was not really designed for live trading and can often be delayed and/or miss alerts sometimes.