Search code examples
pine-scriptalgorithmic-tradingtradingtradingview-api

Pine Strategy barstate.islast false


Scenario

Considering a 5 Minute Time Frame, adding two indicator to the chart - Test Indicator and Test Strategy - before the close of the Candle.

barstate.islast is False

barstate.islast is false for the last bar in Strategy, while it is true for Indicator.

Indicator Example

//@version=5
indicator("Test Indicator", overlay=true)

plotchar(barstate.islast, "islast", "", location = location.top, size = size.tiny)

Strategy Example

//@version=5
strategy("Test Strategy", overlay=true)

plotchar(barstate.islast, "islast", "", location = location.top, size = size.tiny)

Results comparison for Indicator and Strategy

Additional Information

After the close of the 5 Minute Candle, the value barstate.islast is the same for Indicator and Strategy, even for the Last and Current Candle.

We can get the same behavior adding calc_on_every_tick = true for the strategy.

The result are the same after the close of the first bar

Questions

Why the different behavior for bartstate.islast, between Strategy and Indicator, where bartstate.islast is false for Strategy, before the close of the last candle?


Solution

  • By default, strategies are calculated when the bar is closed. That's why it returns false when it is an active bar because your strategy is not being executed on every tick.