This script is designed to provide traders with a fast and intuitive way to manage their active positions within the MetaTrader platform. Instead of manually navigating through menus to find and close specific trades, this tool allows for a "drag and drop" interaction directly on the trading chart.
How the Logic Works
The core functionality revolves around identifying which trade you want to exit based on your physical interaction with the chart:
1. Detection via Interaction When you drop the script onto the chart, the software captures the specific price level at the exact coordinates where you released it. It then scans all your currently open trades to see if any of them were opened near that price point.
2. Precision Filtering To ensure accuracy, the script looks for orders on the current symbol that fall within a very small margin (a predefined "window") of the drop location. This prevents the script from accidentally closing orders that are far away from where you intended to click.
3. Safety Confirmation Dialogs The script includes built-in safeguards to prevent accidental trades:
- Single Match: If the script identifies one specific trade near your drop point, it will pop up a confirmation box showing that trade's ID number. It will only close the position if you click "OK."
- Multiple Matches: In cases where several trades were opened at nearly the same price and you drop the script over them, the software will list all detected tickets. You can then choose specifically which ones to close via a series of prompts.
4. Execution Once you confirm your selection through the pop-up window, the script automatically handles the technical details—calculating the current market price (Bid or Ask) and executing the closure for the correct volume. Upon a successful exit, it provides an audible notification to confirm the action was completed.
Summary for the User
Essentially, this tool turns your trading chart into an interactive workspace. It allows you to "point and click" to exit positions, significantly reducing the number of clicks required to manage your portfolio while maintaining a safety layer that asks for your final approval before any trade is actually closed.
//+------------------------------------------------------------------+
//| CloseOnChart.mq4 |
//| Copyright © 2006, Igor Morozov |
//| "" |
//+------------------------------------------------------------------+
// This script can be used to quickly close a trade by dragging
// and dropping over it.
#property copyright "Igor Morozov"
#property link ""
//----
#include <WinUser32.mqh>
//+------------------------------------------------------------------+
//| script "close position where script was dropped" |
//+------------------------------------------------------------------+
int start()
{
int ret = 0;
double target = PriceOnDropped();
double within = 2.0;
int OpenOrdersBuffer[1];
ArrayResize(OpenOrdersBuffer, OrdersTotal());
ArrayInitialize(OpenOrdersBuffer, 0);
int i, j = 0;
for(i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS))
{
if(OrderSymbol() != Symbol())
continue;
if((OrderOpenPrice() >= target - within*Point) &&
(OrderOpenPrice() <= target + within*Point))
{
OpenOrdersBuffer[j] = OrderTicket();
j++;
}
}
}
if(j > 1)
{
for(i = 0; i < j; i++)
{
if(MessageBox("Multiple Tickets Found\nClose Ticket " +
DoubleToStr(OpenOrdersBuffer[i],0), "Close Order",
MB_YESNO|MB_ICONQUESTION) == IDYES)
{
if(OrderSelect(OpenOrdersBuffer[i], SELECT_BY_TICKET))
if(OrderClose(OrderTicket(), OrderLots(), GetPrice(OrderType()), 2))
PlaySound("ok.wav");
}
}
}
else
if(j > 0)
{
if(MessageBox("Will Close Ticket " + DoubleToStr(OpenOrdersBuffer[0], 0),
"Close Order", MB_OKCANCEL|MB_ICONQUESTION) == IDOK)
{
if(OrderSelect(OpenOrdersBuffer[0], SELECT_BY_TICKET))
if(OrderClose(OrderTicket(), OrderLots(), GetPrice(OrderType()), 2))
PlaySound("ok.wav");
}
}
return(ret);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double GetPrice(int ordertype)
{
switch(ordertype)
{
case OP_BUY: return(Bid);
case OP_SELL: return(Ask);
}
}
//+------------------------------------------------------------------+
Comments