My EA opens many order one after another instead of one

 

Hi, please help me. My EA opens many order, not only one per pair.

I don't know what is the problem in my code.

In this session I am checking the order, if found live order the liveorder=true would block to open more than one order per pair. But doesn't work:

for (counter1=0; counter1<=OrdersTotal()-1; counter1++) {
   if (OrderSelect(counter1,SELECT_BY_POS)==true) {
      if (OrderSymbol()==szimbolum && OrderMagicNumber()==magic_number) {

         Calc_trailing();
         selected_order_typ=OrderType();
         selected_order_ticket=OrderTicket();
         selected_lots=OrderLots();
         selected_sl=OrderStopLoss();
         selected_tp=OrderTakeProfit();
         liveorder=true;
         last_profit=false;
      } else {
         liveorder=false;
      }
   }
}

If liveorder=false comes this condition:

if (liveorder==false)
   {
   if (open_sell==true && adx_okay==true)
      {
      RefreshRates();
      OrderSend(szimbolum,OP_SELL,newlots,Bid,2,Ask+order_sl,Ask-order_tp,"EA "+order_comment,magic_number,0,clrWhite);
      open_sell=false;
//      last_profit=false;
      return;
      }
   if (open_buy==true && adx_okay==true)
      {
      RefreshRates();
      OrderSend(szimbolum,OP_BUY,newlots,Ask,2,Bid-order_sl,Bid+order_tp,"EA "+order_comment,magic_number,0,clrWhite);
      open_buy=false;
//      last_profit=false;
      return;
      }
   }

Thx for help

Cheers

 

Try

for (counter1=0; counter1<=OrdersTotal()-1; counter1++) {
   if (OrderSelect(counter1,SELECT_BY_POS)==true) {
      if (OrderSymbol()==szimbolum && OrderMagicNumber()==magic_number) {

         Calc_trailing();
         selected_order_typ=OrderType();
         selected_order_ticket=OrderTicket();
         selected_lots=OrderLots();
         selected_sl=OrderStopLoss();
         selected_tp=OrderTakeProfit();
         liveorder=true;
         last_profit=false;
         break;
      } else {
         liveorder=false;
      }
   }
}
 
or
liveorder=false; // Didn't find any
 for (counter1=0; counter1<=OrdersTotal()-1; counter1++) {               
   if (OrderSelect(counter1,SELECT_BY_POS)==true) {                      
      if (OrderSymbol()==szimbolum && OrderMagicNumber()==magic_number) {
                                                                         
         Calc_trailing();                                                
         selected_order_typ=OrderType();                                 
         selected_order_ticket=OrderTicket();                            
         selected_lots=OrderLots();                                      
         selected_sl=OrderStopLoss();                                    
         selected_tp=OrderTakeProfit();                                  
         liveorder=true;    // Found one or many
         last_profit=false;                                              
//    } else {           
//       liveorder=false;
      }
   }
}
 

Thx for helping.

I have gave in variables liveorder=false

I correct the code as above with break, and it seems it's working.

 
pecskeke1976: I correct the code as above with break, and it seems it's working.
  1. if (OrderSymbol()==szimbolum && OrderMagicNumber()==magic_number) { liveorder=true; } 
    else liveorder=false;
    With the break version. If it finds an order with symbol/MN you get true. If it finds any that don't match you get false. If it doesn't find any orders, you get liveorder being undefined.
  2. for (counter1=0; counter1<=OrdersTotal()-1; counter1++) {
    
    Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
 
WHRoeder:
  1. With the break version. If it finds an order with symbol/MN you get true. If it finds any that don't match you get false. If it doesn't find any orders, you get liveorder being undefined.
  2. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum

Thx WHRoeder! It's clear why is better the decrement in loop. I am defined the liveorder bollean in the beginning as false.

Now I have another problem. I like to get the profit of the last closed order from the history. I have tried many ways, but I got always wrong function.

for (counter2=OrdersHistoryTotal()-1;counter2 >= 0;counter2--) {
   if (OrderSelect(counter2,SELECT_BY_POS,MODE_HISTORY)==false || OrderTicket() != selected_order_ticket)
      continue;
   break;
}   

if (liveorder==false) {
   if (order_close_by_indi==false && OrderTicket()==selected_order_ticket && last_profit==false) {
        close_order_profit=OrderProfit() + OrderSwap() + OrderCommission();
        selected_order_ticket=0;
        last_profit=true;
.
.
.
.
.

What is that I know before the for... lop begin? I know the orderticket & symbol. This position will be closed. If the closing not with trigger came, the order_close_by_indi=false. The last_profit boolean represent the last position profit we got or not yet.

So.., is it good thinking to try to get with OrderTicket condition the last closed position, or not? If we get it, the last_profit will be true.

But this way is not working well. What would be the fault?

 

Sorry, I don't understand your post.

 But, here you don't use counter2 in your loop, so you try to select the same order position every time

for (counter2=OrdersHistoryTotal()-1;counter2 >= 0;counter2--) {
   if (OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY)==false || OrderTicket() != selected_order_ticket)
      continue;
   break;
}   
 
GumRai:

Sorry, I don't understand your post.

 But, here you don't use counter2 in your loop, so you try to select the same order position every time

 

Hi GumRai!

What is you don't understand? I try to explain it:

I like to get the last closed order profit. I think to get is the easiest way when I loop trought the history and with if... conditions I try to get it. In history each closed trade I examine with OrderTicket. I know the OrderTicket because of this ticket belong to the earlier live trade.

But with this code I don't get the right trade from the history.

I tried this one too:

if (OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY)==true) {
    if (OrderTicket()==selected_order_ticket) {
       profit=OrderProfit()...........
 
GumRai:

Sorry, I don't understand your post.

 But, here you don't use counter2 in your loop, so you try to select the same order position every time

 

Yep sorry I made wrong copy-paste.

for (counter2=OrdersHistoryTotal()-1;counter2 >= 0;counter2--) {
   if (OrderSelect(counter2,SELECT_BY_POS,MODE_HISTORY)==false || OrderTicket() != selected_order_ticket)
      continue;
   break;
}   

With this I get always the first in history, this is the deposit.

 
pecskeke1976:

Hi GumRai!

What is you don't understand? I try to explain it:

I like to get the last closed order profit. I think to get is the easiest way when I loop trought the history and with if... conditions I try to get it. In history each closed trade I examine with OrderTicket. I know the OrderTicket because of this ticket belong to the earlier live trade.

But with this code I don't get the right trade from the history.

I tried this one too:



If you know the ticket number, then just select that ticket

    OrderSelect(ticket,SELECT_BY_TICKET);
    if(OrderCloseTime()!=0)
      Profit=OrderProfit();
 
In addition to the for loop exiting too soon,  I see no condition for determining if OrderProfit() is actually positive or negative meaning (profit or loss)
I see you mentioned order_close_by_indi should know this already, however OrderProfit() does not know this
for (counter2=OrdersHistoryTotal()-1;counter2 >= 0;counter2--) {
   if (OrderSelect(counter2,SELECT_BY_POS,MODE_HISTORY)==false || OrderTicket() != selected_order_ticket)
      continue;
   break;  
}  ?? seems like you don't mean to exit for loop here // next if statement outside of for loop 

if (liveorder==false) { //if OrderTotal()=0 nothing will happen here
   if (order_close_by_indi==false && OrderTicket()==selected_order_ticket && last_profit==false) {
        close_order_profit=OrderProfit() + OrderSwap() + OrderCommission();  //OrderProfit() might be a negative number here meaning (loss), unless order_close_by_indi==false already knows this
        selected_order_ticket=0;
        last_profit=true;
.
.
.
.
.





Such as:
if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY)==true)
 if(magic.......symbol etc etc.
      {
       If(OrderProfit()>0)
            {
                 blah blah some more code
                 last_profit=true;
                 break;  //keeps it from counting on and on, just last order
            }
       If(OrderProfit()<0)last_profit==false;
           {
               more code etc etc
               last_profit=false;
               break;
            }


Also I'm not sure how this gets you what you want
for (counter1=0; counter1<=OrdersTotal()-1; counter1++)

As WHRoader pointed out what is the value for (live_order) if  OrderTotal() = 0  and exits the for loop ?


Anyhow that is about all I know
Reason: