Mastering MQL5: Creating a MT5 Script for MA15 indicator - Day 6

Learn how to create custom indicators in MT5 with our step-by-step guide. Explore the world of forex trading and empower yourself to automate tasks and execute strategies with precision.

Mastering MQL5: Creating a MT5 Script for MA15 indicator - Day 6
4 min read·


2023/09/14 03:28

You can check the final code here:

Hello, everyone! I hope you're doing well. Today, by the end of the day, we will be able to print our first custom indicator on the chart. Our main focus here is on the structure of the code and how to create the script, rather than the logic or some fancy functions themselves. So, let's get started.

Create a Custom Indicator Script in MT5

From day 1 up to now, we've only created a blank script without any pre-built functions. However, MT5 provides us with other advanced options, such as pre-built Expert Advisors or pre-built indicators (which we will use today). Here are the step-by-step instructions to achieve this:

In MetaEditor, go to New > Custom Indicator > Next

Input your file name > Next > Check the box for OnCalculate(...,open,high,low,close) > Next

  • When you enter the file name, you all so can set up some parameters, which allow you to change some input settings without touching the code later. We'll skip this for now.
  • You might wonder what OnCalculate, OnTimer, and OnChartEvent are. They're called Event Handling. All Event Handlers of MT5 are listed here. If you have time, you can read about them. For now, we only care about OnCalculate - this Event Handler will do something for us every time the price changes.

Click Add > Enter your Indicator label and color > Finish

  • We have an option to display our indicator in a separate window called "Separate window". Don't tick it (if you do, you can change it later).

Review of the Script Structure

After following the above steps, you will get a pre-script like this:

The First path is all the setting being displayed as #property.

  • Copyright and version: These are important if you ever want to sell your tool. When discussing selling a tool, you don't provide the source code; you only sell the compiled version of your code with the .ex5 extension to protect the algorithms behind it.
  • The chart indicator_chart_window: This determines where the indicator will be shown. You can change this setting. The color and label of the indicator are also listed here.
  • The buffer: This is the array that holds the price information and then displays it on the chart. We mainly add logic to manipulate this property.

The second and third parts are where we implement the logic.

  • Since we're on day 6, we won't delve into this part too much. We should focus on understanding the overall structure first.

Adding Code and Running the Custom Script

Now let's add the script below to your file and run it by pressing F5 on your keyboard. I've set up a simple Moving Average with a period of 15 candles:

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//---- plot MA
#property indicator_label1  "MA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input bool               AsSeries=true;
input int                period=15;
input ENUM_MA_METHOD     smootMode=MODE_EMA;
input int                shift=0;
//--- indicator buffers
double                   MABuffer[];
int                      ma_handle;
//| Custom indicator initialization function                         |
int OnInit()
//--- indicator buffers mapping
   Print("Parameter AsSeries = ",AsSeries);
   Print("Indicator buffer after SetIndexBuffer() is a timeseries = ",
//--- set short indicator name
//--- set AsSeries (depends on input parameter)
   Print("Indicator buffer after ArraySetAsSeries(MABuffer,true); is a timeseries = ",
//| Custom indicator iteration function                              |
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
//--- check if all data calculated
   if(BarsCalculated(ma_handle)<rates_total) return(0);
//--- we can copy not all data
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<=0) to_copy=rates_total;
      //--- last value is always copied
//--- try to copy
   if(CopyBuffer(ma_handle,0,0,to_copy,MABuffer)<=0) return(0);
//--- return value of prev_calculated for next call

We will get a red line on our chart as bellow.


Custom scripts open the door to a world of possibilities in MT5. They empower traders to automate routine tasks, conduct in-depth technical analysis, and execute complex trading strategies with precision. As you delve deeper into the realm of custom scripts, remember that practice and experimentation are your allies. Don't hesitate to explore more complex scripts and seek out additional resources to expand your scripting skills.

On the 6th day of the challege, we have archive something. Remember every castle begun with a small brick. Let's keep learning.