Close Profitable Trades Only, at Profit Target - page 3

 
af1:

Hi GumRai, thanks for your time. I've already tried with your changes, but orders keep closing at 1. In other words, profitable orders are not waiting to reach 25.

Even using the line of code previously supplied by DeVries and Raptor?

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

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

    if(ClosePendingOnly) ClosePendingOrdersOnly();

Hopefully you didn't change this line, you were considering it

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

If you did change it to 25.00, then you will have all profitable trades of $1+ closed

 
GumRai:

Even using the line of code previously supplied by DeVries and Raptor?

Hopefully you didn't change this line, you were considering it

If you did change it to 25.00, then you will have all profitable trades of $1+ closed


I've tried all the suggestions, but still not closing right.

 
af1:


I've tried all the suggestions, but still not closing right.


Maybe you can show the complete code that you are using at the moment
 
GumRai:

Maybe you can show the complete code that you are using at the moment

//|$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//|              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 = true; // 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 && ProfitTarget == 0.0) CloseAllinProfit();
    
    if(BuyProfit+SellProfit >= ProfitTarget && !CloseProfitableTradesOnly) CloseAll();
    
    if(CloseProfitableTradesOnly && BuyProfit+SellProfit >= ProfitTarget) CloseAllinProfit(); 

    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()

 


 

Please refer back to my post

https://forum.mql4.com/56959/page2#822980

You are not adding up profitable trades only. You are calculating net profit or loss

 
GumRai:

Please refer back to my post

https://forum.mql4.com/56959/page2#822980

You are not adding up profitable trades only. You are calculating net profit or loss



GumRai, I've made the code changes suggested by Raptor and by you:

https://www.mql5.com/en/forum/146091
https://www.mql5.com/en/forum/146091/page2#822980

And now it seems to closing right. Thanks for that!


Just one more thing. If I wish to calculate only the profitable buys, or only the profitable sells trades, instead of calculating all the profitable trades, what should I change?.

I'm thinking to this change to calculate only buys, but I'm not sure. Is this correct?:

from this:

if(OrderType()==OP_SELL) 
         {
            OrdersSELL++;
            SellLots += OrderLots(); 
            ThisTradeProfit=OrderProfit() + OrderCommission() + OrderSwap();
            if(ThisTradeProfit>0)
            BuyProfit += ThisTradeProfit;
         }

to this:

//if(OrderType()==OP_SELL) 
         {
            //OrdersSELL++;
            //SellLots += OrderLots(); 
            //ThisTradeProfit=OrderProfit() + OrderCommission() + OrderSwap();
            //if(ThisTradeProfit>0)
            //BuyProfit += ThisTradeProfit;
         }
 

I guess that you could add 2 extern bool to the input parameters, called something like "BuyTradesOnly" and "SellTradesOnly" both set to false initially

then

if(OrderType()==OP_SELL && BuyTradesOnly == false)
         {
            OrdersSELL++;
            SellLots += OrderLots(); 
            ThisTradeProfit=OrderProfit() + OrderCommission() + OrderSwap();
            if(ThisTradeProfit>0)
            BuyProfit += ThisTradeProfit;
         }
 
GumRai:

I guess that you could add 2 extern bool to the input parameters, called something like "BuyTradesOnly" and "SellTradesOnly" both set to false initially

then


I didn't add the 2 extern bool, I just change this to close "only buy orders"

for(int i=0;i<OrdersTotal();i++)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
         if(OrderType()==OP_BUY)  
         {
            OrdersBUY++;
            BuyLots += OrderLots();
            double ThisTradeProfit=OrderProfit() + OrderCommission() + OrderSwap();
            if(ThisTradeProfit>0)
            BuyProfit += ThisTradeProfit;
         }

and change this to close "only sell orders"

for(int i=0;i<OrdersTotal();i++)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
         if(OrderType()==OP_SELL)  
         {
            OrdersSELL++;
            SellLots += OrderLots();
            double ThisTradeProfit=OrderProfit() + OrderCommission() + OrderSwap();
            if(ThisTradeProfit>0)
            BuyProfit += ThisTradeProfit;
         }

So now I have two EA's. One for close buys, and one for close sells. That's ok for me.


I've tested on both accounts, demo and real. On demo works fine, but in real account, once the profit target is reached, it will start to close positions, but then it will stop closing positions when the remaining orders drops below the profit target. This happens if the price is changing at the moment of close. So it tends to leave open profitable orders, instead of close them all (profitable orders).

I read this post from RaptorUK https://www.mql5.com/en/forum/139654 . It's something like that, but I don't really know what could be the best solution for this issue.


 
af1:


I read this post from RaptorUK https://www.mql5.com/en/forum/139654 . It's something like that, but I don't really know what could be the best solution for this issue.

The solution is given in the thread . . . that is why I created it. Count down in the loop not up.
 
RaptorUK:
The solution is given in the thread . . . that is why I created it. Count down in the loop not up.


Ok Raptor, I'll try to make that loop, but before do that, what if I just change

From this:
}
      Sleep(1000);
   }
To this:
}
      Sleep(5000);
      RefreshRates();
      continue;
   }


Could this do the job?

Reason: