Download MetaTrader 5
To add comments, please log in or register
Interested in MetaTrader 5 features? Learn more about them!
Daniel Petrovai
440
Daniel Petrovai 2014.03.03 23:30 

You need help with closing orders with your EA?

This post is intended for people who don't know or are not sure how to select and close orders.

Please understand that there are many other ways to close orders, this is just one way of many.

So, here we go :

Step 1. Include #include <stdlib.mqh> at the top of your code (copy/paste : #include <stdlib.mqh> )

#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <stdlib.mqh> 

If your variable's name for magic number is not " MagicNumber" you have to rename it to MagicNumber in your EA or rename the variable's name in this function to whatever name you use for magic number in your EA

In this example, the CloseThisSymbolAll () will close ALL orders, including pending ones..

So, copy/paste this function to the end of your EA :

//========================================================================

void CloseThisSymbolAll()
  {
   int trade;
   for(trade=OrdersTotal();trade>=0;trade--)
     {
      OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()!=Symbol()) continue;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)//this is the variable you may want to rename
        {
         if(OrderType()==OP_BUY)OrderClose(OrderTicket(),OrderLots(),Bid,slip,Blue);
         if(OrderType()==OP_SELL)OrderClose(OrderTicket(),OrderLots(),Ask,slip,Red);
         if(OrderType()==OP_BUYLIMIT)OrderDelete(OrderTicket(),Blue);
         if(OrderType()==OP_SELLLIMIT)OrderDelete(OrderTicket(),Blue);
         if(OrderType()==OP_BUYSTOP)OrderDelete(OrderTicket(),Blue);
         if(OrderType()==OP_SELLSTOP)OrderDelete(OrderTicket(),Blue);
         ErrorDescription(GetLastError());
        }
     }
  }
//========================================================================

You can call this function any time throughout your code, any time you want all orders to be closed just by typing :

CloseThisSymbolAll();

Yes, it's as easy as that.

1. Include the library to get error description, it helps .

2. Copy/paste the function at the end of your code.

3.Call the function when you need it.

Have any questions ? Ask away.

Enjoy.

Alain Verleyen
Moderator
30635
Alain Verleyen 2014.03.03 23:38  

Sorry, but I can't agree with the way you do. And of course I am talking about "access restriction" to this topic and about your way to close orders.

This forum is accessible to each and everyone has the right to post and answer to any post/topic. If you don't agree or don't like a post, you are free to ignore it. But you can't prevent someone to answer. So I suggest you friendly to edit your post to remove this restriction.

Thank you.

Daniel Petrovai
440
Daniel Petrovai 2014.03.04 00:02  
angevoyageur:

Sorry, but I can't agree with the way you do. And of course I am talking about "access restriction" to this topic and about your way to close orders.

This forum is accessible to each and everyone has the right to post and answer to any post/topic. If you don't agree or don't like a post, you are free to ignore it. But you can't prevent someone to answer. So I suggest you friendly to edit your post to remove this restriction.

Thank you.


It's not a "restriction " just a suggestion, it says please move along.

But if you think it's inappropriate in any way... you're the moderator.

I really believe that " please " is still a polite way to ask people for something and in no way obstructs access to anyone.

I will remove it if you still think it's restrictive.

Alain Verleyen
Moderator
30635
Alain Verleyen 2014.03.04 00:06  
thrdel:


It's not a "restriction " just a suggestion, it says please move along.

But if you think it's inappropriate in any way... you're the moderator.

I really believe that " please " is still a polite way to ask people for something and in no way obstructs access to anyone.

I will remove it if you still think it's restrictive.

I know you have written "please", but it's really not what is resorting of your 2 first sentences. Anyway, even with a "please", you can't restrict access to a topic in any way. So yes, please, edit your post to change these 2 sentences.
Ubzen
5394
Ubzen 2014.03.04 00:14  

@thrdel: A word of advice.

When I taught I knew mql4 good enough to try helping newbies, I ran into problems posting codes upon the forum.

There seem to always be someone waiting to correct my codes, sometimes its a cosmetic fix, other times its a bug.

Having other people jump upon my code when I was only trying to help seem annoying at first. Then I realize my choices.

Long story short, when someone tries to correct my codes, I just say "thank you and take-care".

I always believed that the poster should ignore comments they do-not like ...

Not that responders should comment in a manner which the poster like ...

If someone cannot handle different responses ... they probably shouldn't be posting on a Public forum IMO.

Alain Verleyen
Moderator
30635
Alain Verleyen 2014.03.04 00:35  
thrdel:
...
Thank you.
Daniel Petrovai
440
Daniel Petrovai 2014.03.04 00:55  
ubzen:

@thrdel: A word of advice.

When I taught I knew mql4 good enough to try helping newbies, I ran into problems posting codes upon the forum.

There seem to always be someone waiting to correct my codes, sometimes its a cosmetic fix, other times its a bug.

Having other people jump upon my code when I was only trying to help seem annoying at first. Then I realize my choices.

Long story short, when someone tries to correct my codes, I just say "thank you and take-care".

I always believed that the poster should ignore comments they do-not like ...

Not that responders should comment in a manner which the poster like ...

If someone cannot handle different responses ... they probably shouldn't be posting on a Public forum IMO.


I agree but I also believe that if the forum is public and we claim to try and help each other, the " respondents " should limit their comment to constructive ones only.

Sort of " If you won't help, don't hurt " kinda thing. Wouldn't it be nice?

I also remember the times when I couldn't figure out x *=23 and I looked for help on this forum and the answers I've got then made me turn away and stay away for a long time.

I don't agree with the suggestion that if the post are subject to malicious attacks from members who have a different agenda in mind from the majority of us,

one should keep silence and say " Thank you and take care. "

Debating different ways of writing code is one thing and attacks intended to bring no good to anyone is a completely different thing .

And standing your grounds when this attacks happen is not the same as "cannot handle different responses ".

What do you think ?

Ubzen
5394
Ubzen 2014.03.04 01:06  
thrdel:


I agree but I also believe that if the forum is public and we claim to try and help each other, the " respondents " should limit their comment to constructive ones only.

Sort of " If you won't help, don't hurt " kinda thing. Wouldn't it be nice?

I also remember the times when I couldn't figure out x *=23 and I looked for help on this forum and the answers I've got then made me turn away and stay away for a long time.

I don't agree with the suggestion that if the post are subject to malicious attacks from members who have a different agenda in mind from the majority of us,

one should keep silence and say " Thank you and take care. "

Debating different ways of writing code is one thing and attacks intended to bring no good to anyone is a completely different thing .

And standing your grounds when this attacks happen is not the same as "cannot handle different responses ".

What do you think ?

I agree with you in terms of someone being rude.

I do-not want to diminish your post into a repeat about the being rude thread.

But as you can see sometime its nice having another set of eyes when we're posting codes.

Example: https://www.mql5.com/en/forum/150140 .

In regards to this code, I would recommend that newbies place the OrderSelect() within an if() check.

Should the OrderSelect() fail, the last selected order could still be selected.

This could cause an un-intended close of a different order.

=================================================================

Added: I also do-not see the need for this line:

if(OrderSymbol()!=Symbol()) continue;

When you're going to be checking

OrderSymbol()==Symbol()
On the next line. The 1st line should probably be omitted to avoid double-checking.
Daniel Petrovai
440
Daniel Petrovai 2014.03.04 01:44  
ubzen:

I agree with you in terms of someone being rude.

I do-not want to diminish your post into a repeat about the being rude thread.

But as you can see sometime its nice having another set of eyes when we're posting codes.

Example: https://www.mql5.com/en/forum/150140 .

In regards to this code, I would recommend that newbies place the OrderSelect() within an if() check.

Should the OrderSelect() fail, the last selected order could still be selected.

This could cause an un-intended close of a different order.

=================================================================

Added: I also do-not see the need for this line:

When you're going to be checking

On the next line. The 1st line should probably be omitted to avoid double-checking.


Point taken, he did express his opinion in a nice and polite manner, his remarks (and yours) and improvements are indeed intended to help and I thank him for it.

Isn't explaining what may be better and why plus code work so much better than just "you're wrong" ?

That is what I've been trying to say all along. If one feels the need to post a comment don't just say it but also explain your view and post code. Easy.

I believe you agree. Like you, I'm not here to attack anyone out of the blue, I just want to give what I can to those who've got nothing and if others can

improve on that, even better. I still remember how bad it is to be desperate for help and not get it.

Your recommendations were included in the code posted by Thirteen. Thanks.

Ubzen
5394
Ubzen 2014.03.04 01:47  
You're welcome.
Tjipke de Vries
6314
Tjipke de Vries 2014.03.04 09:30  
thrdel:

You need help with closing orders with your EA?

This post is intended for people who don't know or are not sure how to select and close orders.

Please understand that there are many other ways to close orders, this is just one way of many.

So, here we go :

Step 1. Include #include <stdlib.mqh> at the top of your code (copy/paste : #include <stdlib.mqh> )

If your variable's name for magic number is not " MagicNumber" you have to rename it to MagicNumber in your EA or rename the variable's name in this function to whatever name you use for magic number in your EA

In this example, the CloseThisSymbolAll () will close ALL orders, including pending ones..

So, copy/paste this function to the end of your EA :

You can call this function any time throughout your code, any time you want all orders to be closed just by typing :

Yes, it's as easy as that.

1. Include the library to get error description, it helps .

2. Copy/paste the function at the end of your code.

3.Call the function when you need it.

Have any questions ? Ask away.

Enjoy.


What i see in your code is that you do an orderloop of your open trades

here counting down as it should be

when you find a trade with the right symbol and with the right magicnumber

you do orderclose or orderdelete depending its ordertype

at beginning of start we get a tick Bid/Ask before you do OrderClose your broker can work with a different price

if you have a lot of trades to check and to close you send them one by one to your broker to know the price you can use RefreshRates()

if TradeContextBusy() and closing or deleting of a trade fails it has no sense to try directly closing or deleting the next trade in the range

that next trade will then also fails if you have still TradeContextBusy()

Some people use for that a Sleep(...) and try then again to close the trade till a max time

OrderClose is type bool

Returns true if successful, otherwise false. To get additional error information, one has to call the GetLastError() function.

it has only sense to get that information if it is false, you call it always...

and depending on that error it is do you try again to handle that trade or do you take the next one

Also a very nice trick you can do closing OP_BUY or OP_SELL is instead of Bid/Ask use OrderClosePrice()

when the whole loop is done you still not be sure all trades your EA are closed/deleted

....

You see I don't call it easy as that

/ /12
To add comments, please log in or register