EA will not run on multiple pairs, need help - page 2

 
Fernando Carreiro:

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!

Thank you very much for helping me with my code so far and your time, I will try to make the necessary adjustments. 
 
Keith Watford:

    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.
While writing this before I did not have the ticket variable there, I was just seeing if putting it there would have an impact on the way the code fuctioned, and thank you for your input and time.
 
Matt_Townsend:
Thank you very much for helping me with my code so far and your time, I will try to make the necessary adjustments. 

Because you have too many logic problems in your EA, I suggest that you have a look at the sample code by MetaQuotes, namely "Moving Average.mq4" and/or "MACD Sample.mq4" and built on that, until you understand the basic principles of how an EA works.

I have attached the files, but you will find them in your "MQL4\Experts" folder.

Files:
 
Fernando Carreiro:

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.

After noticing your edit.....

I had always assumed that an OrderSelect() would be lost when there is a new tick.

So I checked and found that once an order is selected it remains selected even in subsequent new  ticks.

Learn something new every day :)

 
Keith Watford: After noticing your edit.....

I had always assumed that an OrderSelect() would be lost when there is a new tick.

So I checked and found that once an order is selected it remains selected even in subsequent new  ticks.

Learn something new every day :)

In practice however, it is of little use as those details get frozen in time, so things like OrderClosePrice() will not update until you re-select it again!
 
Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect 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 forum
 
Fernando Carreiro:

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!


    for(int i=0;i<OrdersTotal();i++)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
         if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol()) continue;
         //---Check Order Type
         if(OrderType()==OP_BUY)
         {
           if(fastma < slowma)
           {
             if(OrderClose(OrderTicket(),Lots,Bid,10,clrNONE))
             Print("Order Closed : ", OrderClosePrice());
             return(0);
           }
           else
            Print("Error : ", GetLastError());
            return(0);
           break;
         }
      if(OrderType()==OP_SELL)
        {
          if(fastma > slowma)
          {
             if(OrderClose(OrderTicket(),Lots,Ask,10,clrNONE))
             Print("Order Closed : ", OrderClosePrice());
             return(0);
            
          }
          else
               Print("Error : ", GetLastError());
               return(0);
          break;
        }

      } 

Here is the OrderClose section, I am no longer getting the invalid ticket error, but i am now getting invalid price

Here it is directly from the log

 2016.12.09 10:47:10.243 Simple Moving Average Crossover Strategy EURUSDi,H1: invalid price 1.05454000 for OrderClose function


 
Matt_Townsend:

           {
             if(OrderClose(OrderTicket(),Lots,Bid,10,clrNONE))
             Print("Order Closed : ", OrderClosePrice());
             return(0);
           }

...

         {

             if(OrderClose(OrderTicket(),Lots,Ask,10,clrNONE))
             Print("Order Closed : ", OrderClosePrice());
             return(0);

 

Here is the OrderClose section, I am no longer getting the invalid ticket error, but i am now getting invalid price

Here it is directly from the log

 2016.12.09 10:47:10.243 Simple Moving Average Crossover Strategy EURUSDi,H1: invalid price 1.05454000 for OrderClose function


Don't use Bid/Ask when closing, use OrderClosePrice(). It will be automatically the good price.
 
Alain Verleyen:
Don't use Bid/Ask when closing, use OrderClosePrice(). It will be automatically the good price.

Should I just replace it in the OrderClose function? and if so should I remove the OrderClosePrice in the print function underneath it?

Edit: 

I just did that, but now it will not hold any positions open. Just opens and closes positions, although it is now not having any issue working on multiple pairs.  

 

         if(OrderType()==OP_BUY)
         {
           if(fastma < slowma)
           {
             if(OrderClose(OrderTicket(),Lots,Bid,10,clrNONE))
             Print("Order Closed : ", OrderClosePrice());
             return(0);
           }
           else
            Print("Error : ", GetLastError());
            return(0);
           break;
         }

There is no beed for the returns or the break as you need to complete the loop

Also, your error print is dependant on


           if(fastma < slowma)


being false, not related to whether the OrderClose() fails



   if(OrderType()==OP_BUY)
     {
      if(fastma<slowma)
        {
         if(OrderClose(OrderTicket(),Lots, OrderClosePrice(),10,clrNONE))
            Print("Order Closed : ",OrderClosePrice());
         else
            Print("Error : ",GetLastError())
        }
     }


Reason: