OrderModify in MQL5

Para añadir comentario, por favor Autorícese o regístrese
David Diez
1555
David Diez  

I'm translating an own expert form MQL4 to MQL5, when I check TradeFunctions on MQL5 refrence there's no OrderModify function.

This is a piece of the code:

if(!OrderModify(OrderTicket(),0,StopLoss,OrderGetDouble(ORDER_TP),0,clrBlue)){

Thank you in advance.

karachiento
188
karachiento  
David Diez:

Estoy traduciendo una forma experta propia de MQL4 a MQL5, cuando verifico TradeFunctions en MQL5, no hay una función OrderModify.

Esta es una parte del código:

Gracias de antemano.


Puedes usar la clase cTrade (no es la unica forma)

https://www.mql5.com/es/docs/standardlibrary/tradeclasses/ctrade   


Ejemoplo:

//Al inicio, fuera de OnInit()

#include <Trade/Trade.mqh>

CTrade trade;


int OnInit()

{

....

....

....

...

if(!trade.PositionModify(ticket, sl, tp))

   {

    Print ("La orden para modificar sl y tp no se ha podido colocar!!! Error: ",GetLastError());     // si no se ha logrado enviar la solicitud, mostrar el código de error

    return;

   }

....  

}


saludos

Documentación para MQL5: Biblioteca estándar / Clases de comercio / CTrade
Documentación para MQL5: Biblioteca estándar / Clases de comercio / CTrade
  • www.mql5.com
Biblioteca estándar / Clases de comercio / CTrade - manual de usuario para el lenguaje del trading algorítmico/automático para MetaTrader 5
Marco vd Heijden
Moderador
13018
Marco vd Heijden  
#define EXPERT_MAGIC 123456   // MagicNumber of the expert 
// + ------------------------------------- ----------------------------- + 
// | Modification of Stop Loss and Take Profit of position | 
// + ----------------------------------------------- ------------------- + 
void  OnStart () 
  { 
// --- declare and initialize the trade request and result of trade request 
   MqlTradeRequest request; 
   MqlTradeResult   result; 
   int total = PositionsTotal (); // number of open positions    
// --- iterate over all open positions 
   for ( int i = 0; i <total; i ++) 
     { 
      // --- parameters of the order 
      ulong   position_ticket = PositionGetTicket (i); // ticket of the position 
      string position_symbol = PositionGetString ( POSITION_SYMBOL ); // symbol 
      int     digits = ( int ) SymbolInfoInteger (position_symbol, SYMBOL_DIGITS ); // number of decimal places 
      ulong   magic = PositionGetInteger ( POSITION_MAGIC ); // MagicNumber of the position 
      double volume = PositionGetDouble (POSITION_VOLUME );    // volume of the position 
      double sl = PositionGetDouble ( POSITION_SL );  // Stop Loss of the position 
      double tp = PositionGetDouble ( POSITION_TP );  // Take Profit of the position 
      ENUM_POSITION_TYPE type = ( ENUM_POSITION_TYPE ) PositionGetInteger ( POSITION_TYPE );  // type of the position 
      // --- output information about the position 
      PrintFormat ( "#% I64u% s% s% .2f% s sl:% s tp:% s [% I64d]" , 
                  position_ticket,
                  position_symbol, 
                  EnumToString (type), 
                  volume, 
                  DoubleToString ( PositionGetDouble ( POSITION_PRICE_OPEN ), digits), 
                  DoubleToString (sl, digits), 
                  DoubleToString (tp, digits), 
                  magic); 
      // --- if the MagicNumber matches, Stop Loss and Take Profit are not defined 
      if (magic == EXPERT_MAGIC && sl == 0 && tp == 0 ) 
        { 
         // --- calculate the current price levels 
         double price = PositionGetDouble ( POSITION_PRICE_OPEN ); 
         doublebid = SymbolInfoDouble (position_symbol, SYMBOL_BID ); 
         double ask = SymbolInfoDouble (position_symbol, SYMBOL_ASK ); 
         int     stop_level = ( int ) SymbolInfoInteger (position_symbol, SYMBOL_TRADE_STOPS_LEVEL ); 
         double price_level; 
         // --- if the minimum allowed offset distance in points from the current close price is not set 
         if (stop_level <= 0 ) 
            stop_level = 150 ; // set the offset distance of 150 points from the current close price 
         else
             stop_level + =50 ; // set the offset distance to (SYMBOL_TRADE_STOPS_LEVEL + 50) points for reliability 
 
         // --- calculation and rounding of the Stop Loss and Take Profit values
          price_level = stop_level * SymbolInfoDouble (position_symbol, SYMBOL_POINT ); 
         if (type == POSITION_TYPE_BUY ) 
           { 
            sl = NormalizeDouble (bid-price_level, digits); 
            tp = NormalizeDouble (bid + price_level, digits); 
           } 
         else
            { 
            sl = NormalizeDouble (ask + price_level, digits); 
            tp =NormalizeDouble (ask-price_level, digits); 
           } 
         // --- zeroing the request and result values 
         ZeroMemory (request); 
         ZeroMemory (result); 
         // --- setting the operation parameters
          request.action = TRADE_ACTION_SLTP ; // type of trade operation
          request.position = position_ticket;   // ticket of the position
          request.symbol = position_symbol;     // symbol
          request.sl = sl;                // Stop Loss of the position
          request.tp = tp;                // Take Profit of the position
          request.magic = EXPERT_MAGIC;         // MagicNumber of the position 
         // --- output information about the modification 
         PrintFormat ( "Modify #% I64d% s% s" , position_ticket, position_symbol, EnumToString (type)); 
         // --- send the request 
         if (! OrderSend (request, result)) 
            PrintFormat ( "OrderSend error% d" , GetLastError ());  // if unable to send the request, output the error code 
         // --- information about the operation    
         PrintFormat ( "retcode =% u deal =% I64u order =% I64u" , result.retcode, result.deal, result.order ); 
        } 
     } 
  }
// + ----------------------------------------------- ------------------- +

Example of the  TRADE_ACTION_SLTP  trade operation for modifying the Stop Loss and Take Profit values ​​of an open position.

SL & TP Modification

Trade order to modify the StopLoss and/or TakeProfit price levels. It requires to specify the following 4 fields:

  • action
  • symbol
  • sl
  • tp
  • position
Imanol Salazar Garcia
989
Imanol Salazar Garcia  
You have better example in the documentation. But the optimal idea is to use CTrade class like said "karachiento".
Para añadir comentario, por favor Autorícese o regístrese