EA cannot take profit

 

Hi,


This EA can placed order but it cannot take profit, I do not know why, can someone help?


Thank you in advance.

//+------------------------------------------------------------------+
//|                                                  exp_Amstell.mq4 |
//|                                   Copyright © 2009, Yuriy Tokman |
//|                                            yuriytokman@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Yuriy Tokman"
#property link      "yuriytokman@gmail.com"



extern int    TakeProfit       = 30;            // Ðàçìåð òåéêà â ïóíêòàõ
extern int    StopLoss         = 20;
extern double Lots             = 0.01;          // Ðàçìåð ëîòà
extern int Magic=999;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  
  
  Lots = MathMax(MarketInfo(Symbol(),MODE_MINLOT), Lots);
  
  
for(int cnt=0;cnt<OrdersTotal();cnt++)// Ïåðåáèðàåì âñå îðäåðà
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);//îðäåð âûáèðàåòñÿ ñðåäè îòêðûòûõ è îòëîæåííûõ îðäåðîâ
      if( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)// Ñîâïàäàåò ëè ñèìâîë îðäåðà( Çäåñü ïî íàäîáíîñòè åù¸ ìàãèê ìîæíî ïðîâåðèòü)
        {
         if(OrderType()==OP_BUY)//Îòáèðàåì ïîçèöèþ áàé
           {
            if(Bid-OrderOpenPrice()>TakeProfit*Point)//
              {
               OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); //çàêðûâàåì îðäåð
               return(0); // âûõîäèì
              }
           }
          if(OrderType()==OP_SELL)//Îòáèðàåì ïîçèöèþ ñåëë
           {
            if(OrderOpenPrice()-Ask>TakeProfit*Point)//
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); //çàêðûâàåì îðäåð
               return(0); // âûõîäèì
              }
           }
         }
      }
//----
int buy = 0, sell = 0;
//----
   if(!ExistPositions(NULL,OP_BUY))buy=1;
   else if(PriceOpenLastPos(NULL,OP_BUY)-Ask>10*Point)buy=1; 
   
   if(!ExistPositions(NULL,OP_SELL))sell=1;
   else if(Bid-PriceOpenLastPos(NULL,OP_SELL)>10*Point)sell=1;   
//----
    if(buy==1)
    OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,0,"Amstell",0,0,Green);
    
    if(sell==1)
    OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,0,"Amstell",0,0,Red);
//----
   return(0);
  }


bool ExistPositions(string sy="", int op=-1, int mn=-1, datetime ot=0) {
  int i, k=OrdersTotal();

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==sy || sy=="") {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (ot<=OrderOpenTime()) return(True);
            }
          }
        }
      }
    }
  }
  return(False);
}


double PriceOpenLastPos(string sy="", int op=-1, int mn=-1) {
  datetime t;
  double   r=0;
  int      i, k=OrdersTotal();

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==sy || sy=="") {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (t<OrderOpenTime()) {
                t=OrderOpenTime();
                r=OrderOpenPrice();
              }
            }
          }
        }
      }
    }
  }
  return(r);
}
 

Hi

Why don't you put the take profit in the order send?


OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"Amstell",0,0,Green);
 

Assuming that you want the EA (rather than the broker) to invoke the take profit, then the first thing I would do is to count downwards rather than upwards in your loops through orders. This has been well and frequently documented on the forum by me and others. Try that first and let us know how you go. Then we'll look in more depth if necessary.


CB

Reason: