Too many references candles in dynamic lenght (Pinescript/Tradingview)

Viewed 278

I'm trying to get the lowest when there is a open short trade, and the highest with long trades, both WITHOUT REPAINTING. I call a ta.barssince in a int when strategy.position_size is < 0 or > 0 and put the variable in lenght of ta.highest(source, lenght). Now, no problem when position size is > 0, script works well without errors, but if the position size is < 0 i get: Pine cannot determine the referencing length of a series. Try using max_bars_back' error. i tried different solutions but i get other correlated errors like: Too many references candles, or: the study references too many candles in history(15649). Hope someone have a solution without changing the nature of the script.

tradeshort = strategy.position_size > 0 
tradelong = strategy.position_size < 0
Ll = ta.barssince(tradelong)
Sl = ta.barssince(tradeshort)

extreme = strategy.position_size > 0 ? ta.highest(high, (Ll > 1 ? Ll : 1)) :   strategy.position_size < 0 ? ta.lowest(low, (Sl > 1 ? Sl : 1)) : na 
1 Answers

FIND!!! the max bar back in TV is 5000

strategy(title="strategy", max_bars_back=5000)
Related