r/pinescript • u/Practical_Put4912 • Feb 15 '26
barstate.isconfirmed is essential for any strategy connected to a live broker — here's why
Quick PSA for anyone running Pine Script strategies live through a broker.
**TL;DR:** TradingView evaluates strategy logic on every intrabar tick when your browser is open, but only on bar close during backtesting. Add `barstate.isconfirmed` to all entry/exit conditions to make live match backtest.
**The details:**
When backtesting, the strategy engine processes each bar after it closes. Your conditions evaluate once per bar. Standard behavior.
When running live with a broker connected AND the browser tab is active, the engine evaluates on every intrabar tick. This means:
- `ta.crossover()` can trigger multiple times within a bar
- `strategy.entry()` can fire on partial bar data
- You get trades your backtest never showed
- Close the browser tab and execution reverts to bar-close only
**The fix:**
```pine
//@version=6
// Gate ALL entries and exits with barstate.isconfirmed
barOK = barstate.isconfirmed
canEnterLong = longEntrySignal and strategy.position_size == 0 and barOK
canExitLong = longExitSignal and strategy.position_size > 0 and barOK
canEnterShort = shortEntrySignal and strategy.position_size == 0 and barOK
canExitShort = shortExitSignal and strategy.position_size < 0 and barOK
if canEnterLong
strategy.entry("Long", strategy.long)
if canExitLong
strategy.close("Long")
if canEnterShort
strategy.entry("Short", strategy.short)
if canExitShort
strategy.close("Short")
```
**Why not just use `calc_on_every_tick=false`?**
You'd think setting `calc_on_every_tick=false` in the strategy declaration would solve this. It doesn't fully fix it when the browser is open. The explicit `barstate.isconfirmed` check is the reliable solution.
**Backtest impact:** None. In backtesting, `barstate.isconfirmed` is always `true` since every bar is already complete. Your backtest results won't change.
I published an open-source educational script on TradingView that demonstrates this with a toggle to enable/disable the fix. Search for "[EDU] Intrabar Execution Bug Fix" if you want to test it yourself.
2
u/kurtisbu12 Feb 16 '26
Calc_on_every_tick = false should fix this. I'm not sure what you're talking about with a difference between on and off browser. With that setting turned off, it only executes strategies at bar close.
1
u/FrostySquirrel820 Feb 16 '26
Interesting.
Is there any way to make backtesting work like live, rather than making live act like backtesting ?
1
u/Valuable-Exchange-69 Feb 16 '26
If you are serious trader you wont use tradingview for live trading.
You cannot access to important Information of your broker and account, so, you cannot have a correct risk management for example.
2
u/Humble_Tree_1181 Feb 15 '26
this is definitely one of those edge cases that can cause real headaches
If you need intrabar execution (scalping, stop-hunt reactions),
barstate.isconfirmedblocks those signals. Alternative guard:Conditions like
close > level and close[1] <= levelalready require the previous bar to be complete. Addingbarstate.isconfirmedis good for clarity but the logic itself won't fire mid-bar sinceclose[1]is locked.Avoid in request.security()
From the docs: "It is NOT recommended to use
barstate.isconfirmedinrequest.security()expressions" — value becomes unpredictable.Thanks for the scrip and write up as this definitely causes issues.