How to count number of opend positions? - page 3

 

Try this . . .

datetime TC=TimeCurrent;
bool PlaceNewOrder = true;

for(int a=0; a<ArraySize(DebutList); a++)
   {
   if(TC >= StrToTime(DebutList[a]) && TC <= StrToTime(DebutList[a]) + 4)
      {
      if(iClose(Symbol(), TimeFrame, 1) < iClose(Symbol(), TimeFrame, 2))// Bougie 1 < Bougie 2
         {
         for(int OrderPosition = OrdersTotal() - 1; OrderPosition >= 0; OrderPosition--)
            {
            if(OrderSelect(OrderPosition, SELECT_BY_POS, MODE_TRADES) &&
            OrderMagicNumber() == MagicNo &&       // <-- does the Order's Magic Number match our EA's magic number ? 
            OrderSymbol() == Symbol() &&
            OrderOpenTime() >= StrToTime(DebutList[a]) && 
            OrderOpenTime() < StrToTime(DebutList[a]) + (4 * PERIOD_H1 * 60) )
               PlaceNewOrder = false;
            }
         
         if(PlaceNewOrder) ticket = OrderSend(Symbol(), OP_BUY, LotS, NormalizeDouble(ask, digits), 300, 0, 0, "", MagicS, CLR_NONE);
         if(ticket > 0)
            {
            OrderModify(ticket, OrderOpenPrice(), NormalizeDouble(SLBuy,digits), NormalizeDouble(tpSB, digits), 0, CLR_NONE);
            Stop open somes trades and wait for the next string hour.
            }
         else Print("OrderSend failed, Error: ", GetLastError() );
         }  
      }
   }
 
Thanks a lot again i will try it.
 
WHRoeder:
You should use explanatory variable names and self documenting code. A variable named "parameter" says nothing. Your routine doesn't get any information, it get the open order count. Better:Still it can't be used for pending orders. Best:Use of the magic number and pair filtering is done once in MySelect, not in every orderSelect loop. MyOrdersTotal (does what OrdersTotal() does with filtering My) can select All types and can be used for both open orders and history.

(Whroeder code is on page 1 )

 I would like to use this to count Pending orders, although it seems it can check balance etc.

Does anyone know how to put it in to an EA;  https://book.mql4.com/appendix/examples  eg: tradingexpert.mql4 . I have tried to put it in to it as per below:

If any one could guide me better that would be great. Thanks

#property copyright "Copyright © Book, 2007"

#define OP_All -1                                                         Put here?
//--------------------------------------------------------------- 1 --

extern int magicnum = 1000;

int start()
  {
   int
   OP_ALL,
   nbuys = myorderstotal (OP_BUY),
   nsell = myorderstotal (OP_SELL),                                       Put here?
   nall =  myorderstotal() ,
   nhistory =myorderstotal(OP_ALL,MODE_HISTORY);

//-----------------------------------------------------------------------

 // Order Accounting
   
Symb=Symbol();                               
Total=0;                                    
   
     bool myselect(int iwhat, int eselect,int epool=MODE_TRADES){
     if(!OrderSelect(iwhat,eselect,epool) ) return (false);
     if(OrderMagicNumber() != magicnum    ) return (false);
     if(OrderSymbol()      != Symb        ) return (false);
     if( int epool != MODE_HISTORY        ) return (true );
      return (OrderType() <=OP_SELL);
       }                ............................................        // Does the parenthesis cover the whole program. ?       
 
  int myorderstotal(int ordertype=-1, int epool=MODE_TRADES) {   
          if( epool == MODE_TRADES)  int iipos = OrdersTotal()       -1;
          else                          iipos=OrdersHistoryTotal()  -1;
          for(int norders=0; iipos>=0; iipos--)
          if(myselect(iipos,SELECT_BY_POS,epool))
          if(ordertype ==  int OP_ALL  || ordertype ==OrderType()) norders++;
          return(norders);
      }                  ...........................................         // Does the parenthesis cover the whole program. ?                                                     
      
   for(int i=1; i<=OrdersTotal(); i++)          
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) 
        {                                       
         if (OrderSymbol()!=Symb)continue;      
         if(OrderMagicNumber() != magicnum) continue;
         if (OrderType()>5)                     
           {
            Alert("Pending order detected.EA Doesn't work.");
            return;                             
           }
         Total++;                               
         if (Total>5)                            
           {
            Alert("Several market orders.Ea dosen't work...");
            return;                             
           }
         Ticket=OrderTicket();                  
         Tip   =OrderType();                    
         Price =OrderOpenPrice();               
         SL    =OrderStopLoss();                
         TP    =OrderTakeProfit();              
         Lot   =OrderLots();                    
      
        
       } 
     }

//-------------------------------------------------------------------

//     Create Pending Orders  

 if( pendingorders >0 )   {            //1
   
   
     for(int ipend =OrdersTotal()-1; ipend>=0 ; ipend--) {    //2               ?  Cycle is still required ?
     if(OrderSelect(ipend,SELECT_BY_POS))  {    //3                             ?  Delete orderselect, as now it is handled above ?
     
      if( ..............................                                        ?  Pass value from above ?  eg;if( nbuys + nsell)-nall > 3 ; Break; (3 pendig orders)  

     if(OrderType()== OP_BUY &&
      OrderMagicNumber()==magicnum && 
     OrderSymbol() == Symb) {      
    
     OrderSend(Symb,OP_BUYSTOP,Lts,OrderOpenPrice()+(20*Point)*10,2,00,00,"  ",magicnum); 
     Alert("Pending Order Opened order no. ");
//--------------------------------------------------------------------------------------
return;
} (end of program)

 
barnacle7:

(Whroeder code is on page 1 )

 I would like to use this to count Pending orders, although it seems it can check balance etc.

Does anyone know how to put it in to an EA;  https://book.mql4.com/appendix/examples  eg: tradingexpert.mql4 . I have tried to put it in to it as per below:

That is a bad idea . . .  you would be doing yourself a favour by spending the time to understand the code and then write you own version,  you will learn in the process,  it's much better than copy and pasting someone else's code . .  that may have issues in it.
Reason: