min max profit/loss before order closure

 

I am trying to determine the minimum and maximum profit/loss before the order closes.


Somethings wrong with the code. Please review. Thanks


OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES);

{

profitloss=OrderProfit();

if(profitloss>max) max=profitloss;
if(profitloss<min) min=profitloss;
}
 

What I propose is


OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES);

{

max = MathMax(max,OrderProfit());

min = MathMin(min,OrderProfit());

}
 
Dohung:

What I propose is


OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES);

{

max = MathMax(max,OrderProfit());

min = MathMin(min,OrderProfit());

}

Thank you for your reply.


The problem is min and max reset to 0 with each new tick.

Example-

Alert("1 ",min," ",max);

OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES);

{

max = MathMax(max,OrderProfit());

min = MathMin(min,OrderProfit());

Alert("2 ",min," ",max);

}

Alert("3 ",min," ",max);


at alert 1, min and max are reset to 0

at alert 2, min and max are correct

at alert 3, min and max are correct

I do not understand why the values reset to 0.

 
spillstuff:

The problem is min and max reset to 0 with each new tick. [...]

Where are you declaring the variables min and max? If you want to preserve their values across calls to start() you can either declare them inside start() as static double, rather than just double, or you can declare them at global level rather than inside a function.


(If you've already done either of the above and you're still having this problem, then something very strange is going on. You've either found an MQL4 bug which has been hiding for years, or your code is somewhere resetting min and max to zero itself.)

 
jjc:

Where are you declaring the variables min and max? If you want to preserve their values across calls to start() you can either declare them inside start() as static double, rather than just double, or you can declare them at global level rather than inside a function.


(If you've already done either of the above and you're still having this problem, then something very strange is going on. You've either found an MQL4 bug which has been hiding for years, or your code is somewhere resetting min and max to zero itself.)

I declared them across start() and fixed the problem. I will have to study what actually happens as start() is initiated.


Thanks

 
OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES);
You do understand that this will work only if you have only ONE EA on only ONE chart, that opens at most ONE trade at a time.
    for(int pos = OrdersTotal() - 1; pos >= 0; pos--) { // Find my open order,
        if( OrderSelect(pos, SELECT_BY_POS)             // with my magic
        &&  OrderMagicNumber()  == MagicNumber          // number, in my chart.
        &&  OrderSymbol()       == Symbol() ) {
            order.ticket= OrderTicket();		order.type  = OrderType();
            ...
            break;
        }
    }
 

.

Hi,

I need some help about this subject.. Lets see if there are someone who can help me here (after 3 years!).. :)

I need to protect the gains on my EA. I was wondering about something that puts the stop in the break when LEVEL_PROTECT is achieved.

The hard way (for me) is to know the MAX and MIN profit of the current orders by it magic number.

Anyone can help me with a formula for that?

Thanks!

.

I

 
strutch:

.

Hi,

I need some help about this subject.. Lets see if there are someone who can help me here (after 3 years!).. :)

I need to protect the gains on my EA. I was wondering about putting the stop in the break when extern double LEVEL_PROTECT is achieved.

The hard way (for me) is to know the MAX and MIN profit of one of the current orders by it's magic number.

Anyone can help me with a formula for that?

Thanks!

.

I

 

.

Hi!

Can anyone help me to find a solution here?!?

I think that I will have to use an array with the profits of each order... But if I could get away from the arrays would be so nice!!! :)

.

 
strutch:

.

Hi!

Can anyone help me to find a solution here?!?

I think that I will have to use an array with the profits of each order... But if I could get away from the arrays would be so nice!!! :)

.

Show what you tried....

WHRoeder has given already some code you can use also inside your coding.....

 

Hi deVries,

I'm learning MQL4 language but I din't learn yet the use of arrays in Expert Advisors (I couldn't find nothing about arrays like the codeguru's course..).

That's why I'm stucked in this problem.. :(

WHRoeder code help me to make a cicle than runs all orders and select the one I want (by it's MagicNumber). But I don't know how to find that order Maximum Profit! : (

Any ideas?


for(int pos = OrdersTotal() - 1; pos >= 0; pos--) 
   {
   if( OrderSelect(pos, SELECT_BY_POS)&&OrderMagicNumber()== MagicNumber &&OrderSymbol()== Symbol() ) 
      {
      Max_Profit = ?!?!?!
      }
   } 

.

Reason: