0

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)
e2e4
  • 3,428
  • 6
  • 18
  • 30

1 Answers1

0

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

e2e4
  • 3,428
  • 6
  • 18
  • 30
  • Thank You for Your answer. If I change the variable type, is it gonna work on realtime bars? The if statement can change from true to false until the bar is closed, therefore the variable reset will be required sooner than on new bar. Or am I understanding it wrong? – Nebiros QQ Feb 14 '23 at 22:04
  • Yes, it will work on rt bars. In case the if statement will change during real-time, the positive/negative_candles will also rollback to the initial state - 0. – e2e4 Feb 15 '23 at 13:05