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

 
Zvezdochet:

YES. This is exactly what Sergei Kovalev's textbook is all about. It is the one that is missing a lot at the moment.

There is no textbook on mql5 as such on this site, I don't know about others. But there are a lot of articles, read them, you may succeed.

Статьи по MQL5
Статьи по MQL5
  • www.mql5.com
Статьи по программированию на языке MQL5
 

Gentlemen programmers!

Please help me solve a problem I've been struggling with for a month.

My TS sometimes doesn't close orders for some reason.

It looks like this:

The first three orders - 44329646, 44330563 and 44337351 open and close perfectly.

And on the fourth order, the tragedy happens:

and so on to infinity...

That is, an Expert Advisor opens order 44339156 and tries to close order 44337351 that it itself (!!!!) successfully closed several hours earlier.

You have to close it manually; if you fail to do that, you will sometimes suffer from the deepest drawdown. What a disaster!

How is it possible?

The code of the Expert Advisor is as simple as can be:

здесь - условие открытия сделки
{
         RefreshRates();
         total_orders_NZDUSD=TotalOrders("NZDUSD.I");
         if(total_orders_NZDUSD==0)
         {
         Balance=AccountBalance();
         Lots=NormalizeDouble((Balance/(Ask*10.0))*0.01,2);
         double AskNorm=NormalizeDouble(Ask,Digits);
         ticket_buy_NZDUSD=OrderSend("NZDUSD.I",OP_BUY,0.01,AskNorm,0,0,0);
         }
      }
здесь - условие закрытия сделки
      {
         RefreshRates();
         if(OrderSelect(ticket_buy_NZDUSD, SELECT_BY_TICKET)==true)
         {
         total_orders_NZDUSD=TotalOrders("NZDUSD.I");
         ctm_ticket_buy_NZDUSD=OrderCloseTime();
         order_type_NZDUSD=OrderType();
            if(total_orders_NZDUSD>0)
            {
               if(ctm_ticket_buy_NZDUSD==0)
               {
                  if(order_type_NZDUSD==OP_BUY)
                  {
                  double BidNorm=NormalizeDouble(Bid,Digits);
                  OrderClose(ticket_buy_NZDUSD,0.01,BidNorm,0);
                  }
               }
            }
         }

How can one and the same program work "time after time"? I don't understand...

 
Alexander_K2:

Gentlemen programmers!

Please help me solve a problem I've been struggling with for a month.

My TS sometimes doesn't close orders for some reason.

It looks like this:

The first three orders - 44329646, 44330563 and 44337351 open and close fine.

And on the fourth order, the tragedy happens:

and so on to infinity...

That is, an Expert Advisor opens order 44339156 and tries to close order 44337351 that it itself (!!!!) successfully closed several hours earlier.

You have to close it manually; if you fail to do that, you will sometimes suffer from the deepest drawdown. What a disaster!

How is it possible?

The code of the Expert Advisor is as simple as can be:

How can one and the same program work "time after time"? I do not understand...

Read the Help for OrderSelect(). In particular, read the reference on selection by ticket. Even a closed order is selected by ticket. It is already closed, and you are trying to close it again.

If 0, it means that it is still in the market, close it, if > 0, it means that it is already closed and there is no need to kick it again, it will not come to life and will not close.

 
Artyom Trishkin:

Read the help for OrderSelect(). Particularly about selection by ticket. Even a closed order is selected by the ticket. It is already closed, and you are trying to close it again.

If it is selected, check the closing time - if 0, it means it is still in the market - close it, if > 0, it means it has already been closed and you do not have to kick it again - it will not come to life and will not close.

Thank you very much - I will try.

 
Alexander_K2:

Thank you very much - I will give it a try.

You're welcome. You don't have to try it, you have to do it...

 
Artyom Trishkin:

Please. You don't have to try it, you have to do it...

Artem, I have read the help - everything seems to be correct.

ctm_ticket_buy_NZDUSD=OrderCloseTime();
         ...
               if(ctm_ticket_buy_NZDUSD==0)
            ...

I am checking the closing time.

I have read it in MQL4 Reference:

It is recommended to call the OrderSelect() function immediately before calling for the latest order data.

This is also true.

Then what is wrong?

 
Alexander_K2:

Artem, I have read the help - everything seems to be correct.

I am checking the closing time.

I have read it in MQL4 Reference:

It is recommended to call the OrderSelect() function immediately before calling for the latest order data.

This is also true.

Then what's wrong?

What kind of function is this?

total_orders_NZDUSD=TotalOrders("NZDUSD.I");
And after it has been worked, which order is allocated?
 
Alexander_K2:

Gentlemen programmers!

Please help me solve a problem I've been struggling with for a month.

My TS sometimes doesn't close orders for some reason.

It looks like this:

The first three orders - 44329646, 44330563 and 44337351 open and close fine.

And on the fourth order, the tragedy happens:

and so on to infinity...

That is, an Expert Advisor opens order 44339156 and tries to close order 44337351 that it itself (!!!!) successfully closed several hours earlier.

You have to close it manually; if you fail to do that, you will sometimes suffer from the deepest drawdown. What a disaster!

How is it possible?

The code of the Expert Advisor is as simple as can be:

How can one and the same program work "time after time"? I don't understand...

what direction are you looking at the order array ?

If there are possible closures/deletions during "revision", it's better to look in the opposite direction. From OrdersTotal()-1 to 0 inclusive.

Otherwise, you may step on it :-)


 
Artyom Trishkin:

What is this feature?

This function was kindly given to me by Goldtrader, for which I am sincerely grateful.

//+------------------------------------------------------------------+
//| Calculate Market Orders function                                 |
//+------------------------------------------------------------------+
int TotalOrders(string sy) {    // sy - Currency Pair
int orders=0;
   for (int i=0; i<OrdersTotal(); i++) {
     if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
        if (OrderSymbol()==sy) {
           if (OrderType()==OP_SELL || OrderType()==OP_BUY) {
             orders++;
           }
         }
       }
     }
return(orders);
}

It allows you to open orders simultaneously for different currency pairs.

:))) In my opinion, it is what started it all, because I enabled it about a month ago... Without it I cannot see the Grail.

Something wrong with it?

 

There's something really wrong, though...

I mean, I doOrderSelect once and then again from the function... It doesn't make sense...