Why won't my EA Close Orders?!?!?!

Back to topics list To post a new topic, please log in or register
avatar
18
BartWinter 2015.03.13 16:28
 

Hello,

I am attempting to write an EA which opens a trade using moving averages. I can get my EA to open trades but I cannot get it to close live trades. What is wrong with my code??? Here it is:

 extern double    Lots=20;

extern int       StopLoss=20;


double    Ma1;

extern int       Ma1Period=6;


double    Ma2;

extern int       Ma2Period=3;



#define MAX_ORDERS 1 //This controls the total number of that can be placed at any one time.

int static ticketbuy, ticketsell; 


void start() //This process repeats itself after every tick (change in price).

{

Ma1=iMA("GER30",PERIOD_M5,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defining the moving average

Ma2=iMA("GER30",PERIOD_M5,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defining the moving average


 

   if(OrdersTotal()<MAX_ORDERS) //Total number of orders

      //Buy Criteria

      {

       if(Ma1<Ma2)

        {

        ticketbuy = OrderSend("GER30",OP_BUY,Lots,Ask+0.00001,10,StopLoss,0,"test_1",0,0,Green);

      

      

        }

      // Sell Criteria

      else

       {

        ticketsell = OrderSend("GER30",OP_SELL,Lots,Bid+0.00001,10,StopLoss,0,"test_1",0,0,Red);

      

       }

    

      

    

      

//The section below is with regards to closing a live trade


for (int i=0;i<MAX_ORDERS;i++)

   {

    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true) continue;

    else break;

    

    if (OrderType() == OP_BUY)

     {

      if (Ma1>Ma2)

       {

        bool result1;

        result1 = OrderClose(i,Lots,Bid,10,Blue);

        break;

       }

     }

   

   

    if (OrderType() == OP_SELL)

     {

      if (Ma1<Ma2)

       {

        bool result2;

        result2 = OrderClose(i,Lots,Bid,10,Blue);

        break;

       }

     }

   }

     

 }

}


Thanks for looking at this. 

 

avatar
18
BartWinter 2015.03.13 17:32 #
 
 extern double    Lots=20;

extern int       StopLoss=20;



double    Ma1;

extern int       Ma1Period=6;



double    Ma2;

extern int       Ma2Period=3;





#define MAX_ORDERS 1 //This controls the total number of that can be placed at any one time.

int static ticketbuy, ticketsell; 



void start() //This process repeats itself after every tick (change in price).

{

Ma1=iMA("GER30",PERIOD_M5,6,0,MODE_SMA,PRICE_TYPICAL,0); //Defining the moving average

Ma2=iMA("GER30",PERIOD_M5,3,0,MODE_SMA,PRICE_TYPICAL,0); //Defining the moving average



 

   if(OrdersTotal()<MAX_ORDERS) //Total number of orders

      //Buy Criteria

      {

       if(Ma1<Ma2)

        {

        ticketbuy = OrderSend("GER30",OP_BUY,Lots,Ask+0.00001,10,StopLoss,0,"test_1",0,0,Green);

      

      

        }

      // Sell Criteria

      else

       {

        ticketsell = OrderSend("GER30",OP_SELL,Lots,Bid+0.00001,10,StopLoss,0,"test_1",0,0,Red);

      

       }

    

      

    

      

//The section below is with regards to closing a live trade



for (int i=0;i<MAX_ORDERS;i++)

   {

    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true) continue;

    else break;

    

    if (OrderType() == OP_BUY)

     {

      if (Ma1>Ma2)

       {

        bool result1;

        result1 = OrderClose(i,Lots,Bid,10,Blue);

        break;

       }

     }

   

   

    if (OrderType() == OP_SELL)

     {

      if (Ma1<Ma2)

       {

        bool result2;

        result2 = OrderClose(i,Lots,Bid,10,Blue);

        break;

       }

     }

   }

     

 }

}



Thanks for looking at this. 

 
avatar
36
kot_filemon 2015.03.13 17:57 #
 
for (int i=OrdersTotal()-1;i>=;i--)

   {

    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true) 
     if (MagicNumber==OrderMagicNumber())
      if (Symbol()==OrderSymbol())

    if (OrderType() == OP_BUY)

     {

      if (Ma1>Ma2)

       {

        bool result1 = OrderClose(OrderTicket(),OrderLots(),Bid,10,Blue);

       }

     }
      .
      .
      .
  //SELL the same

}

avatar
18
BartWinter 2015.03.13 18:07 #
 
Thanks for your help. Could you please explain what is a 'MagicNumber' and how do I define it with respect to the rest of the code? Thanks again.
avatar
36
kot_filemon 2015.03.13 18:15 #
 
int MagicNumber=OrderMagicNumber();

avatar
12985
WHRoeder 2015.03.13 18:29 #
 
  1. Check your return codes (OrderSelect and OrderClose) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  2. You must count down when closing/deleting in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
  3. You create orders with the magic number, then use an OrderSelect loop to find only those orders. order accounting - MQL4 forum
Back to topics list  

To add comments, please log in or register