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?
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?
Well you can create a flag.
Then you can set the value to one when the order is placed.
But it will give you the warning that you need to check the return value of the ordersend function so
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.
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.
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?
It would probably help you to read about the scope of variables.
What happens to the value of order each tick?
Okay so you declare the ticket with the boolean flag.
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.
bool order and
on global variables, and the rest of the code on OnTick, but still doesn't make any entry now