name of object doesnot change in mql5

 

hello 

I write a code to alarm breaking trendline and i write last alarm time in object name

ObjectSetString(currChart,object_name,OBJPROP_NAME,TimeToString(TimeCurrent()));

it is ok in mql4 but doesnot work in 5 !!

can any body help me

i use OBJPROP_TEXT for another purpose and i want to change only trendlines 's name

can anybody help me ?

 

It doesn't work, I'm not surprised since the name is an object's key.

So create a new one and delete the old one. What's the problem?

 
whroeder1:

It doesn't work, I'm not surprised since the name is an object's key.

So create a new one and delete the old one. What's the problem?

but we can rename it in mql4

and more flexibility is expected in mql5

we rename folder in windows despite it is the only representative feature of them


but it seems a good alternative to delete and  recreate, although it needs too many lines code

thank you

 
Mehrdad S:

but we can rename it in mql4

and more flexibility is expected in mql5

we rename folder in windows despite it is the only representative feature of them

  1. Never tried to.
  2. So it's a bug.
  3. Irrelevant.
 
Mehrdad S:

but we can rename it in mql4

and more flexibility is expected in mql5

we rename folder in windows despite it is the only representative feature of them


but it seems a good alternative to delete and  recreate, although it needs too many lines code

thank you

You can not rename an object in metatrader - deleting and recreating takes only 2 lines of code for a known name
 

You really should be using the CChartObject classes instead of interacting via the direct API functions... also you should not be tracking the last alert time in the name of the object... I hope this isn't offensive, but that's just lazy coding. 

#include <ChartObjects\ChartObjectsLines.mqh>
#include <Arrays\ArrayObj.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

class MyTrend : public CChartObjectTrend
{
   datetime m_last_alert;
public:
   datetime LastAlert()       const { return m_last_alert;}
   void     LastAlert(datetime time){ m_last_alert = time;}
   double   CurrentPrice()    const { return ObjectGetValueByTime(m_chart_id,m_name,TimeCurrent());}
  
   void     Alert(datetime bar_time)
   {
      m_last_alert = bar_time;
      ::Alert("Trendline ["+m_name+"] has been crossed!");
   }
   
   bool    Attach(string sparam)   
   { 
      bool res = Attach(ChartID(),sparam,0,2);
      res = RayRight(true); 
      res = Description(string(0.0));
      return res;
   }
   
   void OnTick() { Description(DoubleToString(CurrentPrice(),_Digits));}
};
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class TrendList : public CArrayObj
{
   double m_last_tick;
   double m_tick;
public:
   TrendList():m_tick(0.0){}
   MyTrend* operator[](int i)const{return At(i);}
   void  OnTick(double price,datetime bar_time)
   {
      for(int i=Total()-1;i>=0;i--)
      {
         this[i].OnTick();
         m_last_tick=m_tick;
         m_tick = price;
         if(m_last_tick != 0.0 && this[i].LastAlert()!= bar_time)
         {
             double trend_price = this[i].CurrentPrice();
             if((m_last_tick>trend_price&&trend_price>=m_tick)||(m_last_tick<trend_price&&trend_price<=m_tick))
               this[i].Alert(bar_time);
         }
      }
   }
   bool OnCreate(string sparam)
   {
      for(int i=Total()-1;i>=0;i--)
         if(this[i].Name()==sparam)
            return true;
      MyTrend *trend = new MyTrend;
      trend.Attach(sparam);
      return Add(trend);
   }
   bool OnDelete(string sparam)
   {
      for(int i=Total()-1;i>=0;i--)
         if(this[i].Name()==sparam)
            return Delete(i);
      return false;
   }
};
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+


TrendList trend_list;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   ChartSetInteger(0,CHART_EVENT_OBJECT_CREATE,true);
   ChartSetInteger(0,CHART_EVENT_OBJECT_DELETE,true);
   ChartSetInteger(0,CHART_SHOW_OBJECT_DESCR,true);
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   trend_list.OnTick(close[rates_total-1],time[rates_total-1]);
   return(rates_total);
}
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   if(id==CHARTEVENT_OBJECT_CREATE && ObjectGetInteger(0,sparam,OBJPROP_TYPE)==OBJ_TREND)
      if(trend_list.OnCreate(sparam))
         Alert("ADDED");
   if(id==CHARTEVENT_OBJECT_DELETE)
      if(trend_list.OnDelete(sparam))
         Alert("DELETED");
      
}
//+------------------------------------------------------------------+
 

thank you very much

I reported the bug in service desk

it is very useful because i have encountered bugs for 3 or 4 times before, but i only hoped sometime to be fixed

 
Mehrdad S:

thank you very much

I reported the bug in service desk

it is very useful because i have encountered bugs for 3 or 4 times before, but i only hoped sometime to be fixed

Support Team 2017.12.06 13:46
Status: Unapproved Open

Hello,

Thank you for contacting us.

If you are still using 1601 build we strongly recommend to update your terminal to the latest build 1690 from MetaQuotes-Demo server. A lot of work has been done, including the MQL5 language.

We have just rechecked:

and they put two picture that show the correct result .



I looked for update  but   didnot find it in menu

maybe we should install another time !

amazing is that my meta trader 's version is 1643 but meta editor is 1601

 
nicholishen:

You really should be using the CChartObject classes instead of interacting via the direct API functions... also you should not be tracking the last alert time in the name of the object... I hope this isn't offensive, but that's just lazy coding. 

thank you very much  nicholishen

I was interested in using standard library, but I never did before

because i haven't seen an good usage and i haven't feel need to them.

but here instead of declaring many variable for trend line's  properties and getting them from old TL and setting them in new one we can do it easily.


and you did it very well

 

I looked for update  but   didnot find it in menu

maybe we should install another time !

.

In order to get the latest build of terminal you have to open new demo account on MetaQuotes-Demo server.