Forex
Google
New signals service!

Go Back   Forex Trading > Discussion Areas > Metatrader 4


Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

 
 
LinkBack Thread Tools
 
Old 09-07-2006, 11:29 PM
Member
 
Join Date: Sep 2006
Posts: 68
ra300z is on a distinguished road
EA - closing all positions at once.

I'm trying to run the following subroutine to close all my short positions at once. It works great with the backtester as it closes all short positions as expected. But when I run against a Demo account and leave the EA on all the time, it only appears to close 1 open order at each bar (since I only analyze charts when a new bar appears). Also, I don't see the !!!Warning message in the journal. So I'm thinking it's only executing the routine once or not looping properly.

Any ideas what I'm doing wrong.


void CloseAllShort() {
int total = OrdersTotal();
for (int cnt = 0 ; cnt < total ; cnt++) {
int order_type;
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
order_type = OrderType();
if(order_type == 1) {
if (!(OrderClose(OrderTicket(),OrderLots(),Ask,0,Blue ))) {
Print("!!!Warning - Order could not be closed, " + OrderTicket() + " " + GetLastError());
}
}
}
total = OrdersTotal();
Alert("<<<Short - All short orders were closed - " + total + " - $" + iClose(NULL,0,0));
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 09-07-2006, 11:55 PM
Senior Member
 
Join Date: Mar 2006
Posts: 787
Maji is on a distinguished road
This is a bad way of counting.

for (int cnt = 0 ; cnt < total ; cnt++)

you should count down instead...

for (int cnt <= total-1 0 ; cnt = 0 ; cnt--)

Also, make sure it checks for the symbol and magic number. Do a search on this forum for other close routines and you will come up with some.

Just a few ideas for your use.

Good luck.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 09-08-2006, 12:37 AM
phoenix's Avatar
Senior Member
 
Join Date: May 2006
Posts: 287
phoenix is on a distinguished road
where is your spread?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 09-08-2006, 01:07 AM
Member
 
Join Date: Sep 2006
Posts: 68
ra300z is on a distinguished road
Quote:
Originally Posted by phoenix
where is your spread?

How do I define the spread? I though so long as I code "Ask" or "Bid" as part of my order close, it will take into account the 2 pip spread. Am I wrong?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 09-08-2006, 01:09 AM
Member
 
Join Date: Sep 2006
Posts: 68
ra300z is on a distinguished road
Quote:
Originally Posted by Maji
This is a bad way of counting.

for (int cnt = 0 ; cnt < total ; cnt++)

you should count down instead...

for (int cnt <= total-1 0 ; cnt = 0 ; cnt--)

Also, make sure it checks for the symbol and magic number. Do a search on this forum for other close routines and you will come up with some.

Just a few ideas for your use.

Good luck.

Do you count down because orders get closed? Also shouldn't it be cnt<0 so that the 0 element gets processed:
for (int cnt <= total-1 ; cnt < 0 ; cnt--)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 09-08-2006, 01:21 AM
phoenix's Avatar
Senior Member
 
Join Date: May 2006
Posts: 287
phoenix is on a distinguished road
here's closeall script

one is close all the whole floating

another one is close all specific symbol
Attached Files
File Type: mq4 CloseAll.mq4 (1.8 KB, 44 views)
File Type: mq4 CloseAll-Symbol.mq4 (1.7 KB, 38 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 09-08-2006, 01:45 AM
Member
 
Join Date: Sep 2006
Posts: 68
ra300z is on a distinguished road
Quote:
Originally Posted by phoenix
here's closeall script

one is close all the whole floating

another one is close all specific symbol

Thank you, I have a better understanding of the Order functions. I thought any calls to the Order functions would handle things like spread, etc.


Also, why would I need the magic number? I thought that was for only running multiple EAs or running multiple pairs. Am I wrong?

Last edited by ra300z; 09-08-2006 at 02:22 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 

Bookmarks
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Closing positions before new day jorgeng Metatrader 4 5 04-11-2007 07:40 AM
Script: Count your net positions in MT4 for a symbol ycomp Metatrader 4 4 11-10-2006 03:23 PM
Can't get EA to close 2 Open positions??? skorcht Expert Advisors - Metatrader 4 4 08-07-2006 10:15 PM
Close all open positions jonjonau Expert Advisors - Metatrader 4 6 07-12-2006 05:01 AM
Close All Open Positions? lonespruce Metatrader 4 9 06-22-2006 07:20 AM


All times are GMT. The time now is 03:19 AM.



Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.