Please help me to close order of the ea once there is a certain profit

 

Can someone please help me to to close the order.

I am using my own simple ea to place order in hedging pair.

I am still very fresh in writing ea, i use 1 window with ea to place buy order for EURUSD, at the same time, another window to place buy order for USDCHF with same ea.

I heard the Expert>Script can close order when at the same time, I am thinking this script can close order at reaching the profit that i have made?

Please Help, thanks

 

From your subject line, I gather you always want orders to be closed once they reach a certain profit level.

Why not just use the TakeProfit parameter in the OrderSend() function?

https://docs.mql4.com/trading/OrderSend


CB

 
FES:

Can someone please help me to to close the order.

I am using my own simple ea to place order in hedging pair.

I am still very fresh in writing ea, i use 1 window with ea to place buy order for EURUSD, at the same time, another window to place buy order for USDCHF with same ea.

I heard the Expert>Script can close order when at the same time, I am thinking this script can close order at reaching the profit that i have made?

Please Help, thanks

Hi..

create this function:

configurate

bool fechaPosicoesLong()
{
   double price;
   bool   result;
   int error;
   double gain = TakeProfit * Point;
   for (int i=0; i<=OrdersTotal()-1; i++)
   {
      price = 0;
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()&&OrderMagicNumber()==MagicNumber)
         if (OrderProfit() > 0)
         {
            RefreshRates();
            if (OrderType()==OP_BUY)
            {
               if ((Ask-OrderOpenPrice()) >= gain)
               {
                  price=Bid;
                  result=OrderClose(OrderTicket(),OrderLots(),price,3,CLR_NONE);
                  if(result!=TRUE)
                  {
                     error=GetLastError();
                     Print("Erro = ",error);
                  }
                  else
                     error=0;
                  if(error==135 || error==138)
                     RefreshRates();
                  else
                     break;
               }
            }
            else
            if (OrderType()==OP_SELL)
            {
               if ((OrderOpenPrice()-Bid >= gain))
               {
                  price=Ask;
                  result=OrderClose(OrderTicket(),OrderLots(),price,3,CLR_NONE);
                  if(result!=TRUE)
                  {
                     error=GetLastError();
                     Print("Erro = ",error);
                  }
                  else
                     error=0;
                  if(error==135 || error==138)
                     RefreshRates();
                  else
                     break;              
                       
               }
            }
         }
     
   }
}

configurate the gain in an external field with the name "TakeProfit"

double gain = TakeProfit * Point;

and put it after the "int start()"

   fechaPosicoesLong();

the system will verify if there is some profit trade with the price in take profit (or more), then, the system will close the order.

Best regards,

Dan(aka BearNaked)

Reason: