Download MetaTrader 5

Select -all- vertical and trend lines on chart

To add comments, please log in or register
Articles can help you to solidify your knowledge. Come and read them!
Chistabo
158
Chistabo 2015.04.24 19:00 

Hi!

I just can't find sample code to select either vertical or trend line (actually both) on chart.

The idea is to (toggle with button) hide/show objects on chart, i.e. vertical and trend lines (which clutter my chart, but i am possessed (LOL) and addicted (LOL again) by them; I need them as much as a guitar needs strings).

First idea was to loop-find all (wanted) objects on chart, and write their color into external file, then set color to CLR_NONE, and back. Quite mind-laborious. Then I found out that easiest way would be to set visibility via timeframe - if all (wanted) objects are drawn on 'OBJ_NO_PERIODS', they will (visibility-wise) disappear from chart; then we set timeframe back.

So, please help me - how do I select ALL OBJ_VLINE and OBJ_TREND, which have varying 'Name' (i.e. "2015.04.07 14:38 +3") and 'Description' (i.e. "Ca@6 fr Nk L Bu") properties, so I can then ObjectSet(?allNames?, OBJPROP_TIMEFRAMES, OBJ_PERIOD_M1|OBJ_PERIOD_M15|OBJ_PERIOD_H1) etc.?

This is the only romance I managed to scratch together:

//--- custom function ---
void HideTrendVerticalLinesToggle() // easiest way - set timeframe to OBJ_NO_PERIODS, EMPTY, then back to TF OBJ_PERIOD_M1|OBJ_PERIOD_M15|OBJ_PERIOD_H1
  {// loop through objects on chart and find ObjectType OBJ_VLINE & OBJ_TREND
// test
   for(int i=ObjectsTotal()-1;i>=0;i--)
     {
      if(ObjectType(0)==OBJ_VLINE) Print("OBjectType OBJ_VLINE prints "+ObjectType(0));
     }
     Print("Button [ Toggle Line Visibility ] pressed!"); // test
  }

No need to tell it does nothing similar to what I would do if I was a computer. LOL!

According to (https://book.mql4.com/appendix/objects), OBJ_VLINE (type) = (has) 0 (value). Lost...

Thank you for your help,

Best regards,

Simon

S love nia

Chistabo
158
Chistabo 2015.04.24 19:48  

OK, I did my homework (trial and error, that is) and this code works as a charm:

//--- HIDE TREND LINES AND VERTICAL LINES ON CHART --------------------------------------------------------
void HideTrendVerticalLinesToggle() // easiest way - set timeframe to OBJ_NO_PERIODS, EMPTY, then back to TF OBJ_PERIOD_M1|OBJ_PERIOD_M15|OBJ_PERIOD_H1
  {// loop through objects on chart and find ObjectType OBJ_VLINE & OBJ_TREND --- https://forum.mql4.com/67461
// test
   for(int i=ObjectsTotal()-1;i>=0;i--)
     {
      string name=ObjectName(i);
      if(ObjectType(name)==OBJ_VLINE) {ObjectSet(name,OBJPROP_SELECTED,true);Print("Names are: "+name);}
     }
//--- Print ---
   Print("Button [ Toggle Line Visibility ] pressed!"); // test
  }

I guess it can be further minimized, but not in the mood now. Any suggestions?

Besta regarda,

Simon

S love nia

Chistabo
158
Chistabo 2015.04.24 21:52  

Here is completed code, it works (mas o menos):

//--- global variable ---
bool ...,HideTrendVerticLines;
...
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   ResetLastError();
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      if(ObjectType(sparam)==OBJ_BUTTON)
        {
         btnName=sparam;
         ButtonPressed(0,sparam);
        }
     }
   ...
// ChartRedraw();
  }
...
//--- custom function ---
void ButtonPressed(long chartID,string sparam)
  {
   ObjectSetInteger(chartID,sparam,OBJPROP_BORDER_COLOR,clrBlack);
   ChartRedraw();
   ...
   if(sparam=="btn_HideTrendVerticLines")HideTrendVerticalLinesToggle(HideTrendVerticLines);
   Sleep(100);
   ObjectSetInteger(chartID,sparam,OBJPROP_BORDER_COLOR,btnBorClr);
   ChartRedraw();
  }
...
//--- HIDE TREND LINES AND VERTICAL LINES ON CHART --------------------------------------------------------
void HideTrendVerticalLinesToggle(bool hideTrendVerticLines)
  {// https://forum.mql4.com/67461
   HideTrendVerticLines=hideTrendVerticLines;
   int i;
   switch(HideTrendVerticLines)
     {
      case false:
        {
         for(i=ObjectsTotal()-1;i>=0;i--)
           {
            string name=ObjectName(i);
            if(ObjectType(name)==OBJ_VLINE || ObjectType(name)==OBJ_TREND || ObjectType(name)==OBJ_ARROW) // TODO: search/show/hide on main window (chart) only
              {
               ObjectSet(name,OBJPROP_TIMEFRAMES,OBJ_NO_PERIODS); // you might try OBJ_PERIOD_W1, OBJ_NO_PERIODS works iratic
               Print("Setting vertical and trend lines and arrows to Timeframe OBJ_NO_PERIODS. Object Name = "+name); // test
              }
           }
         ChartRedraw();HideTrendVerticLines=true;break; // probably works without 'ChartRedraw()' too
        }
      case true:
        {
         for(i=ObjectsTotal()-1;i>=0;i--)
           {
            string name=ObjectName(i);
            if(ObjectType(name)==OBJ_VLINE || ObjectType(name)==OBJ_TREND)
              {
               ObjectSet(name,OBJPROP_TIMEFRAMES,OBJ_PERIOD_M1|OBJ_PERIOD_M15|OBJ_PERIOD_H1);
               Print("Setting vertical and trend lines to Timeframe M1,M15,H1. Object Name = "+name); // test
              }
            if(ObjectType(name)==OBJ_ARROW)
              {
               ObjectSet(name,OBJPROP_TIMEFRAMES,OBJ_PERIOD_H1);
               Print("Setting arrows to Timeframe H1. Object Name = "+name); // test
              }
           }
         ChartRedraw();HideTrendVerticLines=false;break;
        }
     }
   Print("HideTrendVerticLines = "+HideTrendVerticLines); // test
   Print("Button [ Toggle Line Visibility ] pressed!"); // test
  }

It took me some time to figure out that ObjectSet(name,OBJPROP_TIMEFRAMES,OBJ_NO_PERIODS); does NOT hides the objects; therefore I 'beamed them up' into weekly timeframe, which I extremely rarely check. Monthly would do the same trick.

This code will 'hide and show' objects from main and sub windows; I need to figure it out how to hide/show on main window only. Help please!


Best regards,

Simon

S love nia

Chistabo
158
Chistabo 2015.04.25 07:55  

ObjectSet(name,OBJPROP_TIMEFRAMES,OBJ_NO_PERIODS) does work just fine; I must have wrong-coded something.

This:

if((ObjectType(name)==OBJ_VLINE || ObjectType(name)==OBJ_TREND || ObjectType(name)==OBJ_ARROW)&&ObjectFind(0,name)==0)

(...&&ObjectFind(0,name)==0)) will search on main window only.

Best regards,

Simon

S love nia
To add comments, please log in or register