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

 

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++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     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:
 

Hi,

Below is the modified version,

Regards.

Files:
 
Mehrdad Jeddi:

Hi,

Below is the modified version,

Regards.

Hi,

Thank you for your quick reply.
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:
 
would love to see this working!
 
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!!

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

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());
            tickets.Add(OrderTicket());
         }
      }
      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");
            }
         }
      }
   }
}
 
Mehrdad Jeddi:
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 ... !!
 
Hadj Ahmed Slimani:

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

I my side it's working well,
 
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!!

 
Mehrdad Jeddi:

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

I my side it's working well,

ok I'll try to increase it and I'll give you the answer
Thank you so much :-)

Reason: