By calculating the cumulative flow of money into and out of an asset, the Accumulation Distribution Line measures buying and selling pressure that could drive security prices higher or lower. In this post, I will show you how to create the Accumulation Distribution Line using the custom indicators tool of QuantShare. I have chosen this indicator because its calculation requires that we use a loop. We have not seen yet how to implement basic For Loops in this series but this should be done by the end of this post. Introduction The Accumulation Distribution Line is a technical analysis indicator introduced by Marc Chaikin. It is a cumulative indicator based on the Money Flow Multiplier and Money Flow Volume variables. It is a cumulative indicator because its value for bar N is the sum of the previous bar value (N1) + another variable. Accumulation Distribution Line To create a custom indicator, select "Tools" then "Create Functions". Click on "Add", type "ADLine" (or any other name) then click on "Save Item". Here is the formula of the Accumulation Distribution Line indicator: Money Flow Multiplier = ((Close  Low)  (High  Close)) /(High  Low) Money Flow Volume = Money Flow Multiplier x Volume for the Period ADL = Previous ADL + Money Flow Volume Note that the above formula is just an illustration of how this indicator is calculated. It is not the implementation of the accumulation distribution line in QuantShare language or CSharp. Money Flow Multiplier: As you can see, the money flow multiplier is based on the close, high and low price series. These series can be obtained from the "cFunctions" variable. (The custom trading indicators tool explained) Example: "cFunctions.High" returns the high series. It is a vector that contains N elements, where each element corresponds to the high value of a specific bar or date. Here is the implementation of the money flow multiplier in C# or CSharp: VectorD mfm = ((cFunctions.Close  cFunctions.Low)  (cFunctions.High  cFunctions.Close)) /(cFunctions.High  cFunctions.Low); We initialize a variable called "mfm" (associates it with the "VectorD" type) and then assign it the mathematical formula. Money Flow Volume: As with the other OHLC time series, the volume is obtained by calling "cFunctions.Volume". The money flow volume consists of calculating the money flow multiplier by the volume: VectorD mfv = mfm * cFunctions.Volume; This will multiply each element of the "mfm" variable by the corresponding element of the "Volume" variable (element that belongs to the same bar/date). ADL: The accumulation distribution line (ADL) is completed by adding for each bar, the money flow volume of that bar to the ADL value of the previous bar. Why don't we just use "TA.Ref" function to compute the ADL variable? "TA.Ref" is a function that references a previous element. By using this function, the code will look like: VectorD adl = TA.Ref(adl, 1) + mfv; The problem is that this line will generate a compilation error, because the variable "adl" is not yet initialized. In fact, the compiler will first calculate the "TA.Ref" function, sum this value with "mfv" then assign the result to the "adl" variable. And even if we initialize the "adl" variable before adding the above line, the instruction will return wrong results because the variable "adl" passed to the "TA.Ref" will be empty and therefore the vector returned by the latter function (Ref) will be empty too. VectorD adl = cFunctions.CreateNumericVector(); adl = TA.Ref(adl, 1) + mfv; The solution is to create a For loop to calculate this cumulative measure: VectorD adl = cFunctions.CreateNumericVector(); adl[0] = mfv[0]; for(int i=1;i < result.Length;i++) { adl[i] = adl[i  1] + mfv[i]; } The instruction within the For loop is executed for each trading bar. Each time the ADL element with the index "i" is updated (i vary from 1 to result.Length = Vector Size) Note that "i" starts from 1 because if we started "i" at "0" we would get an execution error because "adl[i  1]" would be equal to "adl[1]" which is not correct since a vector/array always starts with an index of zero. Do not forget to assign the "adl" to the "result" variable at the end of the script. result = adl; // "result" is the variable that is returned when QuantShare calls a custom indicator Click on "Save" to compile your new indicator. How to Plot the Accumulation Distribution Line on a Chart  Right click on a chart then select "Create new pane"  Click on the "Add Indicator" icon of the new pane (First icon at the top of the pane)  Select "All Indicators" tab  Type "ADLine" then select the "Accumulation Distribution Line" indicator  Click on "OK" Complete formula: VectorD mfm = ((cFunctions.Close  cFunctions.Low)  (cFunctions.High  cFunctions.Close)) /(cFunctions.High  cFunctions.Low); VectorD mfv = mfm * cFunctions.Volume; VectorD adl = cFunctions.CreateNumericVector(); adl[0] = mfv[0]; for(int i=1;i < result.Length;i++) { adl[i] = adl[i  1] + mfv[i]; } result = adl;
