Mql4, blowing my account ! ahh

 

Hi guys/&gals,


Having a massive problem with my EA, instead of just opening 2 trades with the same magic number and then closing those ones at a set profit target my EA goes nuts and just continually opens the same trade over and over again until I turn off auto trading, I have tried everything and it just wont stop, HELLLP,


Im pretty sure this is the part of code that is doing the damage


 {
      if(MagicNumber1==0) //if no there are no open trades with magic number 0001
         OrderSend("XAUUSD",OP_BUY,1,NULL,100,NULL,NULL,NULL,0001,NULL,NULL);
      OrderSend("USDJPY",OP_BUY,1,NULL,100,NULL,NULL,NULL,0001,NULL,NULL);

     }
     {
      if(MagicNumber2==0) //if no there are no open trades with magic number 0002
         OrderSend("EURUSD",OP_BUY,1,NULL,100,NULL,NULL,NULL,0002,NULL,NULL);
      OrderSend("USDCHF",OP_BUY,1,NULL,100,NULL,NULL,NULL,0002,NULL,NULL);

     }
 
David Buckley:

Hi guys/&gals,


Having a massive problem with my EA, instead of just opening 2 trades with the same magic number and then closing those ones at a set profit target my EA goes nuts and just continually opens the same trade over and over again until I turn off auto trading, I have tried everything and it just wont stop, HELLLP,


Im pretty sure this is the part of code that is doing the damage



lol, please tell me this is a <deleted>-post

 
David Buckley:

Hi guys/&gals,


Having a massive problem with my EA, instead of just opening 2 trades with the same magic number and then closing those ones at a set profit target my EA goes nuts and just continually opens the same trade over and over again until I turn off auto trading, I have tried everything and it just wont stop, HELLLP,


Im pretty sure this is the part of code that is doing the damage



I solved a problem like this before , check my solution


if(//Buy Conditions){
      if(totalSells(mNum)>0){//Close Sell
         CloseSell(mNum);
      }  
      if(totalBuys(mNum)==0){
         Buy(lots,mNum);
      }
}
if(// Sell Conditions){
      if(totalBuys(mNum)>0){//Close Buy
         CloseBuy(mNum);
      }
      if(totalSells(mNum)==0){
         Sell(lots,mNum);
      }
}

and this is the functions

//+------------------------------------------------------------------+
void Buy(double lot, int magic){
   if(OrderSend(Symbol(),OP_BUY,lot,Ask,3,0,0,magic,magic,0,clrNONE)){
      Print(Symbol()+" Buy @ "+Ask);
   }else{Print("Buy Open ERROR");   }
}
//+------------------------------------------------------------------+
void Sell(double lot, int magic){
   if(OrderSend(Symbol(),OP_SELL,lot,Bid,3,0,0,magic,magic,0,clrNONE)){
      Print(Symbol()+" Sell @ "+Bid);
   }else{Print("Sell Open ERROR");   }
}
//+------------------------------------------------------------------+
void CloseBuy(int magic){
   for (int h = OrdersTotal()-1; h>=0; h--){
      if(OrderSelect(h, SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==magic)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_BUY)
                  if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE)){
                     Print(OrderTicket()+" "+Symbol()+" Buy Closed @ "+OrderClosePrice());
                  }else{      Print("Buy Close Error");   }
}}
//+------------------------------------------------------------------+
void CloseSell(int magic){
   for (int h = OrdersTotal()-1; h>=0; h--){
      if(OrderSelect(h, SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==magic)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_SELL)
                  if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE)){
                     Print(OrderTicket()+" "+Symbol()+" Sell Closed @ "+OrderClosePrice());
                  }else{      Print("Sell Close Error");   }
}}
//+------------------------------------------------------------------+