Creating Your First MQL4 Script: A Beginner’s Guide

Watch on YouYube: Step-by-Step: Your First MQL4 Script in MetaTrader!

In this blog post, we’ll explore how to create a simple “Hello, World!” script in MQL4, detailing its structure, features, and how to execute it to display an alert in MetaTrader 4.

What is a Script in MQL4?

Scripts in MQL4 are designed to perform specific tasks immediately when executed. Unlike Expert Advisors that run continuously, scripts complete their function and terminate, making them suitable for one-time operations like closing all positions simultaneously.

Setting Up the Script

To create a new script in MetaEditor, follow these steps:

  1. Open MetaEditor: Start the MetaEditor application that comes with your MetaTrader platform (F4).
  2. Create a New Script: Navigate to File > New, and select Script from the options.
  3. Name Your Script: Name your script, for example, Script_01.
The Code Example

Here’s a simple MQL4 script that displays a “Hello, World!” message through an alert box:


//+------------------------------------------------------------------+
//|                                                    Script_01.mq4 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
Alert("Hello, World!");   
  }

Code Breakdown

  1. Header Section: The lines starting with #property are metadata:
    • #property copyright indicates the copyright holder.
    • #property link can provide a URL to your website or portfolio, allowing you to showcase your work.
    • #property version denotes the version of your script, useful for version control.
    • #property strict is directive for strict compilation mode.
  2. OnStart Function: This function serves as the entry point for the script:
    • In this case, it contains a single command: Alert(“Hello, World!”), which generates a pop-up alert in the MetaTrader platform with the message “Hello, World!”.
  3. Comments: The extra lines like “Script program start function” and “Copyright 2024, MetaQuotes Ltd.” are comments. We’ll dive deeper into comments later, but for now, know that they’re there to clarify and organize the code. Comments are just for information—they won’t actually be executed.

Once you’ve finished writing your code, click the Compile button to check for any errors.

How to Execute the Script

To run the script in MetaTrader, follow these steps:

  1. Open MetaTrader 4: Launch the platform.
  2. Access the Navigator: Press Ctrl + N to open the Navigator window if it isn’t already visible.
  3. Locate Your Script: Find the Scripts section in the Navigator. You should see your “Script_01” file listed there.
  4. Run the Script: Right-click on “Script_01” and select Run or double-click it. This will execute the script, triggering an alert with the message “Hello, World!”.

Conclusion

Creating your first script in MQL4 is an excellent introduction to the language and its capabilities. By understanding the structure and execution of this simple script, you’re taking your first steps toward developing more complex trading tools and strategies in MQL4.

In the upcoming chapters, we’ll use this script file to explore and test various MQL4 features.

Next: Comments, Identifiers and Reserved Words

Previous: Settings and IDE