close_by_type

Author: Copyright � 2004, MetaQuotes Software Corp.
close_by_type
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
2 Views
0 Downloads
0 Favorites
close_by_type
//+------------------------------------------------------------------+
//|                                                        close.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"
#property show_inputs

extern string Trade_Type = "Close Trade Type (0=Buying 1=Selling)";
extern int trade_type_selection = OP_BUY; //default is to close buy trades


//+------------------------------------------------------------------+
//| script "close first market order if it is first in the list"     |
//+------------------------------------------------------------------+
int start()
  {
   bool   result;
   double price;
   int    cmd,error,total;
//----
  total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
      cmd=OrderType();
      //---- first order is buy or sell
      if((cmd==trade_type_selection)&& (OrderSymbol()==Symbol())){
         while(true){
            if(cmd==OP_BUY) price=Bid;
            else            price=Ask;
            result=OrderClose(OrderTicket(),OrderLots(),price,3,CLR_NONE);
            if(result!=TRUE) { error=GetLastError(); Print("LastError = ",error); }
            else error=0;
            if(error==135) RefreshRates();
            else break;
         }
      }
   }
   else Print( "Error when order select ", GetLastError());
  }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments