Count in price direction

 

Hi all,

 I need to trail price once it passes a MinProfit and then when it bounces back and hit Trail value we have a close.

I've already gained a lot of support from WHRoeder,but still to do not understand how to deal with this issue and am afraid to get more of WHRoeder precious time.

Follow is the code that I've to the moment;

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

 

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 Trail;
          
          if(TotalOrders <= 0 || TotalOrderProfit <= MinProfit) Trail = 0;
          else
          if(TotalOrderProfit > MinProfit)
          {
           Trail = TotalOrderProfit - ProfitToProtect;
          }
          else
          if(TotalOrderProfit <= Trail) CloseAll();
                                                          
       }//0 

Could you help me here ?

Thank you in advance.

Luis 

 
luisneves:

Hi all,

 I need to trail price once it passes a MinProfit  . . . . 

I guess you mean profit not price ?  How do you keep track that this has already happened ?
 

  Hi RaptorUK,

  Before all thank you to keep attention to my post.

  Yes, you are right, I mean profit.

  What is happening is that I need that Trail is 0 unless profit is more than MinProfit and here Trail do not start up. Making use of static double Trail I understood that mean that every time a tick comes Trail should be the last one (TotalOrderProfit - ProfitToProtect) value. So from here if TotalOrderProfit bounce back and once it is less than Trail a close should happen.

  Well this is very easy to say, but I can't put that to work with this code and I am not see any other way...

  Luis 

 
Risk = TotalOrderProfit - ProfitToProtect = OrderLots() * DeltaValuePerLot() * ( OrderClosePrice() - OrderStopLoss() )
double DeltaValuePerLot(string pair=""){
   if(pair == "") pair = Symbol();
   //{Value in account currency of a Point of Symbol.
   // In tester I had a sale: open=1.35883 close=1.35736 (0.0147)
   // gain$=97.32/6.62 lots/147 points=$0.10/point    or $1.00/pip.
   // IBFX demo/mini       EURUSD TICKVALUE=0.1 MAXLOT=50 LOTSIZE=10,000
   // IBFX demo/standard   EURUSD TICKVALUE=1.0 MAXLOT=50 LOTSIZE=100,000
   //                      $1.00/point    or $10.0/pip.
   //
   // https://forum.mql4.com/33975 CB: MODE_TICKSIZE will usually return the same
   // value as MODE_POINT (or Point for the current symbol), however, an example
   // of where to use MODE_TICKSIZE would be as part of a ratio with
   // MODE_TICKVALUE when performing money management calculations which need to
   // take account of the pair and the account currency. The reason I use this
   // ratio is that although TV and TS may constantly be returned as something
   // like 7.00 and 0.0001 respectively, I've seen this (intermittently) change
   // to 14.00 and 0.0002 respectively (just example tick values to illustrate).
   // https://forum.mql4.com/43064#515262 zzuegg reports for non-currency DE30:
   // MarketInfo(pair,MODE_TICKSIZE) returns 0.5
   // MarketInfo(pair,MODE_DIGITS)   return 1
   // Point    = 0.1
   // Prices to open must be a multiple of ticksize
   //}
   return( MarketInfo(pair, MODE_TICKVALUE)
         / MarketInfo(pair, MODE_TICKSIZE) ); // Not Point.
}
 

Hi RaptorUk,

 

  I know...you start to hate me...

So, my best attempt so far is the follow code;

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

 

string SettingsComment ="MinProfit: "+TotalOrderProfit+ " Profit: "+Profit+ " Last Trail: " +LastTrail+ "  Trail: "+Trail;
  Comment(SettingsComment); 

 

 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                             
               TotalOrderProfit = OrderProfit() + OrderCommission() + OrderSwap();                                                        
              }//3          
            }//2
          }//1                                                                 
        if(TotalOrderProfit >= MinProfit)StartToTrailPrice();
//----                                
       }//0   
//-----------------------------------------------------------------------------+
 
  void StartToTrailPrice()
//---- 
       {//0
       Trail = Profit - 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 LastTrail;
           if(TotalOrders == 0) Trail = 0;                                         
           else
           if(TotalOrderProfit > MinProfit )
           {
            LastTrail = Trail;
           }            
           else
           if(TotalOrderProfit <= LastTrail)CloseAll();     
           Print("profit : ",Profit);                                                                                             
 //----                             
       }//0  

 

With this code I get to trail profit after it reaches the MinProfit. What I cannot have reach is the way to get LastTrail to work. Could you give a help her ?

Thank you in advance.

Luis 

 
luisneves:

Hi RaptorUk,

 I know...you start to hate me...

So, my best attempt so far is the follow code;

 With this code I get to trail profit after it reaches the MinProfit. What I cannot have reach is the way to get LastTrail to work. Could you give a help her ?

Thank you in advance.

Luis 

  hate is a very strong word . . . and an incorrect one in this case.  I don't have any time right now . . .  take a look at the attached,  I modified it for someone over at ForexFactory, it might give you some ideas . . .

Files: