trendline name check before entering

 

Is there a way to check a specific name in the trendline name property, before entering the return key or clicking OK?

My problem is to detect if name A exists, using a trendline with name B is not allowed.

Thanks for helping,

Sylvain

 

may be :

if ObjectFind("name") ...
 
Sylvain Vervoort:

Is there a way to check a specific name in the trendline name property, before entering the return key or clicking OK?

My problem is to detect if name A exists, using a trendline with name B is not allowed.

With native mql, you can't check the name before hitting the return key or clicking ok (i.e. creating the object manually).

However, using OnChartEvent(), you'll be able to detect when a new trendline is created, and rename it automatically to something you can track, and to prevent name clashes at the same time.

 
Seng Joo Thio:

With native mql, you can't check the name before hitting the return key or clicking ok (i.e. creating the object manually).

However, using OnChartEvent(), you'll be able to detect when a new trendline is created, and rename it automatically to something you can track, and to prevent name clashes at the same time.

Hello Seng,

Thanks for the fast reaction and the suggestion. I gave it a try but, until now without success.

I used some available OnChartEvent function shown below and used this to call the function to get to the name editing of a trendline object.

long   longparam = KEY_DOWN;
double doubleparam;
string stringparam = "Trendline";

  OnChartEvent(CHARTEVENT_OBJECT_ENDEDIT,longparam,doubleparam,stringparam);

What is not clear is what do you put in if dparam or lparam is not used? stringparam is I assume the name of the object, here "Trendline" for the Trendline object. 

I get the print message:  "The text in the Edit field of the object with name Trendline has been changed".

I looked also at CHARTEVENT_KEYDOWN, but again without result.

I wonder if you have some example. The idea is that I create a first manual support trendline, and call it "support" or a manual resistance trendline and call it "resistance".

If one of the two exists, it must not be possible to create the other one, because that will kill the normal functioning of my indicator.

For me the best solution would be to control the name input in the trendline property window, not accepting to create the second trendline.

Thanks again for helping...

Sylvain

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,         // Event identifier  
                  const long& lparam,   // Event parameter of long type
                  const double& dparam, // Event parameter of double type
                  const string& sparam) // Event parameter of string type
  {
//--- the key has been pressed
   if(id==CHARTEVENT_KEYDOWN)
     {
      switch(int(lparam))
        {
         case KEY_NUMLOCK_LEFT:  Print("The KEY_NUMLOCK_LEFT has been pressed");   break;
         case KEY_LEFT:          Print("The KEY_LEFT has been pressed");           break;
         case KEY_NUMLOCK_UP:    Print("The KEY_NUMLOCK_UP has been pressed");     break;
         case KEY_UP:            Print("The KEY_UP has been pressed");             break;
         case KEY_NUMLOCK_RIGHT: Print("The KEY_NUMLOCK_RIGHT has been pressed");  break;
         case KEY_RIGHT:         Print("The KEY_RIGHT has been pressed");          break;
         case KEY_NUMLOCK_DOWN:  Print("The KEY_NUMLOCK_DOWN has been pressed");   break;
         case KEY_DOWN:          Print("The KEY_DOWN has been pressed");           break;
         case KEY_NUMPAD_5:      Print("The KEY_NUMPAD_5 has been pressed");       break;
         case KEY_NUMLOCK_5:     Print("The KEY_NUMLOCK_5 has been pressed");      break;
         default:                Print("Some not listed key has been pressed");
        }
      ChartRedraw();
     }
//--- the object has been deleted
   if(id==CHARTEVENT_OBJECT_DELETE)
     {
      Print("The object with name ",sparam," has been deleted");
     }
//--- the object has been created
   if(id==CHARTEVENT_OBJECT_CREATE)
     {
      Print("The object with name ",sparam," has been created");
     }
//--- the object has been moved or its anchor point coordinates has been changed
   if(id==CHARTEVENT_OBJECT_DRAG)
     {
      Print("The anchor point coordinates of the object with name ",sparam," has been changed");
     }
//--- the text in the Edit of object has been changed
   if(id==CHARTEVENT_OBJECT_ENDEDIT)
     {
      Print("The text in the Edit field of the object with name ",sparam," has been changed");
     }
   }



 
Sylvain Vervoort:

Thanks for the fast reaction and the suggestion. I gave it a try but, until now without success.

I used some available OnChartEvent function shown below and used this to call the function to get to the name editing of a trendline object.

What is not clear is what do you put in if dparam or lparam is not used? stringparam is I assume the name of the object, here "Trendline" for the Trendline object. 

I get the print message:  "The text in the Edit field of the object with name Trendline has been changed".

No, you don't call OnChartEvent() function... the terminal will call for you, as and when changes are detected.

Sylvain Vervoort:

I looked also at CHARTEVENT_KEYDOWN, but again without result.

I wonder if you have some example. The idea is that I create a first manual support trendline, and call it "support" or a manual resistance trendline and call it "resistance".

If one of the two exists, it must not be possible to create the other one, because that will kill the normal functioning of my indicator.

For me the best solution would be to control the name input in the trendline property window, not accepting to create the second trendline.

Ok, here's an example EA (you draw a trendline and rename it as 's' or 'r', and the code will automatically rename them to 'support' or 'resistance' if and only if there wasn't any existing support or resistance line... and whether it is successfully renamed or not, the 's' / 'r' lines will be deleted right after.):

#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

const string rName = "resistance";
const string sName = "support";

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
  }

void OnTick()
  {
  }

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   if(id==CHARTEVENT_OBJECT_CHANGE && (sparam=="s" || sparam=="r"))
      UpdateTLine(sparam);
  }

void UpdateTLine(string name)
{
   long cid = ChartID();

   if (ObjectFind(cid,sName)==0 || ObjectFind(cid,rName)==0)
   {
      if (!ObjectDelete(name))
         Print ("Can't delete ", name, ", Error: ", GetLastError());
      return;
   }
   
   datetime t1 = datetime(ObjectGetInteger(cid,name,OBJPROP_TIME1));
   datetime t2 = datetime(ObjectGetInteger(cid,name,OBJPROP_TIME2));
   double p1 = ObjectGetDouble(cid,name,OBJPROP_PRICE1);
   double p2 = ObjectGetDouble(cid,name,OBJPROP_PRICE2);

   if (name=="s")
      ObjectCreate(cid,sName,OBJ_TREND,0,t1,p1,t2,p2);
   if (name=="r")
      ObjectCreate(cid,rName,OBJ_TREND,0,t1,p1,t2,p2);

   if (!ObjectDelete(name))
      Print ("Can't delete ", name, ", Error: ", GetLastError());
}

EDIT: Note that the UpdateTLine() function will not execute well within indicators (so it's best to be run in EAs), because ObjectDelete() will not work within the same execution thread as OnChartEvent(), apparently after ObjectCreate().

 
Seng Joo Thio:

No, you don't call OnChartEvent() function... the terminal will call for you, as and when changes are detected.

Ok, here's an example EA (you draw a trendline and rename it as 's' or 'r', and the code will automatically rename them to 'support' or 'resistance' if and only if there wasn't any existing support or resistance line... and whether it is successfully renamed or not, the 's' / 'r' lines will be deleted right after.):

EDIT: Note that the UpdateTLine() function will not execute well within indicators (so it's best to be run in EAs), because ObjectDelete() will not work within the same execution thread as OnChartEvent(), apparently after ObjectCreate().

Hello Seng,

Thanks very much for the remark and the code.
Tried it in the indicator, but as you mentioned that did not work. In the expert it works fine.
It looks a bit funny because you give a name to the trendline but, you end up with a different name.

The important thing is that it works! 

Thanks again,best regards,

Sylvain

I note you do freelance work. I may need your help in the future. How do I contact you?

 
Sylvain Vervoort:

I may need your help in the future. How do I contact you?

You can always send private messages to me.