EA stops working after a few trades

 

Hi, I am relatively new to coding EAs so i apologize if my code is sloppy and if the solution to my problem is basic. However,  i have been struggling for weeks, i want my EA to open a buy and a sell on initialization and then as soon as one of them takes profit open another buy and sell and then when a sell for example takes profit i want it to close the previous sell at the open price which will be this sell's take profit, if that makes sense. But what my code is doing is it works for the first 2 trades and then stops opening new orders and if it does then it doesn't close any orders. Can someone please help?


My Code:


void OnTick()
  {
       //---->>>>>Open New Orders After TakeProfit<<<<<----\\       
            for (int b=OrdersTotal()-1;b>=0;b--)
         {
               if (OrderSelect(b,SELECT_BY_POS,MODE_HISTORY))
                  if (OrderMagicNumber()==MagicNumber)
                     if (OrderSymbol()==Symbol())
                        if (OrderType()==OP_BUY)
                           if (OrderClosePrice()>OrderOpenPrice())
                              if (OrderCloseTime()==TimeCurrent())
                                {
                                       MagicNumber++;
                                       if (OrderSend(NULL,OP_BUY,0.1,Ask,3,0,(Ask+200*_Point),NULL,MagicNumber,0,clrGreen)<0)
                                          Print("OrderSend Error #",GetLastError());
                                       if (OrderSend(NULL,OP_SELL,0.1,Bid,3,0,(Bid-200*_Point),NULL,MagicNumber,0,clrRed)<0)
                                          Print("OrderSend Error #",GetLastError());
                                
                                 }
                                                     
               
         }
         
     
      
         
     for (int s=OrdersTotal()-1;s>=0;s--)
         {
               if (OrderSelect(s,SELECT_BY_POS,MODE_HISTORY))
                  if (OrderMagicNumber()==MagicNumber)
                     if (OrderSymbol()==Symbol())
                        if (OrderType()==OP_SELL)
                           if (OrderClosePrice()<OrderOpenPrice())
                              if (OrderCloseTime()==TimeCurrent())
                                {
                                       MagicNumber++;
                                       if (OrderSend(NULL,OP_BUY,0.1,Ask,3,0,(Ask+200*_Point),NULL,MagicNumber,0,clrGreen)<0)
                                          Print("OrderSend Error #",GetLastError());
                                       if (OrderSend(NULL,OP_SELL,0.1,Bid,3,0,(Bid-200*_Point),NULL,MagicNumber,0,clrRed)<0)
                                          Print("OrderSend Error #",GetLastError());
                                
                                 }
                                                     
               
         }
          
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
//---->>>>>Close Previous Order at OpenPrice After TakeProfit hit<<<<<----\\    
     
 datetime LastBuy;          
            for (int B=OrdersTotal()-1;B>=0;B--)
         {
               if (OrderSelect(B,SELECT_BY_POS,MODE_HISTORY))
                     if (OrderSymbol()==Symbol())
                        if (OrderType()==OP_BUY)
                           if (OrderClosePrice()>OrderOpenPrice())
                              
                                {
                                 LastBuy=OrderOpenTime();
                                for (int bb=OrdersTotal()-1;bb>=0;bb--)
                                       {
                                             if (OrderSelect(bb,SELECT_BY_POS,MODE_TRADES))
                                                while((OrderType()==OP_BUY) && (OrderSymbol()==Symbol()) && OrderOpenTime()<LastBuy && OrderCloseTime()==0)  
                                                         {
                                                               
                                                                  RefreshRates();
                                                                  bool res = OrderClose(OrderTicket(),OrderLots(),Bid,3,clrRed);
                                                                  if (res) break;
                                                                
                                                         } 
                                                            
                                             
                                       }
                                 }
                                                     
               
         }
         
     
      
   datetime LastSell;      
      for (int S=OrdersTotal()-1;S>=0;S--)
         {
               if (OrderSelect(S,SELECT_BY_POS,MODE_HISTORY))
                     if (OrderSymbol()==Symbol())
                        if (OrderType()==OP_SELL)
                           if (OrderClosePrice()<OrderOpenPrice())
                                {
                                 LastSell=OrderOpenTime();
                                for (int ss=OrdersTotal()-1;ss>=0;ss--)
                                       {
                                             if (OrderSelect(ss,SELECT_BY_POS,MODE_TRADES))
                                                while((OrderType()==OP_SELL) && (OrderSymbol()==Symbol()) && OrderOpenTime()<LastSell && OrderCloseTime()==0)  
                                                         {
                                                               
                                                                  RefreshRates();
                                                                  bool res = OrderClose(OrderTicket(),OrderLots(),Ask,3,clrRed);
                                                                  if (res) break;
                                                                
                                                         }   
                                             
                                       }
                                 }
                                                     
               
         }  
   
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
    
     

  }



Files:
 
NewTrader20:

Hi, I am relatively new to coding EAs so i apologize if my code is sloppy and if the solution to my problem is basic. However,  i have been struggling for weeks, i want my EA to open a buy and a sell on initialization and then as soon as one of them takes profit open another buy and sell and then when a sell for example takes profit i want it to close the previous sell at the open price which will be this sell's take profit, if that makes sense. But what my code is doing is it works for the first 2 trades and then stops opening new orders and if it does then it doesn't close any orders. Can someone please help?

In MT4, when you need to retrieve closed orders - use OrdersHistoryTotal() instead of OrdersTotal().

 
@NewTrader20
NewTrader20: But what my code is doing is it works for the first 2 trades and then stops opening new orders and if it does then it doesn't close any orders.
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  3.        //---->>>>>Open New Orders After TakeProfit<<<<<----\\       
                for (int b=OrdersTotal()-1;b>=0;b--){
                   if (OrderSelect(b,SELECT_BY_POS,MODE_HISTORY))
    There are not OrdersTotal in history.

  4.                if (OrderSelect(b,SELECT_BY_POS,MODE_HISTORY)
                   && OrderMagicNumber()==MagicNumber
                   && OrderSymbol()==Symbol()
                   && OrderType()==OP_BUY)
                   && OrderClosePrice()>OrderOpenPrice())
                                  if (OrderCloseTime()==TimeCurrent())
    An order closed in profit (ignoring Swap and Commission.) Don't you want to be looking at the last closed order? What if you don't get a tick the exact same second when the order closed?

  5.                                 for (int bb=OrdersTotal()-1;bb>=0;bb--){
                                                 if (OrderSelect(bb,SELECT_BY_POS,MODE_TRADES))
                                                    while((OrderType()==OP_BUY) && (OrderSymbol()==Symbol()) && OrderOpenTime()<LastBuy && OrderCloseTime()==0)  
    
    You just selected a current order and it's a buy. The OrderCloseTime will always be zero.
  6. Your loop never selects a new order, therefor your while condition never changes.
 
Seng Joo Thio:

In MT4, when you need to retrieve closed orders - use OrdersHistoryTotal() instead of OrdersTotal().

Is working now Thanks 
 
William Roeder:
@NewTrader20
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  3. There are not OrdersTotal in history.

  4. An order closed in profit (ignoring Swap and Commission.) Don't you want to be looking at the last closed order? What if you don't get a tick the exact same second when the order closed?

  5. You just selected a current order and it's a buy. The OrderCloseTime will always be zero.
  6. Your loop never selects a new order, therefor your while condition never changes.

1. As i said im new to this.

2. Sorry didn't because again as i said im new to this.

3. i know that now thanks.

4. No im looking for the last order closed in profit (dont know if that is what you meant) and i do not know how to take Swap and Commission into account. I am still learning all this, if you would be kind enough to show me id appreciate it 

5. That is true, I missed that

6. Cool thanks ill change that 

Reason: