Creating our First Expert Advisor ~ forex trading tips provider
Creating our First Expert Advisor
Lets go through a simple example
Open the MetaEditor application. You should see a screen similar to below (fig. 7).
fig. 7
Create a new Expert Advisor (EA) by selecting File, then New (fig. 7b)
fig. 7b
You should see the Expert Advisor Wizard appear (fig. 8).
Select Expert Advisor and click Next
fig. 8
Expert Advisor Wizard
The wizard will next prompt you to enter the Name, Author, website link and parameters of the EA. Name your EA as SimpleEA (fig. 9).
fig. 9
Parameters allow our EA to reference to pre-defined values determined by us.
Common pre-defined values in EA forex trading are Taking Profits, Trading Lots and Trailing Stop loss levels. We will ignore this for now. Click Finish.
Your EA named as SimpleEA.mq4 should now be listed in the right panel (fig. 10), known as the Navigator.
The panel contains the list of EAs that exists in your directory. The code for SimpleEA will be listed in the code editor left panel.
fig. 10
In the code editor, notice that we have the skeleton code generated for us. In the codes, there are three methods, init, deinit and start.
int init()
{
//----
return(0);
}
int deinit()
{
//----
//----
return(0);
}
int start()
{
//----
//----
return(0);
}
//+---------------------+
Methods represent a block of codes in brackets {}. This block of codes performs a specific function.
The init() method
We first explain the init() method. The init method is the method that is called when the EA first starts up.
int init()
{
//----
return(0);
}
It is usually used to initialize variables that we need in the program.
The deinit() method
Next, we explain the deinit() method. The deinit method is the opposite of the init() method. That is, it is the method that is called when the EA program ends.
int deinit()
{
//----
return(0);
}
It is usually used to de-initialize variables when we exit the EA program.
The start() method
Next, we explain the start() method. The start method is the most important method in the program. Bulk of the decision making code will be in the start method. The start method is called every time there is a new quotation of currency rates. This is frequently every 3-4 seconds.
Essentially, our program will evaluate its decision making in every time there is a new rate.
int start()
{
//----
return(0);
}
Life Cycle of the EA
Hence, the life cycle of the EA can be understood as that it will start from init(), then start() is called for every new currency rate quotation, and finally, deinit() when the EA ends.
Having understood the life cycle of an EA, in the code editor panel, fill in the codes in bold below into the start() method.
int start()
{
//----
Print("Bid rate for "+Symbol() +" is " + Bid);
//----
return(0);
}
As an example for the EURUSD pair, what this EA does is that it will print the EURUSD rate every time there is a new rate quotation. Symbol() is a function that returns the current currency pairs name. Bid returns the bid rate for the pair.
Thus, the message Bid rate for EURUSD is 1.4330 for every rate quotation will be printed.
Compiling
Once done, click on Compile to compile the EA (fig. 12).
fig. 12
Click on Terminal to go to the MetaTrader application.
MetaTrader Application
In the MetaTrader left pane, under Expert Advisors, you should see SimpleEA there (fig. 13). This is because you have previously compiled your EA in MetaEditor and it is now ready to be deployed for trading.
fig. 13
Click on SimpleEA and the following form will appear (fig. 13b). Check on the Allow live trading checkbox. This will indicate that this EA is ready for live trading.
fig. 13b
Activating your EA for live trading
To activate your EA for live trading, click on the Expert Advisor button on the top bar.
fig. 14
Your SimpleEA EA is now live. This can also be seen by the smilie face at the top right hand corner of a currency pair chart.
In the below figure, SimpleEA is deployed live to the currency pair EURUSD.
As the EA runs on the currency pair, you can notice the log messages that is printed out by the earlier code you have added.
Just below the charts, click on the Experts tab. This tab shows all the messages printed by the EA. Notice what is printed out.
SimpleEA EURUSD, M5: Bid rate for EURUSD is 1.433250000
fig. 15
By default, the SimpleEA EURUSD, M5 is printed our for you indicating that this message was printed out by SimpleEA on EURUSD pair for timeframe of 5 minutes.
After that, we see our message Bid rate for EURUSD is 1.433250000 being printed out.
Click on the Expert Advisor button on the top panel again to de-activate the Expert Advisor.
Notice that the smilie face on the top right hand corner changes to a cross now indicating that it is de-activated. i.e. will not trade. (fig. 15b)
fig. 15b
You have completed your first EA!
So there you have it, your first EA! Although it doesnt really do much, you have learned important concepts on the basic structure of an EA, namely,
- The life-cycle of an EA, constituted by the init, start and deinit methods,
- Compiling and deploying the EA live,
- Monitoring the running of an EA
- and de-activating an EA.
More info for Creating our First Expert Advisor ~ forex trading tips provider:
0 comments:
Post a Comment