IDT_close_profit_positions
Download scriptThis script is a specialized automated tool designed for MetaTrader 4 users who want to quickly manage their active trades by identifying and closing only those that are currently in a profitable state. It acts as a "one-click" solution to lock in gains across multiple open positions simultaneously.
How the Logic Works
When you run this script, it performs a systematic review of your trading account. Here is the step-by-step process it follows:
1. Initial Audit The tool begins by counting how many total positions are currently open in your account. It plays a notification sound to signal that the process has started and then prepares to scan each trade one by one.
2. Individual Trade Review The script moves through your list of active trades starting from the most recent. For every trade it encounters, it checks two specific pieces of information:
- Profitability: It looks at the current profit figure of the position.
- Trade Direction: It identifies whether the trade is a "Buy" or a "Sell" order.
3. Selective Closing Logic The script is programmed to only take action if a trade is showing a positive profit (greater than zero). If a trade is currently losing money or is at break-even, the script ignores it completely.
If a profitable trade is identified, the script then determines the correct price to execute the closure based on the direction:
- For Buy Positions: It identifies the current market "Bid" price to exit the trade.
- For Sell Positions: It identifies the current market "Ask" price to exit the trade.
4. Execution and Reporting Once a profitable trade is closed, the script keeps an internal tally of how many successful closures have been made. This ensures that it doesn't get "stuck" on a single order and continues until every open position has been evaluated.
5. Completion Notification After the script has finished scanning all open positions, it provides a final summary in the system log showing the total number of trades closed. It then plays a confirmation sound to alert you that the task is complete.
Summary for the User
In simple terms, this tool serves as an automated "profit harvester." Instead of you having to manually check every trade to see if it has hit your target and clicking "Close" on each one individually, this script scans your entire account instantly and closes only the winners, leaving your losing trades untouched.
//+------------------------------------------------------------------+
//| IDT_close_profit_positions.mq4 |
//| Copyright © 2004, Globus |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Globus"
#include <stdlib.mqh>
#include <WinUser32.mqh>
#define SLIPPAGE 1 // value of slippage
//+------------------------------------------------------------------+
//| script "intraday trading - close all profit positions" |
//+------------------------------------------------------------------+
int start()
{
PlaySound("Tick.wav");
double sum=OrdersTotal();
double change=0;
Print ("Count of ticket ",sum);
while (sum>0)
{
OrderSelect(sum-1,SELECT_BY_POS);
Print ("Ticket : ",OrderTicket(), " Profit :" ,OrderProfit(), " Order type :", OrderType());
RefreshRates();
if (OrderProfit()>0)
{
if (OrderType ()==0)
{
OrderClose( OrderTicket(), OrderLots(), Bid, SLIPPAGE, SeaGreen);
}
if (OrderType ()==1)
{
OrderClose( OrderTicket(), OrderLots(), Ask, SLIPPAGE, SeaGreen);
}
change++;
}
sum--;
}
Print ("Count of changes ",change);
PlaySound("Ok.wav");
return(0);
}
//+------------------------------------------------------------------+
Comments