I think that there are many people who want to try automation by EA for transactions such as Forex, but do not know how to create it.
For such people, I will introduce a sample program that summarizes the minimum required functions etc. required for EA creation.
Contents
Sample program
The sample program is described below.
As it is, you can save it as a file with the extension “.mq4” and compile it.
Expert Advisor can also be created based on this program.
/*****************************/
/* propety setting ・・・ A */
/*****************************/
#property copyright "2020-2021, x corp."
#property version "1.00"
#property strict
/*****************************/
/* Input variables ・・・ B */
/*****************************/
input double parameter1 = 1.0;
/******************************/
/* Global variables ・・・ C */
/******************************/
double glovalValue1 = 1.0;
/************************************/
/* Event Handing Functions ・・・ D */
/************************************/
int OnInit(void)
{
int ret = INIT_SUCCEEDED;
// Function called at the following timing
// 1. client terminal loads a program
// 2. financial instrument and/or chart timeframe is changed
// 3. after a program is recompiled
// 4. after input parameters are changed from the setup window of an Expert Advisor
// 5. after the account is changed.
// Set period when using OnTimer ()
if(EventSetTimer(60) == false) { ret = INIT_FAILED; }
// The meaning of the returned value is as follows.
// INIT_SUCCEEDED : Successful initialization
// INIT_FAILED : Initialization failed
// INIT_PARAMETERS_INCORRECT : This value means the incorrect set of input parameters.
return ret;
}
void OnDeinit(const int reason)
{
// The OnDeinit() function run is restricted to 2.5 seconds.
// If during this time the function hasn't been completed, then it is forcibly terminated.
// If there is no processing you want to implement, there is no problem even if you do not implement it.
// Function called at the following timing
// 1. Before global variables are deinitialized and the program is unloaded
// 2. when the client terminal is closed
// 3. when a chart is closed
// 4. before the security and/or timeframe is changed
// 5. successful program re-compilation
// 6. when input parameters are changed
// 7. when account is changed.
// 8. When returning something other than INIT_SUCCEEDED with OnInit()
// process for each reason
if(reason == REASON_PROGRAM) { /*
Expert Advisor terminated its operation by calling the ExpertRemove() function */ }
else if(reason == REASON_REMOVE) { /* Program has been deleted from the chart */ }
else if(reason == REASON_RECOMPILE) { /* Program has been recompiled */ }
else if(reason == REASON_CHARTCHANGE) { /* Symbol or chart period has been changed */ }
else if(reason == REASON_CHARTCLOSE) { /* Chart has been closed */ }
else if(reason == REASON_PARAMETERS) { /* Input parameters have been changed by a user */ }
else if(reason == REASON_ACCOUNT) { /* Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings */ }
else if(reason == REASON_TEMPLATE) { /* A new template has been applied */ }
else if(reason == REASON_INITFAILED) { /* This value means that OnInit() handler has returned a nonzero value */ }
else if(reason == REASON_CLOSE) { /* Terminal has been closed */ }
// If you use OnTimer (), you need to stop the Timer event with this function.
EventKillTimer();
return;
}
void OnTick(void)
{
// In case when OnTick function for the previous quote is being processed when a new quote is received,
// the new quote will be ignored by an Expert Advisor.
// The NewTick event is generated irrespective of whether automated trade is allowed or not ("Allow/prohibit Auto trading" button).
// Function called at the following timing
// 1. new quote is received
return;
}
void OnTimer(void)
{
// Function called at the following timing
// 1. Period timing set by EventSetTimer () in OnInit ()
return;
}
void OnChartEvent(const int id, // Event ID
const long& lparam, // Parameter of type long event
const double& dparam, // Parameter of type double event
const string& sparam) // Parameter of type string events
{
// Function called at the following timing
// 1. Timing for each event that occurred on the Chart(Let's check the following processing for each event type)
// Processing for each chart event
// Please refer to the following URL for detailed parameters for each event.
// URL : https://docs.mql4.com/constants/chartconstants/enum_chartevents
if (id == CHARTEVENT_KEYDOWN) { /* Keystrokes */ }
else if(id == CHARTEVENT_MOUSE_MOVE) { /* Mouse move, mouse clicks (if CHART_EVENT_MOUSE_MOVE=true is set for the chart) */ }
else if(id == CHARTEVENT_OBJECT_CREATE) { /* Graphical object created (if CHART_EVENT_OBJECT_CREATE=true is set for the chart) */ }
else if(id == CHARTEVENT_OBJECT_CHANGE) { /* Graphical object property changed via the properties dialog */ }
else if(id == CHARTEVENT_OBJECT_DELETE) { /* Graphical object deleted (if CHART_EVENT_OBJECT_DELETE=true is set for the chart) */ }
else if(id == CHARTEVENT_CLICK) { /* Clicking on a chart */ }
else if(id == CHARTEVENT_OBJECT_CLICK) { /* Clicking on a graphical object */ }
else if(id == CHARTEVENT_OBJECT_DRAG) { /* Drag and drop of a graphical object */ }
else if(id == CHARTEVENT_OBJECT_ENDEDIT) { /* End of text editing in the graphical object Edit */ }
else if(id == CHARTEVENT_CHART_CHANGE) { /* Change of the chart size or modification of chart properties through the Properties dialog */ }
else if(id == CHARTEVENT_CUSTOM) { /* Initial number of an event from a range of custom events(EventChartCustom()) */ }
else if(id == CHARTEVENT_CUSTOM_LAST) { /* The final number of an event from a range of custom events(EventChartCustom()) */ }
return;
}
Outline explanation of sample program
Although it is the sample program described above, the outline is described in the comments in the sample program, but the outline is also described here.
property setting・・・ A
#property copyright :The company name
#property version : Program version, maximum 31 characters
#property strict : A property created to maintain compatibility due to a wide variety of large differences during the past MQL4 compiler updates. It’s always a good idea to set it when you create a new compiler with Build 600 or later. It also seems to have a function to raise the warning level when compiling the program.(It is better to describe it because it will reduce unintended programming mistakes.)
Input variables ・・・ B
Set the parameters displayed on the EA parameter input screen as shown in the screen below.
Create as many parameters as you need.
Global variables ・・・ C
Set global variables that can be referenced in all functions in program creation.
It is usually said that the fewer external variables, the fewer bugs.
Event Handing Functions ・・・ D
A group of functions called from the MT4 Event handler.
The call timing of each function is described in the sample program.
OnInit():
The first function to be called after loading the program.
I think it’s a good idea to perform external variable initialization processing and input parameter checking processing.
This function should be written as mandatory.
However, the processing in the function is the worst, and it is not necessary.
And also refer to other notes when implementing the OnInit () function.
OnDeinit():
A function that is called at the end of the program.
If there is an Event that needs to be stopped or information that you want to output at the end, I think it is better to process it here.
This function should be written as mandatory.
However, the processing in the function is the worst, and it is not necessary.
OnTick():
A function that is called at new quote is received.
Basically, implement the core of the automatic trading process here, or call the automatic trading function you created.
OnTimer():
A function that is called at a specified cycle.
OnTick () is not called if there is no price movement, so use this if you want to operate without price movement.
If there is no intended use, it is not necessary to include it in the program.
OnChartEvent():
This function is called by events (keyboard operation, mouse operation, object creation / modification / deletion) on the Chart.
If there is no intended use, it is not necessary to include it in the program.