Download MetaTrader 5
To add comments, please log in or register
Publish your article and win recognition from thousands of traders!
fxtrader_
247
fxtrader_ 2016.04.23 19:53 

Hi,

I would like to create two Lines showing the "opening bar" for daily time frame in the lower time frame

The problem with the following code I'm not able to change "color, width, ...) also I don't know if it update auto or not.

More explanation in the attached image.


Any help will be highly appreciated


#property link      "https://www.mql5.com"
#property indicator_chart_window

//+-----------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init(){

         ChartSetInteger(0,CHART_SHOW_OBJECT_DESCR,true);

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
    ObjectCreate(0,PERIOD_D1,OBJ_VLINE, 0,Time[1],0 );
    ObjectSetInteger(0,"D1",OBJPROP_COLOR,clrYellow);
    ObjectSetInteger(0,"D1",OBJPROP_STYLE,STYLE_DOT);
    ObjectSetInteger(0,"D1",OBJPROP_WIDTH,0);

return(0);
}

int deinit()
{
   ObjectDelete("D1");
   return(0);
}



Doerk Hilger
1073
Doerk Hilger 2016.04.23 20:27  
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                                                  |
//|                                                                  |
//|  O B J E C T   H A N D L I N G   M A C R O   F U N C T I O N S   |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+  
//+------------------------------------------------------------------+
enum ENUM_MT_OBJ_FLAGS
   {
      MT_OBJ_HIDDEN = 1,
      MT_OBJ_SELECTABLE = 2,
      MT_OBJ_BACKGROUND = 4
   };
//+------------------------------------------------------------------+
//| Create H Line                                                    |
//+------------------------------------------------------------------+    
bool __MT_Object_Line_CreateH(long chartid, string name, int subwin, double price, int flags=MT_OBJ_HIDDEN)
   {
      if (chartid==NULL) chartid=ChartID();
      return ObjectCreate(chartid,name,OBJ_HLINE,subwin,0,price)
             &__MT_Object_TP_SetFlags(chartid, name, flags);
   }
   
//+------------------------------------------------------------------+
//| Create V Line                                                    |
//+------------------------------------------------------------------+    
bool __MT_Object_Line_CreateV(long chartid, string name, int subwin, datetime dt, uint flags=MT_OBJ_HIDDEN)
   {
      if (chartid==NULL) chartid=ChartID();
      return ObjectCreate(chartid,name,OBJ_VLINE,subwin,dt,0.0)&
             __MT_Object_TP_SetFlags(chartid, name, flags);
   }
//+------------------------------------------------------------------+
//| Create Trend Line                                                |
//+------------------------------------------------------------------+ 
bool __MT_Object_Line_CreateTrend(long chartid, string name, int subwin,
                                  const datetime time1,const double price1,
                                  const datetime time2,const double price2, uint flags=MT_OBJ_HIDDEN)
  {
   if (chartid==NULL) chartid=ChartID();
   return ObjectCreate(chartid,name,OBJ_TREND,subwin,time1,price1,time2,price2)&
            __MT_Object_TP_SetFlags(chartid,name,flags);
   }
   
//+------------------------------------------------------------------+
//| Create Rect                                                      |
//+------------------------------------------------------------------+ 
bool __MT_Object_Rect_Create(long chartid, string name, int subwin,
                             const datetime time1,const double price1,
                             const datetime time2,const double price2, uint flags=MT_OBJ_HIDDEN)
  {
   if (chartid==NULL) chartid=ChartID();
   return ObjectCreate(chartid,name,OBJ_RECTANGLE,subwin,time1,price1,time2,price2)&
            __MT_Object_TP_SetFlags(chartid,name,flags);
   }
   

//+------------------------------------------------------------------+
//| Line Width                                                       |
//+------------------------------------------------------------------+    
bool __MT_Object_Line_SetWidth (long chartid, string name, int w)
   {
      if (chartid==NULL) chartid=ChartID();
      return ObjectSetInteger(chartid,name,OBJPROP_WIDTH,w);
   }
   
int __MT_Object_Line_GetWidth (long chartid, string name)
   {
      if (chartid==NULL) chartid=ChartID();
      return (int)ObjectGetInteger(chartid,name,OBJPROP_WIDTH);
   }
   
//+------------------------------------------------------------------+
//| Line Style                                                       |
//+------------------------------------------------------------------+    
bool __MT_Object_Line_SetStyle (long chartid, string name, ENUM_LINE_STYLE value)
   {
      if (chartid==NULL) chartid=ChartID();
      return ObjectSetInteger(chartid,name,OBJPROP_STYLE,value);
   }
   
ENUM_LINE_STYLE __MT_Object_Line_GetStyle (long chartid, string name)
   {
      if (chartid==NULL) chartid=ChartID();
      return (ENUM_LINE_STYLE)ObjectGetInteger(chartid,name,OBJPROP_STYLE);
   }   
   
//+------------------------------------------------------------------+
//| Object Color                                                     |
//+------------------------------------------------------------------+    
bool __MT_Object_SetColor (long chartid, string name, color clr)
   {
      if (chartid==NULL) chartid=ChartID();
      return ObjectSetInteger(chartid,name,OBJPROP_COLOR,clr);
   }
   
color __MT_Object_GetColor (long chartid, string name)
   {
      if (chartid==NULL) chartid=ChartID();
      return (color)ObjectGetInteger(chartid,name,OBJPROP_COLOR);
   }
   

//+------------------------------------------------------------------+
//| Object Price                                                     |
//+------------------------------------------------------------------+    
bool __MT_Object_SetPrice (long chartid, string name, int point, double value)
   {
      if (chartid==NULL) chartid=ChartID();
      return ObjectSetDouble(chartid,name,OBJPROP_PRICE,point,value);
   }
   
double __MT_Object_GetPrice (long chartid, string name, int point)
   {
      if (chartid==NULL) chartid=ChartID();
      return (double)ObjectGetDouble(chartid,name,OBJPROP_PRICE,point);
   }
   
//+------------------------------------------------------------------+
//| Object Time                                                      |
//+------------------------------------------------------------------+    
bool __MT_Object_SetTime (long chartid, string name, int point, datetime value )
   {
      if (chartid==NULL) chartid=ChartID();
      return ObjectSetInteger(chartid,name,OBJPROP_TIME,point,(long)value);
   }
   
datetime __MT_Object_Line_GetTime (long chartid, string name, int point)
   {
      if (chartid==NULL) chartid=ChartID();
      return (datetime)ObjectGetInteger(chartid,name,OBJPROP_TIME,point);
   }   
      
//+------------------------------------------------------------------+
//| Object Tooltip                                                   |
//+------------------------------------------------------------------+    
bool __MT_Object_SetToolTip (long chartid, string name, string value)
   {
      if (chartid==NULL) chartid=ChartID();
      return ObjectSetString(chartid,name,OBJPROP_TOOLTIP,value);
   }
   
string __MT_Object_GetToolTip (long chartid, string name)
   {
      if (chartid==NULL) chartid=ChartID();
      return ObjectGetString(chartid,name,OBJPROP_TOOLTIP);
   }
      

//+------------------------------------------------------------------+
//| Delete object                                                    |
//+------------------------------------------------------------------+    
bool __MT_Object_Delete(long chartid, string name)
   {
      return ObjectDelete(chartid,name);
   }
bool __MT_Object_DeleteAll(long chartid, string nameprefix, int subwin=-1, int type=-1)
   {
      if (chartid==NULL) chartid=ChartID();
      return ObjectsDeleteAll(chartid,nameprefix,subwin,type);
   }
   
//+------------------------------------------------------------------+
//| Set common object flags                                          |
//+------------------------------------------------------------------+    
bool __MT_Object_SetFlags(long chartid, string name, uint flags)
   {
      if (chartid==NULL) chartid=ChartID();
      bool result=true;
      result&=ObjectSetInteger(chartid,name,OBJPROP_HIDDEN,((flags&MT_OBJ_HIDDEN)>0));
      result&=ObjectSetInteger(chartid,name,OBJPROP_SELECTABLE,((flags&MT_OBJ_SELECTABLE)>0));
      result&=ObjectSetInteger(chartid,name,OBJPROP_BACK,((flags&MT_OBJ_BACKGROUND)>0));
      return result;   
   }

//+------------------------------------------------------------------+
//| Get common object flags                                          |
//+------------------------------------------------------------------+    
uint __MT_Object_GetFlags(long chartid, string name)
   {
      if (chartid==NULL) chartid=ChartID();
      uint flags=0;
      flags |= (bool)ObjectGetInteger(chartid,name,OBJPROP_SELECTABLE) ? MT_OBJ_SELECTABLE : 0;
      flags |= (bool)ObjectGetInteger(chartid,name,OBJPROP_HIDDEN)     ? MT_OBJ_HIDDEN : 0;
      flags |= (bool)ObjectGetInteger(chartid,name,OBJPROP_BACK)       ? MT_OBJ_BACKGROUND : 0;
      return flags;
   }
   
Use this.
fxtrader_
247
fxtrader_ 2016.04.23 20:49  
actually I'm new to mql4 and i couldn't do anything with your code :)

Thank you very much
Doerk Hilger
1073
Doerk Hilger 2016.04.24 08:31  
youre welcome :)
whroeder1
13610
whroeder1 2016.04.24 11:25  

FxTrader_:

I would like to create two Lines showing the "opening bar" for daily time frame in the lower time frame

The problem with the following code I'm not able to change "color, width, ...) also I don't know if it update auto or not.

    ObjectCreate(0,PERIOD_D1,OBJ_VLINE, 0,Time[1],0 );
  1. Perhaps you should read the manual. 1440 is not a valid string for the name.
  2. If you want it to be on the daily open why are you putting it on newest completed bar of the current timeframe instead of the start of current day?
  3. It will not auto update. Once created you have to move it. We just discussed this Object is not automatically refreshing (Mimi) - MQL4 forum
  4. Why don't you use the built in one? options -> chart -> show period separators (control-Y)
fxtrader_
247
fxtrader_ 2016.04.24 13:15  

Hi WHRoeder,

Thank you for reply and suggestion. I know about built in option, but i don't like too many separator lines to show in the chart. any way I think i finally get one step ahead with the help of some code from Mr. GumRai.
But I couldn't add it for the current bar [bar 0].


Any advice will be highly appreciated.


  int Higher_Timeframe=PERIOD_D1;
  datetime higher_time=iTime(Symbol(),Higher_Timeframe,0);
  int shift=iBarShift(Symbol(),0,higher_time);

    string obj_name = "testobj";
    int    chart_id = 0;
    
    ObjectCreate(chart_id,obj_name,OBJ_VLINE,0,Time[shift+1],0);
    
    ObjectSetInteger(chart_id,obj_name,OBJPROP_COLOR,clrYellow);
    ObjectSetInteger(chart_id,obj_name,OBJPROP_STYLE,STYLE_SOLID);
    ObjectSetInteger(chart_id,obj_name,OBJPROP_WIDTH,1);
    ObjectSetInteger(chart_id,obj_name,OBJPROP_BACK,false);
    ObjectSetInteger(chart_id,obj_name,OBJPROP_SELECTABLE,true);
    ObjectSetInteger(chart_id,obj_name,OBJPROP_SELECTED,false);
    ObjectSetInteger(chart_id,obj_name,OBJPROP_HIDDEN,true);
    ObjectSetInteger(chart_id,obj_name,OBJPROP_ZORDER,0);


Thanks

Keith Watford
Moderator
7906
Keith Watford 2016.04.24 18:48  
   int Higher_Timeframe=PERIOD_D1;
   datetime higher_time=iTime(Symbol(),Higher_Timeframe,0);
   int shift=iBarShift(Symbol(),0,higher_time); //NO NEED FOR THE SHIFT, MIDNIGHT IS MIDNIGHT ON ALL TIME-FRAMES

   string obj_name = "testobj"; //DO NOT HARDCODE NAMES IF YOU ARE GOING TO DRAW MORE THAN 1
   int    chart_id = 0;

   ObjectCreate(chart_id,obj_name,OBJ_VLINE,0,Time[shift+1],0); //shift+1 PUTS THE LINE IN THE WRONG PLACE
                                                                //CHECK IF THE OBJECT EXISTS BEFORE TRYING TO CREATE IT
   int Higher_Timeframe=PERIOD_D1;
   datetime higher_time=iTime(Symbol(),Higher_Timeframe,0);
   string obj_name = "testobj"+TimeToStr(higher_time,TIME_DATE|TIME_MINUTES);
   int    chart_id = 0;

   if(ObjectFind(chart_id,obj_name)<0) //CHECK IF THE OBJECT EXISTS BEFORE TRYING TO CREATE IT
     {
      ObjectCreate(chart_id,obj_name,OBJ_VLINE,0,higher_time,0);
      ObjectSetInteger(chart_id,obj_name,OBJPROP_COLOR,clrYellow);
      ObjectSetInteger(chart_id,obj_name,OBJPROP_STYLE,STYLE_SOLID);
      ObjectSetInteger(chart_id,obj_name,OBJPROP_WIDTH,1);
      ObjectSetInteger(chart_id,obj_name,OBJPROP_BACK,false);
      ObjectSetInteger(chart_id,obj_name,OBJPROP_SELECTABLE,true);
      ObjectSetInteger(chart_id,obj_name,OBJPROP_SELECTED,false);
      ObjectSetInteger(chart_id,obj_name,OBJPROP_HIDDEN,true);
      ObjectSetInteger(chart_id,obj_name,OBJPROP_ZORDER,0);
     }


.

fxtrader_
247
fxtrader_ 2016.04.25 06:33  

Thank you very much dear GumRai, I appreciate your kind help

/
To add comments, please log in or register