Issues converting send order code from mql4 to mql5

To add comments, please log in or register
buddler
13
buddler  

Below is the method I'm using to place an order after two minutes if it doesn't go through. I've converted the larger part of it from mql4 to mql5 using the documentation on migrating from mql4 to mql5. It's the commented part that I'm not sure how I'll change to mql5 since in mql5 send orders return bool's and not int's. I also realised that the if block:


if (ObjectGetString(0, name, OBJPROP_NAME, 0)==OBJ_RECTANGLE && ObjectGetString(0,name,OBJPROP_TEXT)=="") {}

 which was originally 

if (ObjectType(name)==OBJ_RECTANGLE && ObjectDescription(name)=="") {}

in mql4 doesn't print the if statement. I'm not sure what's preventing it. I guess that's the major issue with my code. I would really appreciate help to get it working. Please  see the complete method below:

void BeginNewOrder()

{

   static datetime prevTime = 0;

   datetime nowTime = iTime(Symbol(),PERIOD_M2,0);

   
   if (nowTime>prevTime)

   {

      for (int i=ObjectsTotal(0, 0, -1)-1; i>=0; i--)

      {

         string name = ObjectName(0, i, 0, -1);

         if (ObjectGetString(0, name, OBJPROP_NAME, 0)==OBJ_RECTANGLE && ObjectGetString(0,name,OBJPROP_TEXT)=="")

         {

            double startPrice=ObjectGetDouble(0,name,OBJPROP_PRICE,1); 

            double sLoss=ObjectGetDouble(0,name,OBJPROP_PRICE,2); 

            double diffInPriceAndSL =fabs(startPrice-sLoss); 

            double myTP =startPrice-2*diffInPriceAndSL;
            
            MqlTradeRequest request={0};
            MqlTradeResult  result={0};
            //--- parameters of request
            request.action   =TRADE_ACTION_DEAL;                     // type of trade operation
            request.symbol   =Symbol();                              // symbol
            request.volume   =lotSize;                                   // volume of 0.1 lot
            request.type     =ORDER_TYPE_BUY_LIMIT;                        // order type
            request.price    = startPrice; // price for opening
            //request.deviation=5;                                     // allowed deviation from the price
            request.magic    =mNumber;                          // MagicNumber of the order
            request.tp       = myTP;
            request.sl       = sLoss;
            //--- send the request
            if(!OrderSend(request,result))
             PrintFormat("OrderSend error %d",GetLastError());
             
             OrderSend(request,result);
            Print("place SELLLIMIT");

            /*
            int ticketVal = OrderSend(Symbol(),OP_SELLLIMIT,lotSize, startPrice,0,sLoss,myTP,"SellOrder",mNumber,0,Red);           
            
            if (ticketVal>0)

            {

               ObjectSetText(name,string(ticketVal));

               i = ObjectsTotal()-1; 

            }
               */
         }

      }

      prevTime = nowTime;

   }

}
Marco vd Heijden
Moderator
12488
Marco vd Heijden  

Indeed it can be confusing because Object Name is not the same as Object Type Name.

Object Type is OBJ_RECTANGLE but the individual object name can be "Rectangle_1", "Rectangle_2" and etc.

ObjectGetString(0,name,OBJPROP_TEXT)=="")

You can see here https://www.mql5.com/en/docs/constants/objectconstants/enum_object_property#enum_object_property_string

The name of the object is not the type of the object but the string identifier of the (individual) object name.

You can see this when you look at the ObjectCreate() Function.

ObjectCreate

The function creates an object with the specified name, type, and the initial coordinates in the specified chart subwindow. During creation up to 30 coordinates can be specified.

bool  ObjectCreate(
   long         chart_id,      // chart identifier
   string       name,          // object name == "obj_1","obj_2","obj_3"...
   ENUM_OBJECT  type,          // object type == OBJ_RECTANGLE
   sub_window   nwin,          // window index
   datetime     time1,         // time of the first anchor point
   double       price1,        // price of the first anchor point
   ...
   datetime     timeN=0,       // time of the N-th anchor point
   double       priceN=0,      // price of the N-th anchor point
   ...
   datetime     time30=0,      // time of the 30th anchor point
   double       price30=0      // price of the 30th anchor point
   );

Look at these two parameters in Yellow.

name = the object name "obj_1" , "obj_2" and etc, this is a different identifier for every OBJ_RECTANGLE on the chart.

type = OBJ_RECTANGLE so this is the object type name, this is the same type name for all OBJ_RECTANGLES on the chart.


So in essence:

ObjectName

The function returns the name of the corresponding object in the specified chart, in the specified subwindow, of the specified type.

string  ObjectName(
   long  chart_id,           // chart identifier
   int   pos,                // number in the list of objects
   int   sub_window=-1,      // window index
   int   type=-1             // object type
   );

It should really be called ObjectType() Function to prevent accidental mixing of these two parameters.

Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Properties
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Properties
  • www.mql5.com
All objects used in technical analysis are bound to the time and price coordinates: trendline, channels, Fibonacci tools, etc. But there is a number of auxiliary objects intended to improve the user interface that are bound to the always visible part of a chart (main chart windows or indicator subwindows): – defines the chart corner relative...
To add comments, please log in or register