Close Profitable Trades Only, at Profit Target

 

Hi, I'm testing this ea and I think it's very useful to close orders, but in my case I don't want to close all open orders, I just want to close only the profitable orders.

I've changed the "CloseProfitableTradesOnly" value from false to true, but orders keep closing at 1 usd. And what Im looking is to close only profitable orders when, together, reaches 25 usd. All closed orders above 1 usd of profit.

If have any suggestion I really appreciate it. Thanks in advance for your help.

Here is the ea and code:

//|$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//|              Close 
//|   Last Updated 12-12-2006 10:00pm
//|$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
#define     NL    "\n" 

extern int    ProfitTarget     = 25;             // closes all orders once Float hits this $ amount
extern bool   CloseAllNow      = false;          // closes all orders now
extern bool   CloseProfitableTradesOnly = false; // closes only profitable trades
extern double ProftableTradeAmount      = 1;     // Only trades above this amount close out
extern bool   ClosePendingOnly = false;          // closes pending orders only
extern bool   UseAlerts        = false;

//+-------------+
//| Custom init |
//|-------------+
int init()
  {

  }

//+----------------+
//| Custom DE-init |
//+----------------+
int deinit()
  {

  }

//+------------------------------------------------------------------------+
//| Closes everything
//+------------------------------------------------------------------------+
void CloseAll()
{
   int i;
   bool result = false;

   while(OrdersTotal()>0)
   {
      // Close open positions first to lock in profit/loss
      for(i=OrdersTotal()-1;i>=0;i--)
      {
         if(OrderSelect(i, SELECT_BY_POS)==false) continue;

         result = false;
         if ( OrderType() == OP_BUY)  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 15, Red );
         if ( OrderType() == OP_SELL)  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 15, Red );
         if (UseAlerts) PlaySound("alert.wav");
      }
      for(i=OrdersTotal()-1;i>=0;i--)
      {
         if(OrderSelect(i, SELECT_BY_POS)==false) continue;

         result = false;
         if ( OrderType()== OP_BUYSTOP)  result = OrderDelete( OrderTicket() );
         if ( OrderType()== OP_SELLSTOP)  result = OrderDelete( OrderTicket() );
         if ( OrderType()== OP_BUYLIMIT)  result = OrderDelete( OrderTicket() );
         if ( OrderType()== OP_SELLLIMIT)  result = OrderDelete( OrderTicket() );
         if (UseAlerts) PlaySound("alert.wav");
      }
      Sleep(1000);
   }
}
   
//+------------------------------------------------------------------------+
//| cancels all orders that are in profit
//+------------------------------------------------------------------------+
void CloseAllinProfit()
{
  for(int i=OrdersTotal()-1;i>=0;i--)
 {
    OrderSelect(i, SELECT_BY_POS);
    bool result = false;
        if ( OrderType() == OP_BUY && OrderProfit()+OrderSwap()>ProftableTradeAmount)  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
        if ( OrderType() == OP_SELL && OrderProfit()+OrderSwap()>ProftableTradeAmount)  result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
        if (UseAlerts) PlaySound("alert.wav");
 }
  return; 
}

//+------------------------------------------------------------------------+
//| cancels all pending orders 
//+------------------------------------------------------------------------+
void ClosePendingOrdersOnly()
{
  for(int i=OrdersTotal()-1;i>=0;i--)
 {
    OrderSelect(i, SELECT_BY_POS);
    bool result = false;
        if ( OrderType()== OP_BUYSTOP)   result = OrderDelete( OrderTicket() );
        if ( OrderType()== OP_SELLSTOP)  result = OrderDelete( OrderTicket() );
  }
  return; 
  }

//+-----------+
//| Main      |
//+-----------+
int start()
  {
   int      OrdersBUY;
   int      OrdersSELL;
   double   BuyLots, SellLots, BuyProfit, SellProfit;

//+------------------------------------------------------------------+
//  Determine last order price                                       |
//-------------------------------------------------------------------+
      for(int i=0;i<OrdersTotal();i++)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
         if(OrderType()==OP_BUY)  
         {
            OrdersBUY++;
            BuyLots += OrderLots();
            BuyProfit += OrderProfit() + OrderCommission() + OrderSwap();
         }
         if(OrderType()==OP_SELL) 
         {
            OrdersSELL++;
            SellLots += OrderLots();
            SellProfit += OrderProfit() + OrderCommission() + OrderSwap();
         }
      }               
   
    if(CloseAllNow) CloseAll();
    
    if(CloseProfitableTradesOnly) CloseAllinProfit();
    
    if(BuyProfit+SellProfit >= ProfitTarget) CloseAll(); 

    if(ClosePendingOnly) ClosePendingOrdersOnly();
       
   
   Comment("                            Comments Last Update 12-12-2006 10:00pm", NL,
           "                            Buys    ", OrdersBUY, NL,
           "                            BuyLots        ", BuyLots, NL,
           "                            Sells    ", OrdersSELL, NL,
           "                            SellLots        ", SellLots, NL,
           "                            Balance ", AccountBalance(), NL,
           "                            Equity        ", AccountEquity(), NL,
           "                            Margin              ", AccountMargin(), NL,
           "                            MarginPercent        ", MathRound((AccountEquity()/AccountMargin())*100), NL,
           "                            Current Time is  ",TimeHour(CurTime()),":",TimeMinute(CurTime()),".",TimeSeconds(CurTime()));
 } // start()

 


 
if((BuyProfit+SellProfit >= ProfitTarget)  && CloseProfitableTradesOnly) CloseAllinProfit();
 
af1:

Hi, I'm testing this ea and I think it's very useful to close orders, but in my case I don't want to close all open orders, I just want to close only the profitable orders.

I've changed the "CloseProfitableTradesOnly" value from false to true, but orders keep closing at 1 usd. And what Im looking is to close only profitable orders when, together, reaches 25 usd. All closed orders above 1 usd of profit.


I guess you didn't write this code . . .

    if(CloseAllNow) CloseAll();
    
    if(CloseProfitableTradesOnly) CloseAllinProfit();
    
    if(BuyProfit+SellProfit >= ProfitTarget) CloseAll(); 

    if(ClosePendingOnly) ClosePendingOrdersOnly();

. . . but you should be able to read it.

You changed "CloseProfitableTradesOnly" value from false to true, so if you look above CloseAllinProfit() is called . . . regardless of the total profit.

Try this change:

    if(CloseAllNow) CloseAll();
    
    if(CloseProfitableTradesOnly && ProfitTarget == 0.0) CloseAllinProfit();
    
    if(BuyProfit+SellProfit >= ProfitTarget && !CloseProfitableTradesOnly) CloseAll(); 

    if(CloseProfitableTradesOnly && BuyProfit+SellProfit >= ProfitTarget) CloseAllinProfit();

    if(ClosePendingOnly) ClosePendingOrdersOnly();
 

Hi Raptor, thanks for your answer.

I didn't write this code, I just change the "CloseProfitableTradesOnly" input value from false to true, like this:

I'm trying with your changes but still the ea doesn't close at profit target, (in this case 25 usd.)


Do I have to change from 0.0 to 25.0 in the code also?

if(CloseProfitableTradesOnly && ProfitTarget == 0.0) CloseAllinProfit();
 
af1:

Hi Raptor, thanks for your answer.

I didn't write this code, I just change the "CloseProfitableTradesOnly" input value from false to true, like this:

I'm trying with your changes but still the ea doesn't close at profit target, (in this case 25 usd.)


Do I have to change from 0.0 to 25.0 in the code also?


No, this line should be doing what you want, are you sure you made ALL the changes ?

    if(CloseProfitableTradesOnly && BuyProfit+SellProfit >= ProfitTarget) CloseAllinProfit();
 
I've changed from this
if(CloseAllNow) CloseAll();
    
if(CloseProfitableTradesOnly) CloseAllinProfit();
    
if(BuyProfit+SellProfit >= ProfitTarget) CloseAll(); 

if(ClosePendingOnly) ClosePendingOrdersOnly();

To this:

if(CloseAllNow) CloseAll();
    
if(CloseProfitableTradesOnly && ProfitTarget == 0.0) CloseAllinProfit();
    
if(BuyProfit+SellProfit >= ProfitTarget && !CloseProfitableTradesOnly) CloseAll(); 

if(CloseProfitableTradesOnly && BuyProfit+SellProfit >= ProfitTarget) CloseAllinProfit();

if(ClosePendingOnly) ClosePendingOrdersOnly();


And the input "CloseProfitableTradesOnly" from false, to true.


But don't close. What else could it be?

 
af1:
I've changed from this

To this:


And the input "CloseProfitableTradesOnly" from false, to true.


But don't close. What else could it be?

Perhaps your total profit is less than 25.0
 
RaptorUK:
Perhaps your total profit is less than 25.0


If was less than 25, then should close even faster.

 
af1:


If was less than 25, then should close even faster.

No, the profit has to be greater than or equal to 25.0 before it will close . . .

if(CloseProfitableTradesOnly && BuyProfit+SellProfit >= ProfitTarget) CloseAllinProfit();
 
RaptorUK:

No, the profit has to be greater than or equal to 25.0 before it will close . . .


Here is an example Raptor. I have 4 orders with 97.9 of profit. So If my profit target is 25, and the condition is "CloseProfitableTradesOnly" true, then these 4 orders should close. But is not closing any order.

 
af1:


Here is an example Raptor. I have 4 orders with 97.9 of profit. So If my profit target is 25, and the condition is "CloseProfitableTradesOnly" true, then these 4 orders should close. But is not closing any order.

No, the total profit has to be greater than equal to 25.0, yours is -59.80