Open an order (just once)

 

Hi!


This is my simple code:


int start()
  {
//----
  int ticket;
  ticket=OrderSend("EURUSD",OP_BUY,10,Ask,1,Ask-25*Point,Ask+25*Point,"My order #2",16384,0,Green);
  return;
//----
   return(0);
  }

I'd like if this code would run just once, not by every newer tick. How can I do it?

Thanks

 
int start()
  {
//----
  int ticket;
  if(OrdersTotal()>=1) return(0);
  ticket=OrderSend("EURUSD",OP_BUY,10,Ask,1,Ask-25*Point,Ask+25*Point,"My order #2",16384,0,Green);
  return;
//----
   return(0);
  }

This will stop OrderSend() being called - of course you need to have zero orders open to allow it to ever work again...


OrdersTotal() returns count of market (buy,sell) and pending orders.


First time your code runs OrdersTotal() will return zero so that OrderSend() will execute. IF you have no other orders open on Client Terminal...

Second time around, OrdersTotal() will return one so that the if() is true and so return(0); is executed.


return;is not needed, also correct syntax for a typed function return is return(0); in the case of the start() function.

Have you read The mql4 book? there is link at top on forum first page.

hth

 

Thank you!


Let me ask one more question: I'd like to open an order randomly (BUY or SELL position randomly).

Can you help me?

Thanks!

 
I'd like to open an order randomly (BUY or SELL position randomly)

I gotta play dumb here! not understand word randomly in context of market orders.

You have coded a BUY on eur/usd and given all the actuals for the OrderSend() call, yes?

Docs state: "At opening of a market order (OP_SELL or OP_BUY), only the latest prices of Bid (for selling) or Ask (for buying) can be used as open price."

so random unable to apply there unless placing pending order type, then could be closer fit...


not at all get it...

Reason: