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

 
novichok2018:

Hello! Help me resolve the situation: I open a position 1 under the condition of Y1, closed under the condition of Y2. But if before the occurrence of U2 once again occurs U1 and opened position 2, it does not react to come U2, and hangs to the next U2, which came after the new U1 (!) And this new position is not closed by a new U2, and waits for the next condition U1-U2. How to avoid that? My head is killing me. Thank you.

We need a loop for closing OrderClose(...) of all orders on condition U2 - even if we have a hundred of them. We can delete one on every tick - then do the following: 1) by U2 condition, raise the flag. 2) If the flag is raised, delete ONE order. 3) If there are no orders, lower the flag. Then all orders one after another will be deleted....

 
STARIJ:

We need a loop for closing OrderClose(...) of all orders by condition U2 - you can open a hundred of them at once


I'm on a cycle as it is. Is there something wrong with it?

void ClosePoz()
  { int total=OrdersTotal();
  if(total>1)
   {
      for(int i=0;i<OrdersTotal();i++)
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) 
      { Print("позиция для закрытия не найдена");
         break;
      }
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         {double WPR0 = iWPR(NULL,PERIOD_M5,bars,0);
         if(OrderType()==OP_BUY && MathAbs(WPR0)>80)
           OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),int(MarketInfo(OrderSymbol(),MODE_DIGITS))),slippage,clrNONE);                  
         }
      }
    } 
  }   
 
novichok2018:
I'm on a cycle as it is. Is there something wrong with it?
I don't understand - I used SRC to insert code and I can't see it.
You have to press Insert and then Add
 
STARIJ:
You have to press Paste and then Add

Above, though not in the right place, but it reads. Oh! Now it's in the right place. Oh, my goodness!

 
novichok2018:

I have inserted your code correctly.

First press the SRC button, then commit your code into the window, press the "Insert" button and submit your message.

 
tura210577:

Thank you for your reply!

You're welcome. Try using multiplatform features like this:

//+------------------------------------------------------------------+
//| Returns specified Open by shift                                  |
//+------------------------------------------------------------------+
double Open(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const int shift)
  {
   double array[];
   if(CopyOpen(symbol_name,timeframe,shift,1,array)==1) return array[0];
   return 0;
  }
//+------------------------------------------------------------------+
//| Returns specified High by shift                                  |
//+------------------------------------------------------------------+
double High(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const int shift)
  {
   double array[];
   if(CopyHigh(symbol_name,timeframe,shift,1,array)==1) return array[0];
   return 0;
  }
//+------------------------------------------------------------------+
//| Returns specified Low by shift                                   |
//+------------------------------------------------------------------+
double Low(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const int shift)
  {
   double array[];
   if(CopyLow(symbol_name,timeframe,shift,1,array)==1) return array[0];
   return 0;
  }
//+------------------------------------------------------------------+
//| Returns specified Close by shift                                 |
//+------------------------------------------------------------------+
double Close(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const int shift)
  {
   double array[];
   if(CopyClose(symbol_name,timeframe,shift,1,array)==1) return array[0];
   return 0;
  }
//+------------------------------------------------------------------+
//| Returns specified Time by shift                                  |
//+------------------------------------------------------------------+
datetime Time(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const int shift)
  {
   datetime array[];
   if(CopyTime(symbol_name,timeframe,shift,1,array)==1) return array[0];
   return 0;
  }
//+------------------------------------------------------------------+
 
novichok2018: I'm on a cycle as it is. Is there something wrong with it?

There are 2 errors in the code. 1) This page explains the order of orders in the loop. 2) break - exit from the loop and continue - skip this

 
Artyom Trishkin:

I have inserted your code correctly.

First press the SRC button, then commit your code to the window, press the "Insert" button and submit your message.


Thank you. (chuckles) At least you made me laugh at my writing. What's the point of the question? Is everything correct in the loop?

 
novichok2018:

Thank you. (chuckles) At least you made me laugh at my writing. What's the point? Is the cycle right?

You have already been answered:

Forum on trading, automated trading systems and testing of trading strategies

Any questions from beginners on MQL4, help and discussion on algorithms and codes

STARIJ, 2018.01.18 20:19

There are 2 errors in the code. 1) This page explains the order of orders in the loop. 2) break - exit from the loop, and you should continue - skip this


 
Artyom Trishkin:

You have already been answered:



Yes, thank you, I read the page. Only my knowledge in MKL4 was enough only to notice that in my loop positions are looped from zero to the last, while in the example vice versa, from last to zero.I still don't know where to insertthe break. And when I changed my loop to reverse, positions stop closing at all.

Reason: