Questions from Beginners MQL4 MT4 MetaTrader 4 - page 116

 
novichok2018:

Thank you. The break seems to have helped. Let's see what happens next.

No, it hasn't really helped. It only works if one type of position is opened. If, for example, a Sell position is opened and is not yet closed, and simultaneously a Buy position is opened and closed, the Sell position cannot see conditions for its own closing until another Sell position opens and closes (according to its own conditions). And if BAY opens and closes again, it again misses its closing conditions. I tried to determine the presence ofopen positions after closing BAY using I.Kim's function ExistPositions(), but I don't understand where to insert it and where to place Print to control the execution. Any clue.

 
novichok2018:

No, it hasn't really helped. It only works if the same type of position is opened. If, for example, first a Sell position is opened and not yet closed, and at the same time a Buy position is opened and closed, then the Sell position does not see its closing conditions until another Sell position opens and closes (according to its conditions). And if BAY opens and closes again, it again misses its closing conditions. I tried to determine the presence ofopen positions after closing BAY using I.Kim's function ExistPositions(), but I don't understand where to insert it and where to place Print to control the execution. Can you give me a hint?

Doesn't anyone know the solution? I don't.

 
novichok2018: Doesn't anyone know the solution? I don't.

I logged onto the forum - I see you are talking to yourself... Where is the beginning of your conversation and what it's about - hard to understand. To debug a program, it's much more convenient to use Alert instead of Print - it shows up on the screen at once. These Alert's are placed before the if condition is checked. Another method of debugging with a pencil - go through the program as it should be executed, and write down the values of the variables. When it changes - cross out the old value and write the new one. This is how it was done before. Now MetaEditor has a debugger, where you can trace the values of variables. The simplest constructions should be checked separately by writing a script

 
novichok2018:

Doesn't anyone know the solution? I don't.

Try it this way:

//+------------------------------------------------------------------+
void OnTick()
  {
   int total=OrdersTotal();
   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(TimeCurrent()>OrderOpenTime()+100)
           {
            switch(OrderType())
              {
               //_______________________________________________________________________
               case OP_BUY:
                 {
                  if(OrderMagicNumber()==101 && MathAbs(WPR1)<5)
                    {
                     ClosePosBySelect(slippage,Yellow);
                     break;
                    }
                  if(OrderMagicNumber()==111 && MathAbs(WPR1)<20)
                    {
                     ClosePosBySelect(slippage,Yellow);
                     break;
                    }
                  break;
                 }
               //_______________________________________________________________________                 
               case OP_SELL:
                 {
                  if(OrderMagicNumber()==222 && MathAbs(WPR1)>97.55)
                    {
                     ClosePosBySelect(slippage,Yellow);
                     break;
                    }
                  if(OrderMagicNumber()==201 && MathAbs(WPR1)>96)
                    {
                     ClosePosBySelect(slippage,Yellow);
                     break;
                    }
                  break;
                 }
              }
           }
        }
     }
  }
//_______________________________________________________________________
void ClosePosBySelect(int slip,color col) 
  {
   RefreshRates();
   if(OrderType()==OP_BUY)  OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), slip, col);
   if(OrderType()==OP_SELL) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), slip, col);
  }
//_______________________________________________________________________
 
where does iCustom get its data from?
 
Igor Makanu:

try it this way:

Thanks, tried it - it doesn't close any positions even after some adjustment so the compiler doesn't swear.

 
STARIJ:

I logged onto the forum - I see you talking to yourself... It's hard to understand where you started and what you're talking about. For program debugging it's more convenient to use Alert instead of Print - it shows up on the screen at once. These Alert's are placed before the if condition is checked. Another method of debugging with a pencil - go through the program as it should be executed, and write down the values of the variables. When it changes - cross out the old value and write the new one. This is how it was done before. Now MetaEditor has a debugger, where you can monitor the values of variables. The simplest constructs have to be checked separately by writing a script

Thanks for the recommendations, but for my situation they are not quite correct. Just to clarify, below is the code of a custom closing function

 

It does not work correctly -closing of positionshappens under conditions of the first open position. That is, if first a Buy or Sell position was opened with Condition 1 and then a second position was opened with Condition 2, the closing condition is ignored and the second position is also closed with Condition 1 despite a different magic number.

I applied break after the order has been closed but the solution has been half-hearted:only if one position of the same type is opened. If, for instance, a BOY position has been opened first and has not yet been closed yet, while a BAY position is being opened and closed, the BAY position does not see any conditions to be closed as long as another BAY position opens and closes (in accordance with its conditions). And if BAY opens and closes again, it again misses its closing conditions. So what to do?

void ClosePoz()
  { RefreshRates();
         double WPR0 = iWPR(NULL,0,bars,0);
         double WPR1 = iWPR(NULL,0,bars,1);
         bool rez = false;
           
  int total=OrdersTotal();
      for(int i=OrdersTotal()-1;i>=0;i--)
      {
        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         {
              if(TimeCurrent()>OrderOpenTime()+300)
               {
               if(OrderMagicNumber==101 || OrderMagicNumber==102 || OrderMagicNumber==103 || OrderMagicNumber==104)
                 {
                 if(MathAbs(WPR1)<5)
                 rez =  OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),int(MarketInfo(OrderSymbol(),MODE_DIGITS))),slippage,Yellow);
                 break; 
                 }
               if(OrderMagicNumber==111)
                 {
                  if(MathAbs(WPR1)<17) 
                  rez =  OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),int(MarketInfo(OrderSymbol(),MODE_DIGITS))),slippage,Yellow);
                  break;
                 }
               if(OrderMagicNumber==222)
                 {
                  if(MathAbs(WPR1)>96)
                  rez =  OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),int(MarketInfo(OrderSymbol(),MODE_DIGITS))),slippage,Yellow);
                  break; 
                 }
               if(OrderMagicNumber==201)
                 {
                  if(MathAbs(WPR1)>97)
                  rez =  OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),int(MarketInfo(OrderSymbol(),MODE_DIGITS))),slippage,Yellow);
                  break;
                 }
               if(OrderMagicNumber==202)
                 {
                  if(MathAbs(WPR1)>96)
                  rez =  OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),int(MarketInfo(OrderSymbol(),MODE_DIGITS))),slippage,Yellow);
                  break;
                 }
                
                } 
              }
       break;  
      }
  }
 
Mickey Moose:
where does iCustom get its data from?

from the buffers of the custom indicator, practice, for example on the MA

novichok2018:

I applied break after closing an order but the solution is half-way:only if one position of the same type is opened. If, for instance, a BOY position is opened and has not yet been closed, while a BAY position is being opened and closed, the BAY position does not see any conditions to be closed until another BAY position is opened and closed (at its own conditions). And if BAY opens and closes again, it again misses its closing conditions. So what to do?

try replacing break with continue

it's strange that my code example doesn't close anything, maybe it's about price normalization

 
Igor Makanu:


try replacing break with continue


Replaced. Cursed error 138(requotes). Then I gave break back, and it's 138 again. Strange, maybe I didn't notice it before.

 
Igor Makanu:

from the buffers of the custom indicator, practise with the MA, for example


Is this from the field that appears in the terminal data window?
Reason: