Lock in pips. How to avoid multiple "lock ins"?

 

Hello there,

I created a code to lock in profit after a XX number of positive pips has been reached. The problem is that i created this code to use on EA in my live account, where i could only afford 2x the minimum lot and the code only runs if the order's lot is 2x the min_lot.

When i used this EA in a demo account with more money, the code repeated itself several times. I also noticed that the original order changes its ticket number after the lock in.

My question is more of logic than of coding, at least for now. How can i tag a particular ticket so it doesn't get "locked in" more than once?

This is my code:

void LockProf(int ord, int Pips2Lock, int myTicket)  // ord = SIGNAL_BUY, SIGNAL_SELL   
   {
      double PipsVal = NormalizeDouble(OrderLots()/2,2);
      PipsVal = NormalizeDouble(PipsVal,1);
          
      //com3 = "PipsVal : " + NormalizeDouble(PipsVal,2);
           
      RefreshRates();
      if(ord == 1 && PipsVal >= MarketInfo(Symbol(), MODE_MINLOT)) 
         {
            if(NormalizeDouble(Bid - OrderOpenPrice(),Digits) > pips2dbl * Pips2Lock) 
               {
                    // com4 = "Bid: " + NormalizeDouble(Bid,2) + " OpenPrice: " + NormalizeDouble(OrderOpenPrice(),2) + " Point * Pips2Lock: " + NormalizeDouble(Point * Pips2Lock,2); 
    
                     if(!OrderClose(OrderTicket(), PipsVal, Bid, 2, Black)) Print("Error closing half stake: "   + ErrorDescription(GetLastError()));    
// OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + Point * 1, OrderTakeProfit(), 0, Teal);     
               }
         }
         
       if(ord == 2 && PipsVal >= MarketInfo(Symbol(), MODE_MINLOT))
         { 
            if(NormalizeDouble(OrderOpenPrice()- Ask,Digits) > pips2dbl * Pips2Lock)
               {
          Comment("\n Bid: " + Bid + " OpenPrice: " + OrderOpenPrice() + " Point * Pips2Lock: " + pips2dbl * Pips2Lock); 
   
//   Print("Before close => Ticket: " + OrderTicket());          
                     if(!OrderClose(OrderTicket(), PipsVal, Ask, 2, Black)) Print("Error closing half stake: "   + ErrorDescription(GetLastError()));    
        
               }

         }
      return(0);
   }
   


Thanks

 

bentbawer:

and the code only runs if the order's lot is 2x the min_lot.

I also noticed that the original order changes its ticket number after the lock in.

My question is more of logic than of coding, at least for now. How can i tag a particular ticket so it doesn't get "locked in" more than once?

  1. Of course you have have the half size order below min_lot
  2. Of course.
  3. You need a method to determine whether you already have. I simply move the SL above BE before the partial close.
 

Hi WHRoeder,


Thanks for your reply. Just a quick clarification, if you would.

So you are saying that your technique is to move the SL above BE and then close half stake? This way you make your code know if a particular order has been "locked in" by the position of the SL in regards to the OrderOpenPrice()? 

Thank you.

Reason: