This example shows how you can change the formula of the Commodity Channel Index to use Open prices instead of Close prices.

What the study does now

The Commodity Channel Index calculates a so-called typical price (TP) for the given period, subtracts a simple moving average (SMA) of the typical price, and divides the result by its mean deviation (MD) times a constant factor. Written as a formula, this could read:

Where the typical price is calculated as:

The result is then plotted on the screen as a line. Additionally, two overbought/oversold level lines are added.

What you want

For some reason, you believe that the Commodity Channel Index would work better for you if it used the Open prices instead of the Close prices in its calculation of the typical price. Additionally, you feel that the constant factor should be changed from 0.015 to 0.020 to produce better results.

How to code it

▪†††††† Save the Commodity Channel Index under the name cci_open. See Basing Your Study on an Existing Study.

▪†††††† Take a look at the source code. You might notice that for some reason, it seems that no calculations are done here at all Ė only a line and an area study object are drawn.

Function cci_open
Returns Nothing;

†† Security Source;
†† Numeric period;
†† Numeric upperLevel;
†† Numeric lowerLevel;

†† If IsValid(Source.High) And IsValid(Source.Low) And IsValid(Source.Close) Then
††††††††† DrawLine("CCI", funcCci(Source, period));
††††††††† DrawLine("#max", upperLevel);
††††††††† DrawLine("#min", lowerLevel);

▪†††††† Now take a closer look at the drawing function DrawLine that draws the CCI line. You can see that in the part where the drawing function is told what to plot on the screen, another function with the name funcCci is called. This function funcCci is the place where the actual calculation of the Commodity Channel Index is done.

DrawLine("CCI", funcCci(Source, period));

▪†††††† A function call is a simple way to use the calculations of another (helper) program in your own program. When you call a function, you have to know what kind of input parameters it expect and what result it will return to you. In this case, the function funcCci expects two parameters Source and period, and will return the calculated number for the Commodity Channel Index. It not always necessary to separate the calculation and drawing part of a study - in this case it was done because some other studies also make use of the underlying calculations for the CCI, such as the signal CCI AvgCross. For more information about function calls, see Function Call.

▪†††††† Because you want to change the calculation and not the drawing part of this study, you now have to jump to the funcCci function. You can do this by clicking on funcCci to position your cursor there, and then clicking the button Go To at the bottom of the window. The function funcCci opens in a separate Source Editor window.

▪†††††† As you want to change this function too, save it now under the name funcCci_open. The new function will open in an additional tab.

▪†††††† Take a look at the source code. It is quite complex, but donít be intimidated by this. You will only need to identify the parts that have to be changed Ė in this case, you donít have to understand all details of the program to do this.

▪†††††† We will start with changing the constant factor from 0.015 to 0.020. This is pretty straightforward Ė you will only have to find the part where this factor is used and change it. In our case, this is the line at the bottom of the program, were the result of the function is calculated. Change 0.015 to 0.020:

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

▪†††††† The next part is a bit more tricky. You have to identify were in the program the typical price is calculated, so that you can change the price used from Close to Open. First, letís take a look at the line that calculates the result again: If you compare this with the mathematical formula for the Commodity Channel Index that we introduced at the beginning, you can see that the typical price is called X in the program code. Now, you will look into the code to see where X is calculated (look for X = ...). You can find this line near the beginning of the execution section:

X = (Source.High + Source.Close + Source.Low)/3;

▪†††††† Now you only have to change the Close to the Open price:

X = (Source.High + Source.Open + Source.Low)/3;

▪†††††† We have now finished our job of changing the way the Commodity Channel Index is calculated. Now, we have to return to the program where the drawing is done. We there need to tell the study that it should use our new calculation method instead of the old one. First, click Save to save your changes to the new function funcCci_open that you have created.

▪†††††† You should now return to the source code of the function cci_Open. In the line where the function funcCci was called, change the function call so that it now uses your new function funcCci_open:

DrawLine("CCI", funcCci_open(Source, period));

▪†††††† We are now almost finished. One last thing remains to be done: We need to check whether the change we have just done will affect another part of the program. The easiest way to do this is to scan the code for the occurrence of Source.Close (since this is the part that we have changed). You will find the following code line:

If IsValid(Source.High) And IsValid(Source.Low) And IsValid(Source.Close) Then

▪†††††† Now we will need to take a deeper look at what this code does to get a feeling if the changes you have done will affect this procedure. You can see that this is an If statement (see If Conditions for more). If statements check if a condition is true before they go on with executing the program. The parameter Source.Close is a part of this condition Ė the condition checks if the Close price is valid before it continues with executing the program. The question is, does this affect our program? Of course: You are not using the Close price anymore in your calculation, but instead you use the Open price. So it would be more meaningful to check instead if the Open price is valid (since you donít need the Close price anymore). So, you should change this line to reflect this:

If IsValid(Source.High) And IsValid(Source.Low) And IsValid(Source.Open) Then

▪†††††† You have now finished all the necessary changes. Check the syntax of your adapted code by clicking on Check Syntax, save it and test your new study on a chart. The resulting line drawn should be different from the "normal" Commodity Channel Index. You can insert both studies into one pane to see the differences.