Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 287

 
Afternoon. Help with function please, sums loss of closed orders, when used with close orders function metatrader crashes, i think there is some error in this function.
double ClosProfit()
{
double summa=0;
int orders=OrdersHistoryTotal();
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
{
Print("Error in history!");
break;
}
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
continue;
//---
if(OrderProfit()>0) break;
If(OrderProfit()<0)i++;
}
If(i<0)
summa=OrderProfit()+summa;
}
If(OrderProfit()>summa)
summa=OrderProfit()+summa;
return(summa);
}
 
Lowech:
Hello. Please help me with this function, it summarizes closed orders' losses, metatrader crashes when used with close orders function, I think there is some kind of error in this function.

It's better to look at ready-made ones and adjust them if necessary. GetProfitFromDateInCurrency()

 
Lowech:
Afternoon. Help with function please, summarizes the loss of closed orders, when used with close orders function metatrader crashes, I think there is some kind of error in this function.

What do you mean Metatrader crashes? The program compiles with errors!!!

In three places instead of if with a small lowercase letter it says If with a capital letter

a pair of curly brackets is clearly unnecessary: the first one after int orders=OrdersHistoryTotal(); and its pair after summa=OrderProfit()+summa; - but this is unimportant

there is also...

 
Lowech:
Afternoon. Help with this function please, it sums up the loss of closed orders, when used with the close orders function Metatrader crashes, I think there is some kind of error in this function.

Try this :

double ClosProfit()
{ double summa=0;
  int orders=OrdersHistoryTotal(); 
  for(int i=orders-1;i>=0;i--)
  {
    if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
    if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
    if(OrderProfit()<0.0) summa=OrderProfit()+summa;
  }
  return(summa);
}
 
I was 2 minutes late writing ... Let's consider this part carefully:
  for(int i=orders-1;i>=0;i--)
  {
    if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
    {
      Print("Error in history!");
      break;
    }
    if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
    if(OrderProfit()>0) break;
    if(OrderProfit()<0)i++;
  }
  if(i<0)

Compiled it with #property strict directive - it's the last line: 'i' - undeclared identifier. The point is that the variable is declared in a loop and is valid only in the loop. Without the #property strict directive it compiles without errors, but this is bad. The directive must be used.

It is easier to write if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) instead of if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)). Now the next step is a break - this is the exit from the loop. But we need to process the other orders. However, on the history, this is not necessary. The error occurs on market and pending orders, if by the time the order is being processed, it has closed and is missing. The general rule of thumb is if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue; - continue processing the next orders

For the same reason, replace if(OrderProfit()>0) break; with if(OrderProfit()>0) continue;

And here is the reason for the error: if(OrderProfit()<0)i++; - if(OrderProfit()>0; - the index is incremented. And the loop header for(int i=orders-1;i>=0;i--) decreases it. The processing of the same order is repeated and the program gets in a loop. This is probably the way it should be:

#property   strict
double ClosProfit()
{
  double summa=0;
  for(int i=OrdersHistoryTotal()-1; i>=0; i--)
  {
    if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
    if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
    if(OrderProfit()<0) summa+=OrderProfit();
  }
  return(summa);
}
 
STARIJ:
2 minutes late while writing ... Let's consider this part carefully:

Compiled it with #property strict directive - it's the last line: 'i' - undeclared identifier. The point is that the variable is declared in a loop and is valid only in the loop. Without the #property strict directive it compiles without errors, but this is bad. The directive must be used.

It is easier to write if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) instead of if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)). Now the next step is a break - this is the exit from the loop. But we need to process the other orders. However, on the history, this is not necessary. The error occurs on market and pending orders, if by the moment of processing, the order has closed and it is missing. The general rule of thumb is if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue; - continue processing the next orders

For the same reason if(OrderProfit()>0) break; replace with if(OrderProfit()>0) continue;

And here is the reason for the error: if(OrderProfit()<0)i++; - if(OrderProfit()>0; the index is incremented. And the loop header for(int i=orders-1;i>=0;i--) decreases it. The processing of the same order is repeated and the program gets in a loop. This is probably the way it should be:


Thank you, no more metatrader crashes) Regarding

if(OrderProfit()>0) break; replace with if(OrderProfit()>0) continue;

I want this function to ignore this profit when an order closes on profit. I only want it to include the amount of loss before the first order closes on profit. I.e.

if(OrderProfit()>0) break;


I should stop the counter if the order has closed on profit, I see it correctly?
 
Lowech:

Thank you, no more metatrader crashes) What I need is that if an order closes on profit, the function does not take into account this profit, only the amount of loss and up to the first order closed on profit. I.e.
I should stop the counter if the order has closed on profit, I see it correctly?
Your reasoning is almost correct. There is no guarantee of any order placement in the history. For yourself you can, but for sell - bad.
 
STARIJ:
Your reasoning is almost correct. There is no guarantee of any order arrangement in the history. For yourself - yes, but for sell - bad.
Isn't the order-based loop calculating the orders in order?
 
STARIJ:

If there is a source - DROW_NONE - no drawing

Didn't help, still displays numbers in the corner

 
Lowech Doesn't the order loop count orders in order?

In order of order in the list. But there is no guarantee of any ordering in this list

Reason: