How to select by pos and compare 2 orders at a time? How to make a OrderSelect loop? - page 2

 
qjol:

first let me understand what u what compare the OrderOpenPrice or the symbol or TP or what?


thanks
I would like to compare the profit of the order 1 and 2, then order 3 and 4 then order 5 and 6 ... etc  

 
johnnybegoode:


for (int i = 0; i < OrdersTotal(); i++){

(OrderSelect(i, SELECT_BY_TICKET)) = int ticket A

No. Select by pos as usual to loop through the orders. but after selecting immediately save the value of OrderTicket() (and also OrderProfit() or whatever you want to compare with other orders in the inner loop)


then do your inner loop as usual or whatever else that will disturb the current selection.


then, back in the outer loop, if you still need the order of the outer loop, restore the selection with SELECE_BY_TICKET.

 
7bit:

No. Select by pos as usual to loop through the orders. but after selecting immediately save the value of OrderTicket() (and also OrderProfit() or whatever you want to compare with other orders in the inner loop)


then do your inner loop as usual or whatever else that will disturb the current selection.


then, back in the outer loop, if you still need the order of the outer loop, restore the selection with SELECE_BY_TICKET.



thanks, something like these?


for (int i = 0; i < OrdersTotal(); i++){

  (OrderSelect(i, SELECT_BY_POS))

int ticket A = OrderTicket();

double profit A = OrderProfit();

}



for (int j = 0; j < OrdersTotal(); j += 2) {

    (OrderSelect(j, SELECT_BY_POS)) 

int ticket B =  OrderTicket();

double profit B = OrderProfit();

}

ex.

if (profit A + profit B) > 5

{

  then OrderClose both ticket A and ticket B

}

 

but how do I combine them? Both the loop needs to be in a bigger loop right?

Would ticket and profit A and B be updated by the loop to order (1 and 2), then order( 3 and 4), then order (5 and 6), (7 and 8)  and so on ... ?

 

Your code examples look like you have not done much programming before. You would have to nest one loop inside the other and do the comparison in the inner loop. And then break out of the inner loop once you found a match.

Maybe it's a good time to pause your experimenting with mql4 for a while (since it is incredibly painful to learn the basics of programming with it) and rather acquire and practice the most basic ideas and concepts in programming with a language that is easier to use and debug. ANY other language will do. Try Java, it has a quite similar syntax but is much easier to learn the basic concepts. Install NetBeans and try some Java tutorials and then do loops and nested loops and how to break out of them and functions, variables, assignments, etc., all the basic stuff until you get a feel for it.

 

[Edit:] OP has deleted the posting for which the following was the answer:


Java is not harder, its actually exactly the same (or even easier) if you restrict yourself to procedural programming. This is not about learning their huge repository of libraries, you don't use them for learning the language, this is about simple concepts like variables, loops and functions.

The difference is you don't have to fiddle around with MT4 and its requirements and its limited means of debugging and actually seeing what is going on.

You can just write the code and click a button to execute it and see the output or even let it step through the code line by line and show you the contents of variables and how they change as the program proceeds line by line so you can follow the program flow. This is great for learning.

 
7bit:

[Edit:] OP has deleted the posting for which the following was the answer:


Java is not harder, its actually exactly the same (or even easier) if you restrict yourself to procedural programming. This is not about learning their huge repository of libraries, you don't use them for learning the language, this is about simple concepts like variables, loops and functions.

The difference is you don't have to fiddle around with MT4 and its requirements and its limited means of debugging and actually seeing what is going on.

You can just write the code and click a button to execute it and see the output or even let it step through the code line by line and show you the contents of variables and how they change as the program proceeds line by line so you can follow the program flow. This is great for learning.





Thanks GUYS! Going to try if this will work, no error compiling.

void CloseAll() 
{

   for (int i=0; i<OrdersTotal(); i++)
   {
         if (OrderSelect(i, SELECT_BY_POS))
         int ticketA = OrderTicket();
         double profitA = OrderProfit();
         
               for (int j=0; j<OrdersTotal(); j+=2) 
               {
                     if (OrderSelect(j, SELECT_BY_POS)) 
                     int ticketB =  OrderTicket();
                     double profitB = OrderProfit();
                     
                              if ((profitA + profitB) > 5)
                              {
                                    OrderClose(ticketA,OrderLots(),OrderClosePrice(),5,Violet);
                                    OrderClose(ticketB,OrderLots(),OrderClosePrice(),5,Violet);
                                    
                              }     else   OrderDelete(OrderTicket());                                          
               }
   }      
         
}
 
johnnybegoode:



Thanks GUYS! Going to try if this will work, no error compiling.

u r going to compare the first order with himself
 
I still don't understand what this is good for. I mean the actual meaning of it, the IDEA behind it, why would you want to add two order's profit and then close them? Why not three orders or only one? What kind of trading system is this?
 

johnnybegoode:

I would like to loop comparison of (1st with 2nd) orders, (3rd with 4th), (5th with 6th), (7th with 8th), (9th with 10th) ....till i, TotalOrder

(OrderSelect(0, SELECT_BY_POS)) // assuming the 0 is the first order (correct me if I am wrong)

and

(OrderSelect(1, SELECT_BY_POS))

then compare them

while still inside the total order loop

(OrderSelect(2, SELECT_BY_POS)) and

(OrderSelect(3, SELECT_BY_POS))

    for(int pos = 0; pos < OrdersTotal(); pos++) if (
        OrderSelect(pos, SELECT_BY_POS)             // Only my orders w/
    &&  OrderMagicNumber() == Magic.Number          // my magic number
    &&  OrderSymbol()      == Symbol() ){           // and period and symbol

        double profit1=OrderProfit(); // Save what ever you need.

        // Continue searching for the next one.
        for(pos++; pos < OrdersTotal(); pos++) if (
            OrderSelect(pos, SELECT_BY_POS)             // Only my orders w/
        &&  OrderMagicNumber() == Magic.Number          // my magic number
        &&  OrderSymbol()      == Symbol() ){           // and period and symbol

            // Found next one
            double profit2=OrderProfit(); // Save the second one
            if (...) {} // do what you need with both

            break; // Exit inside loop and find the first of the next pair.
        }
    }
 
qjol:
u r going to compare the first order with himself

Ya, does not work and only 1 order is close and not both together

WHRoeder:


 Thanks, but is this comparing 1 with 2, then 2 with 3, then 3 with 4?

I need to specifically compare order

1 & 2 only

3 & 4 only

5 & 6 only

..... and so on



other orders should not be compared.

Reason: