$ £ ¥
¥ £ $

How to Create Basic MQL4 Script to Test Your Code

If you want to learn a programming language, you will have to experiment a lot and you will have to test your code to understand if it does what you programmed it to do. A very simple way to perform such testing is creating a script, placing your code inside, and running it to see the result.

In other guides, you can learn the basics of the MQL4 language and see examples of some specific code. This guide's purpose is to show you how to create a script that will allow you to experiment with what you are learning.

You should be already familiar with MetaTrader, MQL, and MetaEditor. Now, let's get started with the script!

Open MetaEditor and create a new script, you can click the New button in the Toolbar or click File and then New, select Script and click Next.

Creating New Script in MetaEditor

Choose a name for your script. In this example, we will use Demo-1. Click Finish.

Setting a Script Name in MQL4 Wizard

Done! Your brand new script has been created with some basic code already in it. At the moment, it doesn't do anything but we will add something just to test it in action.

Brand New Script in MetaEditor

The code added by default is the following, don't worry if it doesn't make sense, it will make sense.

//+------------------------------------------------------------------+
//|                                                       Demo-1.mq4 |
//|                                                    EarnForex.com |
//|                                       https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   
  }
//+------------------------------------------------------------------+

We are going to modify the code by simply adding Alert("Hello World!"); to the OnStart() function. If you have never programmed a "Hello World!" program before, this is usually the first thing you do when learning a new language. That a message will popup when the script is executed. After adding the code, save (Ctrl+S) and compile (F7) the file.

//+------------------------------------------------------------------+
//|                                                       Demo-1.mq4 |
//|                                                    EarnForex.com |
//|                                       https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   Alert("Hello World!");
  }
//+------------------------------------------------------------------+
Saving and Compiling a Hello World MQL4 Script in MetaEditor

To test the script, open MetaTrader and find the Demo-1 script via the Navigator panel:

Run the Script in MT4

Drag the Script to the chart where you want to run it and look at the result:

Hello World Script Execution Result

The script opens the alert window and shows a "Hello World!" alert. Furthermore, the alert is logged in the Experts tab of the Terminal panel.

Now you know how to test your code when learning the basics of MQL4 coding.