Hi,
I am trying to plot volume at exact price, not distribution. I took inspiration from various codes mentioned in this post. The error I am getting is also mentioned below. My matrix variable is holding correct values as in quote editor. Please suggest me where is the mistake?.
SetBarsRequired(sbrAll,sbrAll);
bi=BarIndex();
styl=ParamStyle("Line Style",styleStaircase,maskAll);
Camarcol=ParamColor("Line Color",colorYellow);
Show=ParamToggle("Show Values","No|YES",0);
mode=0;H1=0;L1=0;
{
MB=Day()!=Ref(Day(),-1);
ME=Day()!=Ref(Day(),1);
H1=ValueWhen(MB,Ref(HighestSince(MB,C,0),-1));
L1=ValueWhen(MB,Ref(LowestSince(MB,C,0),-1));
Plot(IIf(ME,1,Null),"",colorYellow,styleHistogram|styleOwnScale|styleThick,0,1);
stbar=ValueWhen(mb,bi);
enbar=ValueWhen(me,bi);
nobars=BarsSince(mb);
event =Day();
event =Day()!=Ref(Day(),1);
Hv=ValueWhen(event,HighestSince(Day()!=Ref(Day(),-1),C,1));
Lv=ValueWhen(event,lowestSince(Day()!=Ref(Day(),-1),C,1));
}
Plot(H1,"H1",Camarcol,styl);
Plot((H1+L1)/2,"Mid",colorBlue,styl);
Plot(L1,"L1",Camarcol,styl);
GfxSetZOrder(-3);
GfxSetZOrder(zord);
// VAP every month with 240 days historical data. Ref: https://forum.amibroker.com/t/volume-price-distribution/23199/2
// code based on http://www.amibroker.com/guide/afl/pricevoldistribution.html and https://forum.amibroker.com/t/how-to-make-this-code-that-counts-the-number-of-times-a-certain-event-happened-in-the-past-more-elegant/14385/3
// Many thanks for fxshrat for his input!
mxvol[0]=0;maxvol[0]=0;Volpoc[0]=0;k=0;Volpocv[0]=0;VPP[0]=0;VPV[0]=0;fiba=0;enba=0;
cnt=0;
bv = Status("barvisible");
GfxSetCoordsMode( 1 );
GfxSelectPen( Volcol );
function DSVolPro(startbar,endbar)
{
nobs=endbar-startbar;//number of bars in a period
dsmatrix=Matrix(nobs,2,Null);// close and volume
for(n=startbar;n<endbar;n++)
{
//_TRACE("Counter = "+n);
dsmatrix[n][0]=C[n];
dsmatrix[n][1]=V[n];
}
totvol=0;
for(n=startbar;n<endbar;n++)
{
totvol=totvol+dsmatrix[n][1];
}
for(n=startbar;n<endbar;n++)
{
dsmatrix[n][1]=(dsmatrix[n][1]/totvol)*100;
}
return dsmatrix;
}
for ( i = 0; i<BarCount; i++ )
{
if(i==0 || stbar[i])
{
fiba[i]=bi[i];
}
else
{
fiba[i]=fiba[i-1];
}
if(i==BarCount-1 || enbar[i])
{
enba[i]=bi[i];
}
if ( (event[i] && bv[i]) )
{ enba[i]=enbar[i];
x=(hv[i]-lv[i]);
//enbar[i]=cnt-1;
maxivol=0;chkvol=0;totvol=0;
mx=DSVolpro(i-(enba[i]-fiba[i]),i+1);
grlen=(enba[i]=fiba[i])/3;
bins = MxGetSize( mx, 0 );
for(j=0;j<bins;j++)
{
totvol=totvol+mx[j][1];
}
for( j =0; j <bins; j++ )
{
price = mx[ j ][ 0 ]; // price level
relvolume = mx[ j ][ 1 ]; // relative volume 0..1
maxivol=Max(maxivol,relvolume);
relbar = (relvolume) *grlen;
GfxMoveTo(i-(enbar[i]-stbar[i]), price );
//GfxMoveTo(enba[i], price );
GfxLineTo( i-(enbar[i]-stbar[i]) + relbar, price );
//GfxLineTo( enba[i] + relbar, price );
_TRACE("Event = "+price);
}
}
if ( i==BarCount-1 )
{
maxivol=0;chkvol=0;totvol=0;
enba[i]=bi[i];
xx=NullCount(fiba,3);
fiba[i]=IIf(NullCount(fiba,3)>=enba[i],0,fiba[i]);
mx=DSVolpro(i-(i-fiba[i]),i+1);
grlen=(enba[i]-fiba[i])/3;
bins = MxGetSize( mx, 0 );
_TRACE("matrix = "+MxToString(mx));
for( j =0; j <bins; j++ )
{
price = mx[ j ][ 0 ]; // price level
relvolume = mx[ j ][ 1 ]; // relative volume 0..1
maxivol=Max(maxivol,relvolume);
relbar = (relvolume) *grlen;
//GfxMoveTo(i-(enbar[i]-stbar[i]), price );
GfxMoveTo(i-(enba[i]-fiba[i]), price );
//GfxMoveTo(enba[i], price );
//GfxLineTo( i-(enbar[i]-stbar[i]) + relbar, price );
GfxLineTo( i-(enba[i]-fiba[i]) + relbar, price );
//GfxLineTo( enba[i] + relbar, price );
_TRACE("End Bar = "+price);
}
}
}
RequestTimedRefresh(1,True);
