Search code examples
pine-scriptpine-script-v5

Is there some way to only see a label when the candle is bullish/bearish?


I can't find a way to hide/show a label depending on whether the candle is bullish or bearish. Is there any way to do it?

This code shows on labels the distance between the high and the close, and between the low and the close. But I only want to see the label below the candle when the candle is bullish and the label above the candle when the candle is bearish.

//@version=5
indicator(title='PIPS', overlay=true)

// Adquiere la informacion de Pips del simbolo actual
pipSize = syminfo.mintick

// Calcula la distancia desde el Bajo al cierre y desde el Alto al cierre + 20 Ticks
DistanciaDesdeBajoAlCierre = (close - low) / pipSize + 20
DistanciaDesdeAltoAlCierre = (high - close) / pipSize + 20

// Crea las etiquetas y variables
VelaCerrada = label.new(x=bar_index, y=low, text=str.tostring(DistanciaDesdeBajoAlCierre, '#.######'), textcolor = color.white, color=color.rgb(76, 175, 79, 100), style=label.style_label_up, size=size.small)
VelaAbierta = label.new(x=bar_index, y=high, text=str.tostring(DistanciaDesdeAltoAlCierre, '#.######'), textcolor = color.white, color=color.rgb(255, 82, 82, 100), style=label.style_label_down, size=size.small)

// Elimina las etiquetas cada 2 velas
label.delete(VelaCerrada[2])
label.delete(VelaAbierta[2])



I tried with label.delete(VelaCerrada[2]) but that's just deleting the previous labels (which it's okay and that's why it's still there)


Solution

  • Your 1st Q and 'what you want' is some different objective. So if you real want to

    see the label below the candle when the candle is bullish and the label above the candle when the candle is bearish

    you just need to create one label per candle. Now your code create two labels, so you have bearish and bullish label per one candle. You need to check candle type and create label with if/else block.

    Here code sample:

    isBullish = close >= open
    if isBullish
        label.new(x=bar_index, y=low, text=str.tostring(DistanciaDesdeBajoAlCierre, '#.######'), textcolor = color.white, color=color.rgb(76, 175, 79, 100), style=label.style_label_up, size=size.small)
    else
        label.new(x=bar_index, y=high, text=str.tostring(DistanciaDesdeAltoAlCierre, '#.######'), textcolor = color.white, color=color.rgb(255, 82, 82, 100), style=label.style_label_down, size=size.small)