Home > front end >  Pine-script for loop inside function
Pine-script for loop inside function

Time:01-27

I'm trying to create a loop to go over the last bars and figure out what the average gain is when the hma is sloping up. But I'm getting an error I don't understand. Is it not possible to have a loop inside a function?

My code:

// Get Average Gains on long streaks
getAverageGain(hmaValue) =>
    gainStart = 0.00
    gainAmount = 0.00
    gainCount = 0
    gaining = hmaValue[1] < hmaValue
    for i=0 to 2000
        if gaining[i] and not gaining[i 1]
            gainStart := hmaValue[i]
        if gaining[i 1] and not gaining[i] and gainStart != 0.00
            gainAmount  = ((hmaValue[i 1] - gainStart) / gainStart) * 100
            gainCount   
    gainAmount/gainCount

CodePudding user response:

Problem was with the double sign....not the for loop

gainCount

Unfortunately Pine-script's error messages aren't always clear enough

CodePudding user response:

There is no operator in pinescript. Change it to gainCount := gainCount 1.

You can see the list of operators in pinescript from here.

  •  Tags:  
  • Related