Jump to content

How to close all orders in MT4 ?!?! Imprtant!!


spike123

Recommended Posts

Re: How to close all orders in MT4 ?!?! Imprtant!!

 

use this script

 

 

*/

#property copyright ""

#property link ""

 

extern int option = 0;

extern bool BySymbolOnly = FALSE;

extern int magic_number = 0;

extern string comment_text = "";

extern int MaxNumberOfTries = 10;

int g_count_100;

 

int start() {

CloseAll();

return (0);

}

 

int CloseAll() {

int l_ord_total_0 = OrdersTotal();

int l_count_4 = 0;

RefreshRates();

switch (option) {

case 0:

g_count_100 = 0;

while (CountOpenTrades(BySymbolOnly, 1) > 0 && g_count_100 < MaxNumberOfTries) {

CloseAllTrades(BySymbolOnly, 1);

RefreshRates();

Sleep(1000);

g_count_100++;

}

break;

case 1:

g_count_100 = 0;

while (CountOpenTrades(BySymbolOnly, 0) > 0 && g_count_100 < MaxNumberOfTries) {

CloseAllTrades(BySymbolOnly, 0);

RefreshRates();

Sleep(1000);

g_count_100++;

}

break;

case 2:

g_count_100 = 0;

while (CountPending(BySymbolOnly) > 0 && g_count_100 < MaxNumberOfTries) {

CloseAllPending(BySymbolOnly);

Sleep(1000);

g_count_100++;

}

break;

case 3:

for (l_count_4 = 0; l_count_4 <= l_ord_total_0; l_count_4++) {

OrderSelect(0, SELECT_BY_POS, MODE_TRADES);

if (OrderMagicNumber() == magic_number) {

if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Violet);

if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Violet);

if (OrderType() > OP_SELL) OrderDelete(OrderTicket());

}

}

break;

case 4:

for (l_count_4 = 0; l_count_4 <= l_ord_total_0; l_count_4++) {

OrderSelect(0, SELECT_BY_POS, MODE_TRADES);

if (OrderComment() == comment_text) {

if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Violet);

if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Violet);

if (OrderType() > OP_SELL) OrderDelete(OrderTicket());

}

}

break;

case 5:

for (l_count_4 = 0; l_count_4 <= l_ord_total_0; l_count_4++) {

OrderSelect(0, SELECT_BY_POS, MODE_TRADES);

if (OrderProfit() > 0.0) {

if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Violet);

if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Violet);

if (OrderType() > OP_SELL) OrderDelete(OrderTicket());

}

}

break;

case 6:

for (l_count_4 = 0; l_count_4 <= l_ord_total_0; l_count_4++) {

OrderSelect(0, SELECT_BY_POS, MODE_TRADES);

if (OrderProfit() < 0.0) {

if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Violet);

if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Violet);

if (OrderType() > OP_SELL) OrderDelete(OrderTicket());

}

}

break;

case 7:

for (l_count_4 = 0; l_count_4 <= l_ord_total_0; l_count_4++) {

OrderSelect(0, SELECT_BY_POS, MODE_TRADES);

if (TimeDay(OrderOpenTime()) < TimeDay(TimeCurrent())) {

if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Violet);

if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Violet);

if (OrderType() > OP_SELL) OrderDelete(OrderTicket());

}

}

}

return (0);

}

 

void CloseAllTrades(int ai_0, int ai_4) {

for (int li_8 = OrdersTotal() - 1; li_8 >= 0; li_8--) {

OrderSelect(0, SELECT_BY_POS, MODE_TRADES);

if (ai_0 && OrderSymbol() != Symbol()) continue;

if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Violet);

if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Violet);

if (ai_4 && OrderType() > OP_SELL) OrderDelete(OrderTicket());

}

}

 

void CloseAllPending(int ai_0) {

for (int li_4 = OrdersTotal() - 1; li_4 >= 0; li_4--) {

OrderSelect(0, SELECT_BY_POS, MODE_TRADES);

if (ai_0 && OrderSymbol() != Symbol()) continue;

if (OrderType() > OP_SELL) OrderDelete(OrderTicket());

}

}

 

int CountPending(bool ai_0) {

int l_count_4 = 0;

for (int l_pos_8 = OrdersTotal() - 1; l_pos_8 >= 0; l_pos_8--) {

OrderSelect(l_pos_8, SELECT_BY_POS);

if (OrderType() > OP_SELL) {

if (ai_0 && OrderSymbol() == Symbol()) l_count_4++;

else

if (!ai_0) l_count_4++;

}

}

return (l_count_4);

}

 

int CountOpenTrades(bool ai_0, int ai_4) {

int l_cmd_16;

int l_count_8 = 0;

for (int l_pos_12 = OrdersTotal() - 1; l_pos_12 >= 0; l_pos_12--) {

OrderSelect(l_pos_12, SELECT_BY_POS);

l_cmd_16 = OrderType();

if (l_cmd_16 == OP_BUY || l_cmd_16 == OP_SELL) {

if (ai_0 && OrderSymbol() == Symbol()) l_count_8++;

else

if (!ai_0) l_count_8++;

} else {

if (OrderType() > OP_SELL && ai_4) {

if (ai_0 && OrderSymbol() == Symbol()) l_count_8++;

else

if (!ai_0) l_count_8++;

}

}

}

return (l_count_8);

}

Link to comment
Share on other sites

Re: How to close all orders in MT4 ?!?! Imprtant!!

 

Find attached the script which will close all open MT4 positions. If you want to be chosy, It has setting options for which orders to close and is helpful for closing a large number of open or pending positions quickly. It also has the option to just close trades with a specific magic number.

 

INSTRUCTIONS FOR USE

1. Download the files from "hxxp://www-dot-mediafire-dot-com/?sharekey=615a9bc3a712ebace7c82ed4b8f0c380e04e75f6e8ebb871"

2. Copy "CloseOrders.ex4" file inside folder of your broker (Something like "C:\Program Files\(Your Broker Name)\experts\scripts") and when you want to use it, simply drag it on the chart.

 

DO DEMO TESTING BEFORE TRYING IT ON LIVE ACCOUNTS. GOODLUCK!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...