Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 287
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
{
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;
summa=OrderProfit()+summa;
}
}
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()
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...
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 :
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:
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;
if(OrderProfit()>0) break;
I should stop the counter if the order has closed on profit, I see it correctly?
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 arrangement in the history. For yourself - yes, but for sell - bad.
If there is a source - DROW_NONE - no drawing
Didn't help, still displays numbers in the corner
In order of order in the list. But there is no guarantee of any ordering in this list