Mql4 Course: Your First Expert Advisor
Mql4 Course: Your First Expert Advisor
By Coders guru
www.forex-tsd.com
-13Your First Expert Advisor
Part 1
--------------------
In the previous lesson we created our first indicator. Although this indicator wasnt
useful for us as trader, but it was very useful for us as programmers.
The indicators in general- are very important for the technical analysis of the market in
trying to predict the future prices.
But with the indicators we observe the chart then use our hands to sell, buy and modify
our orders manually. You have to set in front of your terminal, and keep your eyes widely
open.
If you get tired, want to drink a cup of tea or even take a short vacation. You have to
consider one of these solutions:
You may rent someone to observe the terminal for you and calling your mobile phone
every five minutes to tell you whats going on. If this employee is an expert, he will cost
you the pips you earn. And if he is novice one, he will cost you your capital.
The second solution is using a program to automate your trades.
Thats what the Expert Advisor is for.
The Expert advisor is a program wrote in MQL4 (we are studying MQL4 huh?) uses your
favorite indicators and trade methods to automate your orders.
It can buy, sell and modify the orders for you. It enables you to drink a cup of tea and
save the salary you gave out to your employee or the bunch of flowers you bring to your
assistant wife.
Today we are going to create our first expert advisor so lets go.
From the MetaEditor File menu click New (you can use CTRL+N hotkey or click the
New icon in the standard toolbar). That will pop up the new program wizard which you
have seen when you created your first indicator (Figure 1).
This time we will choose the first option Expert Advisor program then click Next
button.
Step2:
When you clicked Next, you have got the general properties wizard (Figure 2).
This wizard enables you to set the properties of your expert advisor and to set the external
variables you will use in your expert advisor.
In this step you can set these properties:
1234-
This is the list of the external variables you allow the user of your expert advisor to
change them from the Expert properties window.
To add a new variable you click the Add button, clicking it will add a new record to
the external variables list. Every record has three fields:
Name: double click this field to set the name (identifier) of the variable.
Type: double click this field to set the data type of the variable.
Initial value: double click this field to give your variable initialization value.
This field is optional which means you can leave it without setting
In our sample we have added three variables:
Varaible Type initial value
--------------------------------------TakeProfit double 350
Lots double 0.1
TrailingStop double 35
As you see above, the code generated by the wizard is a template for you to add your code without
bothering you by typing the main functions from scratch.
Now lets add our own code:
//+------------------------------------------------------------------+
//|
My_First_EA.mq4 |
//|
Coders Guru |
//|
http://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright "Coders Guru"
#property link
"http://www.forex-tsd.com"
//---- input parameters
extern double
TakeProfit=250.0;
extern double
Lots=0.1;
extern double
TrailingStop=35.0;
//+------------------------------------------------------------------+
//| expert initialization function
|
//+------------------------------------------------------------------+
int init()
{
//---//---return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function
|
//+------------------------------------------------------------------+
int deinit()
{
//---//---return(0);
}
int Crossed (double line1 , double line2)
{
static int last_direction = 0;
static int current_dirction = 0;
if(line1>line2)current_dirction = 1; //up
if(line1<line2)current_dirction = 2; //down
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
shortEma = iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,0);
longEma = iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,0);
int isCrossed
= Crossed (shortEma,longEma);
total = OrdersTotal();
if(total < 1)
{
if(isCrossed == 1)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,
"My EA",12345,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
if(isCrossed == 2)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,
Bid-TakeProfit*Point,"My EA",12345,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
Note: dont copy and paste the code above because it warped and will not work for you,
use the code provided with lesson in www.forex-tsd.com .
Scared?
Dont be scared of the 160 lines you have seen above, we will know everything about this
code line by line, I promise its an easy task.
We have a lot to say and to do in the next lesson; I hope you are ready for the challenge.
I welcome very much the questions and the suggestions.
See you
Coders Guru
24-11-2005