Secondary order in EA

 

Hi,

Recently I tried to buid build an EA that can open order that dependent in another order.

Fpr example:

 I have for loop that run over my all open orders, and right after I have if function.

So I want the EA to do that:

if(for example) the order is 50 pips profit- or any other condition, not meeter what, the EA open another oreder, that related to the main order(the first order).

if the main order is, let's say, number 5, so the secondary order will be 5.1' so if I  close the first order(5), the secondary orde(5.1) close too.

 I tried to build this EA but I did not understand how to do that. I also serch in the web, but I have not found something useful.

If you know the solution, or have a useful tutorial and you can help me,  I will be grateful.

Have a nice day. 

 
.
 

Ticket numbers are assigned by the broker. They will be random.

You must remember the two order numbers.

Alternative, open one order and do a partial close. Why do you need two?

 
WHRoeder:

Ticket numbers are assigned by the broker. They will be random.

You must remember the two order numbers.

Alternative, open one order and do a partial close. Why do you need two?

 

Thanks for your response.

What do you mean that I must  remember the two order numbers? how? 

The reason  I need two orders (actually more then 2) is because my EA based on kind of headging strategy,

so I need the ability to open order that depending another order. 

 
MF10000:

 

Thanks for your response.

What do you mean that I must  remember the two order numbers? how? 

The reason  I need two orders (actually more then 2) is because my EA based on kind of headging strategy,

so I need the ability to open order that depending another order. 

You can either remember the ticket numbers by storing them in a globally declared or static array.

Another option is to use the ticket number of the first order as the magic number in the 2nd order. Then when you select the dependant order, you can retrieve the magic number and check whether the order with that ticket number has closed or not. 

 
GumRai:

You can either remember the ticket numbers by storing them in a globally declared or static array.

Another option is to use the ticket number of the first order as the magic number in the 2nd order. Then when you select the dependant order, you can retrieve the magic number and check whether the order with that ticket number has closed or not. 

 int Total=OrdersTotal(); 
   for (int i = 0; i < Total; i ++) 
   {
   OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
   double profit = OrderProfit();
   int Ticket = OrderTicket();
     if(profit == -50)
     {
     Ticket1 = OrderSend(Symbol(),1,1,Bid,1,0,0,NULL,Ticket,0,Blue);    
     break;     
     
     if(OrderMagicNumber()==Ticket && profit == -50)
     Ticket2 = OrderSend(Symbol(),0,Lot,Ask,1,0,0,NULL,Ticket1,0,Yellow);
     break;
     }
   }

Something like this?

 
MF10000:

Something like this?

int Total=OrdersTotal(); 
   for (int i = 0; i < Total; i ++) 
   {
   OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
   double profit = OrderProfit();
   int Ticket = OrderTicket();
     if(profit == -50)
     {
     Ticket1 = OrderSend(Symbol(),1,1,Bid,1,0,0,NULL,Ticket,0,Blue);    
     break;     
     
     if(OrderMagicNumber()==Ticket && profit == -50)
     Ticket2 = OrderSend(Symbol(),0,Lot,Ask,1,0,0,NULL,Ticket1,0,Yellow);
     break;
     }
   }

 

 No, you need to re-think the logic

     if(profit == -50)

 Don't compare doubles, very rarely that they will be exactly the same

     if(profit <= -50)

 may be what you mean, but note that profit is in currency, not pips

You have no check to see if a secondary order has already been opened, so a new order will be opened every tick while the condition is true. 

You break after the order open, so the 2nd section of code will not be executed 

 
GumRai:

 No, you need to re-think the logic

 Don't compare doubles, very rarely that they will be exactly the same

 may be what you mean, but note that profit is in currency, not pips

You have no check to see if a secondary order has already been opened, so a new order will be opened every tick while the condition is true. 

You break after the order open, so the 2nd section of code will not be executed 

 

Thank you.

I repaired the rest. 

But how can I check if the  secondary order has already opened?

 
MF10000:

Thank you.

I repaired the rest. 

But how can I check if the  secondary order has already opened?

You can do another loop through the orders and see if there is one with the magic number the same as the ticket. Don't use the same variable name as in the first loop.
Reason: