PHP Code:
//+------------------------------------------------------------------+
//| EMA_CROSS_2.mq4 |
//| Coders Guru |
//| http://www.forex-tsd.com |
//+------------------------------------------------------------------+
// ultima versiune cu micro lots! H1 si D1
#property copyright "Coders Guru"
#property link "http://www.forex-tsd.com"
//---- Trades limits
extern double
TakeProfit = 10,
TrailingStop = 20,
StopLoss = 20;
extern bool
UseStopLoss = false;
//---- EMAs paris
extern int
ShortEma = 1,
LongEma = 5;
//---- Crossing options
extern bool
immediate_trade = true, //Open trades immediately or wait for cross.
reversal = false, //Use the originally reversal crossing method or not
ConfirmedOnEntry = false;
//---- Money Management
extern double
Lots = 1,
HedgePercent = 1; // Used to calcualte the what percent of the lots the user wants to be
// used in the hedged position
extern bool
MM = true, //Use Money Management or not
AccountIsMicro = true; //Use Micro-Account or not
extern int
StartHour = 0, //Indicates when the user wants the EA to start trading
StopHour = 23; //Indicates when the user wants the EA to stop trading
extern int
Risk = 10; //10%
extern int
MAGICMA = 20060301;
extern bool
Show_Settings = true;
//---- Global varaibles
static int
TimeFrame = 0;
datetime
CheckValueTime;
//---- Trend bands
double upper[], middle1[], middle2, lower[];
double Xup[], Xdown[];
extern int period = 34;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
if(Show_Settings) Print_Details();
else Comment("");
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
TimeFrame=Period(); //Prevent counting the cross while the user changing the timeframe
return(0);
}
bool isNewSumbol(string current_symbol)
{
//loop through all the opened order and compare the symbols
int total = OrdersTotal();
for(int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
string selected_symbol = OrderSymbol();
if (current_symbol == selected_symbol)
return (False);
}
return (True);
}
int Crossed (double line1 , double line2)
{
static int last_direction = 0;
static int current_direction = 0;
if(TimeFrame!=Period())
{
TimeFrame=Period();
return (0);
}
if(line1>line2)current_direction = 1; //up
if(line1<line2)current_direction = 2; //down
if(immediate_trade==false)
{
if(last_direction == 0) //first use
{
last_direction = current_direction;
return(0);
}
}
if(current_direction != last_direction) //changed
{
last_direction = current_direction;
return (last_direction);
}
else
{
return (0); //not changed
}
}
//--- Bassed on Alex idea! More ideas are coming
double LotSize()
{
double lotMM = MathCeil(AccountFreeMargin() * Risk / 1000) / 100;
if(AccountIsMicro==false) //normal account
{
if (lotMM < 0.1) lotMM = Lots;
if ((lotMM > 0.5) && (lotMM < 1)) lotMM=0.5;
if (lotMM > 1.0) lotMM = MathCeil(lotMM);
if (lotMM > 100) lotMM = 100;
}
else //micro account
{
if (lotMM < 0.01) lotMM = Lots;
if (lotMM > 1.0) lotMM = MathCeil(lotMM);
if (lotMM > 100) lotMM = 100;
}
return (lotMM);
}
string BoolToStr ( bool value)
{
if(value) return ("True");
else return ("False");
}
void Print_Details()
{
string sComment = "";
string sp = "----------------------------------------\n";
string NL = "\n";
sComment = sp;
sComment = sComment + "TakeProfit=" + DoubleToStr(TakeProfit,0) + " | ";
sComment = sComment + "TrailingStop=" + DoubleToStr(TrailingStop,0) + " | ";
sComment = sComment + "StopLoss=" + DoubleToStr(StopLoss,0) + " | ";
sComment = sComment + "UseStopLoss=" + BoolToStr(UseStopLoss) + NL;
sComment = sComment + sp;
sComment = sComment + "immediate_trade=" + BoolToStr(immediate_trade) + " | ";
sComment = sComment + "reversal=" + BoolToStr(reversal) + NL;
sComment = sComment + sp;
sComment = sComment + "Lots=" + DoubleToStr(Lots,0) + " | ";
sComment = sComment + "MM=" + BoolToStr(MM) + " | ";
sComment = sComment + "Risk=" + DoubleToStr(Risk,0) + "%" + NL;
sComment = sComment + sp;
Comment(sComment);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start() {
int limit;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
double avg;
for(int x=0; x<limit; x++) {
Xdown[x] = 0; Xup[x] = 0;
middle1[x] = iMA(NULL, 0, period, 0, MODE_EMA, PRICE_TYPICAL, x);// drawn line
middle2= iMA(NULL, 0, period, 0, MODE_SMA, PRICE_TYPICAL, x);// only used to calculate outer bands
avg = findAvg(period, x);
upper[x] = middle2 + (3.5*avg);
lower[x] = middle2 - (3.5*avg);
if (MathAbs(upper[x] - High[x]) < 2*Point)
{
Xdown[x] = upper[x];
if (NewBar() && x == 0)
Alert(Symbol()," ",Period()," reach upper edge");
}
if (MathAbs(lower[x] - Low[x]) < 2*Point)
{
Xup[x] = lower[x];
if (NewBar() && x == 0)
Alert(Symbol()," ",Period()," reach lower edge");
}
}
return(0);
}
//+------------------------------------------------------------------+
double findAvg(int period, int shift) {
double sum=0;
for (int x=shift;x<(shift+period);x++) {
sum += High[x]-Low[x];
}
sum = sum/period;
return (sum);
}
bool NewBar()
{
static datetime dt = 0;
if (dt != Time[0])
{
dt = Time[0];
return(true);
}
return(false);
//end of trend bands indicator
{
if(Hour() >= StartHour && Hour() <= StopHour){ //Nothing will happen unless the time is within the trading time
int cnt, ticket, total;
double SEma, LEma, SEmaLAST, LEmaLAST;
string comment = "";
if(reversal==true) comment = "EMA_CROSS_Counter-Trend";
if(reversal==false) comment = "EMA_CROSS_Trend-Following";
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<1)
{
Print("TakeProfit less than 1");
return(0); // check TakeProfit
}
static int isCrossed = 0;
if(ConfirmedOnEntry)
{
if(CheckValueTime==iTime(NULL,TimeFrame,0)) return(0); else CheckValueTime = iTime(NULL,TimeFrame,0);
SEma = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,1);
LEma = iMA(NULL,0,LongEma ,0,MODE_EMA,PRICE_CLOSE,1);
SEmaLAST = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,2);
LEmaLAST = iMA(NULL,0,LongEma ,0,MODE_EMA,PRICE_CLOSE,2);
if(SEmaLAST<LEmaLAST && SEma>LEma) isCrossed = 1;
if(SEmaLAST>LEmaLAST && SEma<LEma) isCrossed = 2;
}
else
{
SEma = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,0);
LEma = iMA(NULL,0,LongEma ,0,MODE_EMA,PRICE_CLOSE,0);
isCrossed = Crossed (LEma,SEma);
}
if(reversal==false)
{
if(isCrossed==1) isCrossed = 2;
else if(isCrossed==2) isCrossed = 1;
}
if(MM==true) Lots = LotSize(); //Adjust the lot size
total = OrdersTotal();
// TRAILING STOP
no errors so far from the compiler..