Deleting pending sell limit orders

 

I have a lot of pending orders working in a EA. My free margin is about to be exhausted and the market is moving fast, I cannot figure out how to delete these pending orders. Please, can someone help me. Note that time is of the essence.

Thank you in advance,

George

 

Hi George, I have enclosed a script which you must place in directory C:\Program Files\MetaTrader 4\experts\scripts and then compile, hope all goes well, take care Ted. The script is as follows just in case.

//+------------------------------------------------------------------+
//| _CloseOrders. mq4 |
//| Copyright © 2007, sx ted |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, sx ted"
#property show_inputs

//+------------------------------------------------------------------+
//| EX4 imports |
//+------------------------------------------------------------------+
#include <stdlib.mqh>

//+------------------------------------------------------------------+
//| input parameters: |
//+-----------0---+----1----+----2----+----3]------------------------+
extern string _ = "Select one of the following:";
extern bool DeleteAllPendingOrders = false;
extern int DeletePendingWithMagicNumber = 0;
extern string DeletePendingMatchingComment = "";

//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
void start() {
int iOrders=OrdersTotal()-1, i;

if(DeleteAllPendingOrders) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderType()>OP_SELL) && !OrderDelete(OrderTicket())) Print(OrderError());
}
}
else if(DeletePendingWithMagicNumber > 0) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderMagicNumber() == DeletePendingWithMagicNumber)) {
if(OrderType()>OP_SELL) {
if(!OrderDelete(OrderTicket())) Print(OrderError());
}
}
}
}
else if(DeletePendingMatchingComment != "") {
DeletePendingMatchingComment=StringChangeToUpperCase(DeletePendingMatchingComment);
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (StringChangeToUpperCase(OrderComment()) == DeletePendingMatchingComment)) {
if(OrderType()>OP_SELL) {
if(!OrderDelete(OrderTicket())) Print(OrderError());
}
}
}
}
}

//+------------------------------------------------------------------+
//| Function..: OrderError |
//+------------------------------------------------------------------+
string OrderError() {
int iError=GetLastError();
return(StringConcatenate("Order:",OrderTicket()," GetLastError()=", iError, " ",ErrorDescription(iError)));
}

//+------------------------------------------------------------------+
//| Function..: StringChangeToUpperCase |
//| Example...: StringChangeToUpperCase("oNe mAn"); // ONE MAN |
//+------------------------------------------------------------------+
string StringChangeToUpperCase(string sText) {
int iLen=StringLen(sText), i, iChar;

for(i=0; i < iLen; i++) {
iChar=StringGetChar(sText, i);
if(iChar >= 97 && iChar <= 122) sText=StringSetChar(sText, i, iChar-32);
}
return(sText);
}
//+------------------------------------------------------------------+

Files:
 
sxTed:

Hi George, I have enclosed a script which you must place in directory C:\Program Files\MetaTrader 4\experts\scripts and then compile, hope all goes well, take care Ted. The script is as follows just in case.

//+------------------------------------------------------------------+
//| _CloseOrders. mq4 |
//| Copyright © 2007, sx ted |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, sx ted"
#property show_inputs

//+------------------------------------------------------------------+
//| EX4 imports |
//+------------------------------------------------------------------+
#include <stdlib.mqh>

//+------------------------------------------------------------------+
//| input parameters: |
//+-----------0---+----1----+----2----+----3]------------------------+
extern string _ = "Select one of the following:";
extern bool DeleteAllPendingOrders = false;
extern int DeletePendingWithMagicNumber = 0;
extern string DeletePendingMatchingComment = "";

//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
void start() {
int iOrders=OrdersTotal()-1, i;

if(DeleteAllPendingOrders) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderType()>OP_SELL) && !OrderDelete(OrderTicket())) Print(OrderError());
}
}
else if(DeletePendingWithMagicNumber > 0) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderMagicNumber() == DeletePendingWithMagicNumber)) {
if(OrderType()>OP_SELL) {
if(!OrderDelete(OrderTicket())) Print(OrderError());
}
}
}
}
else if(DeletePendingMatchingComment != "") {
DeletePendingMatchingComment=StringChangeToUpperCase(DeletePendingMatchingComment);
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (StringChangeToUpperCase(OrderComment()) == DeletePendingMatchingComment)) {
if(OrderType()>OP_SELL) {
if(!OrderDelete(OrderTicket())) Print(OrderError());
}
}
}
}
}

//+------------------------------------------------------------------+
//| Function..: OrderError |
//+------------------------------------------------------------------+
string OrderError() {
int iError=GetLastError();
return(StringConcatenate("Order:",OrderTicket()," GetLastError()=", iError, " ",ErrorDescription(iError)));
}

//+------------------------------------------------------------------+
//| Function..: StringChangeToUpperCase |
//| Example...: StringChangeToUpperCase("oNe mAn"); // ONE MAN |
//+------------------------------------------------------------------+
string StringChangeToUpperCase(string sText) {
int iLen=StringLen(sText), i, iChar;

for(i=0; i < iLen; i++) {
iChar=StringGetChar(sText, i);
if(iChar >= 97 && iChar <= 122) sText=StringSetChar(sText, i, iChar-32);
}
return(sText);
}
//+------------------------------------------------------------------+

 

Thanks for support for the new member. Its very important how to save from big lost in market. Pending order must close or cancelled to save any lose.. But when do I place this EA this can be set at the EA place too. Or can I place at Script place ?