Download MetaTrader 5

trying to reference entry price, to generate stop loss based on price percent change

To add comments, please log in or register
tsferro2 2016.06.07 14:47 

hi all - I am trying to reference entry price (to generate a stop loss based on i.e. -1.5% adverse price change).

I understand I need to use OrderSelect() and then OpenOrderPrice(),  but I am confused as to how to proceed. Should I loop through my blotter somehow?

ultimately, I would like to generate a stop value in pips in order to pass this through to the OrderSend() stop loss argument, which should be pretty straight forward once I can reference my entry price...

Fernando Carreiro
Fernando Carreiro 2016.06.07 22:37  

I suggest you first have a look at the MANY, MANY, MANY examples of EAs in the Code Base. Obviously some are badly coded but others are well coded. You will learn from seeing "the good, the bad and the ugly".

Also, do a search on the forum. Many users have provided explanations and links to code on how to calculate risk on many occasions. Here is a recent quote from one of WHRoeder  which is also relevant (all credit goes to him):

  • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  • Account Balance * percent = RISK = |OrderOpenPrice - OrderStopLoss| * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
  • Do NOT use TickValue by itself - DeltaPerlot
  • You must normalize lots properly and check against min and max.
  • You must also check FreeMargin to avoid stop out
tsferro2 2016.06.09 19:57  

thanks FMIC - this code base looks like a great resource for various exit methods, however I am looking for a simple protective stop in this instance, and regardless, it would be good to know how to reference my entry price...

 I'm thinking something along these lines:

 int myOpenPrice(){

  int total=0;

  for(int i=OrdersTotal()-1; i >= 0; i--){


         if(OrderMagicNumber()== MagicNumber){

           return (OrderOpenPrice())



      else Print("Failed to select order",GetLastError());


return total;



I think I'm close, but not quite there yet ..... 

To add comments, please log in or register