Close orders that are in loss

 

Hi all,

I'm looking to close orders that are in loss once account profit is in profit.

The code that I have is as follow. Does anyone give me a help ?

Thank you in advance.

Luis

 void GetTotalOrderProfit()
       {//0
       TotalOrderProfit = 0;
       int TotalOrders;
       
       for(int Orders = OrdersTotal()-1; Orders >= 0; Orders--)       
          {//1
          if(!OrderSelect(Orders,SELECT_BY_POS,MODE_TRADES))continue;
            {//2
            if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)            
              {//3
               TotalOrders++;
               TotalOrderProfit += OrderProfit() + OrderCommission() + OrderSwap();                                       
              }//3          
            }//2
          }//1
       // if(TotalOrderProfit > 0.02)CloseOrderInLoss();   
        if(TotalOrderProfit > Profit * TickValue)CloseAll();               
        }//0
     
 //---------------------------------------------------------------------------
     
  void CloseOrderInLoss()
     {//0
     
      for(int OrderInLoss = OrdersTotal()-1; OrderInLoss >= 0; OrderInLoss--)
        {//1    
         if(!OrderSelect(OrderInLoss, SELECT_BY_POS, MODE_TRADES))continue;
           {//2
           if(OrderMagicNumber()== MagicNumber && OrderSymbol()== Symbol())
            {//3
             if(OrderProfit() < 0)
              {//4
             CloseAll();
               break;
              }//4                                          
            }//3               
           }//2
        }//1       
     }//0  
 

1- You need a function to calculate overall profit. It returns TotalProfit.

2- You need a function to Close Only the loss trades.

3- If TotalProfit > xxx Value Then Close Trades In Loss.

 
codersguru:

1- You need a function to calculate overall profit. It returns TotalProfit.

2- You need a function to Close Only the loss trades.

3- If TotalProfit > xxx Value Then Close Trades In Loss.


Hi codersguru,

Before all thank you for your prompt response to my issue.

Regarding your advise the fist function should return the trade profit for the ea, I mean, the TotalOrderProfit. so when the TotalOrderProfit is above 0.02 the function that should look for open orders that are in loss is called.

I have changed the CloseAll() by OrderClose() but seems that doesn't work.

Could you show me where is my mistake ?

Thanks in advance for any support provided.

Luis 

 
luisneves:


Hi codersguru,

Before all thank you for your prompt response to my issue.

Regarding your advise the fist function should return the trade profit for the ea, I mean, the TotalOrderProfit. so when the TotalOrderProfit is above 0.02 the function that should look for open orders that are in loss is called.

I have changed the CloseAll() by OrderClose() but seems that doesn't work.


Please show your modified code.
 
RaptorUK:

Please show your modified code.


Hi RaptorUK,

Thank you for keep attention to my issue.

Follow is what I've done;

 

void GetTotalOrderProfit()
       {//0
       TotalOrderProfit = 0;
       int TotalOrders;
       
       for(int Orders = OrdersTotal()-1; Orders >= 0; Orders--)       
          {//1
          if(!OrderSelect(Orders,SELECT_BY_POS,MODE_TRADES))continue;
            {//2
            if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)            
              {//3
               TotalOrders++;
               TotalOrderProfit += OrderProfit() + OrderCommission() + OrderSwap();                                       
              }//3          
            }//2
          }//1
        if(TotalOrderProfit > 0.02)CloseOrderInLoss();   
        if(TotalOrderProfit > Profit * TickValue)CloseAll();               
        }//0
     
 //---------------------------------------------------------------------------
     
  void CloseOrderInLoss()
     {//0
     
      for(int OrderInLoss = OrdersTotal()-1; OrderInLoss >= 0; OrderInLoss--)
        {//1    
         if(!OrderSelect(OrderInLoss, SELECT_BY_POS, MODE_TRADES))continue;
           {//2
           if(OrderMagicNumber()== MagicNumber && OrderSymbol()== Symbol())
            {//3
             if(OrderProfit() < 0)
              {//4
               OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(),Slippage*pips2points, Yellow);<------------------ this is the code instead of CloseAll();
               break;
              }//4                                          
            }//3               
           }//2
        }//1       
     }//0  

 

Once again thank you for any support provided

Luis 

 
luisneves:


Hi RaptorUK,

Thank you for keep attention to my issue.

Follow is what I've done;

You should already know what I am going to say next . . .    check your return value from the OrderClose() and report any error and any useful variables to help you debug,  Bid, Ask, etc, etc.

Is the OrderClose() being called and failing or is it not being called at all ?  add some Print() statements to find out . . .  You may want to add a check to make sure you are not trying to Close pending orders,  although the test for OrderProfit() < 0 should filter out any pending orders. 

The break will exit the for loop,  so if you have more than one order to be closed only the first will be closed . . . 

 
  1. {//4
       OrderClose(...
    What are Function return values ? How do I use them ? - MQL4 forum

    Check the return code and print out the reason it fails so you find out WHY. Add print statements so you know it entered the function and what ticket/profit values it found.
  2.       if(TotalOrderProfit > Profit * TickValue)CloseAll();
    
    TOP is currency (dollars, euros, etc.) If Profit is currency amount, why do you have tickValue? If Profit is the number of points (a target) you should rename the variable.
  3. You should name your functions per what they do. GetX() should return a value, it shouldn't do other things
    Your function
    void GetTotalOrderProfit()                             <=== void function can't get anything
    {//0
     TotalOrderProfit = 0;
     int TotalOrders;                                      <=== value never used, drop.
           
     for(int Orders = OrdersTotal()-1; Orders >= 0; Orders--)
       :
     if(TotalOrderProfit > 0.02)CloseOrderInLoss();        <=== these two lines
     if(TotalOrderProfit > Profit * TickValue)CloseAll();  <=== don't GET anything
    Modular code
    double GetTotalOrderProfit()
    {//0
      TotalOrderProfit = 0;
      for(int Orders = OrdersTotal()-1; Orders >= 0; Orders--)
         :
      return( TotalOrderProfit );                         <=== Get now returns a value
    }
    void CheckExitConditions(){
       double TotalOrderProfit = GetTotalOrderProfit();
       if(TotalOrderProfit > 0.02)CloseOrderInLoss();
       if(TotalOrderProfit > Profit * TickValue)CloseAll()
Reason: