Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 15
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
The cycles must be counted in reverse order, fromOrdersTotal()-1 to 0.
The loops need to count backwards, fromOrdersTotal()-1 to 0.
Hello! I have installed my forex robot on my demo account and it has been running for the second month. I am opening the terminal today and have two open orders by one signal, although I have placed only one order before. I have a restriction on the number of orders. What may be the reason?
Judging by the code, it looks like you have a pending order rather than a market order.
Restriction on the number of open orders only applies to market orders.
That is why there is no error and therefore no problem.
Judging by the code, it looks like you have a pending order rather than a market order.
The limit on the number of open orders only applies to market orders.
Therefore there is no error and therefore no problem.
The restriction applies to open and pending orders. This is the second time in a month and a half, all the other cases are as they should be.
Try it this way:
SetOrder(NULL,OP_BUYSTOP,Lts,sar,sar-SL*Point(),sar+TP*Point(),Magik_number);
return(0);
}
if(NumberOfPositions(Symb,-1,Magik_number)==0 && NumberOfOrders(Symb,-1,Magik_number)==0 && Delta_Sell>High[1] && sar<Close[1]) {
SetOrder(NULL,OP_SELLSTOP,Lts,sar,sar+SL*Point(),sar-TP*Point(),Magik_number);
return(0);
}
//===============================================================================================
//------------------------------- Возвращает количество позиций --------------------------------+
//===============================================================================================
int NumberOfPositions(string sy="", int op=-1, int mn=-1) {
int kp=0;
if(sy=="") sy=Symbol();
for(int i=0; i<OrdersTotal(); i++) {
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) {
if(OrderType()==OP_BUY || OrderType()==OP_SELL) {
if(mn<0 || OrderMagicNumber()==mn) kp++;
}}}}
return(kp);
}
//===============================================================================================
//------------------------------- Возвращает количество ордеров --------------------------------+
//===============================================================================================
int NumberOfOrders(string sy="", int op=-1, int mn=-1) {
int kp=0;
if(sy=="") sy=Symbol();
for(int i=0; i<OrdersTotal(); i++) {
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) {
if(OrderType()>1 && OrderType()<6) {
if(mn<0 || OrderMagicNumber()==mn) kp++;
}}}}
return(kp);
}
Try it this way:
SetOrder(NULL,OP_BUYSTOP,Lts,sar,sar-SL*Point(),sar+TP*Point(),Magik_number);
return(0);
}
if(NumberOfPositions(Symb,-1,Magik_number)==0 && NumberOfOrders(Symb,-1,Magik_number)==0 && Delta_Sell>High[1] && sar<Close[1]) {
SetOrder(NULL,OP_SELLSTOP,Lts,sar,sar+SL*Point(),sar-TP*Point(),Magik_number);
return(0);
}
//===============================================================================================
//------------------------------- Возвращает количество позиций --------------------------------+
//===============================================================================================
int NumberOfPositions(string sy="", int op=-1, int mn=-1) {
int kp=0;
if(sy=="") sy=Symbol();
for(int i=0; i<OrdersTotal(); i++) {
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) {
if(OrderType()==OP_BUY || OrderType()==OP_SELL) {
if(mn<0 || OrderMagicNumber()==mn) kp++;
}}}}
return(kp);
}
//===============================================================================================
//------------------------------- Возвращает количество ордеров --------------------------------+
//===============================================================================================
int NumberOfOrders(string sy="", int op=-1, int mn=-1) {
int kp=0;
if(sy=="") sy=Symbol();
for(int i=0; i<OrdersTotal(); i++) {
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) {
if(OrderType()>1 && OrderType()<6) {
if(mn<0 || OrderMagicNumber()==mn) kp++;
}}}}
return(kp);
}
The functions have been replaced, it works fine in the test. Let's see how it will be in trading on the demo. But for some reason it seems to me that in my case both functions perform the same task, i.e. they answer the question whether there are orders and positions.
If double setting of the order is repeated, then the"SetOrder" function should be reviewed, perhaps it gives two requests, after the first request there is no exit from the function, and manages to send the second one.
If this is the case, why does it not always happen, something between tics? I'm not very good at this.