Activation of the code after position number 13

 
Hello to all professors
I created a close code that works properly
But I want it to start working after we had 13 buying positions, and also for sales positions, this close code will be activated in the same way after we had 13 selling positions.
I wrote this and it works well for sales positions. After we have 13 sales positions, the code is activated.
But it does not work properly in the case of buying positions and does not count the positions

And in the first position, the close code purchase is activated and starts working

In which part of the code of buying positions do I make a mistake?

please guide

input int step=12;  //steps to change distance

int start()
  {
       if(Volume[0]<=1)
       {
       if(Orders1()==113) 
       {
       closebuy3=false;
       closesell3=false;
       if(BuysTotal()>step)
       {
       closebuy3=true;
       }
       if(SellsTotal()>step)
       {
       closesell3=true;
       }
       if(Stoch1()=="buy")
       {
       Pendbuy();
       }
       if(Stoch1()=="sell")
       {
       Pendsell();
       }
       }
       }
       GetProfitBuy();
       GetProfitSell();
       closePostion(Magic1 && closebuy3);
       closePostion(Magic2 && closesell3);
       
       return(0);
  }

//+------------------------------------------------------------------+
int BuysTotal()
 {
      int num=0;
      for(int i=OrdersTotal()-1;i>=0;i--)
      {
      if(!OrderSelect(i,SELECT_BY_POS))continue; 
      if(OrderMagicNumber()==Magic1&&"EURUSD"==OrderSymbol())
      num++;
      }
      return(num);
 }
//+------------------------------------------------------------------+
int SellsTotal()
  {
       int num=0;
       for(int i=OrdersTotal()-1;i>=0;i--)
       {
       if(!OrderSelect(i,SELECT_BY_POS))continue;
       if(OrderMagicNumber()==Magic2&&"EURUSD"==OrderSymbol())
       num++;
       }
       return(num);
 }
//+------------------------------------------------------------------+


 
  1.        if(Volume[0]<=1)

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 (2014)

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011)

  2. Your code
    closebuy3=false;
    closesell3=false;
    if(BuysTotal()>step)
    {
        closebuy3=true;
    }
    if(SellsTotal()>step)
    {
       closesell3=true;
    }
    Simplified
    closebuy3 = BuysTotal()>step;
    closesell3=SellsTotal()>step;
    
  3. int start()

    You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

  4. GoldenDiamonds: I created a close code that works properly

    No idea what you mean by “close code”. If it works properly, what's the problem?

  5. GoldenDiamonds: In which part of the code of buying positions do I make a mistake?

    Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

  6.        closePostion(Magic1 && closebuy3);
           closePostion(Magic2 && closesell3);

    Always post all relevant code (using Code button) or attach the file. We can't know what closePostion uses.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    Any non-zero is true, so you are passing true && closexxx3 which equals closexxx3. Does closePostion really take an boolean? Do you actually mean:

    if(closebuy3)       closePostion(Magic1);
    if(closesell3)      closePostion(Magic2);
  7. No need for two magic numbers as you can get the direction from the order itself.

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020.02.21)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles 2011

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

 
William Roeder #:
  1. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 (2014)

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011)

  2. Your code
    Simplified
  3. You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

  4. No idea what you mean by “close code”. If it works properly, what's the problem?

  5. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

  6. Always post all relevant code (using Code button) or attach the file. We can't know what closePostion uses.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    Any non-zero is true, so you are passing true && closexxx3 which equals closexxx3. Does closePostion really take an boolean? Do you actually mean:

  7. No need for two magic numbers as you can get the direction from the order itself.

Hello dear master

The problem was that I had created a function close position

And I wanted this function to be activated for the same position whenever there are more than 13 buy or sell positions.

This function was activated in the sell positions properly and after opening the 13th sell position

But it did not work properly on buy positions and did not count buy positions

But this command solved the problem

if(closebuy3)       closePostion(Magic1);
if(closesell3)      closePostion(Magic2);

The links you put help a lot

Thankful.