I need help with my code, how can I close my positions both in the average of 200 and in the average of 50?

 
//+------------------------------------------------------------------+
//| Close Opened Orders                                              |
//+------------------------------------------------------------------+
void OrdersClose()
{  
   for(int i = 0; i < OrdersTotal(); i++) 
      {
       bool OrSel = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);    
       if(OrderSymbol() == Symbol() && (MagicNumber == 0 || OrderMagicNumber() == MagicNumber))
       
         {
          if(OrderType() == OP_BUY && OrderComment() ==EAComment  && MA2() <= iClose(NULL,0,0) && iTime(NULL,0,0) > iTime(NULL,0,iBarShift(NULL,0,OrderOpenTime(),1)) );
        if(OrderType() == OP_BUY && OrderComment() ==EAComment  && MAS() <= iClose(NULL,0,0) && iTime(NULL,0,0) > iTime(NULL,0,iBarShift(NULL,0,OrderOpenTime(),1)) );
            {
             bool close = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, clrBlue); 
             int err = GetLastError(); if(err!=ERR_NO_ERROR){ Print("Error on Order closing = ", ErrorDescription(err));}
            }               
          if(OrderType() == OP_SELL && OrderComment()==EAComment  && MA2() >= iClose(NULL,0,0) && iTime(NULL,0,0) > iTime(NULL,0,iBarShift(NULL,0,OrderOpenTime(),1)))  ;
       if(OrderType() == OP_SELL && OrderComment()==EAComment  && MA2() >= iClose(NULL,0,0) && iTime(NULL,0,0) > iTime(NULL,0,iBarShift(NULL,0,OrderOpenTime(),1)));
            {
             bool close = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, clrRed); 
             int err = GetLastError(); if(err!=ERR_NO_ERROR){ Print("Error on Order closing = ", ErrorDescription(err));}
            } 
            
Documentation on MQL5: Standard Library / Indicators / Timeseries classes / CiClose
Documentation on MQL5: Standard Library / Indicators / Timeseries classes / CiClose
  • www.mql5.com
CiClose - Timeseries classes - Indicators - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.

Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing and order count:

    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in an index loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum

    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions (from zero).
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11

    3. and check OrderSelect in case later positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

  3. Not a good idea to use comments, brokers can change comments, including complete replacement.

    You already filtered by Magic Number.

  4. both in the average of 200 and in the average of 50?

    Meaning less words. Explain.

 
I wanted to say that, if there is the possibility of closing in more than one point, I want the positions to close at the average of 200 and also at the average of 50, it's possible ?
 
Mars 108 #: close at the average of 200 and also at the average of 50, it's possible ?

Of course, it's possible. Read the candle open and current close/Bid, does the candle cross either MA? Then close the position.

 
William Roeder #:

Of course, it's possible. Read the candle open and current close/Bid, does the candle cross either MA? Then close the position.

//+------------------------------------------------------------------+
//| Close Opened Orders                                              |
//+------------------------------------------------------------------+
void OrdersClose()
{  
   for(int i = 0; i < OrdersTotal(); i++) 
      {
       bool OrSel = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);    
       if(OrderSymbol() == Symbol() && (MagicNumber == 0 || OrderMagicNumber() == MagicNumber))
       
         {
          if(OrderType() == OP_BUY && OrderComment() ==EAComment  && MA2() <= iClose(NULL,0,0) && iTime(NULL,0,0) > iTime(NULL,0,iBarShift(NULL,0,OrderOpenTime(),1)) );
       
            {
             bool close = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, clrBlue); 
             int err = GetLastError(); if(err!=ERR_NO_ERROR){ Print("Error on Order closing = ", ErrorDescription(err));}
            }               
          if(OrderType() == OP_SELL && OrderComment()==EAComment  && MA2() >= iClose(NULL,0,0) && iTime(NULL,0,0) > iTime(NULL,0,iBarShift(NULL,0,OrderOpenTime(),1)))  ;
    
            {
             bool close = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, clrRed); 
             int err = GetLastError(); if(err!=ERR_NO_ERROR){ Print("Error on Order closing = ", ErrorDescription(err));}
            } 

//I'm sorry but I don't know much about programming, I just wanted to add another closing mean for this code, the "MA2" is the moving average of 50 and the "MAS" is the moving average of 200, how could I add that to the code? 


how can i add the "MAS" in this code? //

 
Mars 108: //I'm sorry but I don't know much about programming, I just wanted to add another closing mean for this code, the "MA2" is the moving average of 50 and the "MAS" is the moving average of 200, how could I add that to the code?

how can i add the "MAS" in this code? //

Show us your attempt (using the CODE button) and state the nature of your difficulty.
          No free help (2017)

Or pay someone. Top of every page is the link Freelance.
          Hiring to write script - General - MQL5 programming forum (2018)

We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
          No free help (2017)