Adding Parameters to a Study (Moving Average Simple Cont’d)

We will revisit the three-line Moving Average Simple we created earlier (see Drawing More Lines on the Screen (Moving Average Simple)) and add a parameter to the program so that we can draw the additional two lines at any percentage we like.

What the study does now

The modified Moving Average Simple calculates a moving average (sum of a number of values divided by their number) and then draws three lines on the screen:

▫       A central line for the moving average

▫       A line 10 percent above the central line

▫       A line 10 percent below the central line

What you want

You want to change the program so that you do not have to always use the fixed value of 10 percent, but can draw the additional lines at any percentage. You want to be able to change the percentage on the Parameters tab of the Insert Study dialog.

How to code it

▪       Save the three-line Moving Average Simple you have created before under the name movs3_percentage. See Basing Your Study on an Existing Study.

▪       You should already be familiar with the source code of the study:

Function movs3_percentage
Returns Numeric;

Parameters
Numeric Field source;
Numeric period;
Numeric shift;

Begin
Result = Average(source, period) * (1 + shift /
100);
DrawLine("MovS_above", 1.1 * Result);
DrawLine("MovS", Result);
DrawLine("MovS_below", 0.9 * Result);
End.

▪       You want to establish and use a parameter in the program called percentage. As you know, you are not allowed to use a parameter without declaring it first. To do this, you have to edit the function header (or declaration section) of your program. Add a parameter named percentage of the type Numeric. Your parameters list should now read:

Parameters
Numeric Field source;
Numeric period;

Numeric shift;
Numeric percentage;

▪       We can now use this parameter in the program. We want to replace the part where we explicitly state the 10 percent value and instead use the parameter value. You now have to decide which values you expect the user to type into the parameter percentage. It would of course be most convenient if the user just types "20" if he wants 20 percent lines, and not "0.20", which is not very intuitive. So, you have to replace the hard-coded value 1.1 that you use now with a formula that can take the expected value of the parameter percentage (for example 20) and convert it to the value that the computer needs (for example 1.2):

DrawLine("movs_above", (1 + percentage / 100) * Result);

▪       You now do the same with the lower line:

DrawLine("movs_below", (1 - percentage / 100) * Result);

▪       And this is it already for the source code changes. Check the syntax of your adapted code by clicking on Check Syntax and save the new study.

▪       Now we should set some default values for the new parameter. In the Study Editor, go to the Parameters tab of your study. You should see your new parameter percentage there. Enter 10 as the default value, 0 as the lower limit and 100 as the upper limit. You can also enter a description for the parameter.

▪       Save your study again. Now you can try to insert it on a chart. Try different settings of your new parameter on the Parameters tab of the Insert Study dialog and see how it affects the results!

Note             If you want some further practice on creating parameters for a study, you could take another go at the Commodity Channel Index (Changing the Formula of a Study (Commodity Channel Index)): Try if you can change the program so that you can define the constant factor (which was 0.015 originally, then changed to 0.020 in the code) as a parameter, so that you can change it more easily later on. You will find the solution below.

▪       In the function funcCci_open, add a parameter called factor of the type Numeric.

▪       Use this parameter in the line where the result is calculated instead of the fix value:

Result = (X - avgX) / (factor * avgMD);

▪       In the function cci_open, you have to add the parameter factor too and change the source code so that this new (third) parameter is included in the function call:

DrawLine("CCI", cci_open(Source, period, factor));

▪       Don't forget to assign some default values to your new parameter (default, maximum and minimum values)! Be aware that depending on the configuration of your operating system, you might have to use a decimal comma when entering rational parameter values on the Parameters tab (for example 0,015).