A code for identifying most traded price in stock

Hey guys.
so the code I want to write is basically the idea similar to volume at price concept but only price-wise. I want to know what price or price range the stock has been traded the most.
For the sake of coding and my poor knowledge, I came up with the idea to take the highest and lowest price of the stock history and break it into 5 price ranges. and count the times each candle occur in those ranges and calculate the percentage and the range the stock been traded the most.
but I know there is something wrong with my coding and that is, I don't know how the code calculate the price range from the last bar history and start counting price occurence in ranges from first bar.

_SECTION_BEGIN("price frequency");
// defining 5 price ranges
Midv= Highest(Close) - Lowest(Close);
midc= midv/5;
f1= Lowest(Close) + midc;
f2= f1 + midc;
f3= f2 + midc;
f4= f3 + midc;

onefifth= C >= Lowest(Close) AND C <= f1;
twofifth= C > f1 AND C <= f2;
threefifth= C > f2 AND C <= f3;
forthfifth= C > f3 AND C <= f4;
fifth = C > f4 AND C <= Highest(Close);

//calculating price occurence frequency in the ranges
xa = Cum(onefifth);
xb = Cum(twofifth);
xc = Cum(threefifth);
xd = Cum(forthfifth);
xe = Cum(fifth);
xs= xa + xb + xc + xd + xe;
//which range is bigger
xabigger=  xa > xb AND xa > xc AND xa > xd AND xa > xe;
xbbigger=  xb > xc AND xb > xa AND xb > xd AND xb > xe;
xcbigger=  xc > xb AND xc > xa AND xc > xd AND xc > xe;
xdbigger=  xd > xc AND xd > xa AND xd > xb AND xd > xe;

highestfifth = iif(xabigger,xa,iif(xbbigger,xb,iif(xcbigger,xc,iif(xdbigger,xd,xe))));
hfp = (highestfifth/xs) * 100;
hfr = WriteIf(highestfifth == xa," "+Lowest(C)+" to "+f1+" ",WriteIf(highestfifth == xb," "+f1+" to "+f2+" ",WriteIf(highestfifth == xc," "+f2+" to "+f3+" ",WriteIf(highestfifth == xd," "+f3+" to "+f4+" ", " "+f4+" to "+Highest(C)+" "))));

title = "  "+hfp+" percent of time traded in "+hfr+" price range ";

_SECTION_END();

This can be achieved using AFL Function Reference - PRICEVOLDISTRIBUTION with 1 as a volume, but keep in mind that getting AFL help on this forum at this level requires license to be verified:

1 Like