Here is a little script I wrote that closes all open positions. You need to check "Alow Live Trading" option under Options > Expert Advisor tab in order for this script to work.
To Setup:
- Open up MetaEditor.
- Click File > New
- Select "Script program"
- Select replace all text with code below
- Compile & Run
I hope you find it as useful as I do.
Code:
//+------------------------------------------------------------------+
//| CLOSE_ALL.mq4 |
//| pileo|
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "pileo"
#property link "http://www.metaquotes.net"
#include <stdlib.mqh>
#include <WinUser32.mqh>
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
//----
if(MessageBox("Do you really want to close all positions?", "Close All", MB_YESNO|MB_ICONQUESTION) !=IDYES) return(1);
double prc;
int prc_mode;
int total = OrdersTotal();
int orders[];
int size;
size = ArrayResize(orders, total);
//Get original positions
for(int i=0;i<total;i++)
{
OrderSelect(i, SELECT_BY_POS);
orders[i] = OrderTicket();
}
if(size <= 0)
{
//No open orders
return(-1);
}
int ticketSent;
for(int x=0;x<size;x++)
{
OrderSelect(orders[x], SELECT_BY_TICKET);
if(OrderType()==OP_BUY)
prc_mode = MODE_BID;
else
prc_mode = MODE_ASK;
prc = MarketInfo(OrderSymbol(), prc_mode);
Print("Closing Order # ", orders[x], " symbol: ", OrderSymbol(), " price: ", prc);
if(OrderClose(orders[x],OrderLots(),prc,3))
{
Print("Order # ", orders[x], " closed");
}
else
{
Print("Failed to close Order # ", orders[x], " Error: ", GetLastError());
return(false);
}
}
//----
return(0);
}
//+------------------------------------------------------------------+