I would like to create a moving average exclusively of green candles on the close.
A script that I found (and placed below) by another user, unfortunately, returns false results. The current result is a straight line that correctly connects the green if consecutive, but if there are one or more red candles in the middle, the same line is horizontal and broken because it keeps the last value of the previous green closure. How can I have a line that connects each green closure without interruption?
in the image the result of the script enter image description here
averagePastgreenCandles(amount) =>
// number of counted candles
candles = 0
// current average
sum = 0.0
// check if the number of candles so far has exceeded the amount of bars on the chart
if bar_index > amount
// start counting with a limit of the current bars in chart
for i=0 to bar_index - 1
// confirm if the candle is green
if close[i] > open[i]
// add the average
sum := sum + close[i]
// add count of the candles we have counted
candles := candles + 1
// check if we have reached the amount of the candles that we want
if candles == amount
//close the loop
break
// return the average
avarege = sum/amount
smagreenbar = averagePastgreenCandles(1)
plot(smagreenbar, color=color.green, linewidth=2)