In this video we are taking a look at the ICCI Indicator, that’s the Commodity Channel Index Indicator.
You can see that this signal here is creating buy and sell signals and whenever the blue line here is above the upper dotted line that would be a sell signal, in the other case if the blue line here is below the lower dotted line that would be a buy signal.
Now, how can we create an Expert Advisor that is able to calculate the Commodity Channel Index Indicator?
To do that please click on the little button here or press F4 on your keyboard and now you should see the Metaeditor window and here you want to click on “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this one: “SimpleCommodityChannelIndex”, click on “Continue”, “Continue” and “Finish” and now you can delete everything that is above the “OnTick” function and let’s also delete the two comment lines here.
Here we need to create a string for the signal that will be also called: “signal” but we don’t assign any value here.
We also need to create an array for the price data that will be called: “myPriceArray”, it’s a double array so it can hold floating type values and now we define the ICCI Expert Advisor by using the “iCCI” function that is built in into MQL5, it uses a few parameters; the first one is for the current chart – we use “_Symbol” to get the current currency pair on the chart and “_Period” to get the currently selected period. Right now we are trading the 1-minute chart but another period could be 30 or 60 minutes, the third parameter is the number of the candles. We use 14 candles and if you click on: “Insert/ Indicators/ Custom/ CCI/ Inputs” you would see a period of 14 candles is the default value. When I click on “OK” that’s what we see within the round brackets here, let’s right-click and select “Properties”, go to the “Parameters” tab until you see that we use a typical price and in MQL5 that’s “PRICE_TYPICAL” for the last parameter and now that we have defined the ICCI we want to use “ArraySetAsSeries” to store the price data from the current candle downwards for our price array (myPriceArray) we have created here and with “CopyBuffer” we fill our price array (myPriceArray) according to our “ICCIDefinition” for buffer 0 from the current candle 0 and for 3 candles and to get the ICCI value (ICCIValue) we just need to have a look at candle 0 in our price array (myPriceArray) and we assign the result to the variable ICCI value (ICCIValue) and whenever the ICCI value (ICCIValue) is greater than 100 that would be considered to be a sell signal so we assign the word: “sell” to our signal.
In the other case if the ICCI value (ICCIValue) is below -100 that would be a buy signal so we assign the word: “buy” to our signal.
Now let’s make that a capital and in the last step we want to create an output on the chart that will output the word “Signal:” followed by the calculated signal.
Please don’t forget the closing bracket. I will enable the toolbar so now I can click on the “Compile” button here or press F7 on the keyboard to compile the code. We have one warning here because we used the float value here, usually that would be a double value so let’s correct that, press F7 and now the warning is gone, it doesn’t really make a difference because we use the ICCI value (ICCIValue) to find out if something is above 100 or below -100, you can store a lot of digits behind the dot in double value but that’s not very helpful if you just want to find out if something is above plus or below minus 100.
Okay, if you could compile your code without any errors you could now click on the little button here or press F4 on your keyboard to go back to Metatrader.
In Metatrader you want to click on “View/ Strategy Tester” or press CTRL and R on your keyboard and here in a Strategy Tester please select the “SimpleCommodityChannelIndex.ex5” file, mark the visualization option here and start your test and here’s our little Expert Advisor at work it creates outputs for buy and sell signals on your chart and now you know how you can code it and you have coded it yourself with a few lines of MQL5 code.
Download “MQL5 TUTORIAL - SIMPLE COMMODITY CHANNEL INDEX”SimpleICCI.txt – Downloaded 355 times – 911.00 B