Author: Copyright � 2004, MetaQuotes Software Corp.
close

This script is designed to automate the closing of a specific trade within your trading platform. Instead of closing all active positions at once, it targets only one specific entry based on its position in your current trade list.

How the Logic Works:

1. Identification of the Primary Trade The script begins by looking at your active trades. It specifically identifies the very first order at the top of your "Market" list. If there are no active trades, or if the first item in your list is not a standard buy or sell position (such as a pending order), the script will simply do nothing.

2. Determining the Order Type Once it identifies the first trade, the script checks whether you are currently in a "Buy" position or a "Sell" position. This step is crucial because the closing price differs depending on which direction you are trading.

3. Executing the Closure The script then attempts to close that specific trade immediately:

  • If it is a Buy order, it uses the current market selling price (Bid).
  • If it is a Sell order, it uses the current market buying price (Ask).

4. Handling Market Fluctuations Financial markets move rapidly. To ensure the trade closes successfully even if the price changes during the split-second the command is sent, the script includes a "retry" loop. If the platform rejects the closing request because the price moved too much, the script automatically refreshes the current market data and tries again until the order is successfully closed or a different error occurs.

Summary for the User

Think of this as a "Close First Trade" button. It is intended for situations where you want to exit your oldest or primary market position without affecting any other trades you might have open simultaneously.

Orders Execution
It Closes Orders by itself
12 Views
0 Downloads
0 Favorites
close
//+------------------------------------------------------------------+
//|                                                        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_confirm

//+------------------------------------------------------------------+
//| script "close first market order if it is first in the list"     |
//+------------------------------------------------------------------+
int start()
  {
   bool   result;
   double price;
   int    cmd,error;
//----
   if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
     {
      cmd=OrderType();
      //---- first order is buy or sell
      if(cmd==OP_BUY || cmd==OP_SELL)
        {
         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