EA will not run on multiple pairs, need help

 
I am very new to coding mql4, and have completed my first EA. My only problem now is that I cannot get it to open trades on more than one pair. In the EA section on the terminal next to journal, it send a stream of the error, invalid ticket for orderclose function, even though the EA opens and closes trades just fine on one pair. Could anyone please lend a hand. 
 
Matt_Townsend:
I am very new to coding mql4, and have completed my first EA. My only problem now is that I cannot get it to open trades on more than one pair. In the EA section on the terminal next to journal, it send a stream of the error, invalid ticket for orderclose function, even though the EA opens and closes trades just fine on one pair. Could anyone please lend a hand. 

How to you expect us to know if you don't show your code nor your log of the errors?

We are not mind readers!  If you want our help, you will have to show your code as well as the errors it gives (the results in the log). It will also be helpful to know what currency pairs and time-frames you are testing on, when we have a look at your code.

Also, please note, that on MetaTrader 4 you can only test a single currency at a time in the Strategy Tester. If your EA trades multiple currencies simultaneously from a single chart, you will not be able to test it in the Strategy Tester - only on a live Demo or real account!

 
Matt_Townsend: even though the EA opens and closes trades just fine on one pair.
This is why I recommend
Do not trade multiple currencies in one EA
 
Matt_Townsend: Here is my code, and I'm having the problem on a live demo, I have even tried to see if I could use multiple terminals, to resolve the issue. Thank you for your feedback thusfar.

Please edit you post and use the SRC button to add your code! Don't just copy/past as standard text as it is difficult to read. I am sure you can see from other threads how it should be done. Please use the SRC button to post your code.

EDIT: As requested before - ... It will also be helpful to know what currency pairs and time-frames you are testing on ...

EDIT2: As requested before - ... as well as the errors it gives (the results in the log) ...

 
Fernando Carreiro:
Please edit you post and use the SRC button to add your code! Don't just copy/past as standard text as it is difficult to read. I am sure you can see from other threads how it should be done. Please use the SRC button to post your code.
//+------------------------------------------------------------------+
//|                     Simple Moving Average Crossover Strategy.mq4 |
//|                                                 Matthew Townsend |
//+------------------------------------------------------------------+
#property version   "1.05"
#property strict
//---

//---Inputs
extern  double Lots         =0.01;
extern  int    FastMA       =5;
input   int    FastMAMode   =MODE_EMA;
extern  int    SlowMA       =8;
input   int    SlowMAMode   =MODE_SMA;

//+----------------------------------------+
//|undesirables                            |
//+----------------------------------------+
int init()
{
   //----
   //----
   return(0);
}

int deinit()
{
   //----
   //----
   return(0);
}
//+---------------------------------------------+
//|System start                                 |
//+---------------------------------------------+
int start()
{


    double fastma, slowma;
    int    ticket, total;
    //---define Ma's
    
    fastma = iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,0);
    slowma = iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,0);
    
    total = OrdersTotal();
    //---Start operations
    
    if(total < 1)
    {
       if(fastma > slowma)
       {
         ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,10,0,0,"",0,0,Blue);
         if(ticket > 0)
         {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
            Print("Buy order opened : ", OrderOpenPrice());
         }
         else
           Print("Error opening order : ", GetLastError());
           return(0);
       }
       if(fastma < slowma)
       {
         ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,10,0,0,"",0,0,Red);
         if(ticket > 0)
         {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
            Print("Sell order opened : ", OrderOpenPrice());
         }
         else
           Print("Error opening order : ", GetLastError());
           return(0);
       }
       return(0);
    }
    bool ans;
    
        
         if(OrderType()==OP_BUY)
         {
           while(fastma < slowma)
           {
             ans = OrderClose(OrderTicket(),Lots,Bid,10,clrNONE);
             if(ticket < 1)
             {      
                if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                Print("Buy order closed : ", OrderClosePrice());    
             }    
             else
               Print("Error closing order : ", GetLastError());
               return(0);
           }
         }
      if(OrderType()==OP_SELL)
        {
          while(fastma > slowma)
          {
             ans = OrderClose(OrderTicket(),Lots,Ask,10,clrNONE);
             if(ticket < 1)
             {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("Sell order closed : ", OrderClosePrice());
              
             }
             else
               Print("Error closing order : ", GetLastError());
               return(0);
          }
          
        }
    
    return(0);
}

//+-----------------------------------------------------------------------------------+
 
Fernando Carreiro:
I requested that you EDIT your previous post, not add a new one, so as not to make the thread so long!
Sorry I am very new to this forum as well, can I delete the 2 posts?
 
Matt_Townsend:
I was testing on 1 hr timeframe, EURUSD and 1hr GBPUSD
There is already two compiler warnings for the following code:
...
ans = OrderClose(OrderTicket(),Lots,Bid,10,clrNONE);
...
ans = OrderClose(OrderTicket(),Lots,Ask,10,clrNONE);
...

You are not checking the result for the close operation correctly.

In terms of logic there are several problems with your EA, but as it stands at the moment, it is running without any errors on my end in the Strategy Tester, so please supply the list of errors you are getting!

 
Fernando Carreiro:
There is already two compiler warnings for the following code:
...
ans = OrderClose(OrderTicket(),Lots,Bid,10,clrNONE);
...
ans = OrderClose(OrderTicket(),Lots,Ask,10,clrNONE);
...

You are not checking the result for the close operation correctly.

In terms of logic there is several problems with your EA, but as it stands at the moment, it is running without any errors on my end, so please supply the list of errors you are getting!

The only error I have been getting from the EA is invalid ticket for orderclose function, with a number next to it, I'd like to show the actual log, but on the second chart with the EA running it is not doing anything, as for normally it would attempt to open a trade immediately. If, and when the log starts filling with the error, how would I go about showing you the actual log?
 
Matt_Townsend:
The only error I have been getting from the EA is invalid ticket for orderclose function, with a number next to it, I'd like to show the actual log, but on the second chart with the EA running it is not doing anything, as for normally it would attempt to open a trade immediately. If, and when the log starts filling with the error, how would I go about showing you the actual log?

Select the lines in question with Click, Ctrl-Click or Shift-Click (standard windows operation), then Right-Click the selection and choose "Copy"!

EDIT: With regards to the code, you are using the OrderSelect, but you are always assuming that it works every time. And when it does not work you just continue to use the Order details functions like OrderTicket() in your close, which could fail if the initial OrderSelect() failed too. As I stated, you have many logic problems in your code!

 

    bool ans;

   

         //You do not select an order

         if(OrderType()==OP_BUY)
         {
           while(fastma < slowma)                                 //Use if not while
           {
             ans = OrderClose(OrderTicket(),Lots,Bid,10,clrNONE);
             if(ticket < 1)                                        //ticket is a local variable and has not been assigned a value
             {     
                if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                Print("Buy order closed : ", OrderClosePrice());   
             }   
             else
               Print("Error closing order : ", GetLastError());
               return(0);
           }
         }
Re-write the close order section of code to be logical, you should not be using the variable ticket here at all.
 
Matt_Townsend:
The only error I have been getting from the EA is invalid ticket for orderclose function, with a number next to it, I'd like to show the actual log, but on the second chart with the EA running it is not doing anything, as for normally it would attempt to open a trade immediately. If, and when the log starts filling with the error, how would I go about showing you the actual log?

Another very important note with regards to use of you EA on multiple currency pairs or time-frames.

Your code does not use the "Magic Number" selection nor does it check the Symbol (nor "Magic number") in use when checking for the open orders in History. So in essence your EA will not work correctly neither with itself nor other EA's.

As it is, you can only use this EA by itself with no other EA running, not even itself on another chart!