Forex



Go Back   Forex Trading > Discussion Areas > Metatrader 4
Forex Forum Register More recent Blogs Calendar Advertising Others Help






Register
Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.

From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.

Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
  • Elite Section
    Get access to private discussions, specialized support, indicators and trading systems reported every week.
  • Advanced Elite Section
    For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
See more

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 09-08-2006, 12:29 AM
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!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #2 (permalink)  
Old 09-08-2006, 12:55 AM
Senior Member
 
Join Date: Mar 2006
Posts: 793
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!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #3 (permalink)  
Old 09-08-2006, 01:37 AM
phoenix's Avatar
Senior Member
 
Join Date: May 2006
Posts: 319
phoenix is on a distinguished road
where is your spread?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #4 (permalink)  
Old 09-08-2006, 02: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!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #5 (permalink)  
Old 09-08-2006, 02: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!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #6 (permalink)  
Old 09-08-2006, 02:21 AM
phoenix's Avatar
Senior Member
 
Join Date: May 2006
Posts: 319
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, 58 views)
File Type: mq4 CloseAll-Symbol.mq4 (1.7 KB, 46 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #7 (permalink)  
Old 09-08-2006, 02: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 03:22 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
Forum Jump

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


All times are GMT. The time now is 11:48 AM.



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