MQL4 TUTORIAL BASICS – 46 SIMPLE COMMODITY CHANNEL INDEX EA

video

In this video we are going to create an expert advisor for this oscillator here, it’s the commodity channel index, usually, it’s used as a filter but we will use it to create buy and sell signals, so let’s find out how to do that with mql4.
To get started please click on the little button here or press F4 on your keyboard.
Now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this one simple commodity channel index ea, click on continue, continue and finish.
Now you can delete everything above the on tick function and the two comment lines here.
We start by creating an empty string variable called signal, we don’t assign any value here because this is what we need to calculate later on.
The commodity channel index can be calculated based on the icci function that comes with mql4.
It is calculated based on the current symbol on the chart and the currently selected period on that chart. Actually, this is the old way to describe it.
We could use underscore symbol and underscore period, and the result is calculated based on fourteen candles. In mql4 the default value seems to be price typical, we could use the close price so let’s actually do that and use price underscore close – all in capital letters –, the last value here is for a shift, we don’t need it so let’s set that to zero.
And when the icci value is above one hundred – that would be the case when the signal line here is above the upper dotted line – we want to sell so we assign sell to our signal.
In the other case if the current icci value is below minus one hundred – that would be the case when the signal line is below the lower dotted line – we consider that to be a buy signal so we assign the word buy to our signal.
And if we have a buy signal and the return value for orders total is zero, that would mean that we don’t have any open trades and in that case, we use order send to open a buy trade for ten micro lot.
Otherwise, when the signal equals sell and we also have no open orders we use order send to open a sell trade for ten micro lot.
Finally, we use comment to output the text, the current signal is and the calculated signal directly on our chart. That’s about it.
If you don’t understand what all the code here does or if this was too fast for you, maybe you want to watch one of the other basic videos in this video series or maybe even the premium course on our website might be interesting for you.
For now please click on the compile button, and if you don’t get any errors you can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader we click on insert, indicators, oscillators, commodity channel index, the default value here is to calculate the value for fourteen candles, I will change the typical price here to close price as we have done that in the code.
Now let’s click on okay, right-click into the chart, select template, save template and save it as tester tpl, you can overwrite the old one because tester tpl is the template that is going to be used in the strategy test otherwise you wouldn’t see the commodity channel index here.
Now let’s click on view, strategy tester or press control and r, please pick the new file simple commodity channel index EA dot ex4, mark the option for the visual mode here and start a test.
Here we are! It started right away with a buy signal so our little expert advisor is working and in this little video you have learned how to create an expert advisor for the commodity channel index that is able to open buy and sell trades and you have coded it yourself with a few lines of mql4 code.