How to execute function only once?

 

Hello I'd like to execute this function only once.


Example: I open 2 lots. When the market get the "Take Profit", I'd like to close

half of the position. In this case, 1 lot. The probleme are each time the condition are meet,

the function closed half of the position again and again. 2 lots => 1 lot => 0.5 lot until 0.


I'd like to close half position at "Take Profit" and close the second half when other condition

are meet.

//---- Function First Take Profit
for(i=0;i<Total;i++) 
  { 
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES==true))
     { 
     if(PipsVal>=TakeProfit && NextTP<1)
     {
     if(OrderType()==1) OrderClose(Ticket,CloseLots,Ask,3,DarkViolet);
     else OrderClose(Ticket,CloseLots,Bid,3,DarkViolet);
     NextTP=1;// I try this to execute only once
     }
    } 
  }
//---- End Function 

Thank's

pgforex

 

Firstly - get rid of the i++ and change the loop to decrement rather than increment. Its been said umpteen times on this forum. As you close orders in the pool, the remaining orders get re-indexed. If you are running an incrementing loop, then you will miss certain orders.


Secondly, "how to do something just once" is probably the most asked question on this forum. If only we could find a way to have that question asked "only once". The answer is to create a boolean (true or false) variable which stores whether or not a certain operation has been completed. So - in this specific case, you'd check for the value of the bool in your if statement. And you'd set it once you close your order.


CB