Search code examples
pine-scriptpine-script-v5trading

Pine Script Volume Indicator Not Quite Working


I've wanted to create an indicator that will show me when the current candle's buying or selling volume is higher than the previous (x) candles.
I am pretty sure that I have everything, somewhat correct, but I am not plotting anything on my chart.
I tried using labels before I wanted to change the bar color instead).

Preferably, I'd like to know if the current bars buying volume is higher than any of the previous bars buying volume.
Same with selling volume.

I honestly do not have a clue how to do that though. I've pasted the script below if someone could take a look at it please. If anyone could help me to figure out buying and selling volume intra-bar, that'd be cool too. I am not sure if it's even possible or not. Thank you everyone!

//@version=5

indicator("Volume", overlay=true)

// Inputs
lookback = input(20, "Lookback Period", tooltip = "The number of previous candles to check for volume")
bullishcolor = input(color.green, "Bullish Color")
bearishcolor = input(color.red, "Bearish Color")

// Variables
highestvolume = ta.highest(volume, lookback)
bullishvolume = false
bearishvolume = false

// Calculations
if volume > highestvolume and (close>=open)
    bullishvolume := true
if volume > highestvolume and (close<open)
    bearishvolume := true

// Plot volume indication
barcolor(bullishvolume ? bearishcolor : bearishvolume ? bearishcolor : na)

I've tried plotting as label.new() and that did not work. I've also tried using all volume instead of splitting them up between bullish and bearish, and still have not had luck


Solution

  • When you test from the highest bars, it include the current bar.
    So you will never have this condition met :

    volume > highestvolume 
    

    Because highestvolume is always at least equal to volume.

    You should change your if condition with :

    // Calculations
    if volume >= highestvolume and (close>=open)
        bullishvolume := true
    if volume >= highestvolume and (close<open)
        bearishvolume := true
    

    I tested it and see bar colored when the condition is met.