# Secondary order in EA [Deleted]

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. [Deleted]
. 23488

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? [Deleted]
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?

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. Moderator
26123

MF10000:

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. [Deleted]
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 ++)
{
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)
break;
}
}
```

Something like this? Moderator
26123

MF10000:

Something like this?

```int Total=OrdersTotal();
for (int i = 0; i < Total; i ++)
{
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)
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 [Deleted]
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? Moderator
26123

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.