• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 1:40am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 1:40am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Can you run an EA on multiple accounts at same time? 6 replies

Running multiple EAs on Multiple Pairs for the same account 2 replies

Closing multiple positions with just 1 click? 5 replies

How can i run several platforms from the same broker on my system at the same time? 4 replies

avoid multiple orders at the same time 8 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 10
Attachments: Closing multiple positions at the same time
Exit Attachments

Closing multiple positions at the same time

  • Post #1
  • Quote
  • First Post: Jan 11, 2007 3:31pm Jan 11, 2007 3:31pm
  •  kaypee
  • | Joined Nov 2006 | Status: Member | 4 Posts
Hi guys,

I am learning to code MQL4 , what function can i use to close multiple positions at once. If there is EA for that, i will appreciate if someone can post it.

Here is the cript I wrote, the problem is it does not work if there are more than 2 positions, it returns error #129
========================================================
extern double R.O.I=3; //Return On Investment in %
extern double L.O.I=-3; //Loss On Investment in %

int start()
{
// Variables, in which tickets of orders of each type will be
// memorized
int BuyStopOrder = 0, SellStopOrder = 0, BuyOrder = 0,
SellOrder = 0;
int _GetLastError = 0, _OrdersTotal = OrdersTotal();
// search in all open positions and memorize, positions of which
// type have already been opened:
for ( int z = _OrdersTotal - 1; z >= 0; z -- )
{
// if an error occurs at searching for a position, go
// to the next one
if ( !OrderSelect( z, SELECT_BY_POS ) )
{
_GetLastError = GetLastError();
Print("OrderSelect(", z, ", SELECT_BY_POS) - Error #",
_GetLastError );
continue;
}

// if the position was opened not for the current symbol, skip it
if ( OrderSymbol() != Symbol() ) continue;

// depending on the position type, change value of the
// variable:
switch ( OrderType() )
{
case OP_BUY: BuyOrder = OrderTicket(); break;
case OP_SELL: SellOrder = OrderTicket(); break;
case OP_BUYSTOP: BuyStopOrder = OrderTicket(); break;
case OP_SELLSTOP: SellStopOrder = OrderTicket(); break;
}
}

if ( (AccountEquity()/AccountBalance()-1)*100>=R.O.I || (AccountEquity()/AccountBalance()-1)*100<=L.O.I)
{
if ( SellStopOrder > 0 )
{
if ( !OrderDelete( SellStopOrder ) )
{
Alert("OrderDelete Error #", GetLastError());
return(-1);
}
}
if ( BuyStopOrder > 0 )
{
if ( !OrderDelete( BuyStopOrder ) )
{
Alert("OrderDelete Error #", GetLastError());
return(-1);
}
}
if ( BuyOrder > 0 )
{
if ( !OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue) )
{
Alert("OrderClose Error #", GetLastError());
return(-1);
}
}
if ( SellOrder > 0 )
{
if ( !OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue))
{
Alert("OrderClose Error #", GetLastError());
return(-1);
}
}
}
}
========================================================
Thanks in advance
Happy trading
  • Post #2
  • Quote
  • Jan 12, 2007 10:45am Jan 12, 2007 10:45am
  •  Seavo
  • | Joined Aug 2006 | Status: Member | 78 Posts
Sorry I can't help more but I'm more of a code hacker than a programmer. Maybe you'll find some interesting code in here though, especially the CloseAll() function.

Good luck :-)
Attached File
File Type: mq4 CloseByPL.mq4   5 KB | 390 downloads
 
 
  • Post #3
  • Quote
  • Jan 16, 2007 7:44am Jan 16, 2007 7:44am
  •  kaypee
  • | Joined Nov 2006 | Status: Member | 4 Posts
Thanks, I also got something experts that can close all positions at the same time. I will just attach them here in case someone needs. Make sure you test them on a demo first before using them on live account.

Happy Trading
Attached File
File Type: mq4 Close all open and pending orders1.mq4   1 KB | 365 downloads
 
 
  • Post #4
  • Quote
  • Jan 16, 2007 7:48am Jan 16, 2007 7:48am
  •  kaypee
  • | Joined Nov 2006 | Status: Member | 4 Posts
Here is some more
Attached Files
File Type: mq4 Close all open and pending orders.mq4   1 KB | 303 downloads
File Type: mq4 Close all open positions.mq4   1 KB | 357 downloads
File Type: mq4 Close all pending orders.mq4   1 KB | 273 downloads
File Type: mq4 Close_all_profitable_positions loss.mq4   1 KB | 341 downloads
 
 
  • Post #5
  • Quote
  • Jan 16, 2007 7:51am Jan 16, 2007 7:51am
  •  kaypee
  • | Joined Nov 2006 | Status: Member | 4 Posts
Here are the last 4...

Happy trading guys
Attached Files
File Type: mq4 CloseAllTrades.mq4   2 KB | 383 downloads
File Type: mq4 Close_all_profitable_positions_loss.mq4   1 KB | 306 downloads
File Type: mq4 Close_all_profitable_positions.mq4   1 KB | 319 downloads
File Type: mq4 Close_all_profitable_positions Profit1.mq4   1 KB | 320 downloads
 
 
  • Post #6
  • Quote
  • Last Post: Jan 16, 2007 8:37am Jan 16, 2007 8:37am
  •  aparsai
  • | Joined Mar 2006 | Status: Member | 1,120 Posts
Thanks for sharing all these with us.
 
 
  • Platform Tech
  • /
  • Closing multiple positions at the same time
  • Reply to Thread
0 traders viewing now
Top of Page
Forex Factory Blog Updated: Alerting All Members
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2022