Problem with a simple indicator that indicates a positive/negative candle

226 Views Asked by At

My goal was to create a simple indicator that places a marker above or below the candle, depending on whether the candle is negative or positive. Positive candle = candle that closes above 50%. A negative candle closes below 50%. Although the code seems clear and simple to me, on the chart it then marks all candles, not just the negative and positive ones. Thanks for the advice on where the problem is.

// Positive / negative candle 
center = low + ((high - low) / 2)
var positive_candle = 0
var negative_candle = 0

if (close > center)
    positive_candle := 1

else
    if (close < center)
        negative_candle := 1

    else
        positive_candle := 0
        negative_candle := 0
            
plotchar(series = positive_candle, title = "Positive Candle", char = "+", color = color.yellow, location = location.abovebar)
plotchar(series = negative_candle, title = "Negative Candle", char = "-", color = color.yellow, location = location.belowbar)
1

There are 1 best solutions below

2
e2e4 On BEST ANSWER

It's better to reset the positive/negative_candle variables to 0 on every bar instead of resetting them in a nested statement:

//@version=5
indicator("My script", overlay = true)

// Positive / negative candle 
center = low + ((high - low) / 2)

plot(center, style = plot.style_stepline)

positive_candle = 0
negative_candle = 0

if (close > center)
    positive_candle := 1

else if (close < center)
    negative_candle := 1
            
plotchar(series = positive_candle, title = "Positive Candle", char = "+", color = color.yellow, location = location.abovebar)
plotchar(series = negative_candle, title = "Negative Candle", char = "-", color = color.yellow, location = location.belowbar)

enter image description here