How to get the values of PriceVolDistribution() bins using gfxTextOut or exploration or in title

@Sandy, please, review the documentation of the PriceVolDistribution() function.

If you need to see a distribution for the last few days, you should change your code accordingly.

Play a little with this modified snippet (where I also added some user-defined param to show the different results you get using the "absolute" vs. the "relative" parameter) and to change the bins and bar range to include (either using visible bars or the last few bars).


// The absolute parameter decides whenever returned volumes are absolute sum or relative (0...1) values.
absoluteMode = ParamToggle( "Print Volume values as", "Relative|Absolute", 0 );
numBins = Param( "Number of bins", 7, 2, 10, 1 );
lbBars  = Param( "Number of lookback bars", 10, 2, 50, 1 );
lastBarsMode = ParamToggle( "Distribution matrix on", "Visible Bars|Last bars", 0 );
vapColor = ParamColor( "Color used for text and VAP lines", colorGold );

if( lastBarsMode )
    SetBarsRequired( sbrAll, sbrAll );

bi = BarIndex();

if( lastBarsMode ) // the calculation is done based on the last bars
    lvb = LastValue( bi );
else              // the calculation is done based on the visible bars
    lvb = LastVisibleValue( bi );

fvb = Max( 0, lvb - ( lbBars - 1 ) );

// calculate the Volume distribution in the selected mode
mx = PriceVolDistribution( H, L, V, numBins, absoluteMode, fvb, lvb );
bins = MxGetSize( mx, 0 );

// additional loop used to calculate the absolute percentages / total volume
totVolume = 0;
totPercent = 0;

for( i = 0; i < bins; i++ )
{
    if( absoluteMode )
    {
        absVolume = mx[ i ][ 1 ];
        totVolume += absVolume;
    }
    else
    {
        relPercent = mx[ i ][ 1 ]; // relative volume in a range 0..1
        totPercent += relPercent;
    }
}

// Low-level printout of values
GfxSelectFont( "Tahoma", 10 );
GfxSetBkMode( 1 );
GfxSetTextColor( vapColor );
lc = 1; // line counter used to calculate the y coordinate

for( i = 0; i < bins; i++ )
{
    // Print Price level
    price     = mx[ i ][ 0 ]; // price level
    s = StrFormat( "%02.0f - Price: %2.2f", i + 1, price );
    GfxTextOut( s, 10, 20 * lc );
    lc++;

    // Print corresponding Volume
    if( absoluteMode )
    {
        absVolume = mx[ i ][ 1 ]; // this is zero for absolute mode when volume data is zero
        pctVolume = IIf( totVolume == 0, 0, absVolume / totVolume );
        s = StrFormat( "%02.0f - Volume: %1.0f (%2.2f%%)", i + 1, absVolume, pctVolume * 100 );
        GfxTextOut( s, 10, 20 * lc );
    }
    else
    {
        relVolume = mx[ i ][ 1 ]; // relative volume in a range 0.1 - multiply it by 100 to get %
        // treat {empty} values as zero
        relVolume = IIf( relVolume, relVolume, 0 );
        totPercent = IIf( totPercent, totPercent, 0 );
        absPercent = IIf( totPercent == 0, 0, relVolume / totPercent );
        s = StrFormat( "%02.0f - Rel. Volume: %2.2f%% - Absolute %2.2f%%", i + 1, relVolume * 100, absPercent * 100 );
        GfxTextOut( s, 10, 20 * lc );
    }

    lc++;
}

if( absoluteMode )
    GfxTextOut( StrFormat( "-- Total Volume: %1.0f", totVolume ), 10, 20 * lc );

// Plot the "relative" distribution matrix, as per the examples
// To make it simple recalculate it, if needed, with relative parameter
if( absoluteMode )
    mx = PriceVolDistribution( H, L, V, numBins, false, fvb, lvb );

GfxSelectPen( vapColor );
GfxSetCoordsMode( 1 );
bins = MxGetSize( mx, 0 );

for( i = 0; i < bins; i++ )
{
    price = mx[ i ][ 0 ];     // price level
    relvolume = mx[ i ][ 1 ]; // relative volume 0..1
    relbar = relvolume * ( lvb - fvb + 1 );
    GfxMoveTo( fvb, price );
    GfxLineTo( fvb + relbar, price );
}

Plot( C, "Price", colorDefault, styleCandle );
SetChartOptions( 0, chartShowDates );

It is not clear to me how you may use these values in your strategies, but I hope the example will give you some hints to go ahead.

In any case, there are (at least) a couple of previous threads where the PriceVolDistribution() function is used in a very clever way (I recommend you to study the supplied code):

11 Likes