Close_all_profitable_positions
Download scriptThis automated script is designed for traders using the MetaTrader platform who want to manage their open positions based on a specific financial milestone rather than just price points. Its primary purpose is to monitor your active trades and automatically close them once they reach a predefined profit goal in your preferred currency.
How it Works
The script functions as an automated "take-profit" overseer. Instead of requiring you to manually watch every trade to see when it hits a specific dollar amount, the tool scans all your currently open positions on the market.
Here is the logical flow of the process:
- Continuous Monitoring: The script looks at every active trade in your account simultaneously. It performs a complete sweep of your open positions to ensure nothing is overlooked.
- Profit Evaluation: For every trade it identifies, it checks the current floating profit. It compares this live value against a specific dollar amount that you have set as your "Profit Target."
- Automatic Execution: If a trade's profit meets or exceeds your target, the script immediately sends a command to the market to close that position. This happens instantly for both "Buy" and "Sell" types of trades.
- Error Handling: The system is built with a fail-safe mechanism. If the script attempts to close a trade but encounters an issue (such as a connection glitch or a sudden price move that prevents execution), it will trigger an alert on your screen. This notification informs you of the specific order number and the error encountered, ensuring you are aware if a manual intervention is needed.
Key Benefit
By using this logic, you remove the emotional stress of "watching the clock" on your trades. The script ensures that as soon as your financial goal for a position is met, the profit is secured automatically, allowing for more disciplined and consistent trade management.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| close-all-orders.mq4 |
//| Copyright © 2005, Matias Romeo. |
//| Custom Metatrader Systems. |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Matias Romeo."
#property link "mailto:matiasDOTromeoATgmail.com"
extern int ProfitTarget = 10; // Profit target in dollars
int start()
{
int total = OrdersTotal();
for(int i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;
switch(type)
{
//Close opened long positions
case OP_BUY : if ( OrderProfit() >= ProfitTarget) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;
//Close opened short positions
case OP_SELL : if ( OrderProfit() >= ProfitTarget) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
}
if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}
return(0);
}
Comments