searching arrays

 


I have a system where every time I open an immediate live order, I also open 2 pending orders. I now want to add to this system so that whenever a live order is closed (manually or by Take Profit or Stop Loss)
then its associated 2 pending orders are closed as well.

I have made an effort towards this but am now stuck.

I have 2 arrays (both 1-dimensional): ray and dee

double ray[1000];
int m;
double dee[1000];
int n;

Whenever an order is opened (live or pending) its OrderTicket() number is put in array "ray".
Whenever an order is closed its OrderTicket() number is put in array "dee".

I want to search the array "ray" for every OrderTicket() that is in the array "dee". When it finds a match in ray I want it to then look for the OrderTicket() values at positions one and two along from the match and then close these orders.

MY START: (but not working)


for (n=0; n<=1000; n++) {
int rupt=ArrayBsearch(ray,dee[n],WHOLE_ARRAY,0,MODE_ASCEND);
if (ray[rupt] == dee[n])
{
kit = rupt+1;
des = kit+1;
dexx = ray[kit];
muxx = ray[des];
}
}


for (int f=1; f<=OrdersTotal(); f++) //Cycle for all orders..
{
if(OrderSelect(f-1,SELECT_BY_POS)==true)
{if (OrderTicket() == dexx || muxx) {
OrderDelete(OrderTicket());
}
}

Thanks in advance. im really stuck. really need someone help me out here.

 
maybe you could use a different approach.
double giOrderTicket[][3];

int start(){

   checkTickets();

   int liTicket = OrderSend(.., BUY, ...);
   //error check
   int liOrdersCNT = ArraySize(giOrderTicket);
   ArrayResize(giOrderTicket, liOrdersCNT+1);
   giOrderTicket[liOrdersCNT][0] = liTicket;
   
   liTicket = OrderSend(.., BUYSTOP, ...);
   //error check
   giOrderTicket[liOrdersCNT][1] = liTicket;
   liTicket = OrderSend(.., SELLSTOP, ...);
   //error check
   giOrderTicket[liOrdersCNT][2] = liTicket;
}

void checkTickets(){

   int liOrdersCNT = ArraySize(giOrderTicket);
   for (i = 0; i < OrderHistoryTotal(); i++){
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) != FALSE){
         // other checks
         for (j = 0; j < liOrdersCNT ; j++){
            if (giOrderTicket[j][0] == OrderTicket()){
               OrderDelete(giOrderTicket[j][1]);
               //error check
               OrderDelete(giOrderTicket[j][2]);
               //error check
               giOrderTicket[j][0] = -1;
               giOrderTicket[j][1] = -1;
               giOrderTicket[j][2] = -1;
            }
         }
      }
   }
}

something like this. The code is not tested


hth

 
mikey:


I have a system where every time I open an immediate live order, I also open 2 pending orders. I now want to add to this system so that whenever a live order is closed (manually or by Take Profit or Stop Loss) then its associated 2 pending orders are closed as well ....

Your request I've marked bold is clear, but the rest I assume is your attempt/approach to the problem, not an additional/separate problem...

Why not make good use of the OrderMagicNumber() to group the orders? This is in pseudo-code obviously..

bool group_Order = true; int BuyTicket[], BuyStopTicket[], SellStopTicket[]; int i;

if(group_Order && buy_condition) {

int group_MN = Generate_Group_MN(i);

BuyTicket[i] = (OrderSend(...Buy...group_MN...);

BuyStopTicket[i] = OrderSend(...BuyStop...group_MN...);

SellStopTicket[i] = OrderSend(...SellStop...group_MN...); i++;

}
// later scan and filter with OrderMagicNumber and submit ticket from their respective array 
// (you can also store their ticket with MN on 2-dimensional array and call ticket by their MN, or 
// more uglier way: concatenate it and later substring it and string to int it to call tickets 

regards

cameo

 

Thanks so much for your replies. Yes, this is what I am trying to achieve: (BTW - i am so sorry to write again, I am just so really stuck on this)

every time I open an immediate live order, I also open 2 pending orders. I want to add to this system so that whenever a live order is closed (manually or by Take Profit or Stop Loss) then its associated 2 pending orders are closed as well.

My chosen approach is to have a 2-dimensional array (called ray) and to put the Orderticket number of the immediate live order in

ray[m,0]

and the Orderticket numbers of the 2 pending orders in

ray[m,1]

ray[m,2]

For the first set (one live, 2 pending orders) m = 0, for the second set (one live, 2 pending orders) m = 1, for the third set (one live, 2 pending orders) m = 2 and so on and so on with m increasing each time. Below is the code for setting up this array: (though NOT working properly)

// --------------------------------------

if (MathRand() < 16000)
{
// immediate Buy
OrderSend(Symbol(),OP_BUY,0.1,Ask,1,Bid-200*Point,Bid+200*Point);
if(OrderSelect(d,SELECT_BY_POS)==true) {
ray[m,0] = OrderTicket();
d++;
}
double deck = Ask;
// Pending sells
OrderSend(Symbol(),OP_SELLSTOP,0.1,deck-100*Point,1,Ask+200*Point,Ask-200*Point,"comm",12345);
if(OrderSelect(d,SELECT_BY_POS)==true) {
ray[m,1] = OrderTicket();
d++;
}
OrderSend(Symbol(),OP_SELLSTOP,0.1,deck-100*Point,1,Ask+200*Point,Ask-200*Point,"comm",12345);
if(OrderSelect(d,SELECT_BY_POS)==true) {
ray[m,2] = OrderTicket();
}
d++;
m++;
}

// ------------------------------------

BUT the problem is that with successive calls of this loop "m" just wont increase above "1". And i cant for the life of me understand why. In coding this I had hoped that m would increase by 1 at the end of each call of this code block, so m would iterate up and up by value 1 at each call of it.

On a probably related note, "d" just wont get above value of 3, no matter how many times this code block is called. And I dont understand this either.

Once I fix this issue above I'm hoping then that the following code, added on the end of it, will do the business:

// ------------------------------------

// If a live order is found to have been closed, its 2 associated pending orders are then closed.


for (g=0; g<=1000; g++)
{
if(OrderSelect(g,SELECT_BY_POS,MODE_HISTORY)==true) // cycling through CLOSED orders
{
for (m=0; m<=1000; m++)
{
if (OrderTicket() == ray[m,0])
{
for (int f=1; f<=OrdersTotal(); f++)
{
if(OrderSelect(f-1,SELECT_BY_POS)==true) // cycling through OPEN or PENDING orders
{
OrderDelete(ray[m,1]);
OrderDelete(ray[m,2]);
}
}
}
}
}
}

// ----------------------------------------

 
mikey:

Thanks so much for your replies. Yes, this is what I am trying to achieve: (BTW - i am so sorry to write again, I am just so really stuck on this)

every time I open an immediate live order, I also open 2 pending orders. I want to add to this system so that whenever a live order is closed (manually or by Take Profit or Stop Loss) then its associated 2 pending orders are closed as well.

My chosen approach is to have a 2-dimensional array (called ray) and to put the Orderticket number of the immediate live order in

ray[m,0]

and the Orderticket numbers of the 2 pending orders in

ray[m,1]

ray[m,2]

For the first set (one live, 2 pending orders) m = 0, for the second set (one live, 2 pending orders) m = 1, for the third set (one live, 2 pending orders) m = 2 and so on and so on with m increasing each time. Below is the code for setting up this array: (though NOT working properly)


Mikey,

- ray[m,2] would make it 3-dimensional... Added : I'll take a try at it on a later post...

- on a lighter note, if you call your array 'ray' and the other array 'donna' and put them in a loop they might end up with 'ray_junior.' :)))))))) Use descriptive names for identifiers,
e.g. Order_ID_Array. etc.

I'm sure you can crack this one...

regards

cameo

 
cameofx:
Mikey,

- first of all, finding an error on a not properly indented code is a pain in the neck... this is true for any programmer (especially, when code lines goes to thousands) it's not surprising you can't make it work.

when writing you should know beforehand if a piece of code is the parent or child or the same level (sibling?) of the other piece. And then indent accordingly. To match braces try pasting to a dedicated Editor like Notepad++ (google for it). Click on a brace/bracket it will highlight to find its match......................

I just now realized you may have already properly indented it but it doesn't show coz' you didn't paste with SRC button. No offense intended, I edited my post... please use SRC button next time...

good luck,

cameo

 
cameofx:

Mikey,

- ray[m,2] would make it 3-dimensional...

Can you believe that? :)))) Two strikes! S O R R Y, It's late afternoon & I'm drowsy (bla..bla..excuses). I'll make it up to you Mikey...
 

If you're still interested, here's my attempt FWIW. Note : It won't compile, I left some functions to be defined by you/later,

It may have algo error as well, I haven't test it. but you'll get the picture... feel free to correct / comment...

// --------------------------------------
// setup an array container Arr_Gr_Order_tickets[counter_resizer][3];
// at every succesful Group Order, increment counter_resizer and resize Arr_Gr_Order_tickets
int    counter_resizer=1;
int    Arr_Gr_Order_tickets[counter_resizer][3]; 
double Price_Buy , Price_SellStop_1, Price_SellStop_2; 
double SL_Buy, SL_SellStop_1, SL_SellStop_2;
double TP_Buy, TP_SellStop_1, TP_SellStop_2;
int    Group_MN;
int    f_Group_MN():

init()
{
ArrayInitialize(Arr_Gr_Order_tickets,0); 
ArrayResize(Arr_Gr_Order_tickets,counter_resizer);
return(0);
}// end init

// Function for Sending orders
int Ticket_Buy()        = { return (OrderSend( NULL, OP_BUY,      vol, Price_Buy       , Slip, SL_Buy       , TP_Buy       , NULL, Group_MN ) ); }
int Ticket_SellStop_1() = { return (OrderSend( NULL, OP_SELLSTOP, vol, Price_SellStop_1, Slip, SL_SellStop_1, TP_SellStop_1, NULL, Group_MN ) ); }
int Ticket_SellStop_2() = { return (OrderSend( NULL, OP_SELLSTOP, vol, Price_SellStop_2, Slip, SL_SellStop_2, TP_SellStop_2, NULL, Group_MN ) ); }


int start()
{
// First OrderSend Attempt, Resizing ticket array & incrementing array's counter
// if either one succeeded increment counter and resize array
   if( Ticket_Buy() > 0 || Ticket_SellStop_1() > 0 || Ticket_SellStop_2() > 0 ) 
   { 
     counter_resizer++;
     ArrayResize(Arr_Gr_Order_tickets,counter_resizer) // resizing array for future use
     Arr_Gr_Order_tickets[counter_resizer][0] = 0;
     Arr_Gr_Order_tickets[counter_resizer][1] = 0;
     Arr_Gr_Order_tickets[counter_resizer][2] = 0;
   }
// Checking & Resending Order Part

   int OrderAttempt=0;

   while ( Ticket_Buy < 0 || Ticket_SellStop_1 < 0 || Ticket_SellStop_1 < 0 || 
           Done_Buy == false || Done_SellStop_1 == false || Done_SellStop_2 == false )
   {
        if (Ticket_Buy()        > 0     ) Arr_Gr_Order_tickets[counter_resizer-1][0] = Ticket_Buy;
      else { ... Done_Buy        = true; } // handle errors & attempt resending then quite 
        if (Ticket_SellStop_1() > 0     ) Arr_Gr_Order_tickets[counter_resizer-1][1] = Ticket_SellStop_1;
      else { ... Done_SellStop_1 = true; } // handle errors & attempt resending then quite 
        if (Ticket_SellStop_2() > 0     ) Arr_Gr_Order_tickets[counter_resizer-1][2] = Ticket_SellStop_2;
      else { ... Done_SellStop_2 = true; } // handle errors & attempt resending then quite 

      OrderAttempt++; counter_resizer++; Print("OrderAttempt = ", OrderAttempt, " counter_resizer = ",counter_resizer);
   }
   
// finding first occurence of non-zero Arr_Gr_Order_tickets (Newest_counter_resizer) from the maximum counter down 
// for retrieving tickets by calling Arr_Gr_Order_tickets[][]
int size = ArraySize(Arr_Gr_Order_tickets);
int Newest_counter_resizer;

   for(i=size ; i>=0; i--)
   { 
      if ( Arr_Gr_Order_tickets[i-1][0] !=0 || Arr_Gr_Order_tickets[i-1][1] !=0 || Arr_Gr_Order_tickets[i-1][2] !=0 )
      {     break; Newest_counter_resizer = i-1; 
      }
   }

// Closing Order Part
// This part is suppose to be separate and with conditions

bool Close_Buy()         = OrderClose( Arr_Gr_Order_tickets[i][0], vol, Price_Buy       , Slip );
bool Close_SellStop_1(() = OrderClose( Arr_Gr_Order_tickets[i][0], vol, Price_SellStop_1, Slip );
bool Close_SellStop_2(() = OrderClose( Arr_Gr_Order_tickets[i][0], vol, Price_SellStop_2, Slip );

   for(i=Newest_counter_resizer ; i>=0 ; i--)

      if ( Arr_Gr_Order_tickets[i][0] != 0 && OrderSelect(Arr_Gr_Order_tickets[i][0],SELECT_BY_TICKET) )
      { 
         Print(" Arr_Gr_Order_tickets[",i,"][0] = ", Arr_Gr_Order_tickets[i][0] ," is not empty and was grabbed ");
         if ( Close_Buy() )
         { Arr_Gr_Order_tickets[i][0] = 0; ) // resetting ticket array to zero }
      }
      if ( Arr_Gr_Order_tickets[i][1] != 0 && OrderSelect(Arr_Gr_Order_tickets[i][1],SELECT_BY_TICKET) )
      { 
         Print(" Arr_Gr_Order_tickets[",i,"][1] = ", Arr_Gr_Order_tickets[i][1] ," is not empty and was grabbed ");
         if ( Close_SellStop_1() )
         { Arr_Gr_Order_tickets[i][1] = 0; ) // resetting ticket array to zero }
      }
      if ( Arr_Gr_Order_tickets[i][2] != 0 && OrderSelect(Arr_Gr_Order_tickets[i][0],SELECT_BY_TICKET) )
      { 
         Print(" Arr_Gr_Order_tickets[",i,"][2] = ", Arr_Gr_Order_tickets[i][2] ," is not empty and was grabbed ");
         if ( Close_SellStop_1() )
         { Arr_Gr_Order_tickets[i][2] = 0; ) // resetting ticket array to zero }
      }
   }
} // end start

Reason: