Scripts: Close Orders - page 2

 
sxTed:
//+------------------------------------------------------------------+
//|                              CloseOrdersLessThanStatedEquity.mq4 |
//|                                          Copyright © 2008, sxTed |
//|                                               sxted@talktalk.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, sxTed"
#property link      "sxted@talktalk.net"
 
//+------------------------------------------------------------------+
//| EX4 imports                                                      |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
 
//+------------------------------------------------------------------+
//| input parameters:                                                |
//+-----------0---+----1----+----2----+----3]------------------------+
extern double CloseOrdersWhenEquityLessThan = 0.0;
 
//+------------------------------------------------------------------+
//| global variables to program:                                     |
//+------------------------------------------------------------------+
double Price[2];
int    giSlippage;
 
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init() {
  return(0);
}
 
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit() {
  return(0);
}
 
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() {
  int iOrders=OrdersTotal(), i;
  if(AccountEquity() < CloseOrdersWhenEquityLessThan && iOrders > 0) {
    for(i=iOrders-1; i>=0; i--) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
        if((OrderType()<=OP_SELL) && GetMarketInfo()) {
          if(!OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage)) Print(OrderError());
        }
        else if(OrderType()>OP_SELL) {
          if(!OrderDelete(OrderTicket())) Print(OrderError());
        }
      }
    }
    Alert("Equity low - all open orders closed and pending orders cancelled");
  }
  return(0);
}
 
//+------------------------------------------------------------------+
//| Function..: OrderError                                           |
//+------------------------------------------------------------------+
string OrderError() {
  int iError=GetLastError();
  return(StringConcatenate("Order:",OrderTicket()," GetLastError()=",iError," ",ErrorDescription(iError)));
}
 
//+------------------------------------------------------------------+
//| Function..: GetMarketInfo                                        |
//| Returns...: bool Success.                                        |
//+------------------------------------------------------------------+
bool GetMarketInfo() {
  RefreshRates();
  Price[0]=MarketInfo(OrderSymbol(),MODE_ASK);
  Price[1]=MarketInfo(OrderSymbol(),MODE_BID);
  double dPoint=MarketInfo(OrderSymbol(),MODE_POINT);
  if(dPoint==0) return(false);
  giSlippage=(Price[0]-Price[1])/dPoint;
  return(Price[0]>0.0 && Price[1]>0.0);
}
//+------------------------------------------------------------------+
he74, please try the above expert.
 
thanks, but i don't understand how system works. qeuity was 6900 i set up 5000 (level after close orders). system closed orders but queity was 3900. when i type 3000 (level of lost), every order is automaticly close. it doesn't matter quantity 100k, 50k, 10k. what should i put that system works good?
 
he74, I do not understand your problem, sorry.
 
i don't know why but system made more lost than digit in system (inside). at the present i can't open any orders. everyone is close directly after opening. is there any minimum capital i need on the acoount or other parameters?
 
he74, If you are trading on a live account - then close all orders and stop. Ask your question on the main forum - as I do not understand, sorry.
 
thank you for interest.
 
Works great!  Thx!!  :-)
 
Does this script close just the orders from the currently viewd pair/chart, or does it close all open orders in the terminal regardless of how many pairs are being traded?
 
chris phs, sorry for the delay I only check mail at the week end, if open orders are EURUSD and USDJPY and current chart where the script is launched from is GBPUSD then orders for EURUSD and USDJPY will be closed.
 
bnbb2004, thank you for the compliment.
Reason: