0% found this document useful (0 votes)
788 views10 pages

MT5 Expert Advisor Template, Guide

The MT5 expert advisor template includes well-structured code with comments, risk management features, and customizable parameters for trading strategies. It is not a fully automated trading strategy, requiring users to implement their own entry and exit signals. This template is designed for users with basic MQL5 programming knowledge and is intended for the MetaTrader 5 platform.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
788 views10 pages

MT5 Expert Advisor Template, Guide

The MT5 expert advisor template includes well-structured code with comments, risk management features, and customizable parameters for trading strategies. It is not a fully automated trading strategy, requiring users to implement their own entry and exit signals. This template is designed for users with basic MQL5 programming knowledge and is intended for the MetaTrader 5 platform.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

What does MT5 expert advisor

template include
Comments for each function to understand the logic

Well-written code

Modular structure

Risk management through automatic position size calculation

Selection of fixed or automatic stop-loss

Selection of fixed or automatic take-profit

Trading hours filter

Partial close

Custom testing parameter

https://www.earnforex.com/metatrader-expert-advisors/mt5-ea-template/ 4/10/25, 6 18 a.m.


Página 3 de 17
:
What MT5 expert advisor template IS
NOT
MT5 expert advisor template IS NOT a fully automated strategy.

https://www.earnforex.com/metatrader-expert-advisors/mt5-ea-template/ 4/10/25, 6 18 a.m.


Página 4 de 17
:
It is necessary to add your own entry and exit signals code to the template in order for it to
make any trades.

Why use MT5 expert advisor template


Save time — you can save many hours of learning and coding by using these ready-made
functions.

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.

What is the logic of an expert advisor

A basic EA is divided into three main functions:

 OnInit() is theTable of contents


initialization of the EA. It is the first function executed when you load the EA
on a chart. It runs only once.
 OnTick() runs every time MT5 receives a new quote for the current trading instrument.
 OnDeinit() executed once just after you remove the EA from the chart.

https://www.earnforex.com/metatrader-expert-advisors/mt5-ea-template/ 4/10/25, 6 18 a.m.


Página 5 de 17
:
Modules included in MT5 expert
advisor template
MT5 expert advisor template is written in a modular way so that each operation is logically
separated from the rest into independent functions. This allows for source code that is easier
to read and understand and also makes code customization easier.

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.

https://www.earnforex.com/metatrader-expert-advisors/mt5-ea-template/ 4/10/25, 6 18 a.m.


Página 6 de 17
:
Requirements to start using MT5
expert advisor template
MT5 expert advisor template is a great tool in the right hands, but it might not be for everyone.
Consider using this expert advisor template if you satisfy the following conditions.

Basic knowledge of MQL5 programming


Although most of the code is provided and it is well-commented, you need to be able to add
your own code for entry and exit signals and for any further customization (like trailing stop for
example).

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.

https://www.earnforex.com/metatrader-expert-advisors/mt5-ea-template/ 4/10/25, 6 18 a.m.


Página 7 de 17
:
Understanding how to compile
It is advisable to be familiar with the compilation process. This template is actually source code
to be edited in MetaEditor and compiled to become a working EA.

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.

From the source code


You can see some extracts from the code. This is a good way to understand if the product is
suitable for you.

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

https://www.earnforex.com/metatrader-expert-advisors/mt5-ea-template/ 4/10/25, 6 18 a.m.


Página 8 de 17
:
#property version "1.00"
#property copyright "EarnForex.com - 2024"
#property description "A basic expert advisor template for MT5."
#property description ""
#property description "WARNING: There is no guarantee that this expert advisor wi
#property description ""
#property description "Find more on www.EarnForex.com"
#property icon "\\Files\\EF-Icon-64x64px.ico"

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

input string Comment_1 = "=========="; // Trading Hours Settings


input bool UseTradingHours = false; // Limit trading hours
input ENUM_HOUR TradingHourStart = h07; // Trading start hour (Broker server hour)
input ENUM_HOUR TradingHourEnd = h19; // Trading end hour (Broker server hour)

input string Comment_2 = "=========="; // ATR Settings


input int ATRPeriod = 100; // ATR period
input ENUM_TIMEFRAMES ATRTimeFrame = PERIOD_CURRENT; // ATR timeframe
input double ATRMultiplierSL = 2; // ATR multiplier for stop-loss
input double ATRMultiplierTP = 3; // ATR multiplier for take-profit

// General input parameters


input string Comment_a = "=========="; // Risk Manageme
input ENUM_RISK_DEFAULT_SIZE RiskDefaultSize = RISK_DEFAULT_FIXED; // Position size
input double DefaultLotSize = 0.01; // Position size
input ENUM_RISK_BASE RiskBase = RISK_BASE_BALANCE; // Risk base

https://www.earnforex.com/metatrader-expert-advisors/mt5-ea-template/ 4/10/25, 6 18 a.m.


Página 9 de 17
:
input int MaxRiskPerTrade = 2; // Percentage to
input double MinLotSize = 0.01; // Minimum posit
input double MaxLotSize = 100; // Maximum posit
input int MaxPositions = 1; // Maximum numbe

input string Comment_b = "=========="; // Stop-Loss and


input ENUM_MODE_SL StopLossMode = SL_FIXED; // Stop-loss mod
input int DefaultStopLoss = 0; // Default stop-
input int MinStopLoss = 0; // Minimum allow
input int MaxStopLoss = 5000; // Maximum allow
input ENUM_MODE_TP TakeProfitMode = TP_FIXED; // Take-profit m
input int DefaultTakeProfit = 0; // Default take-
input int MinTakeProfit = 0; // Minimum allow
input int MaxTakeProfit = 5000; // Maximum allow

input string Comment_c = "=========="; // Partial Close


input bool UsePartialClose = false; // Use partial c
input double PartialClosePerc = 50; // Partial close
input double ATRMultiplierPC = 1; // ATR multiplie

input string Comment_d = "=========="; // Additional Se


input int MagicNumber = 0; // Magic number
input string OrderNote = ""; // Comment for o
input int Slippage = 5; // Slippage in p
input int MaxSpread = 50; // Maximum allow

Tick processing
// Entry and exit processing
void ProcessTick()
{
if (!GetIndicatorsData()) return;

if (CountPositions())

https://www.earnforex.com/metatrader-expert-advisors/mt5-ea-template/ 4/10/25, 6 18 a.m.


Página 10 de 17
:
{
// There is a position open. Manage SL, TP, or close if necessary.
if (UsePartialClose) PartialCloseAll();
CheckExitSignal();
}

// 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
}

Entry signal check template


// Entry signal
void CheckEntrySignal()
{
if ((UseTradingHours) && (!IsCurrentTimeInInterval(TradingHourStart, TradingHou

bool BuySignal = false;


bool SellSignal = false;

// Buy signal conditions

https://www.earnforex.com/metatrader-expert-advisors/mt5-ea-template/ 4/10/25, 6 18 a.m.


Página 11 de 17
:
// This is where you should insert your entry signal for BUY orders.
// Include a condition to open a buy order, the condition will have to set BuyS

//!! Uncomment and modify this buy entry signal check line:
//if ((Indicator_current > iClose(Symbol(), Period(), 1)) && (Indicator_previou

if (BuySignal)
{
OpenBuy();
}

// Sell signal conditions

// 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

https://www.earnforex.com/metatrader-expert-advisors/mt5-ea-template/ 4/10/25, 6 18 a.m.


Página 12 de 17
:

You might also like