Download MetaTrader 5

EA erroneously buying when no signal should be on chart

To add comments, please log in or register
sspecia
19
sspecia  

Hey guys, I have an EA that puts a buy trade when basically there is no signal. What could be the problem? Any ideas?

‌Here is the code:

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
extern int takeProfit = 0;
extern int stopLoss=50;
/*extern int fastEMA = 25;
extern int fastEMA2 = 27;
extern int slowEMA = 68;*/

extern double lotSize=0.01;
extern int magicNumber=1234;
extern bool useMoveToBreakeven = false;
extern int whenToMoveToBe = 100;
extern int pipsToLockIn = 5;
double pips;

extern bool useTrailingStop = true;
extern int whenToTrail = 20;
extern int trailAmount = 10;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {

   double ticksize=MarketInfo(Symbol(),MODE_TICKSIZE);
   if(ticksize==0.001 || ticksize==0.00001)
      pips=ticksize*10;
   else pips=ticksize;

   return(0);

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   if(useMoveToBreakeven)moveToBreakeven();
   if(useTrailingStop)adjustTrail();
   if(isNewCandle())checkForTrade();
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool isNewCandle()
  {

   int static barsOnChart=0;
   if(Bars==barsOnChart)
      return(false);
      barsOnChart=Bars;
      return(true);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void moveToBreakeven(){

for(int b=OrdersTotal()-1; b>=0; b--)
   {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber()==magicNumber)
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_BUY)
               if(Bid-OrderOpenPrice()>whenToMoveToBe*pips)
                  if(OrderOpenPrice()>OrderStopLoss())
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(pipsToLockIn*pips),OrderTakeProfit(),0,CLR_NONE);
   }

for(int s=OrdersTotal()-1; s>=0; s--)
   {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
      if(OrderMagicNumber()==magicNumber)
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_SELL)
               if(OrderOpenPrice()-Ask>whenToMoveToBe*pips)
                  if(OrderOpenPrice()<OrderStopLoss())
                     OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(pipsToLockIn*pips),OrderTakeProfit(),0,CLR_NONE);
   }   

}

void adjustTrail()
{

   for(int b=OrdersTotal()-1; b>=0; b--)
      {
      if(OrderMagicNumber()==magicNumber)
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_BUY)
               if(Bid-OrderOpenPrice()>whenToTrail*pips)
                  if(OrderStopLoss()<Bid-pips*trailAmount)
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(pips*trailAmount),OrderTakeProfit(),0,CLR_NONE);
               
      }
   for(int s=OrdersTotal()-1; s>=0; s--)
      {
      if(OrderMagicNumber()==magicNumber)
         if(OrderSymbol()==Symbol())
            if(OrderType()==OP_SELL)
               if(OrderOpenPrice()-Ask>whenToTrail*pips)
                  if(OrderStopLoss()>Ask+trailAmount*pips)
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(pips*trailAmount),OrderTakeProfit(),0,CLR_NONE);
               
      }

}


void OrderEntry(int direction)
  {
   if(direction==0);
      if(openOrdersThisPair(Symbol())==0)
         OrderSend(Symbol(),OP_BUY,lotSize,Ask,3,Ask-(stopLoss*pips),Ask+(takeProfit*pips),NULL,magicNumber,0,Green);
   if(direction==1);
      if(openOrdersThisPair(Symbol())==0)
         OrderSend(Symbol(),OP_SELL,lotSize,Bid,3,Bid+(stopLoss*pips),Bid-(takeProfit*pips),NULL,magicNumber,0,Red);

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void checkForTrade()
  {
   double fastEMA   = iMA(NULL,0,25,0,MODE_EMA,PRICE_CLOSE,1);
   double fastEMA2  = iMA(NULL,0,27,0,MODE_EMA,PRICE_CLOSE,1);
   double slowEMA   = iMA(NULL,0,68,0,MODE_EMA,PRICE_CLOSE,1);

//buy
   if(fastEMA>fastEMA2 && fastEMA>slowEMA)OrderEntry(0);
//sell
   if(fastEMA<fastEMA2 && fastEMA<slowEMA)OrderEntry(1);
  }
//+------------------------------------------------------------------+

int openOrdersThisPair(string pair){

   int total = 0;
      for(int i=OrdersTotal()-1; i>=0; i--)
         {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==pair) total++;
         
         }
         return(total);

}


honest_knave
Moderator
2325
honest_knave  

Print your variables to see what is going on: 

//buy
   if(fastEMA>fastEMA2 && fastEMA>slowEMA)
     {
      printf("Buy signal. fastEMA=%f fastEMA2=%f slowEMA=%f",fastEMA,fastEMA2,slowEMA);
      OrderEntry(0);
     }
//sell
   if(fastEMA<fastEMA2 && fastEMA<slowEMA)
     {
      printf("Sell signal. fastEMA=%f fastEMA2=%f slowEMA=%f",fastEMA,fastEMA2,slowEMA);
      OrderEntry(1);
     }

To add comments, please log in or register