Close order setting a specific price

 

Hello guys,


Someone could help, I don't know how to close an order at a specific price. For example, I say I want to close my order at 1.1850, this value is an external input. My problem is that when I set the value, my script closes my order instantly regardless of the price that I had set (in example 1.1850)


if(OrderSelect(tikect, SELECT_BY_TICKET)==true){ ///////////here i select the order that i want to close by tiket

      if(Digits==3 || Digits==5){

      Slippage=Slippage*10;

      }

         RefreshRates();

         bool res=false;

            if(OrderType()==OP_BUY) {   // here i identify if the order is buy or sell                  

                  double ClosePrice=NormalizeDouble(save,5);

                  res=OrderClose(tikect,OrderLots(),ClosePrice,Slippage,CLR_NONE);  ////////////here i must close de order depending the value that i write as extern input in the value "save" for example 1.1850        

            }            

            if(OrderType()==OP_SELL) {

                  

                  double ClosePrice=NormalizeDouble(save,5);

                  res=OrderClose(tikect,OrderLots(),ClosePrice,Slippage,CLR_NONE);  ///////// here i must close de order depending the value that i write as extern input in the value "save"

                  }

            if(res==false) Print("ERROR - Unable to close the order - ",OrderTicket()," - ",GetLastError());      

}


 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button


 
cristian polanco: I don't know how to close an order at a specific price.
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. You don't. Either you set the TP to that price and it will close itself if and when the market reaches the price, or you wait for the market to reach the specific price and then close it.

  3. You don't check if the order you select (by ticket) has already been closed.

  4. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect / Position select loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.

    On a network disconnection you might get ERR_NO_RESULT or ERR_TRADE_TIMEOUT. There is nothing to be done, but log the error, return and wait for a reconnection and a new tick. Then reevaluate.

Reason: