Protect Profit

 

Hi all,

I'm looking to get a function where once total order profit get say $10, if it bounces back to $5, our open orders are closed.

I'm try to make use of the follow code, but can't get the result expected. Is that possible get some help to put me in the right direction ?

extern double ProfitToProtect        =  5.0;
       
  void EAOrderProfit()
       {//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 <= ProfitToProtect*TickValue)CloseBuy();CloseSell();                       
       }//0 

 

Thank You in advance for any support provided.

Luis 

 
luisneves:

Hi all,

I'm looking to get a function where once total order profit get say $10, if it bounces back to $5, our open orders are closed.

I'm try to make use of the follow code, but can't get the result expected. Is that possible get some help to put me in the right direction ?


Is this correct ?

TotalOrderProfit <= ProfitToProtect * TickValue

 . . . shouldn't it be

TotalOrderProfit  >=  ProfitToProtect * TickValue
 
RaptorUK:. . . shouldn't it be

neither. The OP was "order profit get say $10, if it bounces back to $5," Needs something more like

 void EAOrderProfit()
       {//0
        int TotalOrders;
        TotalOrderProfit = 0;       
       
        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        

        static double MaxOrderProfit;                                                               
        if(TotalOrders == 0)                        MaxOrderProfit = 0.;               // None open.
        else if (TotalOrderProfit > MaxOrderProfit) MaxOrderProfit = TotalOrderProfit; // Remember. 
        else                                                                                        
        if(MaxOrderProfit   >= ProfitTrigger  *TickValue)                              // Was higher
        if(TotalOrderProfit <= ProfitToProtect*TickValue)
           {                                                                                        
           CloseBuy();CloseSell();                       
           } // Don't always close sell orders.                                                     
       }//0 
 
WHRoeder:

neither. The OP was "order profit get say $10, if it bounces back to $5," Needs something more like



Hi WHRoeder,

Before all thank you for your support.

Nevertheless I should doing something wrong because I can't get orders to close(do not know if have to do with what you said about "Don't always close sell orders...").

Updating the first post I forgot to say that the ProfitToProtect  should trail the price after ProfitTrigger.

Here in gray, is my attempt to get this to work using your code and my attempt to try to get the ProfitTProtect to trail TotalOrderProfit and close once the TotalOrderProfit bounce back and hit ProfitToProtect.

Here my issue is how to get that ProfitToProtect just go in the direction of the price. 

extern double ProfitTrigger          =  10;
extern double ProfitToProtect        =   5;
extern double MinimumProfit          =  10;
extern double TrailProfit            =   5;

 

 

void EAOrderProfit()
       {//0
        int TotalOrders;
        TotalOrderProfit = 0;       
               
        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 
          static double MaxOrderProfit;
          if(TotalOrders == 0)       MaxOrderProfit = 0.;
          else if(TotalOrderProfit > MaxOrderProfit)MaxOrderProfit = TotalOrderProfit;
          else
          if(MaxOrderProfit   >= ProfitTrigger   * TickValue)
          if(TotalOrderProfit <= ProfitToProtect * TickValue)
          {
           CloseBuy();CloseSell();
           }       
        //if(TotalOrderProfit>= MinimumProfit*pips2dbl)StartToProtect();                        
       }//0 
 //-----------------------------------------------------------------------------+
 /*
  void StartToProtect()
       {//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 
          ProfitToProtect = TotalOrderProfit - TrailProfit*pips2dbl;        
          if(TotalOrderProfit <= ProfitToProtect)
          {
           CloseBuy();CloseSell();
          }                       
       }//0 
       */

 

 Without to take more of your time, when possible please clarify me.

Thank you in advance.

Luis 

 
luisneves:

do not know if have to do with what you said about "Don't always close sell orders...").

Updating the first post I forgot to say that the ProfitToProtect  should trail the price after ProfitTrigger.

Here in gray, is my attempt to get this to work using your code and my attempt to try to get the ProfitTProtect to trail TotalOrderProfit and close once the TotalOrderProfit bounce back and hit ProfitToProtect.

extern double ProfitTrigger          =  10;
:
          if(MaxOrderProfit   >= ProfitTrigger   * TickValue)
  1. Separate problem.
    Original code
    if(TotalOrderProfit <= ProfitToProtect*TickValue)CloseBuy();CloseSell();
    Same as
    if(TotalOrderProfit <= ProfitToProtect*TickValue)CloseBuy();
    CloseSell(); // Always closes sells.

  2. Get it working first, then add your trailing.
  3. TotalOrderProfit is in currency (Dollars, Euros,... ) ProfitToProtect == 10. No idea what TickValue is since A) you never showed it, B) it depends on number lots. But ProfitToProtect * TickValue must be a currency. Print your variables before and inside IF statements and find out why it's not working.
 
WHRoeder:
  1. Separate problem.
    Original code
    Same as

  2. Get it working first, then add your trailing.
  3. TotalOrderProfit is in currency (Dollars, Euros,... ) ProfitToProtect == 10. No idea what TickValue is since A) you never showed it, B) it depends on number lots. But ProfitToProtect * TickValue must be a currency. Print your variables before and inside IF statements and find out why it's not working.


Hi WHRoeder,

Thank you for your code.

I rephrase what am looking for. When TotalOrderProfit hits  MinimumProfit then we start (StarToProtect) to trail the price some pips apart.Then if TotalOrderProfit bounces back and hit ProfitToProtect a close should happen.

The code that follows is doing:

a) Detects when TotalOrderProfit hits MinimumProfit.

b) Starts the StartToProtect function.

c) StartToProtect function trails TotalOrderProfit 

extern double MinimumProfit          =  5.0;
extern double TrailProfit            =  3.0;

 

void EAOrderProfit()
       {//0
        TotalOrderProfit = 0;       
               
        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                             
               TotalOrderProfit += OrderProfit() + OrderCommission() + OrderSwap();
                Print(" TotalOrderProfit: ",TotalOrderProfit);                                        
              }//3          
            }//2
          }//1                                                                 
        if(TotalOrderProfit >= MinimumProfit)StartToProtect();                        
       }//0 
 //-----------------------------------------------------------------------------+

  void StartToProtect()
       {//0
        TotalOrderProfit = 0;                
                       
        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               
               TotalOrderProfit += OrderProfit() + OrderCommission() + OrderSwap();                                       
              }//3          
            }//2
          }//1 
           ProfitToProtect = TotalOrderProfit - TrailProfit;        
          if(TotalOrderProfit <= ProfitToProtect)CloseBuy();           
           CloseSell();
           {        
            Print("hit ProfitToProtect! Close open orders");                                                      
           }
         }//0 

 Except that:

a) When  TotalOrderProfit  bounces ProfitToProtect bounces as well until the TotalOrderProfit hits the MinimumProfit. Here this shouldn't be happening. What I mean is that the trail have to go in just the direction of profit.

b) Once TotalOrderProfit hits ProfitToProtect I have any close and the print that is after the close call gives the message "hit ProfitToProtect! Close open orders".

Could you show  how can  I make that ProfitToProtect just go in the direction profit  and point down why the close is not happening if the print show up after the close call ?

Thank you in advance for any further support provided.

Luis 

 

Hi WHRoeder,

While you are doing your up most  to show me the way to get the objective to get to have the profit protected trailing the price I regret to admit that I have been unsuccessful.

Follow is the code that I've to the moment and in the it is your idea , but I can't get it to work (I'm really an idiot...) .

extern double MinProfit              =   2;
extern double ProfitToProtect        =   1;

 

 void MinimumProfit()
//---- 
       {//0
        TotalOrderProfit = 0;             
        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
              Total = OrdersTotal();                             
               TotalOrderProfit += OrderProfit() + OrderCommission() + OrderSwap();                                                        
              }//3          
            }//2
          }//1                                                                 
        if(TotalOrderProfit >= MinProfit)StartToTrailPrice();
//----                                
       }//0   
//-----------------------------------------------------------------------------+
 
  void StartToTrailPrice()
//---- 
       {//0
       TrailedPrice = TotalOrderProfit - ProfitToProtect;      
        Profit = 0;                                    
        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++;                            
                Profit += OrderProfit() + OrderCommission() + OrderSwap();                                                        
              }//3          
            }//2
          }//   
           static double TrailedPrice;
           if(TotalOrders == 0) TrailedPrice = 0;
           else 
           if(TotalOrderProfit > TrailedPrice) TotalOrderProfit = TrailedPrice;
           else
           if(TotalOrderProfit <= TrailedPrice)CloseAll();     
           Print("orderprofit : ",Profit);                                                                                             
 //----                             
       }//0  
 //-----------------------------------------------------------------------------+

The first code detect when the price is above a minim profit and it put the second block to work. Here the goal is to have the Trailed price keep going after the price and that is ok.

Issue:

When price bounces back TrailedPrice should maintain  is last value and here is where am blocked. 

Make use of Comment I can see that when price bounces back it stop when it reaches StartToTrailPrice and so it never comes to TrailedPrice and subsequently CloseAll() is never called.

 

I really regret to keep take your time, but is that possible to get from you a support ?

Thank you in advance.

Luis Neves 

 
if maxProfit > minProfit and maxProfit - currentProfit > tooMuch exit
Reason: