Close_Basket_Profit_v1
Download scriptThis automated script is designed to manage a "basket" of open trades simultaneously, focusing on a specific total profit goal rather than looking at individual positions in isolation. Its primary purpose is to systematically clear out your entire trading portfolio once a collective monetary milestone has been reached.
How the Logic Works:
1. Profit Monitoring The script constantly scans all currently active trades across your account. It calculates the real-time combined profit or loss of every open position to determine the "Total Profit."
2. The Goal Threshold You define a specific dollar amount as your target. The script will remain passive as long as the total profit is below this number. Once the combined profit of all your trades meets or exceeds that specific dollar amount, the script triggers a two-stage closing sequence.
3. Stage One: Removing the "Laggards" Instead of closing everything at once, the script first identifies any individual trades within the basket that are currently in a losing position (negative profit). It closes these losing trades first. This is a strategic move to secure the gains from your winning trades while removing the weight of the underperforming ones.
4. Stage Two: Final Liquidation After the losing trades have been cleared, the script performs a final sweep. It identifies all remaining open positions—which are now the profitable components of your basket—and closes them immediately.
5. Error Handling To ensure reliability, the script includes a safety mechanism. If any trade fails to close due to market conditions or connectivity issues, the script will trigger an alert notification and pause briefly before attempting to continue, ensuring you are informed of any execution hiccups.
Summary for the User
Think of this tool as an automated "harvesting" system. It allows you to group multiple trades together; once that group collectively reaches your desired profit goal, the script automatically cleans up the losing trades and then cashes out all remaining positions to lock in your total profit.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| Close_Basket_Profit.mq4 |
//| Copyright © 2006, Robert Hill . |
//| Custom Metatrader Systems. |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Robert Hill."
extern int ProfitTarget = 10; // Profit target in dollars
int start()
{
double TotalProfit = 0.0;
int total, i;
bool result = false;
// Calculate total profit on all trades
total = OrdersTotal();
for(i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
TotalProfit += OrderProfit();
}
if (TotalProfit >= ProfitTarget)
{
// First close losing trades
total = OrdersTotal();
for(i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
result = false;
switch(OrderType())
{
//Close opened long positions
case OP_BUY : if ( OrderProfit() < 0) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;
//Close opened short positions
case OP_SELL : if ( OrderProfit() < 0) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
}
if(result == false)
{
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}
// Now close remaining trades
total = OrdersTotal();
for(i=total-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
result = false;
switch(OrderType())
{
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;
//Close opened short positions
case OP_SELL : 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