Which design is correct? - page 4

 
Martingeil:
Make it like this.

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

It's this line that's confusing. I don't think it's needed here at all. Or am I wrong?

 
valenok2003:

It's this line that's confusing. I don't think it's needed here at all. Or am I wrong?

It's to cut off the magic symbol

When searching it first searches for orders simply by the symbol without a magik, if there are no such orders the function stops working........., if it finds orders with such a symbol, then it searches among them to find a magik, and closes those that match the magik, if the trading flow is free.

 
valenok2003:

It's this line that's confusing. I don't think it's needed here at all. Or am I wrong?

If we are talking about deleting all orders, then this line is unnecessary, as for normalization, what makes you think that the built-in scripts are such that they should be taken as an example?
 
Martingeil:

Needed to cut off that particular character's majic

When searching it first searches for orders simply by the symbol without a majic, if there are no such, stops the function, if there are orders with such a symbol, goes through them, and finds those that match the majic, and closes them if the trade flow is free.

And how do we know in advance MagicNumber, because we get it using OrderMagicNumber(). And besides the line
OrderSymbol() == Symbol()

is duplicated.

Also this line

Print ("close ALL orders Type : order :  Bid  "+OrderType()+" :  "+OrderOpenPrice()+"  :  "+Bid);

If there is no such a majik, the function stops working and searches them if there is such a majik.

 
Techno:
If we are talking about deleting all orders, then this line is unnecessary, as for normalisation, what makes you think that the built-in scripts are such that they should be taken as an example?


well, marx, after all.
 
Martingeil:

Needed to cut off the magik of this particular symbol

when searching for an order it first searches for an order without a magic number; if there are no such orders the function will stop working......... if it finds an order with such a number then it goes through all of them to find a magic number and closes all of them which match a magic number if the trade flow is free.

I see, I am just talking about closing all orders, that's why I did not understand it at first.

I took the liberty of rearranging your example like this:

  for (int trade = OrdersTotal() - 1; trade >= 0; trade--) 
  {
    OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
    if (OrderSymbol() == Symbol()) 
    { 
      if (OrderMagicNumber() == MagicNumber)
      {
        while (!IsTradeAllowed()) Sleep(1000);
        if(OrderType() == OP_BUY ) OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid,Digits), 5, CLR_NONE);
        if(OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask,Digits), 5, CLR_NONE);
      }
    }
  }

but to close all the orders

  for (int trade = OrdersTotal() - 1; trade >= 0; trade--) 
  {
    OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
    if (OrderSymbol() == Symbol()) 
    { 
      while (!IsTradeAllowed()) Sleep(1000);
      if(OrderType() == OP_BUY ) OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid,Digits), 5, CLR_NONE);
      if(OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask,Digits), 5, CLR_NONE);
    }
  }
 
valenok2003:


Well, Marx, after all.
even Engels ) no need for personality cults, write based on your knowledge)
 
valenok2003:
If you mean this

then there is an extra variable here for code transparency.

Or maybe you're talking about something else?

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;
      }        
    }
  }  
}

In this case it is this.

The "common standard" for C/C++ is :

void Close_All()
{
  int Total = OrdersTotal();
  for (int i=Total-1; i >=0; i--)                                                        
  {                                                                                          
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
    {
      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;
      }        
    }
  }  
}

The difference in the number of operations. If it does not affect much in this case, the difference in styles for the tester, for instance, will have a noticeable effect on the optimization time. For the real world, it affects the speed, which is sometimes critical.

Especially braking is such a design (I haven't seen it on your site, but for some reason most people prefer it ? ) :

for (int i=0; i<OrdersTotal(); i++)

Here a function is called on every pass of the loop. It is the most "expensive" operation in terms of computing costs.

Good luck.

I see you've fixed the codes in that sense.

 

The result is this script

//+------------------------------------------------------------------+
//|                                           CloseThisSymbolAll.mq4 |
//+------------------------------------------------------------------+
int start()
{
//----
  for (int trade = OrdersTotal()-1; trade >= 0; trade--) 
  {
    OrderSelect(trade, SELECT_BY_POS, MODE_TRADES);
    if (OrderSymbol() == Symbol()) 
    { 
      while (!IsTradeAllowed()) Sleep(1000);
      if(OrderType() == OP_BUY ) OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid,Digits), 5, CLR_NONE);
      if(OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask,Digits), 5, CLR_NONE);
    }
  }
//----
   return(0);
}
//+------------------------------------------------------------------+
Question - Why doesn't it always close all orders? For example, I open three Sell orders in a row, then try to close them with the script, it may close one or two or all of them. What is the reason?
 

Man, that's a hell of a way to pick the wrong one out of all the options. You're not supposed to be coding, kid.

It's not closing because of the re-quotes.