Search code examples
pine-scriptpine-script-v4pine-script-v5

Count Up tick volume and down tick volume in a real time bar without security function


I want to build a pine script which will count total up ticks volume and total down ticks volume in a real time daily bar. How do I do that? I don't want to use security function because it has limitations.


Solution

  • This can be done by making use of varip to declare your variables.
    Please read through the documentation on varip to fully understand how it works.
    Then have a look at some open source example scripts to see how it's used.

    Example of how it could be implemented:

    //@version=5
    indicator("Intrabar Tick Volume", "ITV", overlay=true)
    
    varip int   tickDirection   = na
    varip int   tickCount       = 0
    varip float volumeUp        = 0
    varip float volumeDown      = 0
    varip float prevTickVolume  = 0
    varip float prevTickClose   = 0
    
    if barstate.isnew
        tickCount       := 0
        volumeUp        := 0
        volumeDown      := 0
        prevTickVolume  := 0
        prevTickClose   := close[1]
    else
        tickCount       += 1
        tickDirection   := close > prevTickClose ? 1 : close < prevTickClose ? -1 : 0
    
        if tickDirection == 1 //uptick
            volumeUp += volume - prevTickVolume
        else if tickDirection == -1 //downtick
            volumeDown += volume - prevTickVolume
    
        // Save data of current tick to use as previous tick values on next tick.
        prevTickClose   := close
        prevTickVolume  := volume
    
    plotchar(tickCount, 'tickCount', '')
    plotchar(close, 'close', '')
    plotchar(tickDirection, 'tickDirection', '')
    plotchar(volume, 'volume', '')
    plotchar(volumeUp, 'volumeUp', '')
    plotchar(volumeDown, 'volumeDown', '')