Search code examples
pine-scripttradingview-api

Too many references candles in dynamic length


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 length of ta.highest(source, length). 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'.

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).

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 

Solution

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