Here you can download a Metatrader 4 Expert Advisor Template which you can use to start to build your automated system. The template has a lot of tools, all of them works fine (I use it on live), you only have to add the rules for open an close position. I've written this Expert Advisor, some piece of the code are mine, others has been modified from open source EAs I've found on the net.

The template includes a ma croos example for open and close position, it just an example, please don´t use it with real money!!!

Download CyberEA-Template

Start() function



The start() is the only one that is needed in order to work. This function is executed every tick is recieved in Metatrader. So you must set in this function all parameters and variables that have to be calculate at each tick and call the others functions based on it.
In CyberEA-Template, all function calls is envolved into trading hours variables.

void start(){
First piece of code of the loop: check for trade stratus and trade context:
bool tradeallowed=IsTradeAllowed();
if(!tradeallowed){
Print("Trade not allowed");
}
bool contextbusy=IsTradeContextBusy();
if (contextbusy){
Print("Trade context is busy");
}
Next part: On each tick CyberEA take the current time as value of the local variable named hour. Based on the value of hour we set the next action. On this way, if it's time to trade (we are set the trading hours in the external varialbes) . In this way if hour is equal to expiration hour the function Clean() (for close all orders) is called. If hour is between start and end hour the function CheckForSignal() (check for signal) is called. Also is called the function SearhPositions() (check for orders) and if SearchPositons() is not equal to zero the EA will call CheckForClose() (check for close). If SearchPositions() is equal to zero CheckForOpen() function (check for open) is called. Also I have to comment that TrailStop and BreakEven function will be called only if hour is between trading hours and if we have set it to true in the external variables.
int hour = Hour(); if (hour == Expiration) Clean(); if ((StartHour <= hour) && (EndHour >= hour)) {
CheckForSignal();
if (SearchPositions() != 0)
CheckForClose(); else CheckForOpen();
if (SearchPositions() != 0) {
if (UseTrailingStop == true) TrailStop();
if (UseBreakEven == true) BreakEven();
}
}

Commenting(); Commenting function is called on each tick.
}

Head and global variables



Head and global vaiables are the first piece of code of the expert advisor where you must set the copyright information, the variables that you are going to use in some functions and the externs variables that you are going to use in some functions and you want to be able for after compile changes.

Let go to comment this piece of code:

Head with copyright information:
#property copyright "Copyright 2008, http://cablebot.blogspot.com/"
#property link "http://cablebot.blogspot.com/"

stdlib library call:
#include <stdlib.mqh>

external variables:
extern string Version = "CyberEA"; This is a comment asociate to all orders open by this expert advisor. This comment can be used to identify all orders from the EA.
extern string Settings="Settings"; Here start the external variables that you can chage in the experts properties window at the time of use. Set the default values before compile.

Trading hours:
extern int StartHour = 0;
extern int EndHour = 24;
extern int Expiration = 24;

Slippage:
extern int Slippage = 5;

Order take profit and stoploss:
extern double TakeProfit =450;
extern double StopLoss = 150;

Trainling stop:
extern bool UseTrailingStop = false;
extern int TrailingStart = 80;
extern int TrailingStop =100;

BreakEven or Profit Protect variables:
extern bool UseBreakEven = false;
extern int ProfitProtectStart1 = 60;
extern int ProfitToProtect1 =30;
extern int ProfitProtectStart2 = 120;
extern int ProfitToProtect2 =60;
extern int ProfitProtectStart3 = 180;
extern int ProfitToProtect3 =90;
extern int ProfitProtectStart4 = 240;
extern int ProfitToProtect4 =120;

Money Management tool:
extern string MM = "Money Management";
extern bool UseMoneyManagement=true;
extern bool UseMicroLots = false;
extern int MaximumRisk = 5;
extern string FixedLot = "Fixed Lots";
extern double FixedLots = 0.1;

Magic Number: This number is used to identify singles orders from the EA, be sure that is different for each chart and for each currencies pair you trade.
extern int MagicNumber = 852745962;

Indicators settings:
extern int FastMAPeriod = 57;
extern int FastMAShift = 2;
extern int FastMAMode = MODE_EMA;
extern int FastMAApplyPrice = PRICE_CLOSE;
extern int SlowMAPeriod = 19;
extern int SlowMAShift = 12;
extern int SlowMAMode = MODE_EMA;
extern int SlowMAApplyPrice = PRICE_CLOSE;
extern int Bar = 1;

Others global variables the EA use to calculate some parameters in several loops of the automated system.The next variables can not been change after compilation.
int Number = 20;
int Kumo = 1;
bool signalBuy;
bool signalSell;
bool goShort;
bool goLong;
int ordersent;
int Arko;
int time_now;
double fastMA;
double slowMA;
int conditionalcheck = 0;
double LotSize;

If you want to change the name of some variables you can do but make sure you change it every time the same variable appear in the expert advisor code.

In the next posts I will comment all the loops and functions of the EA line by line.

CyberEA-Template Overview



CyberEA-Template is an Expert Avsisor for Metatrader, this is, an automated trading program. Please note that I've not said system, the system must be added to the Template. As an example the cyberEA has a MA cross simple rules.

CyberEA include a lot of tools, all of them works fine: I can say that because I use the exact same code for my live trading without errors, obviously with differents rules for open and close position.

Head and global variables

Head: copyriht, owner link, etc.
Global variables: In this piece of the code we set all variables that we are going to use in several function of CyberEA. I prefer not inlcude here variables that are only used in one function, in this case the variable will be generated at he specific loop.

Start() Function

This function is the only one that is executed every tick that CyberEA recieve from the broker.
Every time this function is executed all the parameters in this loop are calculated and based on the result others function can be called.

Money Management: LotRisk()
In this loop CyberEA will calculate the lot size for the orders.

Check for Orders: BuscarOdernes()
CyberEA use this one for find orders from the Expert Advisors and return a certain value for a variable that will be use later in other functions and loops.

Check for Signal: BuscarSenal()
This is the core of the EA. Here is where you must write the indicators an rules for open and close position.

Check for Open: BuscarAbrir()
When this loop is executed the EA will take action based on the result of BuscarSenal(): The EA will send orders to the broker.

Check for Close: BuscarCierre()
Like BuscarAbrir() this loop will take action based on the result of BuscarSenal(), in this case for close position, not for open.

TrailStop():
I think all of you know what means trailstop ;))

BreakEven():
Loop used for modifiying orders and move stoploss to a certain levels.

Read carefully details of each function and start to code your BuscarSenal() loop with your indicators and rules.

Before starting


What are you going to programming?
Before you start to write the automated system you have to have a full and clear description of your system. Let take an example with a MA cross system:

Overview: signals for open and close position are going to be generated by a fast and low MA croos. If fast MA cross up slow MA open buy and close sell if there is some one.

This is easy but you are going to programming the rules so you have to take into consideration all variables:

- Type of MA? Simple, exponential, linear, ...

- Period?

- Apply price? The value are going to be calculated for close price of bar, open , high, low, middle, ...

- When the system are going to take action: a MA cross can happen in the current bar several times so maybe you want to wait to the current bar be completed.

- Are you going to use others indicators for confirmation?

- Which time frame are the system going to work?

All of this questions and others have to be answer before programming. Anyway you can set this variables as external variable for modify them later and try several configuration, so if this questions have no answer at the first moment they must be taken into consideration. If you download the CyberEA-Template you can see that all variables of the Fast and Slow MA are externals variables, so is easy change the configuration after compilation on the properties of the expert advisor.

Message Board

Do you have questions or comments? Use comments of this post to make them.