Evox402
Evox402

Reputation: 111

Problems with "referencing lengths max_bars_back" error

I'm currently working on a new script but encounter some strange behaviour depending on what stock or index I'm currently working on.
My new script seems to work totally fine on some stocks but throws the following error on other stocks, indexes or ForEx pairs:

Pine cannot determine the referencing length of a series. Try using max_bars_back in the study or strategy function

I already read the entry in the Pine documentation and I also tried adding "max_bars_back" to the study without any results.

So I broke my script down for demonstration purposes and now have a version that will throw this error on every stock.

Here is the code:

//@version=4
study("StackOverflow_Test", overlay = true, max_bars_back=100)

var int loopRange = 0                  
var int trendState = 2                  

var bool firstBreak = true

var float hp_last = low                   
var float hp_current = high               

float tempHighest = 0                   
int tempHighPosition = 0                

var bool waitForConfirmation = false

float source = close              // Used Price

for i = loopRange to 0
    if ( source[i] > tempHighest )
        tempHighest := source[i]
        tempHighPosition := i

if ( source > hp_current )          
    waitForConfirmation := true     
    hp_current := source            

if ( waitForConfirmation )                  
    if ( firstBreak )                   
        firstBreak := false             
        if ( trendState == 1 )          
            trendState := 2
        else                            
            trendState := 0
        hp_last := hp_current[1]           
    
    if ( tempHighPosition > 3 )       
        loopRange := 3                
        waitForConfirmation := false                    
        firstBreak := true                              

loopRange := loopRange + 1                  

color trendLineColor = color.green
if ( trendState == 0 )
    trendLineColor := color.green
else if ( trendState == 1 )
    trendLineColor := color.red
else
    trendLineColor := color.silver

// Plot trendline
plot(source, color = trendLineColor)

If I remove the first for-loop or the lines:

if ( trendState == 1 )          
    trendState := 2
else                            
    trendState := 0

The error won't occur.

I thought the error means that I try to access data out of bounds but I fail to see where exactly...
Does anyone of you have an idea what line causes this behaviour?

Upvotes: 0

Views: 2589

Answers (2)

Don Coder
Don Coder

Reputation: 556

Instead of loop you can try the following code

solution 1:

tempHighest = highest(source, loopRange)
tempHighPosition = highestbars(source, loopRange)

However, these builtin functions may loop too. The following code will not loop

solution 2:

var tempHighest = 0.0
var tempHighPosition = 0

if (source > tempHighest)
    tempHighest := source
    tempHighPosition := 0
else
    tempHighPosition := tempHighPosition + 1

Upvotes: 0

Andrey D
Andrey D

Reputation: 1714

The problem is here:

for i = loopRange to 0
    if ( source[i] > tempHighest )
        tempHighest := source[i]
        tempHighPosition := i
...              
loopRange := loopRange + 1  

you go through all available history on every bar for find Highest High. This is inefficient and give you the error "referencing length of a series". If you describe final goal of your script we can offer more efficient workaround.

Upvotes: 2

Related Questions