Slawa can you please help me to solve this problem??

 
Hello Slawa,

Hope you had nice Christmas.

I have not been able to solve this problem for 6 months. Can you please help me do it ?

Here is problem. Lets assume we have 49 orders open: Order 1, Order 2, Order 3, .........., Order 50 (Order 17 is not open).

I want to write code so that program checks ALL of these orders before doing any action. I want program to do Action "X" if Order 17 is not open in trade terminal (but all other orders are open ie 1,2,3,4,.....48,49,50)

Problem I have is that if I use the "for" operator and code checks first order in trade terminal and this is not Order 17 then it will do action "X". I dont want this to happen. I want code to check ALL 49 orders first, and then if order 17 has not been opened in trade terminal , then do action "X"............ structure of code I was using is below:


for(cnt=0;cnt<OrdersTotal();cnt++)
{
If MagicNumber != 17 then
{
do action "X"
}
}


To uniquely identify, I attach a Magic Number to each of 50 Orders ie MagicNumber1=Order1, etc., etc.

I dont want to have to solve this problem by having 50 nested loops - this very messy and long and confusing way to solve this problem.

Can you think of better way to solve this probelm Slawa?

Thank you Slawa.

Kind regards

RJF
 
My idea is to make a loop to check all orders first, then store the result into an array,
after that, only array checking is needed, which is fast and simple. you may even skip
checking order list every time and update the array directly when order added/changed.

have a try with the code below (not tested)

btw: can you also explain the advantage of using order checking method you said in EA?
usually most EA only set a maximum number of buy/sell orders total, and open order
based on it. your method will also need different handling on each order, which making it complex?

#define ORDER_MAGICNUMBER_PREFIX 0512301
#define ORDER_ENTRY_LIST_MAX     1000

int init()
{
   Print("Init");
   OrderListInit(50);
   return(0);
}

int deinit()
{
   Print("Deinit");
   return(0);
}

int OrderEntryList[];

int OrderListInit(int size, int MagicNumberPrefix = ORDER_MAGICNUMBER_PREFIX)
{
   int i, n, m, cnt;

   if (size < 0 || size >= ORDER_ENTRY_LIST_MAX) return (-1);
   ArrayResize(OrderEntryList, size);
   for (i = 0; i < size; i++) {
      OrderEntryList[i] = 0;
   }
   n = MagicNumberPrefix * ORDER_ENTRY_LIST_MAX;
   i = 0;
   for (cnt = OrdersTotal(); cnt >= 0; cnt--) {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      m = OrderMagicNumber() - n;
      if (m <0 || m >= size) {
         //magic number not in range
         continue;
      }
      i++;
      OrderEntryList[m] = OrderTicket();
   }
   return (i);
}

int OrderMagicNumberCreate(int index, int MagicNumberPrefix = ORDER_MAGICNUMBER_PREFIX)
{
   return (MagicNumberPrefix * ORDER_ENTRY_LIST_MAX + index);
}

int OrderListFind(int index)
{
   if (index >= ArraySize(OrderEntryList)) return (0);
   return (OrderEntryList[index]);
}

void OrderListAdd(int index, int ticket)
{
   if (index >= ArraySize(OrderEntryList)) return (0);
   OrderEntryList[index] = ticket;
   return;  
}


int start()
{
   int ticket;
   OrderListInit(50);
   ticket = OrderListFind(0);
   if (ticket != 0) {
      //order 0 already opened, you can also check the order using the ticket
   } else {
      //order 0 not opened yet, open if condition met.
   }

   //do same to other orders...   
   ticket = OrderListFind(17);

  
   //or you can do it in a single loop
   for (int i = 0; i < 50; i++) {
      ticket = OrderListFind(i);
      if (ticket != 0) {
         //order i already opened
      } else {
         //order i not opened yet
      }
   }
   return(0);
}
 
Hi there,

thank you so much for this. reason i needed to know this is that i have 50 different systems all combined in the one EA - yes very confusing re order handling.

Your idea to store order details in array and then check array, is brilliant idea.

Thanks for this. Have a great 2006 :-)

Kind regards

RJF
Reason: