Anthony Darvall Leaves easyMarkets after 15 Years, Launches Startup

Algo Trading: How to Automate Trading Process and Avoid Human Mistakes

by FM
Disclaimer
  • To maximize profits, traders should react quickly, be mentally agile, and control emotions
algo trading
Join our Telegram channel

Constant news monitoring, data analysis, patterns and price change tracking are part of a trader's routine that takes a long time, concentration, and much effort. This is challenging even for experienced traders.

To maximize profits, traders should react quickly, be mentally agile, and control emotions. Even simple fatigue can lead to trading losses. So automation of market analysis and data collection is a good way to save time and nerves and avoid human mistake factors. Traders who use automated systems and algorithms are called algo traders.

What and why to automate?

Any process can be automated, from data collection and analysis to the latest events notifications (for example, via Telegram). Process automation allows traders to avoid human errors, spend more time for themselves or family, relax, and recover.

How to start?

The MetaTrader platform provides the MQL programming language to automate most trading processes. Thanks to this specially adapted language, even beginners in programming can use the whole range of MetaTrader tools, from getting indicator signals to fully automated trading based on their algorithm.

The MQL4 and MQL5 languages corresponding to MetaTrader 4 and MetaTrader 5 differ from each other. For newbies in programming, it's better to start with MQL4 because you can even write a simple trading robot (EA - expert advisor) using this language. By the way, we recommend visiting the website page [https://www.mql5.com/] to find more articles, scripts, and other materials about algo trading and ask like-minded people any questions on the forum.

MQL4 is simple

We’ll demonstrate the simplicity of working with MQL4 by describing a simple robot (EA) in MQL4.

First, open an account with any of the brokers. We advise the FBS broker and their site FBS.com due to its international licenses, good reputation, and favorable conditions for algo traders. When your account is opened, log in to it via MT4.

Open MT4 Trading Platform and follow these steps: In the Navigator window, find the Expert Advisor section, right-click, and select Create in MetaEditor. Next, select Expert Advisor (template) – Next – Specify a name (Expert\FirstEA, etc.) – Next – Next – Done. The MetaEditor code editor window will open.

Below is a full listing of the program, which you can copy into MetaEditor and click Compile. You will see that your EA named FirstEA (etc.) has appeared in the Expert Advisors section.

#property copyright "Copyright 2017, MetaQuotes Software Corp."

#property link "https://www.mql5.com"

#property version "1.00"

#property strict

//+------------------------------------------------------------------+

//| Expert initialization function |

//+------------------------------------------------------------------+

int OnInit()

{

//---

//---

return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+

//| Expert deinitialization function |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

//---

}

//+------------------------------------------------------------------+

//| Expert tick function |

//+------------------------------------------------------------------+

void OnTick()

{

if(iRSI(NULL,0,14,PRICE_CLOSE,0)>=70 && AmountSell()==0)

{

if (AmountBuy()>0) CloseBuy();

OrderSend(Symbol(),1,0.01,Bid,0,0,0,"",0,0,0);

}

if(iRSI(NULL,0,14,PRICE_CLOSE,0)<=30 &&="" AmountBuy()="=0)

{

if (AmountSell()>0) CloseSell();

OrderSend(Symbol(),0,0.01,Ask,0,0,0,"",0,0,0);

}

}

//+------------------------------------------------------------------+

//Close Sell orders function

void CloseSell()

{

for(int i=OrdersTotal()-1; i>=0; i--)

{

if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;

if(OrderType()==OP_SELL) OrderClose(OrderTicket(), OrderLots(),Ask,0,0);

}

}

//+------------------------------------------------------------------+

//Close Buy orders function

void CloseBuy()

{

for(int i=OrdersTotal()-1; i>=0; i--)

{

if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;

if(OrderType()==OP_BUY) OrderClose(OrderTicket(), OrderLots(),Bid,0,0);

}

}

//+------------------------------------------------------------------+

//Counting the number of open Buy orders

int AmountBuy()

{

int amount = 0;

for(int i = OrdersTotal() -1; i>=0; i--)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

{

if (OrderType()==OP_BUY)

amount++;

}

}

return(amount);

}

//+------------------------------------------------------------------+

//Counting the number of open Sell orders

int AmountSell()

{

int amount = 0;

for(int i = OrdersTotal() -1; i>=0; i--)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

{

if (OrderType()==OP_SELL)

amount++;

}

}

return(amount);

}

Try to start your EA on your account by moving it to the chart. You also need to activate the AutoTrading button (at the top of MT4) and allow automated trading in the Tools – Options – Expert Advisors – Allow automated trading settings.

Now, if the value of the RSI (Relative Strength Index) indicator is below 30, a Buy order with a volume of 0.01 is opened, while an opened Sell order is closed. If the indicator value is above 70, a Sell order with a volume of 0.01 is opened, while an opened Buy order is closed. That’s it! We launched a trading robot based on the values of the RSI indicator.

What does a code mean?

Let’s understand the code’s structure. The code consists of one main function and several auxiliary functions.

The main function is OnTick() which executes its code (inside curly braces) every tick. A tick is an event when the instrument price direction changes. OnTick() is the main function where other functions are called to help perform special actions, such as closing and counting opened Sell and Buy orders.

Auxiliary functions are:
CloseSell() - closes all Sell orders
CloseBuy() - closes all Buy orders
AmountSell()- counts all Sell orders
AmountBuy()- counts all Buy orders

Imagine your TV is broken, and you call a specialist to fix it because you don't know the TV mechanisms, but a specialist knows everything about fixing the device. It means this specialist serves a particular function for you – a TV repair. Similarly, the main function calls OnTick() auxiliary functions to perform certain actions.

if(iRSI(NULL,0,14,PRICE_CLOSE,0)>=70 && AmountSell()==0)

The above code literally means the following: if the value of the RSI indicator is greater than or equal to 70 and the number of opened Sell orders is 0. To avoid opening more than one Sell order, we call for the “AmountSell” function, which only counts the number of opened Sell orders.

Then we check if there are open Buy orders, and if there are (greater than 0), then we close them:

if (AmountBuy()>0) CloseBuy()

Here, we call the CloseBuy() function that helps us to close Buy orders. This function only performs the closing Buy orders, nothing else.

Then, we open a Sell order:

OrderSend(Symbol(),1,0.01,Bid,0,0,0,"",0,0,0);

Similarly, we write a condition for opening a Buy order:

if(iRSI(NULL,0,14,PRICE_CLOSE,0)<=30 &&="" AmountBuy()="=0)

The above condition similarly checks the value of the RSI indicator, and if it is less than 30, then Sell orders are closed and Buy orders are opened, i.e., actions described in curly brackets below.

It's not that hard, is it?

To get more information on the functions provided by the MQL4 language, choose the function you are interested in and press F1. There you will find relevant documentation and descriptions of all features.

About FBS

FBS is an international licensed broker (IFSC license) providing global markets with transparent and reliable services and products for professional and semi-professional CFD and Margin FX traders. With its solid experience of 13 years, high-quality services, and dozens of awards, FBS conquered the trust of 27M+ clients and became the Official Principal Partner of Leicester City Football Club.

Constant news monitoring, data analysis, patterns and price change tracking are part of a trader's routine that takes a long time, concentration, and much effort. This is challenging even for experienced traders.

To maximize profits, traders should react quickly, be mentally agile, and control emotions. Even simple fatigue can lead to trading losses. So automation of market analysis and data collection is a good way to save time and nerves and avoid human mistake factors. Traders who use automated systems and algorithms are called algo traders.

What and why to automate?

Any process can be automated, from data collection and analysis to the latest events notifications (for example, via Telegram). Process automation allows traders to avoid human errors, spend more time for themselves or family, relax, and recover.

How to start?

The MetaTrader platform provides the MQL programming language to automate most trading processes. Thanks to this specially adapted language, even beginners in programming can use the whole range of MetaTrader tools, from getting indicator signals to fully automated trading based on their algorithm.

The MQL4 and MQL5 languages corresponding to MetaTrader 4 and MetaTrader 5 differ from each other. For newbies in programming, it's better to start with MQL4 because you can even write a simple trading robot (EA - expert advisor) using this language. By the way, we recommend visiting the website page [https://www.mql5.com/] to find more articles, scripts, and other materials about algo trading and ask like-minded people any questions on the forum.

MQL4 is simple

We’ll demonstrate the simplicity of working with MQL4 by describing a simple robot (EA) in MQL4.

First, open an account with any of the brokers. We advise the FBS broker and their site FBS.com due to its international licenses, good reputation, and favorable conditions for algo traders. When your account is opened, log in to it via MT4.

Open MT4 Trading Platform and follow these steps: In the Navigator window, find the Expert Advisor section, right-click, and select Create in MetaEditor. Next, select Expert Advisor (template) – Next – Specify a name (Expert\FirstEA, etc.) – Next – Next – Done. The MetaEditor code editor window will open.

Below is a full listing of the program, which you can copy into MetaEditor and click Compile. You will see that your EA named FirstEA (etc.) has appeared in the Expert Advisors section.

#property copyright "Copyright 2017, MetaQuotes Software Corp."

#property link "https://www.mql5.com"

#property version "1.00"

#property strict

//+------------------------------------------------------------------+

//| Expert initialization function |

//+------------------------------------------------------------------+

int OnInit()

{

//---

//---

return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+

//| Expert deinitialization function |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

//---

}

//+------------------------------------------------------------------+

//| Expert tick function |

//+------------------------------------------------------------------+

void OnTick()

{

if(iRSI(NULL,0,14,PRICE_CLOSE,0)>=70 && AmountSell()==0)

{

if (AmountBuy()>0) CloseBuy();

OrderSend(Symbol(),1,0.01,Bid,0,0,0,"",0,0,0);

}

if(iRSI(NULL,0,14,PRICE_CLOSE,0)<=30 &&="" AmountBuy()="=0)

{

if (AmountSell()>0) CloseSell();

OrderSend(Symbol(),0,0.01,Ask,0,0,0,"",0,0,0);

}

}

//+------------------------------------------------------------------+

//Close Sell orders function

void CloseSell()

{

for(int i=OrdersTotal()-1; i>=0; i--)

{

if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;

if(OrderType()==OP_SELL) OrderClose(OrderTicket(), OrderLots(),Ask,0,0);

}

}

//+------------------------------------------------------------------+

//Close Buy orders function

void CloseBuy()

{

for(int i=OrdersTotal()-1; i>=0; i--)

{

if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;

if(OrderType()==OP_BUY) OrderClose(OrderTicket(), OrderLots(),Bid,0,0);

}

}

//+------------------------------------------------------------------+

//Counting the number of open Buy orders

int AmountBuy()

{

int amount = 0;

for(int i = OrdersTotal() -1; i>=0; i--)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

{

if (OrderType()==OP_BUY)

amount++;

}

}

return(amount);

}

//+------------------------------------------------------------------+

//Counting the number of open Sell orders

int AmountSell()

{

int amount = 0;

for(int i = OrdersTotal() -1; i>=0; i--)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

{

if (OrderType()==OP_SELL)

amount++;

}

}

return(amount);

}

Try to start your EA on your account by moving it to the chart. You also need to activate the AutoTrading button (at the top of MT4) and allow automated trading in the Tools – Options – Expert Advisors – Allow automated trading settings.

Now, if the value of the RSI (Relative Strength Index) indicator is below 30, a Buy order with a volume of 0.01 is opened, while an opened Sell order is closed. If the indicator value is above 70, a Sell order with a volume of 0.01 is opened, while an opened Buy order is closed. That’s it! We launched a trading robot based on the values of the RSI indicator.

What does a code mean?

Let’s understand the code’s structure. The code consists of one main function and several auxiliary functions.

The main function is OnTick() which executes its code (inside curly braces) every tick. A tick is an event when the instrument price direction changes. OnTick() is the main function where other functions are called to help perform special actions, such as closing and counting opened Sell and Buy orders.

Auxiliary functions are:
CloseSell() - closes all Sell orders
CloseBuy() - closes all Buy orders
AmountSell()- counts all Sell orders
AmountBuy()- counts all Buy orders

Imagine your TV is broken, and you call a specialist to fix it because you don't know the TV mechanisms, but a specialist knows everything about fixing the device. It means this specialist serves a particular function for you – a TV repair. Similarly, the main function calls OnTick() auxiliary functions to perform certain actions.

if(iRSI(NULL,0,14,PRICE_CLOSE,0)>=70 && AmountSell()==0)

The above code literally means the following: if the value of the RSI indicator is greater than or equal to 70 and the number of opened Sell orders is 0. To avoid opening more than one Sell order, we call for the “AmountSell” function, which only counts the number of opened Sell orders.

Then we check if there are open Buy orders, and if there are (greater than 0), then we close them:

if (AmountBuy()>0) CloseBuy()

Here, we call the CloseBuy() function that helps us to close Buy orders. This function only performs the closing Buy orders, nothing else.

Then, we open a Sell order:

OrderSend(Symbol(),1,0.01,Bid,0,0,0,"",0,0,0);

Similarly, we write a condition for opening a Buy order:

if(iRSI(NULL,0,14,PRICE_CLOSE,0)<=30 &&="" AmountBuy()="=0)

The above condition similarly checks the value of the RSI indicator, and if it is less than 30, then Sell orders are closed and Buy orders are opened, i.e., actions described in curly brackets below.

It's not that hard, is it?

To get more information on the functions provided by the MQL4 language, choose the function you are interested in and press F1. There you will find relevant documentation and descriptions of all features.

About FBS

FBS is an international licensed broker (IFSC license) providing global markets with transparent and reliable services and products for professional and semi-professional CFD and Margin FX traders. With its solid experience of 13 years, high-quality services, and dozens of awards, FBS conquered the trust of 27M+ clients and became the Official Principal Partner of Leicester City Football Club.

FM
Disclaimer
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|} !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}