orders not closing

 

hi this code is not closing orders, could you help me to figure out why?

void OnTick()
  {
string signal="";
int PeriodMA = iMA(_Symbol, PERIOD_H1, 9, 0, MODE_SMA, PRICE_CLOSE,0);
int SlowMA = iMA(_Symbol, PERIOD_H1, 12, 0, MODE_EMA, PRICE_CLOSE, 0);
int FastMA = iMA(_Symbol, PERIOD_H1, 26, 0, MODE_EMA, PRICE_CLOSE,0);
int SignalMA = SlowMA - FastMA;

   if ( SignalMA > PeriodMA)
   
      { 
         signal = "buy";
        
         }
         
   if ( SignalMA < PeriodMA)
   
      {
         signal = "sell";
         
            }
            
           {
   
if (signal=="buy" && OrdersTotal()==0)
  int ticket = OrderSend(_Symbol, OP_BUY, 0.01, Ask, 2, 0, 0, NULL, 1, 0, Green);  
 
 }
 
  {
   
if (signal=="sell" && OrdersTotal()==0)
   ticket = OrderSend(_Symbol, OP_SELL, 0.01, Bid, 2, 0, 0, NULL, 1, 0, Red);  
             
             {
      for(int i=OrdersTotal();i>=0;i--)
      
     
         if(OrderSelect(i,SELECT_BY_POS)==true)
            if( SignalMA < PeriodMA && OrderType()==OP_BUY && OrdersTotal()==1)
         {
           bool Ans = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID),2, Red);
          }
      }
  
   {
      for( i=OrdersTotal();i>=0;i--)
      
     
         if(OrderSelect(i,SELECT_BY_POS)==true)
            if( SignalMA > PeriodMA && OrderType()==OP_SELL && OrdersTotal()==1)
         {
            Ans = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK),2, Red);
          }
      } 
 
  }
 }
 
Merlinou:

hi this code is not closing orders, could you help me to figure out why?

if it also only sells its because you never get a buy signal to close the sells .

I think SignalMA is almost always smaller than PeriodMA

 
      for(int i=OrdersTotal();i>=0;i--)
      
     
         if(OrderSelect(i,SELECT_BY_POS)==true)
            if( SignalMA < PeriodMA && OrderType()==OP_BUY && OrdersTotal()==1)
  1. If there are n orders, their positions are [0 … n-1]. Your first select always fails.

  2. 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)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

    You need one Magic Number for each symbol/timeframe/strategy.
         Trade current timeframe, one strategy, and filter by symbol requires one MN.
         If trading multiple timeframes, and filter by symbol requires use a range of MN (base plus timeframe).
              Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 (2020)

  3. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool).

  4. if (signal=="buy" 

    You are looking at a signal. Act on a change of signal.
              Too many orders - MQL4 programming forum #1 (2017)

  5. Merlinou: this code is not closing orders, could you help me to figure out why?

    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)

Reason: