LIBB and BarsSinceEntry/CurrentBar Issue
There is an issue in TradeStation when trading real-time using BarsSinceEntry with LIBB turned on. This only effects real-time trading. Backtesting is not effected. Kevin Davey posted the issue to TradeStation’s forum here.
The short answer:
BarsSinceEntry does NOT work correctly during real-time trading when LIBB is turned on. It should not be used. Use this replacement, BarsSinceTradeEntry, instead. Also, do not use CurrentBar in strategies. Use BarNumber instead.
The longer explanation:
This issue appears to occur when:
- The strategy is transitioning from a historical bar to a new real-time bar (i.e. it has just been loaded onto a chart up to current date)
- A new trade position occurs on the new bar
- LIBB is enabled on the chart (for any bar interval)
If all these conditions occur, then BarsSinceEntry will either be zero or some large random corrupted value on the first real-time bar and, from that point forward, BarsSinceEntry will be off by one compared to the chart that had LIBB off. The chart with LIBB off ran correctly in this scenario.
In other words, this error will not occur during strategy backtesting, whether using LIBB or not. It only shows up when trading the strategy in real-time, sim or live, with LIBB active and a new position occurs on the first real-time bar.
To determine if a successful workaround could be created, I ran several different configurations and bar types of a strategy using BarsSinceEntry with and without LIBB. I found two curious things with the charts running with LIBB turned on:
- There were times when CurrentBar does not change and remains the same when the strategy changes from the last historical bar to the first real-time tick of the next new bar.
- BarsSinceEntry was incorrect when a new trade was placed on the first real-time bar.
EasyLanguage code was posted to Kevin Davey’s Strategy Factory forum (here and here) for a function that replaces BarsSinceEntry. I called the function BarsSinceTradeEntry and modified the code so that it would also work with strategies that used “open next bar”. I added it to my test strategy for a side-by-side comparison with BarsSinceEntry.
These were the results once the three above criteria occurred:

This gives me two rules that I now use in all my strategy development:
- Do not use TS keyword CurrentBar. Use BarNumber. TS keyword BarNumber was always correct.
- Do not use TS keyword BarsSinceEntry. Use BarsSinceTradeEntry.
For example:
If you have this:
If BarsSinceEntry > XX then sell next bar at market;
Change it to this:
If BarsSinceTradeEntry > XX then sell next bar at market;
That’s it! Strategies that use BarsSinceEntry will now trade correctly in real-time when using LIBB.