Problem with multiples trades opened.

 

Hi fellows, I am trying to create a simple expert advisor with a moving average crossover criteria.

Open a ticket when they(MAs) cross, and close the ticket when they cross again. 

the problem consist, when Im trying to specified only one trade (ticket) opened, and dont open more ticket until this is closed:

if(open_b == True || open_s == True)
{
for(int i=1; i>=OrdersTotal(); i++)
 {   
    total++;
   if(total<=1)
   
   {
    open_b = False;
    open_s= False;
   
   }
   
 }
}

The simulation is stopped when the first trade is opened.

extern int period_ma1 = 50,
           period_ma2= 20;
          

bool open_b = false,
     open_s = false,
     close_b = false,
     close_s = false,
     ans = false; 
    
int ticket,
    total= 0;    
      
int start()

{

//Orders Control//

if(open_b == True || open_s == True)
{
for(int i=1; i>=OrdersTotal(); i++)
 {   
    total++;
   if(total<=1)
   
   {
    open_b = False;
    open_s= False;
   
   }
   
 }
}

//trading criteria//

double MA1 = iMA(NULL,0,period_ma1,0,MODE_SMA, PRICE_CLOSE,0);

double MA11 = iMA(NULL,0,period_ma1,1,MODE_SMA, PRICE_CLOSE,0);

double MA2 = iMA(NULL,0,period_ma2,0,MODE_SMA, PRICE_CLOSE,0);

double MA22 = iMA(NULL,0,period_ma2,1,MODE_SMA, PRICE_CLOSE,0);


if( MA11 > MA22 && MA1 < MA2) 

 {

  open_b = True;

  close_s = True;

 }

if (MA11 < MA22 && MA1 > MA2)

 { 
 
  open_s = True;

  close_b = True;

 }
 
 //Closing trade criteria//
 
 if(close_b == true && ticket!=0)
 
 { 
  ans = OrderClose(ticket,0.01, Ask,2);
  
 } 
 
 if(close_s == True && ticket!=0)
 { 
   ans = OrderClose(ticket,0.01, Bid,2);
   
 }
 //opening trade criteria//

 
if(close_b == true && open_s == true)
{

ticket = OrderSend(NULL, OP_SELL,0.01,Bid,2,0,0);

}

if(close_s == true  && open_b == true)
{

ticket = OrderSend(NULL, OP_BUY,0.01,Ask ,2,0,0);

}

return;

}

 
  1. for(int i=1; i>=OrdersTotal(); i++){   
        total++;
       if(total<=1)
    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 and MetaTrader 4 - MQL4 programming forum

  2. What is the value of total before the loop? When will it ever go back to zero?

  3. if( MA11 > MA22 && MA1 < MA2){
      open_b = True;
      close_s = True;
    }
    if (MA11 < MA22 && MA1 > MA2){ 
      open_s = True;
      close_b = True;
    }
    What are the values before the test? When will they ever be reset?

  4.  if(close_b == true && ticket!=0){ 
      ans = OrderClose(ticket,0.01, Ask,2);
    } 
    If close_b is true how can ticket ever be zero?

  5. Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  6. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.

  7. if(close_b == true && open_s == true){
       ticket = OrderSend(NULL, OP_SELL,0.01,Bid,2,0,0);
    }
    What does close_b have to do with opening a sell (open_s)?
 
whroeder1:
  1. 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 and MetaTrader 4 - MQL4 programming forum

  2. What is the value of total before the loop? When will it ever go back to zero?

  3. What are the values before the test? When will they ever be reset?

  4. If close_b is true how can ticket ever be zero?

  5. Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  6. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.

  7. What does close_b have to do with opening a sell (open_s)?
Thank you a lot for the answer, I am going to process all the correction you provide me. Kudos.
 
whroeder1:
  1. 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 and MetaTrader 4 - MQL4 programming forum

  2. What is the value of total before the loop? When will it ever go back to zero?

  3. What are the values before the test? When will they ever be reset?

  4. If close_b is true how can ticket ever be zero?

  5. Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  6. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.

  7. What does close_b have to do with opening a sell (open_s)?

Hello whroeder, what i am trying to do is to see aplied my trading criteria, for the moment just for one currency. I did some of your corrections that i could understand. But the code, isnt working yet, is not applying my trading criteria just stay freeze. 

My trading criteria, consist:

1. If the Moving Average of 20 crossover to upside the 50 Moving Average, excute a OP_BUY, if there is an OP_SELL at that moment, close it.

2. If the Moving Average of 20 crossover to downside the 50 Moving Average, excute a OP_SELL, if there is an OP_BUY at that moment, close it.


I suspect the code is staying freeze, because of this:


for(int i=1; i>=OrdersTotal(); i++)
 {   
  if (OrderSelect(i-1,SELECT_BY_POS)==true)
  {
    total++;

  if(total <1 )
   
   {
    return;    
   }
   tip = OrderType();
  }
 }

Below you will see the modifications I did, according to what you explaint to me.

extern int period_ma1 = 50,
           period_ma2= 20;
          

bool open_b = false,
     open_s = false,
     close_b = false,
     close_s = false,
     ans = false; 
    
int ticket,
    total= 0,
    tip = -1;    
      
int start()

{

//Orders Control//

for(int i=1; i>=OrdersTotal(); i++)
 {   
  if (OrderSelect(i-1,SELECT_BY_POS)==true)
  {
    total++;

  if(total <1 )
   
   {
    return;    
   }
   tip = OrderType();
  }
 }


//trading criteria//

double MA1 = iMA(NULL,0,period_ma1,0,MODE_SMA, PRICE_CLOSE,0);

double MA11 = iMA(NULL,0,period_ma1,1,MODE_SMA, PRICE_CLOSE,0);

double MA2 = iMA(NULL,0,period_ma2,0,MODE_SMA, PRICE_CLOSE,0);

double MA22 = iMA(NULL,0,period_ma2,1,MODE_SMA, PRICE_CLOSE,0);


if( MA11 > MA22 && MA1 < MA2) 

 {

  open_b = True;

  close_s = True;

 }

if (MA11 < MA22 && MA1 > MA2)

 { 
 
  open_s = True;

  close_b = True;

 }
 
 //Closing trade criteria//

  while(true)                                    {      if( tip == 0 && close_b == true) {   ans = OrderClose(ticket,0.01, Bid,2);    } return; } while(true) { if( tip == 1 && close_s == True) {   ans = OrderClose(ticket,0.01, Ask,2); } return; } //opening trade criteria// while(true) { if(total == 0 && open_s == true) { ticket = OrderSend(NULL, OP_SELL,0.01,Bid,2,0,0); } return; } while(true) { if( total ==0 && open_b == true) { ticket = OrderSend(NULL, OP_BUY,0.01,Ask ,2,0,0); } return; } return; }