This script is designed to automate the management of your trading positions by identifying and closing the specific trade that is closest to a price point you select manually on your chart.
How the Logic Works
1. Identifying the Target Price When you run the script, it first detects the exact price level where you dropped or placed the script on the trading window. This acts as your "target zone."
2. Scanning Active Trades The program then performs a systematic review of all your currently open trades. It filters these trades based on two main criteria:
- Instrument Match: It only looks at trades that belong to the specific currency pair or asset you are currently viewing (for example, if you are looking at EURUSD, it ignores other active trades in different pairs).
- Proximity Search: It calculates the distance between each open trade's entry price and your target price. The script is designed to find the single trade that is geographically closest to that specific price point.
3. Determining Action (Close vs. Delete) Once the script identifies the closest trade, it determines what action to take based on the type of order:
- Market Orders: If the trade is an active, open position (a "buy" or "sell"), the script proceeds to close that position at the current market price.
- Pending Orders: If the trade is a pending order (an order waiting to be triggered), the script proceeds to cancel/delete that order from the system.
4. Execution and Error Handling After identifying the correct trade, the script executes the closure or deletion immediately. It includes a built-in verification step: if for any reason the platform cannot complete the action (such as a connection issue or insufficient margin), the script will trigger an alert on your screen to notify you of the specific error code.
Summary for the User
Essentially, this tool saves you from manually hunting through a long list of trades to find which one is nearest to a specific price. You simply drop the script near the price you want to act upon, and it automatically handles the "cleanup" by closing the nearest active position or removing the nearest pending order.
//+------------------------------------------------------------------+
//| CloseOrd.mq4 |
//| Íèêîëàé Åôðåìîâ |
//| eftd@ya.ru |
//+------------------------------------------------------------------+
#property copyright "Nic E"
#property link ""
extern int Slippage = 3; // Ïðîñêàëüçûâàíèå öåíû
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
//----
string Symb=Symbol(); // Ôèíàíñ. èíñòðóìåíò
double Dist=1000000.0; // Ïðåäóñòàíîâêà
bool rOrd=true, Resultat=true;
double Win_Price=WindowPriceOnDropped();//Çäåñü áðîøåí ñêðèïò
//----1
for(int i=1; i<=OrdersTotal(); i++) // Öèêë ïåðåáîðà îðäåðîâ
{
if (OrderSelect(i-1,SELECT_BY_POS)==true)// Åñëè åñòü ñëåäóþùèé
{
//----2
if (OrderSymbol()!= Symb) continue;// Íå íàø ôèí.èíñòðóì
//----3
if (NormalizeDouble(MathAbs(OrderOpenPrice()-Win_Price),Digits)
<NormalizeDouble(Dist,Digits)) //Âûáèðàåì áëèæàéøèé
{
Dist=MathAbs(OrderOpenPrice()-Win_Price);// Íîâîå çíà÷åíèå
int Tip =OrderType(); // Òèï âûáðàííîãî îðä.
if (Tip>1)rOrd=false; // Îòëîæåííûé îðäåð
int Ticket =OrderTicket(); // ¹ âûáðàííîãî îðä.
double Lots=OrderLots();
double Price =OrderOpenPrice(); // Öåíà âûáðàííîãî îðä.
}
}
}
//----4
while(true) //Öèêë Çàêðûòèå îðäåðà
{
if (rOrd==true) //
{
if(Tip==OP_BUY)
Resultat=OrderClose(Ticket,Lots,Bid,Slippage,CLR_NONE);
else
Resultat=OrderClose(Ticket,Lots,Ask,Slippage,CLR_NONE);
if(Resultat!=true)
{
int Error=GetLastError();
Alert("Îøèáêà ¹ = ",Error);
}
else Error=0;
}
else Resultat = OrderDelete( Ticket);// Óäàëåíèå îòëîæåííîãî îðäåðà
if(Resultat!=true)
{
Error=GetLastError();
Alert("Îøèáêà ¹ = ",Error);
}
else Error=0;
break; // Âûõîä èç öèêëà çàêð
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments