#What is an Expert Advisor?

An Expert Advisor (EA) is a small program that contains set of trading instructions for MetaTrader. You can also call it a Robot, because it does something instead of you. Expert Advisors can be put to work in real time or they can be back-tested over past periods of time.

Expert Advisors (EAs) are written in MQL4 (for MetaTrader 4) or MQL5 (for MetaTrader 5) programming language, and none of them can work on its own as a standalone program. To run any EA you need MetaTrader. MetaTrader loads your EA and feeds it with data, contros its events and executes all instructions inside.

Expert Advisors are files and you can find them at these locations:

  • For MetaTrader 4: "%Data Folder%/MQL4/Experts/"
  • For MetaTrader 5: "%Data Folder%/MQL5/Experts/"
Add Expert Advisor to the Chart

You can run as many EAs as you want at the same time, but you can only have 1 EA per chart.

Always backtest your EAs first, then try them on a live DEMO account. Then, and only if you are sure that your EA works properly and is not dangerous, allow it to wotk on a REAL account.

#What you can do with an Exper Advisor?

An Expert Advisor can...

  • Read your account information - Balance, Equity, Margin, Leverage, Server name, Account name and more
  • Communicate with you using different kind of messages - Alert message, Print message (Experts/Journal tabs), Message box, Write comment on the chart (upper left corner), Play sound, Send mail and more
  • Read OHLC (Open, High, Low, Close) data of any bar that you can see on the chart.
  • Read indicators data for each bar
  • Read files and write files
  • Send/Read data to other EAs using Global Variables (hit F3 on MetaTrader to see the list of Global Variables)
  • Create different objects on the chart (arrows, lines...) / Read attributes of created objects on chart / Modify their attributes
  • Create trades and pending orders / Access to all the current trades and orders and those from the history / Modify their parameters (stop-loss, take-profit, volume size...)
  • Do any mathematical and logical stuff like any other program
  • ... and more

#How does the Exper Advisor work?

In short, every EA works like this: Event => Checks and Calculations => Trading Actions

In general, your EA should get some data from MetaTrader, make some checks and calculations with that data and then tell MetaTrader what trading action to make - Buy, Sell or whatever.

Below you can see one very basic EA. You see that the blocks are put under the on Tick event, which means that the No trade block runs on every tick. When there is no trade, the Buy now block runs and creates a new trade:

For this example we have the following structure:

Event (on Tick) => Checks and Calculations (No trade) => Trading Actions (Buy now)

#Events

When an Event is detected by MetaTrader, it executes certain part of the EA code. The most important event is when you receive a new Tick and there are few other events:

It's very important to understand that every EA works because of few basic Events. Read about them below.

  • Tick - fired when a new tick comes. Normally the biggest portion of your EA works on every tick
  • Init - fired once when the EA starts
  • Deinit - fired once when the EA is stopped
  • Trade - fired every time you open, close or modify trades or orders
  • Timer - fired on certain period of time, for example every 60 seconds
  • Chart - fired when you create, modify, delete or click on objects on the chart