Download MetaTrader 5

help me please [solved]

To add comments, please log in or register
mrluck1
989
mrluck1  

I'm new in mql4 and this code opens multiple orders, how can i avoid it? it should just open 1 order until it closes, 

how can i do this?

 

Thanks

 

void OnTick()
  {

  
  double up= iIchimoku (NULL, 5,9,26,52,3,0);
  double down= iIchimoku (NULL,5,9,26,52,4,0);

  
  if (MathMin (up,down) < Bid )
  

  OrderSend (NULL,0,0.01,Ask,3, Bid-150*Point, Bid+100*Point);
    
    
  }
  


 

Marco vd Heijden
Moderator
6151
Marco vd Heijden  
void OnTick()
  {

  if(OrdersTotal()==0)
    {
     double up= iIchimoku (NULL, 5,9,26,52,3,0);
     double down= iIchimoku (NULL,5,9,26,52,4,0);

     if (MathMin (up,down) < Bid )

     OrderSend (NULL,0,0.01,Ask,3, Bid-150*Point, Bid+100*Point);
    }
    
  }
mrluck1
989
mrluck1  
thank you very much sir , you were strict to the point, not sending me into circles 
Marco vd Heijden
Moderator
6151
Marco vd Heijden  

It's a solution but it's is not the best solution as you will learn if you proceed.

But it can help you at this point in time.

mrluck1
989
mrluck1  
Marco vd Heijden:

It's a solution but it's is not the best solution as you will learn if you proceed.

But it can help you at this point in time.

yes it solved the problem in the topic, but  when it closes it also creates a new order, if it's still above the cloud. Could you help me to solve this new problem?

How block all new orders after sucessfully closing the first? 

Marco vd Heijden
Moderator
6151
Marco vd Heijden  

Well you can create a flag.

bool order=0;


Then you can set the value to one when the order is placed.

void OnTick()
  {

  if(order==0)
    {
     double up= iIchimoku (NULL, 5,9,26,52,3,0);
     double down= iIchimoku (NULL,5,9,26,52,4,0);

     if (MathMin (up,down) < Bid )

     OrderSend (NULL,0,0.01,Ask,3, Bid-150*Point, Bid+100*Point);
     order=1;
    }
    
  }

But it will give you the warning that you need to check the return value of the ordersend function so

void OnTick()
  {

  if(order==0)
    {
     double up= iIchimoku (NULL, 5,9,26,52,3,0);
     double down= iIchimoku (NULL,5,9,26,52,4,0);

     if (MathMin (up,down) < Bid )

     int ticket=OrderSend (NULL,0,0.01,Ask,3, Bid-150*Point, Bid+100*Point);
      {
       if(ticket!=-1)
        {
         order=1;
        }
      }
    }
  }

Why not set the flag depending on if the order was successfully placed or not ?

In that case if your order fails it will continue to try.

honest_knave
Moderator
2342
honest_knave  
mrluck1:

yes it solved the problem in the topic, but  when it closes it also creates a new order, if it's still above the cloud. Could you help me to solve this new problem?

How block all new orders after sucessfully closing the first? 

As Marco has already alluded to, you will probably run into other problems.

What if there is an open order on another symbol?

What if there are orders being controlled by another EA?

What if there are pending orders?

What if the trigger is still active when you close the first one?

In response to the latter, you could use a flag as Marco has suggested. Just remember you need to decide when to reset the flag or the EA never takes another trade again.

Alternatively, you could check for a cross / change of state from below/in the cloud to above the cloud.

mrluck1
989
mrluck1  

i put your code and says ticket is undleclared, so i declared it, but now the code opens multiple orders, just like before, how to proceed now? 

 

void OnTick()
  {

bool order= 0;
int ticket;

  if(order==0)
    {
     double up= iIchimoku (NULL, 5,9,26,52,3,0);
     double down= iIchimoku (NULL,5,9,26,52,4,0);

     if (MathMax (up,down) < Bid )

    int ticket= OrderSend (NULL,0,0.01,Ask,3, Bid-150*Point, Bid+100*Point);
      {
       if( ticket!=1)
        {
         order=1;
        }
      }
    }
  }


 

And to honest knave: In this example, its a script, so just 1 trade is ok
honest_knave
Moderator
2342
honest_knave  
mrluck1:

i put your code and says ticket is undleclared, so i declared it, but now the code opens multiple orders, just like before, how to proceed now? 

 

void OnTick()
  {

bool order= 0;
int ticket;

  if(order==0)
    {
     double up= iIchimoku (NULL, 5,9,26,52,3,0);
     double down= iIchimoku (NULL,5,9,26,52,4,0);

     if (MathMax (up,down) < Bid )

    int ticket= OrderSend (NULL,0,0.01,Ask,3, Bid-150*Point, Bid+100*Point);
      {
       if( ticket!=1)
        {
         order=1;
        }
      }
    }
  }


 

It would probably help you to read about the scope of variables.

What happens to the value of order each tick? 

Marco vd Heijden
Moderator
6151
Marco vd Heijden  

Okay so you declare the ticket with the boolean flag.

bool order=0;
int ticket;

And then

void OnTick()
  {

  if(order==0)
    {
     double up= iIchimoku (NULL, 5,9,26,52,3,0);
     double down= iIchimoku (NULL,5,9,26,52,4,0);

     if (MathMax (up,down) < Bid )

    ticket= OrderSend (NULL,0,0.01,Ask,3, Bid-150*Point, Bid+100*Point);
      {
       if( ticket>0)
        {
         order=1;
        }
      }
    }
  }

You can not declare them in OnTick() because their value will be reset on every tick of course.

Also note that the OrderSend either returns the ticket number , or -1 minus one if it fails.

mrluck1
989
mrluck1  

I put 

 bool order and

int ticket 

on global variables, and the rest of the code on OnTick, but still doesn't make any entry now 

123
To add comments, please log in or register