[SOLVED] Pine-script for loop inside function

Issue

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

Solution

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

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

Answered By – vitruvius

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *