The Dynamic Market Lab The Dynamic Market Lab right border
Knowledge Base » Examples and Formulas » Loop Conversion 1

This is an example that illustrates the steps of the General Loop Conversion Technique:

Here is the loop (in EasyLanguage code) that we will be converting:
Inputs: MP((H+L)/2);

{The output of CyclePeriod() doesn't matter for this example, it simply represents a period value that varies from bar-to-bar}

periods = CyclePeriod(MP, .18, .1, 1.1);

temp = 0;
For count = 0 to periods - 1 begin
temp = temp + (periods - count) * (MP[count]);
End;

output = temp / (periods) * (periods+1) / 2;

Step 1.) Separate the loop into as many individual loops as possible, and convert each loop separately.

This loop cannot be separated further.

NOTE: It is often times helpful to include calculations outside of the loop (for example, the last line in the formula above) when trying to convert a loop. This last step is usually a division that calculates an average or something similar. As you will soon see, this can be very important for step 3, because it results in output that represents a common indicator. However, there will be other times when it will help to ignore any extra calculations after the loop. You may find it helpful to try it both ways.

Step 2.) If the loop uses a "varying" (non-constant) counter variable, replace it with a constant, preferably a small number, like 3 or 5 (here we use 4).

Inputs: MP((H+L)/2);

periods = 4;

temp = 0;
For count = 0 to periods - 1 begin
temp = temp + (periods - count) * (MP[count]);
End;

output = temp / 10;

Step 3.) Create an equivelant function in Metastock for the static version of the loop (i.e., a loop with a constant counter).

By using algebraic substitution and stepping through the loop, we can see that this "static version" of the loop in Metastock can be computed as follows:
output := (4*MP() + 3*Ref(MP(),-1) + 2*Ref(MP(),-2) + Ref(MP(),-3)) / 10;

Step 4.) Analyze the logical output of the formula and try to convert it into existing Metastock indicators.

In this example, we find this to be of particular help, because after a close look, we find the the loop is really nothing more than a simple 4-bar Weighted Moving Average (WMA) of the Median Price! It is often the case that loops that are algebraically complex, turn out to be simple indicators.

Thus we now have:
Mov(MP(), 4, Weighted);

Step 5.) Compare the output from Steps 3 and 4, and make sure they are equal.

This step gives us an always helpful error checking mechanism.

Step 6.) Finally, replace the calls to fixed-period Metastock indicators with calls to the ASI functions, and replace the fixed length with the original "variable loop counter" function.

This is still a weighted moving average, but it is now an Adaptive weighted moving average! Which, of course, can be computing in ASI as follows:
{ periods should match the output of the CyclePeriod() function in the original EasyLanguage formula}
periods := ExtFml("ADSI.CyclePeriod", MP(), .18, .1, 1.1);
ASI.ExtFml("ASI.WMA", MP(), periods);