CloseOnTargetPrice
Download scriptThis script is designed to automate the management of your trading positions by automatically closing active trades or canceling outstanding orders based on specific price conditions you define. It acts as an automated "exit strategy" tool, ensuring that your trades are liquidated once they reach a predetermined level.
How the Logic Works
The script constantly monitors all open and pending activities in your trading account. It evaluates each transaction individually to determine if it meets your criteria for closing:
1. Price-Based Exit for Active Trades The primary function of this tool is to monitor your "Buy" and "Sell" positions relative to a specific price goal (the Target Price).
- For Buy Positions: If the current market price reaches or exceeds your specified target, the script will automatically trigger a command to close the position at the market rate.
- For Sell Positions: If the market price falls to or below your specified target, the script will execute the closure of the trade immediately.
2. Management of Pending Orders Beyond closing active trades, the script includes an optional feature to manage "Pending Orders" (orders that are waiting for a specific price to be hit before they activate). You can choose whether the script should automatically delete these pending orders as soon as it runs. If this option is enabled, any limit or stop orders currently sitting in your account will be cleared out.
Key Operational Details
- Safety Margin (Slippage): The logic includes a "Slippage" parameter. This allows the script a small amount of flexibility to execute a trade even if the price moves slightly during the split second it takes to process the command, ensuring your exit happens as close to your target as possible.
- Error Reporting: If the script is unable to close a trade—due to market connectivity issues or other external factors—it is designed to record an error message in the system log. This allows you to review any failed actions after the fact.
- Sequential Processing: The script reviews your orders in reverse order (from the most recent back to the oldest). This is a standard reliability practice to ensure that every single order is accounted for without skipping any entries during the loop.
Summary of Usage
Essentially, this tool removes the need to manually watch the charts to hit "Close" at the right moment. You simply set your target price and decide if you want to clear out pending orders; the script handles the rest by scanning your account and executing the necessary exits automatically.
//+------------------------------------------------------------------+
//| close-all-orders.mq4 |
//| Copyright © 2005, Matias Romeo. |
//| Custom Metatrader Systems. |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Matias Romeo."
#property link "mailto:matiasDOTromeoATgmail.com"
#property show_inputs
#include <stdlib.mqh>
#include <stderror.mqh>
extern double TargetPrice=0;
extern int Slippage=5;
extern bool ClosePendingOrder=false;
int start()
{
if(TargetPrice==0) return(0);
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = true;
//Print(i, OrderTicket(), OrderType());
switch(type)
{
//Close opened long positions
case OP_BUY : if(Bid>=TargetPrice)
result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage, Yellow);
break;
//Close opened short positions
case OP_SELL : if(Ask>=TargetPrice)
result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage, Yellow);
break;
//Close pending orders
case OP_BUYLIMIT :
case OP_BUYSTOP :
case OP_SELLLIMIT :
case OP_SELLSTOP : if(ClosePendingOrder==TRUE)
result = OrderDelete( OrderTicket() );
}
if(result == false)
{
Print("Order " , OrderTicket() , " failed to close. Error:" , ErrorDescription(GetLastError()) );
//Sleep(3000);
}
}
return(0);
}
Comments