Download MetaTrader 5

EA function not working properly

To add comments, please log in or register
Vantages
148
Vantages  

Please help, with this code platform becomes unresponsive for 100% cpu usage

Function is supposed to close one position at a time per ticket when reaching total position and profit,

Please note that this came from a loop of buying and selling and there are positions always left open with

positive and negative result - p/l.


Thanks.

//-------------------------------------------------------------------+
// Close at profit Function                                          |
//-------------------------------------------------------------------+
   void CloseatProfit( string Symb ) 
      {
         total = OrdersTotal();
         int ticket = 0;
         while ( OrdersTotal() >= 10 )
            {
               for( int cnt = total()-1; cnt >= 10; cnt-- )
                  {
                     if ( OrderSelect( cnt, SELECT_BY_POS, MODE_TRADES ))
                        {
                           if (  OrderProfit() > 1 )
                                 {
                                    ticket = OrderTicket();
                                 }
                        }
                  }
               
               if( ticket != 0 && OrderSelect(ticket,SELECT_BY_TICKET))
                        {
                           if (  OrderType() == OP_BUY && OrderSymbol() == Symb )
                           if ( OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(),MODE_BID), 10, clrNONE ) ) Print("Close order success");
                           else Print( "Close order failed");
                     
                           if (  OrderType() == OP_SELL && OrderSymbol() == Symb ) 
                           if ( OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(),MODE_ASK), 10, clrNONE ) ) Print("Close order success");
                           else Print( "Close order failed");
                        }                  
               if (ticket == 0 ) break; // I also tried this > if ( OrdersTotal() < 10 ) break; 
            }
      }


This one doesn't work either


//-------------------------------------------------------------------+
// Close at profit Function                                          |
//-------------------------------------------------------------------+
   void CloseatProfit( string Symb )
      {
         total = OrdersTotal();
         int ticket = 0;
         if(OrdersTotal() <= 10) return;
                for( int cnt = total-1; cnt >= 9; cnt-- )
                  {
                     if ( OrderSelect( cnt, SELECT_BY_POS, MODE_TRADES ))
                        {
                           if ( OrderSymbol() == Symb && OrderProfit() > 1 )
                                 {
                                 if ( OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 10, clrNONE ) ) Print("Close order success");
                                 else Print( "Close order failed");
                                 }
                        }
                  }
               
      }

Trinh Dat
37697
Trinh Dat  
//-------------------------------------------------------------------+
// Close at profit Function                                          |
//-------------------------------------------------------------------+
   void CloseatProfit( string Symb )
      {
         total = OrdersTotal();
         int ticket = 0;
         if(OrdersTotal() < 1) return;
                for( int cnt = total-1; cnt >= 0; cnt-- )
                  {
                     if ( OrderSelect( cnt, SELECT_BY_POS, MODE_TRADES ))
                        {
                           if ( OrderSymbol() == Symb && OrderProfit() > 1 )
                                 {
                                 if ( OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 10, clrNONE ) ) Print("Close order success");
                                 else Print( "Close order failed");
                                 }
                        }
                  }
               
      }
Vantages
148
Vantages  
tuoitrecuoi:
//-------------------------------------------------------------------+
// Close at profit Function                                          |
//-------------------------------------------------------------------+
   void CloseatProfit( string Symb )
      {
         total = OrdersTotal();
         int ticket = 0;
         if(OrdersTotal() < 1) return;
                for( int cnt = total-1; cnt >= 0; cnt-- )
                  {
                     if ( OrderSelect( cnt, SELECT_BY_POS, MODE_TRADES ))
                        {
                           if ( OrderSymbol() == Symb && OrderProfit() > 1 )
                                 {
                                 if ( OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 10, clrNONE ) ) Print("Close order success");
                                 else Print( "Close order failed");
                                 }
                        }
                  }
               
      }
Thank you. Will try and test it...
Vantages
148
Vantages  

I hope you don't mind my last inquiry,

during the course of accumulation ( positions of more than 5 ),

it closes positions or tickets with negative result ( p/l ) when the

expression or conditions is supposed to be OrderProfit > 1 only.

Just like closing the whole positions with symb specific...


Thanks.

To add comments, please log in or register