Cannor see why this is not working

 

I have written a small EA that must close some manual transactions when the NLMA indicator changes.

But it does not want to close the transactions when it should and I cannot find out why! Is it perhaps my empty deinit?

Can you possibly help me solve what is wrong that I am totally blind for? I will appreciate it very highly!

Here is the simple code:

//External Variables

extern double     FilterShort = 0.0;
extern double     FilterLong = 0.0;
extern bool       ProfitShort = true;
extern int        Slippage = 3;
extern int        LengthLong = 50;
extern int        LengthShort = 40;
extern int        PriceModeLong = 0;      //0-Close;1-Open;2-High;3-Low;4-Median price;5-Typical price;6-Weighted Close
extern int        PriceModeShort = 0;      //0-Close;1-Open;2-High;3-Low;4-Median price;5-Typical price;6-Weighted Close
extern int        MagicNumber = 99999;

//Global Variables


bool        CloseBuy = false;
bool        CloseSell = false;
double      UpLong;
double      UpShort;
double      DownLong;
double      DownShort;
double      UsePoint;
int         OrderNo;
int         i;
int         ErrorCode;
int         UseSlippage;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
        {
        UsePoint = PipPoint(Symbol());
        UseSlippage = GetSlippage(Symbol(),Slippage);
        }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
   {
   
   }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
         
        UpLong = iCustom(NULL,0,"NonLagMA_v7.1",PriceModeLong,LengthLong,0,FilterLong,1,0,0,0,0,1,1);
        DownLong = iCustom(NULL,0,"NonLagMA_v7.1",PriceModeLong,LengthLong,0,FilterLong,1,0,0,0,0,2,1);
        UpShort = iCustom(NULL,0,"NonLagMA_v7.1",PriceModeShort,LengthShort,0,FilterShort,1,0,0,0,0,1,1);
        DownShort = iCustom(NULL,0,"NonLagMA_v7.1",PriceModeShort,LengthShort,0,FilterShort,1,0,0,0,0,2,1);
        
              
         if (UpLong == 2147483647) UpLong = 0;
       
         if (UpShort == 2147483647) UpShort = 0;
         
         if (DownLong == 2147483647) DownLong = 0;
       
         if (DownShort == 2147483647) DownShort = 0;
        
         
       
         if ((UpShort != 0 && DownShort != 0) || (UpLong != 0 && DownLong != 0))
         return(0);
         

      int Count = 0;
      CloseSell = false;
         
             for (Count = OrdersTotal()-1; Count >= 0; Count--)
                    if (OrderSelect(Count, SELECT_BY_POS)
                    && OrderType() == OP_SELL
                    && OrderMagicNumber() == MagicNumber
                    && OrderSymbol() == Symbol())
                    
                    {
                       OrderNo = OrderTicket();
                       if (ProfitShort == true && UpShort != 0) CloseSell = true;
                       if (ProfitShort == false && UpLong != 0) CloseSell = true;
                       if (CloseSell == true)
                        { while(IsTradeContextBusy()) Sleep(10);        
                          bool Closed = OrderClose(OrderNo,OrderLots(),Ask,UseSlippage,Red);
                                                    
                            // Error handling
                  if(Closed == false)
                     {
                        ErrorCode = GetLastError();
                        string ErrDesc = ErrorDescription(ErrorCode);

                        string ErrAlert = StringConcatenate("Close Sell Order - Error ",ErrorCode,": ",ErrDesc);
                        Alert(ErrAlert);

                        string ErrLog = StringConcatenate("OrderTicket: ",OrderTicket());
                        Print(ErrLog);
                        
                        CloseSell = false;
                     }
                    }
                }
                  
  Print("After CloseSell and Before CloseBuy");
  
  CloseBuy = false;
  Count = 0;
         
             for (Count = OrdersTotal()-1; Count >= 0; Count--)
                    if (OrderSelect(Count, SELECT_BY_POS)
                    && OrderType() == OP_BUY
                    && OrderMagicNumber() == MagicNumber
                    && OrderSymbol() == Symbol())
                    
                    {
                       OrderNo = OrderTicket();
                       if (ProfitShort == true && DownShort != 0) CloseBuy = true;
                       if (ProfitShort == false && DownLong != 0) CloseBuy = true;
                       if (CloseBuy == true)
                        { while(IsTradeContextBusy()) Sleep(10);        
                          Closed = OrderClose(OrderNo,OrderLots(),Bid,UseSlippage,Blue);
                                                    
                            // Error handling
                  if(Closed == false)
                     {
                        ErrorCode = GetLastError();
                        ErrDesc = ErrorDescription(ErrorCode);

                        ErrAlert = StringConcatenate("Close Buy Order - Error ",ErrorCode,": ",ErrDesc);
                        Alert(ErrAlert);

                        ErrLog = StringConcatenate("OrderTicket: ",OrderTicket());
                        Print(ErrLog);
                        
                        CloseBuy = false;
                     }
                    }
                 
           }     
 
                            
   return(0);
  }
 
ernest02:

I have written a small EA that must close some manual transactions when the NLMA indicator changes.

But it does not want to close the transactions when it should and I cannot find out why! Is it perhaps my empty deinit?

Can you possibly help me solve what is wrong that I am totally blind for? I will appreciate it very highly!

Here is the simple code:

Print() the variables you use to save the values from the iCustom() calls and verify that they are correct.

Also read this thread, especially the later posts, it is very relevant: https://www.mql5.com/en/forum/146570
 
Is 99999 the Magic number of manual transactions? I though it couldn't be set (I assumed 0) https://www.mql5.com/en/articles/1359 If so, that's your problem I guess?
 

Ydrol,

I am embarrassed! You hit the nail on the head! The MagicNumber 99999 is based on another EA that I wrote to generate the transactions. BUT I am now only using this EA to generate Alerts and then I decide to trade it manually or not.

So this code does not work, because the code cannot find the MagicNumber 99999!!!

So I will have to identify these transactions in another way, e.g. by order number.

Thank you so much. You have helped me a lot!

Ernest.