Help with closing orders with same magic on a chart when profit is reached

 

Hello everyone.  I am struggling to understand where I am going wrong with this code.   My goal is to every tick to have the EA totaling profit for all orders on a chart/symbol with the same magic number and when the profit target is reached, the EA should close all the orders under that same magic on that chart and continue to look for the next signal.  However the profit total will go well beyond the total I has set up without closing the orders.  Could someone help me understand where I might be going wrong?   I really appreciate any help. :) here is my code:


//+------------------------------------------------------------------+
//|                                                    testEA.mql4|
//+------------------------------------------------------------------+

extern double Lot_Size=0.03;
extern double Target_In_Dollars=2;
extern int Trailing_Stop=50;
extern int Max_Orders=2;
extern int Max_ADX=70;
extern int Distance_Back=24;
extern int Magic=290809;

int cbars=0;

int start() {

 double profit=0;
 double EAOrdersTotal=0;
 int j=OrdersTotal()-1;
 for (int i=j;i>=0;i--)
 
  {
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   if(OrderMagicNumber()==Magic && OrderSymbol()==Symbol())  EAOrdersTotal++;
   if(OrderMagicNumber()==Magic && OrderSymbol()==Symbol())  profit=OrderProfit()+OrderSwap()+profit;
  }
 
 if (profit>=Target_In_Dollars)
  {
   j=OrdersTotal()-1;
   for (i=j;i>=0;i--)
    {
     OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
     RefreshRates();
     if(OrderType()==OP_BUY && OrderMagicNumber()==Magic && OrderSymbol()==Symbol())
      OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue);
     RefreshRates();
     if(OrderType()==OP_SELL && OrderMagicNumber()==Magic && OrderSymbol()==Symbol())
      OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue);
    }
  }
 
 if(EAOrdersTotal < Max_Orders)
 if(iADX(NULL, 0, 8, PRICE_CLOSE, MODE_MAIN, 0) < Max_ADX)

 double sig = Lowest(NULL,0,MODE_LOW,Distance_Back,0);
 if(cbars!=Bars && sig==1)
  {
   RefreshRates();
   OrderSend(Symbol(),OP_BUY,Lot_Size,Ask,3,0,0,"DoppkeEA_8_27",Magic,0,Blue);
   string AN="ArrBuy "+TimeToStr(CurTime());
   ObjectCreate(AN,OBJ_ARROW,0,Time[1],Low[1]-6*Point,0,0,0,0);
   ObjectSet(AN, OBJPROP_ARROWCODE, 233);
   ObjectSet(AN, OBJPROP_COLOR , Blue);
  }
 
 
  
 if(EAOrdersTotal < Max_Orders)
 if(iADX(NULL, 0, 8, PRICE_CLOSE, MODE_MAIN, 0) < Max_ADX)

 sig = Highest(NULL,0,MODE_HIGH,Distance_Back,0);
 if(cbars!=Bars && sig==1)
  {
   RefreshRates();
   OrderSend(Symbol(),OP_SELL,Lot_Size,Bid,3,0,0,"DoppkeEA_8_27",Magic,0,Magenta);
   AN="ArrSell "+TimeToStr(CurTime());
   ObjectCreate(AN,OBJ_ARROW,0,Time[1],High[1]+6*Point,0,0,0,0);
   ObjectSet(AN, OBJPROP_ARROWCODE, 234);
   ObjectSet(AN, OBJPROP_COLOR , Magenta);
  }



cbars=Bars;
 
return(0);
}
 
     if(OrderType()==OP_BUY && OrderMagicNumber()==Magic && OrderSymbol()==Symbol())
      OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue);
     RefreshRates();
     if(OrderType()==OP_SELL && OrderMagicNumber()==Magic && OrderSymbol()==Symbol())
      OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue);

 You do close sells at Bid, close at Ask

Why not just

     if(OrderMagicNumber()==Magic && OrderSymbol()==Symbol())
      OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Blue);

 

 You need to check whether the order close was succesful or not. If not print details in the log and it will help you find any errors.

Reason: