Dear Ashvin,
I am giving the Larry Williams' Acc/Disr formula as AmiBroker AFL here. You may try to write the Meta Stock code from this.
I am quoting here what Larry Williams has written in his book "The Secret of Selecting Stocks for Immediate and Substantial Gains", published by Windsor Books, Brightwaters, New York. This book is available as e-book.
... there is a battle ... every day on the floor of the Exchange between buyers and sellers in each stock.... The battle between the buyers and sellers ends each day with the final bell. Someone has won that round. The next day it is a new battle, but at the end of each day we can sit back and see who was the winner.
.... the daily high for the stock is established by the bulls. The distance from the morning's open to the daily high shows the power of the bulls. ... The Bears show their daily power by driving prices down. Hence, the distance from morning's opening to the low point represents their pressures, or the amount of selling.
.... accumulation and distribution is calculated by finding the difference between the stock's high and low for the day. We then find the difference between close and open. ... The next step is to divide the close-to-open distance by the high-to-low distance. This resulting figure is percentage of net buying or selling for the day.
....
In a concise form the equation is this: Close minus the opening, divided by the high minus the low times volume = net buying or selling for the day (net daily A/D). ... On the days when there is more buying the net daily A/D figure is added to yesterday's figure. On days of more selling the net daily A/D figure is subtracted from yesterday's figure.
Based on my understanding of the above I have written the Accumulation/Distribution AFL for AmiBroker as follows:
Code:
DR = H-L; //Day's Range
DC = C-O; //Day's Change
F = 0; //Factor to calculate Acc/Dist. Initialised with zero.
WAD = 0; //William's Acc/Distr Value. Initialised with zero.
If(H[0] == L[0]) //In case first day's Hi and Lo are same
F[0] = 1; //Factor is taken as 1.
else //Otherwise
F[0]=DC[0]/DR[0]; //Calculate the ratio and assign it to factor for first day.
WAD[0]=V[0]*F[0]; //First day's Acc.Dist
for (i=1;i<BarCount;i++) //For second day onwards
{
if (H[i]==L[i] AND O[i]<C[i-1]) F[i]=-1; //Lower circuit
else
if (H[i]==L[i] AND O[i]==C[i-1]) F[i]=F[i-1]; //Very rare case
else
if (H[i]==L[i] AND O[i]>C[i-1]) F[i]=1; //Upper circuit
else
F[i]=DC[i]/DR[i]; //Normal case
WAD[i]=V[i]*F[i]
}
Plot(Cum(WAD),"AccDist",colorRed,styleOwnScale+styleLine);
A few points explained:
1. When Volume is large number, say in lakhs, then divide the volume by a suitable number such as 1000 so that you get a smaller number which is easy to manage. The final A/D graph is not affected by this division.
2. When the stock is locked in lower circuit throughout the day, the High and Low are same and the Open would be lower than previous Close. In this case the factor F is taken as -1.
3. When the stock is locked in upper circuit throughout the day, the High and Low are same and the Open would be higher than previous Close. In this case the factor F is taken as 1.
4. In very rare cases the stock opens at previous Close and High and Low are same. In this case the factor F is taken to be same as previous day's value.
5. In the last line, the plot function uses styleOwnScale because we don't want the A/D graph to distort the price chart when it is used as an indicator in the same graph.
If the above claculation steps are used you will never get 'Divide by zero' error. I hope this will help you overcome the problem you are facing. Unfortunately I am not in a position to write the AFL in MS code. I hope the explanations will help you write your own MS code. Or somebody else may be able to write it for you and help.
As far as the method of using this indicator and its interpretation, you may refer to the book mentioned above. The space is not sufficient to explain it here. I may have to write a separate post for that.
Regards
-Anant