This video is about the commodity channel index.
The commodity channel index is an oscillator and it will tell you if something is overbought or oversold.
This is one of the oscillators I like because it is very easy to understand.
It has one hundred plus and a one hundred minus line here and whenever the value rises above the upper line, it’s overbought.
In the other case if the indicator falls below the lower line, it’s oversold.
The commodity channel index can be created with the MQL 5 function ICCI that is built-in into MQL 5.
And now we want to create a little Expert Advisor that will tell us when the market is overbought or oversold.
To do that please click the little button here or hit the F4 key on your keyboard. That will bring up the Meta Editor, and here we want to select File, New, Expert Advisor from template, continue.
And we will call the Expert Advisor Simple Commodity Channel Index. Now click on Continue, Continue, Finish, everything above the Ontick function can be removed and we will also delete the two comment lines here and we will start by creating a little array for our prices.
It’s a double array and we will call it my price array. And for the definition of our commodity channel index, we will use the ICCI function of MQL 5.
For the current symbol on the chart and the selected period, this might be Euro/U.S. dollar and this might be the minute chart or the hourly chart, and the calculation is done for fourteen candles, because this is the default value of the commodity channel index when you drag it on your chart by using Insert, Indicators, Oscillators, commodity channel index.
And it is calculated based on the close prices.
In the next step we need to sort the price array from the current candle downwards by using the function arraysetasseries for the price we have created here.
And now we use copy buffer for the CCI definition from line here.
This is for the first line, it is line zero. And for the current candle that’s candle zero, and we need the values for three candles, that’s the three here, and we want to store the results in my price array.
We want to get the CCI value of the current candle in our price array.
Okay now we need the chart output depending on the value so if our value is above the upper line we want to put the text “Overbought” on our chart by using the comment function.
And if it’s below the lower line we want to output the text “Oversold.”
And when it’s between the lines we just use an empty output because we have no signal.
Okay that’s it. If you’re ready, please click on the little compile button here or hit the F7 key on your keyboard.
That should compile your Expert Advisor without any error or warnings. And in that case we want to bring up the MetaTrader by pressing the F4 key or hitting the little button over here.
In Metatrader please click on View, Strategy Tester or hit control and R. That should show you the strategy tester panel, and here we want to select the simple commodity channel index.ex5 file.
Please make sure to mark the visualisation option here and click on start. And here is our Expert Advisor at work.
Now it’s below the lower line and then it says oversold and when it’s above the upper line it says overbought.
Okay, now you know how you can use the Commodity Channel Index Indicator to create entry signals in your own Expert Advisor. And you have done it yourself with a few lines of MQL 5 code.
Download “MQL5 Simple Commodity Channel Index Expert Advisor from MQL5 Tutorial”SimpleCommodityChannelIndex.txt – Downloaded 288 times – 928.00 B