Which design is correct? - page 5

 
valenok2003:

The result is this script

Question - Why doesn't it always close all orders? For example, I open three Sell orders one after another and try to close them, but the script may close one or two or all of them. What is the reason?

Do you need a script?

Then you don't need a while statement , scripts work in loops as it is.

For the script you need this construction...

I don't remember the link, here is the script which closes all open orders.

//+------------------------------------------------------------------+
//|                                               CloseAllOrders.mq4 |
//|                       Copyright © 2008, PRMQuotes Software Corp. |
//|                                           Jedimedic77@gmail.com  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, PRMQuotes Software Corp."
#property link      ""
//+------------------------------------------------------------------+
//| EX4 imports                                                      |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| global variables to program:                                     |
//+------------------------------------------------------------------+
double Price[2];
int    giSlippage;
bool   CloseOpenOrders = true;
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
void start() {
  int iOrders=OrdersTotal()-1, i;
  
  if(CloseOpenOrders) {
    for(i=iOrders; i>=0; i--) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderType()<=OP_SELL) && GetMarketInfo() && 
      !OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage)) Print(OrderError());
    }
  }
}
//+------------------------------------------------------------------+
//| Function..: OrderError                                           |
//+------------------------------------------------------------------+
string OrderError() {
  int iError=GetLastError();
  return(StringConcatenate("Order:",OrderTicket()," GetLastError()=",iError," ",ErrorDescription(iError)));
}
//+------------------------------------------------------------------+
//| Function..: GetMarketInfo                                        |
//+------------------------------------------------------------------+
bool GetMarketInfo() {
  RefreshRates();
  Price[0]=MarketInfo(OrderSymbol(),MODE_ASK);
  Price[1]=MarketInfo(OrderSymbol(),MODE_BID);
  double dPoint=MarketInfo(OrderSymbol(),MODE_POINT);
  if(dPoint==0) return(false);
  giSlippage=(Price[0]-Price[1])/dPoint;
  return(Price[0]>0.0 && Price[1]>0.0);
}
//+------------------------------------------------------------------+
 
valenok2003:

The result is this script

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?
And the quotes are how many digits?
 
khorosh:
And the quotes are how many digits?
five
 
Martingeil:

Do you need a script?

Thank you, of course. But it's easy to get an off-the-shelf script. I want to find out what the reason is.
 
Martingeil:

Do you need a script?

then you don't need the while statement , scripts work in loops as it is.


The script only works once. There is no self-looping there and never has been.

Valenok, do you need the script to close all orders at all, or only this symbol?

 
Martingeil:

Do you need a script?

Then you don't need the while statement , scripts work in loops as it is.

For a script you need this construction...

I don't remember the link, here is a script that closes all open orders.

In the while code, only to delay while the thread is busy.
 
Techno:

the script only works once. No self-triggering there and never has been.

Valenok, do you want the script to close all orders at all, or only this symbol?

Yes, you're right wrong, I meant it does not depend on the ticks...
 
Techno:

the script only works once. There is no self-triggering there and never has been.

Valenok, you need the script to close all of the orders, or only this symbol?



Only open orders of one symbol. The pending ones are not affected.

The funny thing is that code criticized in kodobase

int start()
{
//----
  while(OrdersTotal()>0)
  {
    for (int i=0; i < OrdersTotal(); i++)                                                        
    {                                                                                          
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
      {
        OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5);  
      }
    }
  }                                               
//----
   return(0);
}

works without any problems, but when i started to improve it, i started having problems.

zy:

On the subject of mistaken string

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

I already figured it out.

 
valenok2003:



Only open orders of one symbol. We do not touch the pending ones.

So, let it go straight to the postponement and let it be. In general form.

int start()
{
   for(int i=OrdersTotal()-1;i>=0;i--)
    {
    if(OrderSelect(i,SELECT_BY_POS)&&OrderSymbol()==Symbol())
      switch(OrderType())
         {
         case 0  : OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),5); break;
         case 1  : OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),5); break;
         default : OrderDelete(OrderTicket()); break;
         }
    }
        
}
 
Martingeil:
You're right, I didn't mean it like that, regardless of the tics...


I'm aware of that.