// Defined MA1, one line,current candle,3 candles, store result
CopyBuffer(CCIDefinition,0,0,3,myPriceArray);
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