Selecting 2 orders with biggest profit

 

Can any one tell me how to select the 2 open trades with the highest profits from a number of open trades...


e.g. : if I have 6 open trades and want to close only to the 2 with the highest profits...how do I do this?


I know how to use a for loop to cycle through all the open trades and then use OrderSelect with an if statement to select the ones with the right magic numbers, ticket numbers, Symbol, etc. etc.


I just am not sure how to select the 2 with the highest profit...anybody know how to do this?


thanx

 
Curious as to why you would want to do that but anyway, you would have to find the order with the highest profit first then find the second highest. So you would cycle through the orders, comparing and swapping each order that is higher then the last. At the end of the cycle you would have the highest profit order. Repeat the cycle to find the next highest profit order. Use the
 
mqldelta:
Curious as to why you would want to do that but anyway, you would have to find the order with the highest profit first then find the second highest. So you would cycle through the orders, comparing and swapping each order that is higher then the last. At the end of the cycle you would have the highest profit order. Repeat the cycle to find the next highest profit order. Use the

Thanx for the reply but its the structure of what you are saying that i am not sure of...can you post an example of the cycle and profit comparison by ticket number for me please?


I want to do this as it is a hedging strategy and I want to close the 2 largest profitable orders when an opposite order hits stop loss, this way cover the losses.

 
23510:

Can any one tell me how to select the 2 open trades with the highest profits from a number of open trades...


e.g. : if I have 6 open trades and want to close only to the 2 with the highest profits...how do I do this?


I know how to use a for loop to cycle through all the open trades and then use OrderSelect with an if statement to select the ones with the right magic numbers, ticket numbers, Symbol, etc. etc.


I just am not sure how to select the 2 with the highest profit...anybody know how to do this?


thanx

you would have to write a loop that uses this:

double MathMax( double value1, double value2)

 
c0d3:

you would have to write a loop that uses this:

double MathMax( double value1, double value2)


ok so the MathMax() would allow me to determine the maximum value of the 2 orders...

so it follows then (if I understand correctly) that I can use more than 2 values...


e.g : double MathMax(double value1,double value2,double value3,double value4,double value5,double value6);


so then after the loop has determined the highest value of the 6 I do an orderselect with an if statement to match up the ticket that has the orderprofit that matches the result of the MathMax and then Orderclose that specific ticket.....


quite a long process but if it works, it will be worth it...

Thank you for pointing me in the right direction! I appreciate it!!!


If there is a flaw in my reasoning abive please correct me, if there is no flaw...thanx again for the help.

 
23510 wrote >>

ok so the MathMax() would allow me to determine the maximum value of the 2 orders...

so it follows then (if I understand correctly) that I can use more than 2 values...

e.g : double MathMax(double value1,double value2,double value3,double value4,double value5,double value6);

so then after the loop has determined the highest value of the 6 I do an orderselect with an if statement to match up the ticket that has the orderprofit that matches the result of the MathMax and then Orderclose that specific ticket.....

quite a long process but if it works, it will be worth it...

Thank you for pointing me in the right direction! I appreciate it!!!

If there is a flaw in my reasoning abive please correct me, if there is no flaw...thanx again for the help.

Files:
 
mqldelta:

Thanx a Mill!!! I really appreciate it.

 
mqldelta wrote >>

Script will generate an error if there isn't two profitable orders.

Reason: