Technical Overview
ProjectTemplateGen.mq5 is a MetaTrader 5 script utility that automates the creation of standardized Expert Advisor project structures. It implements programmatic file system operations within MQL5's security constraints while establishing a consistent foundation for scalable trading system development.
Core Capabilities
- Programmatic File Operations: Demonstrates MQL5's file handling API for creating directories and writing structured content
- Template-Based Generation: Produces ready-to-compile MQL5 source files with proper event handler skeletons
- Configurable Output: Allows runtime customization through input parameters without modifying source code
- Sandbox Compliance: Operates within MetaTrader 5's secure execution environment
Implementation Details
File Operations Architecture
The script implements a modular approach to file creation, with distinct functions handling different aspects of project generation:
// Primary orchestration in OnStart()
string projectPath = "Files\\" + ProjectName + "\\";
if(!FolderCreate(projectPath)) { /* Error handling */ }
if(!CreateMainEA(projectPath + ProjectName + ".mq5")) return;
if(CreateIncludeFile) CreateInclude(projectPath + ProjectName + ".mqh");
CreateManifest(projectPath + "README.txt");
Generated Project Structure
MQL5/Files/[ProjectName]/
├── [ProjectName].mq5 # Main Expert Advisor source file
├── [ProjectName].mqh # Optional header/class definition file
└── README.txt # Project documentation and instructions
Key Technical Components
1. Dynamic Path Construction
string projectPath = "Files\\" + ProjectName + "\\";
- Uses relative paths compliant with MQL5 sandbox restrictions
- Demonstrates string concatenation for dynamic file system navigation
2. Robust File Creation with Error Handling
int h = FileOpen(filePath, FILE_WRITE | FILE_TXT | FILE_ANSI);
if(h == INVALID_HANDLE)
{
Print("ERROR: Cannot create main EA file. Error: ", GetLastError());
return false;
}
- Implements comprehensive error checking for file operations
- Provides meaningful error messages with system error codes
- Ensures proper resource cleanup with FileClose() calls
3. Structured Content Generation
FileWrite(h,"//+------------------------------------------------------------------+");
FileWrite(h,"//| " + ProjectName + ".mq5");
FileWrite(h,"//| Author: " + AuthorName);
FileWrite(h,"#property strict");
FileWrite(h,"int OnInit()");
FileWrite(h,"{");
FileWrite(h," return INIT_SUCCEEDED;");
FileWrite(h,"}");
- Generates syntactically valid MQL5 code
- Maintains consistent formatting and coding standards
- Creates compilable boilerplate code
Usage Instructions
Basic Operation
- Compilation: Compile ProjectTemplateGen.mq5 in MetaEditor (F7)
- Execution: Drag script from Navigator onto any chart
- Configuration: Set parameters in the input dialog:
- ProjectName: Destination folder and file naming
- AuthorName: Copyright and documentation attribution
- CreateIncludeFile: Toggle header file generation
- Output: Check Experts tab for generation status and path information
Post-Generation Workflow
The script creates projects in MQL5\Files\[ProjectName]\ due to MQL5 script security restrictions.
Complete the setup by:
- Manually moving the folder to MQL5\Experts\
- Opening the main .mq5 file in MetaEditor
- Implementing your trading logic in the provided function skeletons
Expansion Opportunities for Advanced Workflows
Scalability Enhancements
- Multi-File Templates: Extend to generate indicator scripts, library files, or resource manifests
- Configuration Files: Add JSON/XML configuration file generation for parameter management
- Build Scripts: Incorporate batch compilation or dependency management files
Code:
//+------------------------------------------------------------------+
//| ProjectTemplateGen.mq5 |
//| Copyright 2025, Clemence Benjamin |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, Clemence Benjamin"
#property link "https://www.mql5.com"
#property strict
#property description "Generates EA project template in MQL5//Files"
#property script_show_inputs
#property version "2.01"
//--- Inputs
input string ProjectName = "Type your project name";//Project Name
input string AuthorName = "Type your name eg. Clemence Benjamin"; // Author Name
input bool CreateIncludeFile = true;// Generate a header file
//+------------------------------------------------------------------+
//| Script entry |
//+------------------------------------------------------------------+
void OnStart()
{
Print("=== MQL5 Project Template Generator Started ===");
Print("Project Name: ", ProjectName);
Print("Author: ", AuthorName);
//--- ALLOWED sandbox path
string projectPath = "Files\\" + ProjectName + "\\";
Print("Target path (sandbox-safe): ", projectPath);
//--- Create project folder
if(!FolderCreate(projectPath))
{
Print("ERROR: Cannot create project folder. Error: ", GetLastError());
return;
}
//--- Create EA file
if(!CreateMainEA(projectPath + ProjectName + ".mq5"))
return;
//--- Create include file
if(CreateIncludeFile)
CreateInclude(projectPath + ProjectName + ".mqh");
//--- Create manifest
CreateManifest(projectPath + "README.txt");
Print("SUCCESS: Project created in MQL5\\Files\\", ProjectName);
Print("ACTION REQUIRED: Move folder to MQL5\\Experts\\ manually");
Print("Folder path: ", TerminalInfoString(TERMINAL_DATA_PATH) + "\\" + projectPath);
}
//+------------------------------------------------------------------+
//| Create main EA |
//+------------------------------------------------------------------+
bool CreateMainEA(const string filePath)
{
int h = FileOpen(filePath, FILE_WRITE | FILE_TXT | FILE_ANSI);
if(h == INVALID_HANDLE)
{
Print("ERROR: Cannot create main EA file. Error: ", GetLastError());
return false;
}
FileWrite(h,"//+------------------------------------------------------------------+");
FileWrite(h,"//| " + ProjectName + ".mq5");
FileWrite(h,"//| Author: " + AuthorName);
FileWrite(h,"//+------------------------------------------------------------------+");
FileWrite(h,"#property strict");
FileWrite(h,"");
FileWrite(h,"int OnInit()");
FileWrite(h,"{");
FileWrite(h," return INIT_SUCCEEDED;");
FileWrite(h,"}");
FileWrite(h,"");
FileWrite(h,"void OnTick()");
FileWrite(h,"{");
FileWrite(h,"}");
FileClose(h);
return true;
}
//+------------------------------------------------------------------+
//| Create include file |
//+------------------------------------------------------------------+
void CreateInclude(const string filePath)
{
int h = FileOpen(filePath, FILE_WRITE | FILE_TXT | FILE_ANSI);
if(h == INVALID_HANDLE)
return;
FileWrite(h,"#ifndef __" + ProjectName + "_MQH__");
FileWrite(h,"#define __" + ProjectName + "_MQH__");
FileWrite(h,"class C" + ProjectName + " {};");
FileWrite(h,"#endif");
FileClose(h);
}
//+------------------------------------------------------------------+
//| Create manifest |
//+------------------------------------------------------------------+
void CreateManifest(const string filePath)
{
int h = FileOpen(filePath, FILE_WRITE | FILE_TXT | FILE_ANSI);
if(h == INVALID_HANDLE)
return;
FileWrite(h,"PROJECT TEMPLATE");
FileWrite(h,"Name: " + ProjectName);
FileWrite(h,"Author: " + AuthorName);
FileWrite(h,"");
FileWrite(h,"NOTE:");
FileWrite(h,"Move this folder to MQL5\\Experts\\ to compile.");
FileClose(h);
}
//+------------------------------------------------------------------+