# How to close two orders of different pairs with the same magic number at a certain profit without touching the other orders?

206

Hello,

I develop a simple little strategy that consists of opening 6 orders at a time from different pairs, and I want that when two pairs with the same magic number reach a certain profit on money together they close without touching the 4 remaining orders, after closing both first i also want them to open just after closing, so on for all 3 pairs, so the strategy works by 2 pairs * 3!!

The part of the code I want you to help me close two orders of different pairs at a certain profit together:

```int TotalOrdersCount()
{
int result=0;
for(int i=0;i<OrdersTotal();i++)
{
if (OrderMagicNumber()==100) result++;
if (OrderMagicNumber()==101) result++;

if ((OrderSymbol() == "EURUSD" && OrderMagicNumber()== 101 && OrderProfit()) - (OrderSymbol() == "USDJPY" && OrderMagicNumber()== 101 && OrderProfit())
>= AccountBalance() * ProfitPercent) CloseEUUJ();

if ((OrderSymbol() == "GBPJPY" && OrderMagicNumber()== 100 && OrderProfit()) - (OrderSymbol() == "AUDUSD" && OrderMagicNumber()== 100 && OrderProfit())
>= AccountBalance() * ProfitPercent) CloseGJAU();
}
return (result);
} ```

attached mq4 file

I know my code is wrong, if someone can help me, thanks in advance!!

or

`if someone can download the mq4 code and corrects it will make me a lot of fun :-)`
Files:
30869

Hi,

Below is the modified version,

Regards.

Files:
206

Hi,

Below is the modified version,

Regards.

Hi,

I have tried your corrected version but all close in negative profit, attached the history of closed orders.
If you can review the code and try it on a demo account to see if it really works.
Still remains the reopening of orders after their closures
Thank you my brother.

Files:
5

would love to see this working!
206

Robertwllee:
would love to see this working!

I hope he will be well corrected, I trust our friend "Mehrdad Jeddi" and all the other friends!!

30869

OK,I improved it,it's working well on my side,
Files:
2638

Since you are categorizing groups of trades by magic number then there is no reason to hard-code symbols. Just iterate over each magic number and select orders based on that magic number.

```#property strict

input double inpProfitPercent=0.001;
const int MAGIC_NUMBERS[] = {100, 101, 102};

#include <stdlib.mqh>
#include <arrays/arrayint.mqh>
//+------------------------------------------------------------------+
int OnInit()
{
EventSetTimer(1);
return(INIT_SUCCEEDED);
}

void OnTick() { }

void OnTimer()
{
check_and_close(MAGIC_NUMBERS, profit_target());
}

double profit_target()
{
return AccountBalance()* inpProfitPercent;
}

void print_last_error(string error)
{
printf("%s: %s", error, ErrorDescription(GetLastError()));
}

void check_and_close(const int &magic_numbers[], const double min_profit)
{
for (int i=ArraySize(magic_numbers)-1; i>=0; --i) {
int magic_number = magic_numbers[i];
double profit = 0.0;
CArrayInt tickets;
RefreshRates();
for (int j=OrdersTotal()-1; j>=0; --j) {
if (OrderSelect(j, SELECT_BY_POS)
&& OrderType() < 2
&& OrderMagicNumber() == magic_number
){
profit += (OrderProfit() + OrderSwap() + OrderCommission());
}
}
if (profit >= min_profit) {
for (int j=tickets.Total()-1; j>=0; --j) {
if (OrderSelect(tickets[j], SELECT_BY_TICKET)) {
RefreshRates();
if (!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 100)) {
print_last_error("OrderCloseError");
return;
}
}
else {
print_last_error("OrderSelectError");
}
}
}
}
}```
206

OK,I improved it,it's working well on my side,
`Thank you for what you do for me and for the community of traders, I tried your new correction but still his close in loss by pair is bizard and I do not know why it does not work ... !!`
30869

So you should increase the "ProfitPercent",0.001 is very low on your low amount balance,

I my side it's working well,
206

nicholi shen:

Since you are categorizing groups of trades by magic number then there is no reason to hard-code symbols. Just iterate over each magic number and select orders based on that magic number.

Thank you "Mr nicholi shen" to you all too wonderful, I adapted the code that you posted and apparently it works well because it closed on a first win with two pairs, it's true that this a good idea of simplify the code, but the work that you sent us frankly I could never do it, because I am new in the programming mq4.
I have a second small service to ask you if possible, I want orders that close and reopen just after closing.
In the meantime I check again and again if your code works well and I'll tell you tomorrow.
thank you so much!!

206