EMA Cross

//---- input parameters
extern double StopLoss = 30;
extern double TakeProfit = 1000;
extern double Lots = 0.1;
extern double TrailingStop = 200;
extern int ShortEma = 5;
extern int LongEma = 13;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function +
//| This is where we initialize internal variables for program use +
//+------------------------------------------------------------------+
int start()
{
//----
//LotCalc Declarations
int cnt, ticket, total;
//MAs
double FEMAnow,FEMAprevious,FEMA2previous,SEMAnow,SEMAprevious,SEMA2previous;
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
//MA and Filter Variable Definition
FEMAnow = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,0);
FEMAprevious = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,1);
FEMA2previous = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,2);
SEMAnow = iMA(NULL,0,LongEma,0,MODE_EMA,PRICE_OPEN,0);
SEMAprevious = iMA(NULL,0,LongEma,0,MODE_EMA,PRICE_OPEN,1);
SEMA2previous = iMA(NULL,0,LongEma,0,MODE_EMA,PRICE_OPEN,2);
int isCrossed = 0;
if (FEMAnow > SEMAnow && FEMAprevious > SEMAprevious && FEMA2previous < SEMAprevious) {isCrossed = 1; } if (FEMAnow < SEMAnow && FEMAprevious < SEMAprevious && FEMAprevious > SEMAprevious) {isCrossed = 2; }
//We check the trading terminal to see if it is empty
total = OrdersTotal();
if(total < 1) //If trading terminal is empty…
{
if(isCrossed == 1) //if cross indicates LONG swing…
{ ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,2,Ask-StopLoss*Point,Ask+TakeProfit*Point,"EMa cross",12345,0,Yellow); 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) //if cross indicates SHORT swing... { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,2,Bid+StopLoss*Point,Bid-TakeProfit*Point,"EMA cross",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); }
//Position exit function…
for(cnt=0;cnt0)
{
if(Bid-OrderOpenPrice()>PointTrailingStop) { if(OrderStopLoss()TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-PointTrailingStop,OrderTakeProfit(),0,Green); return(0); } } } } else // go to short position { // Check if we have a LONG cross condition… if(isCrossed == 1) { OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position return(0); // exit } // Check our trailing stop to see if it needs to be adjuste if(TrailingStop>0) { if((OrderOpenPrice()-Ask)>(PointTrailingStop))
{
if((OrderStopLoss()>(Ask+PointTrailingStop)) || (OrderStopLoss()==0)) { OrderModify(OrderTicket(),OrderOpenPrice(),Ask+PointTrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}
//+------------------------------------------------------------------+