Which design is correct? - page 3

 
Techno:
PapaYozh, both these options are only for closing, but what about modification? Better to have2 ticks for full processing than 2 different loops for closing and modification?


If something has to be closed and something has to be modified, then the construction for (i=0; i<OrderTotals(); i++) is all the more incorrect.

We have to move from OrdersTotal()-1 to 0.

for ( i=OrderTotals()-1; i>=0; i-- )
{ 




 

i.e. is this the correct design?

void Close_All()
{
  int Total = OrdersTotal();
  for (int i=Total; i >=1; i--)                                                        
  {                                                                                          
    if(OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES)==true)
    {
      switch(OrderType())
      {
        case OP_BUY : OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),5); break;
        case OP_SELL: OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),5); break;
        default     : break;
      }        
    }
  }  
}
 

However, I still don't understand the normalisation.

It is one thing if I calculate the price, then it is clear that it must be brought to the digits of the terminal.

But in this case, we are closing at the current price obtained from the flow and, of course, it cannot be longer than Digits.

 
valenok2003:

But here it is also closing at the current price derived from the flow, which of course cannot be longer than Digits.

This is for a tester with non-normalised quotes.
 
valenok2003:

However, I still don't understand the normalisation.

It is one thing if I calculate the price, then it is clear that it must be brought to the digits of the terminal.

But in this case, we are closing at the current price obtained from the flow and, of course, it cannot be longer than Digits.

Sometimes with Digits=4 Bid may be equal to 1.32343545654. Not often, but it happens.
 
Techno:
Sometimes when Digits=4 Bid can be 1.32343545654 this happens. Not often, but it happens.


Live and learn. Thank you.
 
valenok2003:

i.e. would this design be correct?


Not really: there is no check for the "symbol" of the instrument. If the orders will be open for more than one instrument, then you risk closing the orders of one instrument at the prices of the other. If you need to close the order of the symbol, on whose chart the Expert Advisor has been moved, then you have to use the "symbol" of the chart. If you need to close all orders, no matter what chart the EA is moved to, you have to read the "symbol" of the order and request to use the Ask and Bids of the appropriate "symbol". And there are unnecessary actions in the code.

Good luck.

 
valenok2003:

i.e. is this the correct design?

void CloseThisSymbolAll() {
   for (int trade = OrdersTotal() - 1; trade >= 0; trade--) {
      OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()) {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         while (!IsTradeAllowed()) Sleep(1000);
            if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid,Digits), slip, Blue);
            if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask,Digits), slip, Red);
            Print ("close ALL orders Type : order :  Bid  "+OrderType()+" :  "+OrderOpenPrice()+"  :  "+Bid);            
         }
      }
   }
}
Do it like this.
 
VladislavVG:


Yes and there are extra actions in the code.

If you mean this
int Total = OrdersTotal();

then there is an extra variable here for code transparency.

Or maybe you are talking about something else?

 
Techno:
Sometimes at Digits=4 Bid may be equal to 1.32343545654. Not often, but it happens.

However, in the example of the close .mq4 script (in the standard MT4 delivery), there is no normalization.

   if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
     {
      cmd=OrderType();
      //---- first order is buy or sell
      if(cmd==OP_BUY || cmd==OP_SELL)
        {
         while(true)
           {
            if(cmd==OP_BUY) price=Bid;
            else            price=Ask;
            result=OrderClose(OrderTicket(),OrderLots(),price,3,CLR_NONE);
            if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
            else error=0;
            if(error==135) RefreshRates();
            else break;
           }
        }
     }
Reason: