Pine Script lookahead bug makes backtests profitable but live trades lose money
A documented Pine Script mechanic causes strategies to appear profitable in TradingView's Strategy Tester while losing money in live trading. The issue stems from using 'barmerge.lookahead_on' in the request.security() function, which inadvertently feeds future price data into historical bar calculations during backtesting. On live bars the problem is invisible, since no future data exists yet, but once those bars become historical they are recalculated with complete data, causing the chart to 'repaint' into a different signal history. This discrepancy means a paper-trading session can falsely confirm a strategy's validity until a chart reload reveals the backtest was built on information unavailable in real time. TradingView's own documentation explicitly warns against this setting, advising developers to verify it does not compromise logic by leaking future data into past bars.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in