MT5 Expert Advisor Template, Guide
MT5 Expert Advisor Template, Guide
template include
Comments for each function to understand the logic
Well-written code
Modular structure
Partial close
Error handling — the template checks for common runtime errors in all of its functions.
Risk management — fixed and risk-based position sizing for flexible risk management.
Easy to edit — adding and modifying just a few lines of code will give you a working EA
based on your favorite indicator.
Here you can see how the functions are separated in the runtime flow:
OnInit function
OnTick function
There is nothing in the DeInit function of this EA template — just a placeholder. You can easily
add your own code there, but most likely you won't ever need to add anything there.
Looking at the source code of other expert advisors may help you with this.
MT5 platform
The expert advisor files that you download are intended for MetaTrader 5 platform. They won't
work in MetaTrader 4.
Willingness to experiment
It should be noted that this is not a fully working trading strategy. You will have to come up with
your own entry and exit signals and strategy. You might need to run many backtests and
change the code multiple times before you find something worthwhile.
If what you see makes sense, then you will likely be able to use this EA template without any
issues.
If it doesn't make any sense to you, but you are interested to see how the code for an expert
advisor works, then it can help you.
If you are not the coding type of person and all this doesn't interest you, then probably, this
isn't something for you.
Expert properties
//-PROPERTIES-//
// Properties help the software look better when you load it in MT5.
// They provide more information and details
// This is what you see in the About tab when you attach the expert advisor to a ch
#property link "https://www.earnforex.com/metatrader-expert-advisors/mt5-e
Input parameters
// EA Parameters
input string Comment_0 = "=========="; // EA-Specific Parameters
// !! Declare parameters specific to your EA here.
// For example, a moving average period, an RSI level, or anything else your EA nee
// All input parameters start with 'input' keyword.
// input int example = 10; // This is an example input parameter
Tick processing
// Entry and exit processing
void ProcessTick()
{
if (!GetIndicatorsData()) return;
if (CountPositions())
// A block of code that lets the subsequent code execute only when a new bar ap
// This means that the entry signals will be checked only twice per bar.
/* static datetime current_bar_time = WRONG_VALUE;
datetime previous_bar_time = current_bar_time;
current_bar_time = iTime(Symbol(), Period(), 0);
static int ticks_of_new_bar = 0; // Process two ticks of each new bar to allow
if (current_bar_time == previous_bar_time)
{
ticks_of_new_bar++;
if (ticks_of_new_bar > 1) return; // Skip after two ticks.
}
else ticks_of_new_bar = 0; */
// The number is recalculated after the first call because some trades could ha
if (CountPositions() < MaxPositions) CheckEntrySignal(); // Check entry signals
}
//!! Uncomment and modify this buy entry signal check line:
//if ((Indicator_current > iClose(Symbol(), Period(), 1)) && (Indicator_previou
if (BuySignal)
{
OpenBuy();
}
// This is where you should insert your entry signal for SELL orders.
// Include a condition to open a sell order, the condition will have to set Sel
//!! Uncomment and modify this sell entry signal check line:
//if ((Indicator_current < iClose(Symbol(), Period(), 1)) && (Indicator_previou
if (SellSignal)
{
OpenSell();
}
}
Downloads