Download MetaTrader 5

Opinion,suggestions _EA function

To add comments, please log in or register
Why to sell products in MetaTrader AppStore? Find that out from the article!
Vantages
148
Vantages 2014.12.22 01:21 

Hi. I have a function that keeps my platform unresponsive and 100% CPU usage each time it executes, 

I hope someone could help... 

Please note, I have a loop of continuous buying and selling and means that there are always

positions left open at negative or positive result... 

 

//-------------------------------------------------------------------+
// 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; 
            }
      }
Trinh Dat
37213
Trinh Dat 2014.12.22 01:49  

Try it


//-------------------------------------------------------------------+
// 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");
                                 }
                        }
                  }
               
      }
Vantages
148
Vantages 2014.12.22 01:59  
tuoitrecuoi:

Try it


//-------------------------------------------------------------------+
// Close at profit Function                                          |
//-------------------------------------------------------------------+
   void CloseatProfit( string Symb )
      {
         total = OrdersTotal();
         int ticket = 0;
         while ( OrdersTotal() > 10 )
            {
               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");
                                 }
                        }
                  }
               
            }
      }


Wow! Thank you.

May I ask, no -break-  - IsStopped()- ?

If I have 10 or 100 open positions, the function will only close,per ticket,per symbol of only OrderProfit() > 1? 

To add comments, please log in or register