MQL5 Tutorial – How To Code A Simple Commodity Channel Index

video
play-sharp-fill

Here is a step-by-step description for creating an Expert Advisor using the Commodity Channel Index (CCI) indicator in MQL5 for Metatrader5:

  1. Open the Metatrader5 platform and click on the “Expert Advisors” button in the toolbar or press the F4 key on your keyboard.
  2. Select “File” > “New” > “Expert Advisor” from the menu.
  3. Name the Expert Advisor “Simple Commodity Channel Index” and click “Finish”.
  4. Remove all the code above the “OnTick()” function and delete the two comment lines.
  5. Declare a double array for storing prices: double myPriceArray[] = {};
  6. Define the CCI indicator using the built-in ICCI() function:

      // define the properties of the CCI EA  
      int CCIDefinition =iCCI(_Symbol,_Period,14,PRICE_CLOSE);
  1. Sort the price array from the current candle downwards using the ArraySetAsSeries() function:

      // sort the price array from the current candle downwards
      ArraySetAsSeries(myPriceArray,true);
  1. Copy the CCI buffer for the current candle using the CopyBuffer() function:

      // Defined MA1, one line,current candle,3 candles, store result 
      CopyBuffer(CCIDefinition,0,0,3,myPriceArray);
  1. Get the CCI value of the current candle in the myPriceArray array:

      // Get the value of the current candle
      float CCIValue=(myPriceArray[0]);
  1. Use the Comment() function to output “Overbought” if the CCI value is above the upper line (default value is 100), “Oversold” if it’s below the lower line (default value is -100), and an empty output if it’s between the lines:

      // if it is above the upper line
      if (CCIValue> 100) 
      Comment ("OVERBOUGHT");

      // if it is below the lower line      
      if (CCIValue< -100) 
      Comment ("OVERSOLD");         

      // if it is between the lines
      if ((CCIValue > -100) &&(CCIValue<100))
	   Comment ("");
  1. Compile the Expert Advisor by clicking the “Compile” button or pressing the F7 key on your keyboard.
  2. Open the Metatrader5 platform and click on “View” > “Strategy Tester” or press Ctrl + R.
  3. Select the “Simple Commodity Channel Index.ex5” file and make sure the “Visualization” option is marked.
  4. Click “Start” to see the Expert Advisor in action.

Note: This is a basic implementation of a CCI-based Expert Advisor. You may need to adjust the code to fit your specific needs.

In this article, we will show you how to create a simple Expert Advisor using the Commodity Channel Index (CCI) indicator in MQL5 for Metatrader5. The CCI is an oscillator that can help you identify overbought and oversold conditions in the market.

To get started, open the Metatrader5 platform and click on the “Expert Advisors” button in the toolbar or press the F4 key on your keyboard. Then, select “File” > “New” > “Expert Advisor” from the menu. Name the Expert Advisor “Simple Commodity Channel Index” and remove all the code above the OnTick() function.

Next, we will declare a double array for storing prices and define the CCI indicator using the built-in ICCI() function. The ICCI() function takes several parameters, including the symbol, period, and calculation method. For this example, we will use the default values for these parameters.

After defining the CCI indicator, we will sort the price array from the current candle downwards using the ArraySetAsSeries() function. This will allow us to access the price data for the current and previous candles.

Next, we will copy the CCI buffer for the current candle using the CopyBuffer() function. This will allow us to access the CCI value for the current candle.

After copying the CCI buffer, we will get the CCI value of the current candle in the myPriceArray array. We will then use the Comment() function to output “Overbought” if the CCI value is above the upper line, “Oversold” if it’s below the lower line, and an empty output if it’s between the lines.

Once we have finished coding the Expert Advisor, we will compile it by clicking the “Compile” button or pressing the F7 key on your keyboard. If there are no errors or warnings, we can move on to testing the Expert Advisor.

To test the Expert Advisor, open the Metatrader5 platform and click on “View” > “Strategy Tester” or press Ctrl + R. Select the “Simple Commodity Channel Index.ex5” file and make sure the “Visualization” option is marked. Click “Start” to see the Expert Advisor in action.

It’s important to note that this is a basic implementation of a CCI-based Expert Advisor. You may need to adjust the code to fit your specific needs. Additionally, it’s important to thoroughly backtest and optimize your Expert Advisor before using it in live trading. Always use risk management techniques to protect your capital and avoid overbought or oversold conditions that can lead to large losses.

With these steps, you should be able to create a simple Expert Advisor using the CCI indicator in MQL5 for Metatrader5.

Video Time Stamps

0:00 – Introduction to Commodity Channel Index
0:30 – Overbought and Oversold Conditions
0:50 – Creating an Expert Advisor in MQL5
1:03 – Selecting File > New > Expert Advisor
1:13 – Naming the Expert Advisor
1:20 – Removing Unnecessary Code
1:30 – Creating a Double Array for Prices
1:41 – Defining the Commodity Channel Index
1:56 – Selecting the Current Symbol and Period
2:01 – Calculating for 14 Candles
2:12 – Inserting the Commodity Channel Index Indicator
2:23 – Calculating Based on Close Prices
2:35 – Sorting the Price Array
2:42 – Copying the CCI Buffer
2:46 – Getting the CCI Value for the Current Candle
3:13 – Outputting Overbought or Oversold on the Chart
3:36 – Outputting an Empty Signal for No Signal
3:48 – Compiling the Expert Advisor
4:01 – Bringing Up MetaTrader
4:11 – Selecting View > Strategy Tester
4:23 – Selecting the Simple Commodity Channel Index Expert Advisor
4:30 – Marking the Visualization Option
4:34 – Starting the Expert Advisor
4:41 – Observing the Expert Advisor in Action
4:52 – Using the Commodity Channel Index Indicator for Entry Signals
5:02 – Conclusion