unstable behavior of EA: how to prevent and fix ?

 

Please view the following code:

//----------------------------------------------------------------------
 
 if (total(OP_BUY)==0)
              
     {
      static int base=0;
      if (base==0)      base = Bid;
      pendup=NormalizeDouble ( MathRound (base/5)*5 + 3 , 2  );
               
      if (total(OP_BUYSTOP)>0  && priceof(OP_BUYSTOP)!=pendup) Delete (OP_BUYSTOP);

      if (total(OP_BUYSTOP)==0) 
           OrderSend(Symbol(),OP_BUYSTOP,1,pendup,3,pendup-5,0,0,0,0,Blue);   
      }

 if (total(OP_BUY)+total(OP_SELL)>0) if (base>0) base=0;
        
              // .v.v.
//-------------------------------------------------------------------------------------
// ----------- with total() , priceof() and Delete() defined as below: ----------------

      int total(int type)
            {
            int count=0;
            
            if (OrdersTotal()==0) return(0);
            for (int i=0; i<OrdersTotal();i++)
                  {
                  OrderSelect(i,SELECT_BY_POS);
                  if (OrderType()==type) count++;
                  }
            return(count);
           }
           
      double priceof(int type)
            {
             double price=0;
             if (total(type)==0) return(0);
             for (int i=0; i<OrdersTotal();i++)
                  {
                  OrderSelect(i,SELECT_BY_POS);
                  if (OrderType()==type) price=OrderOpenPrice();
                  }
             return(price);
            }

      void Delete (int type)
            {
            
            if (total(type)==0) return;
            for (int i=0; i<OrdersTotal();i++)
                  {
                  OrderSelect(i,SELECT_BY_POS);
                  if (OrderType()==type) 
                        OrderDelete(OrderTicket()) ;
                 }
           }

Explanation:

The above coding is for putting a BUYSTOP order if none placed before.

Output is ok in general, with the Buystopline and Buystop 's stoploss line presented on the chart.

But it 's not ok for all the time.

Sometimes, the Buystop order is continuously deleted and recreated at every quote without tiny change on the order open price .

As consequence, the BS line and the BS 's SL line on the chart is flickering (blink) according to quotes

(And of course, the Order ticket of BS order shown in "Trade tab" of Terminal window keep changing at every code)


I doubt if there is some error like data overloaded or something like that.

If so, which Properties Directive statements would I use to prevent the EA from overloading ?

Thanks a lot

 
Where did people learn to write OrderSelect() like that, anyway ???
 
sonthanhthuytu:

Please view the following code:

Explanation:

The above coding is for putting a BUYSTOP order if none placed before.

Output is ok in general, with the Buystopline and Buystop 's stoploss line presented on the chart.

But it 's not ok for all the time.

You should read this: Loops and Closing or Deleting Orders you MUST count down in loops if you are deleting/closing orders
 

wow ! this is the first time I hear about this rule (count down).

Thank you for your useful information.

 
sonthanhthuytu:

wow ! this is the first time I hear about this rule (count down).

You should read other posts . . . it gets mentioned quite often, that is why I created the thread.
 
Count down and use magic numbers
Reason: