Thread: Super Ea
View Single Post
  #70 (permalink)  
Old 12-15-2006, 09:21 PM
AQUILEZ's Avatar
AQUILEZ AQUILEZ is offline
Sophomore
 
Join Date: Jul 2006
Posts: 91
Thanks: 2
Thanked 20 Times in 9 Posts
AQUILEZ will become famous soon enough
Default

Here is a little script I find 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|
//| //+------------------------------------------------------------------+
#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);
}
//+------------------------------------------------------------------+
Reply With Quote

Registered members have access to special online forex currency trading tools, software, mt4 expert advisors and indicators. Register now