Why Tradingview “strategy” and “study” Pine scripts behave differently when Backtesting

Tradingview strategy vs study markers2

Tradingview’s “study” and “strategy” scripts behave quite differently, when backtesting the “strategy” and try to match the behavior of the “study” script.

First thing what is different is the missing support for all startegy.xyz functions in a “study” script. This requires that that the internal Tradingview backtest-engine behavior must be reengineered in the “study”, which leads to considerable more lines of script code.

Then, the “strategy” script is executed once at the end of each bar; if that happens the script can go through all conditions which might trigger a new positions and submits the order via strategy.entry function. This order is getting executed at the beginning of the next bar using the close price (if simple market order).

When running the “study”, which mimics the code of the “strategy”, when running with historical data, the script is also only called once at the end of each bar. Same behavior here: the “study” can check for conditions and mark the position entry/exit in chart by using Tradingview’s plot functions for instance. It can also raise alertcondition, which can be hooked up in Tradingview to emails, webhooks and so on. But as demonstrated in the screenshot below, the “study” script plots the marker exactly at the bar where the entry/exit conditions were detected, while the “strategy” shows entry/exit points in the next bar. This can be quite confusing, but please keep in mind this is still the backtest behavior. If dealing with realtime data, the behavior between “strategy” and “study” and be aligned, if using “tick mode” for “strategy” and “study” or “end-of-bar mode” for “strategy” and “study”.

Tradingview strategy vs study markers

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *