Close_Basket_Profit

Download script
Author: Copyright � 2006, Robert Hill.
Close_Basket_Profit

This script is designed as an automated management tool for your trading account, specifically focused on "basket" profit taking. Instead of looking at each individual trade in isolation, this tool evaluates the combined performance of all your currently open positions to determine when to exit the market.

How the Logic Works

1. Monitoring Total Account Performance The script constantly scans all your active trades to calculate a running total of your current profit or loss. It aggregates these figures into a single sum, representing your total exposure at that moment.

2. The Profit Milestone You can define a specific dollar amount as your "Profit Target." The script acts as an automated gatekeeper; it waits until the combined profit from all your open trades meets or exceeds this target value before taking any action.

3. Sequential Execution Strategy Once the total profit target is reached, the script follows a two-step execution process to clear your positions:

  • Step One: Offsetting Losses The system first identifies any individual trades within your basket that are currently in a negative (losing) state. It will close these losing positions first. This is designed to "lock in" the gains from your winning trades by removing the drag of the underperforming ones.

  • Step Two: Final Liquidation After the losing trades have been closed, the script moves to the remaining open positions—which are now your profitable trades. It proceeds to close every remaining position one by one until your entire basket is cleared.

Key Features

  • Automated Alerts: If the script encounters any issues while attempting to close a trade (such as a connection glitch or price movement), it will provide an on-screen alert and wait briefly before trying again.
  • Basket Management: Rather than requiring you to manually monitor every single trade, this tool allows you to manage your risk globally based on a specific financial goal.
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
Miscellaneous
It issuies visual alerts to the screen
17 Views
0 Downloads
0 Favorites
Close_Basket_Profit
//+------------------------------------------------------------------+
//|                                         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