help needed with too many orders opened. - page 2

 
qjol:

Sorry, that's the max I see, I tried to help, Unfortunately without success


no problem qjol, it's much appreciated
 

seems your delete_pending_sell() makes the riddle.

why are you useing two different magic numbers? so realy strange looking code. should the function return 0 when all sells are deleted or when no sell exists?

 
zzuegg:

seems your delete_pending_sell() makes the riddle.

why are you useing two different magic numbers? so realy strange looking code. should the function return 0 when all sells are deleted or when no sell exists?


Hi zzuegg,

I use 2 different magic numbers to just differentiate between buys and sells, both pending and open orders. I suppose i could also use just one per ea and further differentiate with ordertype()..?

What I had in mind for this function was to delete pending sell orders and to count the number of buy orders, both open and pending, in the same loop. So it should count and return the number of buy orders while deleting any pending sells.

I also tried to separate these two tasks (counting and deleting) in separate functions but still the same problem. Am I on the wrong track here?

 
remco:

I also tried to separate these two tasks (counting and deleting) in separate functions but still the same problem.

thats a better aproach. use somthing like getSellPendingCount() and doSellPendingDelete() and buys seems only allowed when getSellPendingCount()==0. i still think there is a error in your DeleteAndReturnCount function.


regarding the magic numbers. by convention magicNr are used to differentiate order belonging to your EA or not. you can always use if(Ordertype==OP_BUY) and if(Ordertype==OP_SELL) to differentiate between type of orders.

 
zzuegg:

thats a better aproach. use somthing like getSellPendingCount() and doSellPendingDelete() and buys seems only allowed when getSellPendingCount()==0. i still think there is a error in your DeleteAndReturnCount function.


regarding the magic numbers. by convention magicNr are used to differentiate order belonging to your EA or not. you can always use if(Ordertype==OP_BUY) and if(Ordertype==OP_SELL) to differentiate between type of orders.


I tried it again with separate functions and one magic. The strange thing is that most of the time the first few trades are processed correctly and when I pick a random week to test, sometimes the error does not occur. But when I extend the same test

period with an extra week, the problem returns right away in the first week. This means that the same data was processed in a different way without changing the settings of the ea.

The second strange thing is that I also tried the exact same ea on another computer (i've started using a newer and faster one) and it ran without any problem. I tested it on terminals from two different brokers on each pc,
and I only have problems with the ea on my new pc. Could it have something to do with that maybe?


Here's the the code like I tested it:

bool buy=false;
bool sell = false;

if (Ask>=boven) 
    { 
     if (countopen_buy()==0)
        {
        delete_pending_sell(); 
        buy =true;    
        }
    } 



if (buy==true)
   {
   ticket=   OrderSend(Symbol(),OP_BUYSTOP,lots,Ask+kick,0,(Ask+kick)-straal,0,0,magic,0,0);    
   
   }


void delete_pending_sell()
{
   int i;
   for(i=OrdersTotal()-1;i==0;i--) 
      {
      OrderSelect(i, SELECT_BY_POS);
    
         if(OrderMagicNumber() == magic&&OrderType()==OP_SELLSTOP)
         {
          OrderDelete(OrderTicket());
         }      
     } 
}

int countopen_buy()
{
int t= 0;
int i;
   for(i=OrdersTotal()-1;i==0;i--) 
      {
      OrderSelect(i, SELECT_BY_POS);

         if(OrderMagicNumber()==magic&&OrderType()==OP_BUY||OrderType()==OP_BUYSTOP)
           {
           t++;
           
           return(t);
           }
      }      
}
 
bool buy=false;
bool sell = false;

if (Ask>=boven){ 
  if (countopen_buy()==0){
    delete_pending_sell(); 
    buy =true;    
  }
} 



if (buy==true){
   ticket=   OrderSend(Symbol(),OP_BUYSTOP,lots,Ask+kick,0,(Ask+kick)-straal,0,0,magic,0,0);    
}


void delete_pending_sell()
{
   int i;
   for(i=OrdersTotal()-1;i>=0;i--){
     OrderSelect(i, SELECT_BY_POS);
     if(OrderMagicNumber() == magic&&OrderType()==OP_SELLSTOP){
       OrderDelete(OrderTicket());
     }      
   } 
}

int countopen_buy()
{
int t= 0;
int i;
   for(i=OrdersTotal()-1;i>=0;i--){
     OrderSelect(i, SELECT_BY_POS);
     if(OrderMagicNumber()==magic&&OrderType()==OP_BUY||OrderType()==OP_BUYSTOP){
       t++;
     }
   }  
   return (t);    
}

this code seems good now, i corrected the loops and the return in teh countopen should be after the loop.

are you shure you got the pending/open order logic correct? since error happend only on some terminal (which is strange) there may be a error with some OrderClose() somwhere else in the program

 
zzuegg:

this code seems good now, i corrected the loops and the return in the countopen should be after the loop.

are you shure you got the pending/open order logic correct? since error happend only on some terminal (which is strange) there may be a error with some OrderClose() somwhere else in the program


I have tested the code again and in the backtest the function int countopen_buy() seems to return 0 (with openen trades in the terminal) at certain moments, thats when a huge load of trades are executed at once. I haven't found out why it returns 0 sometimes as the code looks ok.
With forward testing on a single currency pair it seems to work for the time I have tested it. But when I attach this EA to a second chart, for this second chart it starts to generetae a lot of trades immediately.

For the time being, I have included if(orderstotal()==0) so it won't produce so many trades, but it won't also be possibel to run this EA at multiple charts.

Thanks for your help so far, zzuegg. I might come back on this issue later, when I've done some more testing.