MQL5 TUTORIAL- SIMPLE ICCI STANDALONE EA

video
play-sharp-fill

In this video we are going to create an Expert Advisor for this signal here; it’s the Commodity Channel Index, and you see that we have already opened a position, it produces buy and sell signals here on the chart. Whenever the signal is below the lower line we are going to buy and when it’s above we are going to sell.
Now how can we create an Expert Advisor in MQL5 that is actually able to trade the Commodity Channel Index?
To do that 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/ Expert Advisor (template)” from template, “Continue”, I will call this one: “SimpleICCIStandaloneEA”, click on “Continue”, “Continue” and “Finish”, now you can remove everything that is above the “OnTick” function and let’s also delete the two comment lines here.
First we want to include the file: “Trade.mqh”, it comes with MQL5 and it will help us to create an instance of the class “CTrade” that will be called: “trade” and we are going to use it to actually open positions later on but before we can do that we need to calculate a few things. For example the Ask price. We get that by using “SymbolInfoDouble” for the current symbol and with “SYMBOL_ASK” we will get the Ask price. “NormalizeDouble” and “_Digits” will help us to get the price with either 3 or 5 digits behind the dot depending on the currency pair.
Let’s do the same for the Bid price, it’s almost exactly the same but this time we use “SYMBOL_BID”.
Now we use “MqlRates” to create an array for the prices, we will call that:”PriceInfo” and we sort the array from the current candle downwards by using “ArraySetAsSeries” and we use “CopyRates” to fill our “PriceInfo” array with price data for the last candles. We will use the current symbol on the chart, the currently selected period and we will start with the prices for candle 0 (zero) – that’s the current candle – and we only need prices for 3 candles and store that result in the “PriceInfo” array.
We also need to create a string for the signal that will be called: “signal” and it has no value right now because we are going to calculate that later on.
We need to create another array, this time we will call it: “myPriceArray”.
Well, this one was for the candle prices (PriceInfo) and the second one will be for the Expert Advisor (myPriceArray).
Let’s define the Commodity Channel Index here (CCIDefinition), we are going to use the built-in: “iCCI” function for the current symbol and the current period on the chart, it will be calculated based on 14 candles – that’s also what you see if you click on: “Insert/ Indicators/ Oscillators/ Commodity Channel Index”, you see 14 candles – and our result will be calculated based on the close prices.
Let’s sort the price array (myPriceArray) from the current candle downwards by using “ArraySetAsSeries” and with “CopyBuffer” we fill our price array (myPriceArray) according to the Commodity Channel Index definition (CCIDefinition) that we have created here for the first signal buffer, – that’s the line here – we start with candle 0 (zero) – that’s the current candle – and we copy the values of 3 candles and store it in our price array (myPriceArray).
Finally we are able to get the value of the current candle by looking at candle 0 (zero) in our price array (myPriceArray) and the result will be stored in the variable called: ”CCIValue” and if the “CCIValue” is bigger than 100 – that would be the case whenever the blue line is above the upper dotted line, you see the 100 mark here – and if that is the case we want to sell so we assign the word: “sell” to our signal.
Otherwise if the “CCIValue” is below the -100 (minus 100) line – that is the lower dotted line here – we want to buy so we assign the word: “buy” to our signal.
And if the signal equals sell and “PositionsTotal” is less than 1 – with other words we wouldn’t have any open positions on the chart – we want to sell so we use “trade.Sell” to sell 10 micro lot.
Otherwise if the signal equals buy and we have no open positions we want to buy so we use “trade.Buy” to buy 10 micro lot.
Finally we create an output on the chart by using the “Comment” function, it will output the words: “The current signal is:” followed by the “signal” that we have calculated.
Okay, that’s it so far, please click on the “Compile” button or press F7. We don’t see any errors here so now we can click on the little button here or press F4 to go back to Metatrader.
In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, please select the “SimpleICCIStandaloneEA”, mark the visualization option here and click on start.
Here we are! This is our first position, we can speed up the process a little bit and you see that it is creating buy and sell signals, we already have opened a position here and now you know how to create an Expert Advisor that is able to trade the Commodity Channel Index in MQL5 and you have coded it yourself with a few lines of MQL5 code.

Download “MQL5 Tutorial - Simple ICCI Standalone Expert Advisor”

SimpleICCIStandaloneEA.txt – Downloaded 321 times – 1.72 KB