Displaying correct hours for your time zone ...

 

Hello ...

I have been trying to write a script that displays the correct hours of my time zone, which is at -7h of the server time.

I have been partially successful, the problem is that the time labels don't move when a new bar is added, and when I change time frames, some labels don't get deleted.

Can some one be so kind to help me finish this script?

Thanks in advance.

 

//+------------------------------------------------------------------+
//|                                                    TimeLines.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#include <Controls\Label.mqh>

//--- input parameters
input int      TimeOffset=-7;

datetime tm[];
CChartObjectLabel label;

int arraySize;


int ChartFirstVisibleBar(const long chart_ID=0)
  {
//--- prepare the variable to get the property value
   long result=-1;
//--- reset the error value
   ResetLastError();
//--- receive the property value
   if(!ChartGetInteger(chart_ID,CHART_WINDOW_YDISTANCE,0,result))
     {
      //--- display the error message in Experts journal
      Print(__FUNCTION__+", Error Code = ",GetLastError());
     }
//--- return the value of the chart property
   return((int)result);
  }


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   //int bars=(int)ChartGetInteger(0,CHART_VISIBLE_BARS);
   //Alert(bars);
   for(int i=0;i<ObjectsTotal(0,0,OBJ_LABEL);i++){
      string oname=ObjectName(0,i,0,OBJ_LABEL);
      if(ObjectFind(0,oname)>=0) ObjectDelete(0,oname);
   }
//---
   return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
  {
//---
   for(int i=0;i<ObjectsTotal(0,0,OBJ_LABEL);i++){
      string oname=ObjectName(0,i,0,OBJ_LABEL);
      if(ObjectFind(0,oname)>=0) ObjectDelete(0,oname);
   }
  }  

//+------------------------------------------------------------------+
//| 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[])
  {
//---

   /*for(int i=0;i<rates_total-100;i++){
      string oname=ObjectName(0,i,0,OBJ_LABEL);
      if(ObjectFind(0,oname)>=0) ObjectDelete(0,oname);
   }*/

   arraySize=rates_total;
   MqlDateTime stm;
   ChartRedraw();
   CopyTime(_Symbol,PERIOD_CURRENT,0,rates_total,tm);
   int n=0;//ChartFirstVisibleBar(0)
   for(int i=rates_total-100;i<rates_total;i++){
      TimeToStruct(tm[i],stm);
      if(stm.min==0){
         
         int t=stm.hour+TimeOffset;
         if(t<0)t=24+stm.hour+TimeOffset;
         
         int x;
         long y;int y2;
         ChartTimePriceToXY(0,0,time[i],close[i],x,y2);
         ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0,y);
         
         string label_name=(string)i;
         if(ObjectFind(0,label_name)<0){
            //Print("Object ",label_name," not found. Error code = ",GetLastError());
            ObjectCreate(0,label_name,OBJ_LABEL,0,0,0);
            n=n+100;           
            ObjectSetInteger(0,label_name,OBJPROP_XDISTANCE,x-7);
            ObjectSetInteger(0,label_name,OBJPROP_YDISTANCE,y-20);
            ObjectSetInteger(0,label_name,OBJPROP_COLOR,White);
            ObjectSetString(0,label_name,OBJPROP_TEXT,t);
            ObjectSetString(0,label_name,OBJPROP_FONT,"Arial");
            ObjectSetInteger(0,label_name,OBJPROP_FONTSIZE,10);
            //ObjectSetDouble(0,label_name,OBJPROP_ANGLE,-45);
            ObjectSetInteger(0,label_name,OBJPROP_SELECTABLE,false);
            ChartRedraw(0);                                      
           } else {
              ObjectMove(0,label_name,0,time[i],0);
           }
      
         //Alert(stm.hour);
      }
   }
   
   /*for(int i=0;i<rates_total;i++){
      string oname=ObjectName(0,i,0,OBJ_LABEL);
      if(ObjectFind(0,oname)>=0) ObjectMove(0,oname,0,time[i],0);
   }*/
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
marcovth:

Hello ...

I have been trying to write a script that displays the correct hours of my time zone, which is at -7h of the server time.

I have been partially successful, the problem is that the time labels don't move when a new bar is added, and when I change time frames, some labels don't get deleted.

Can some one be so kind to help me finish this script?

Thanks in advance.

 

If I understood well you are trying to create a substitute to the current horizontal scale (time) ?
 
angevoyageur:
If I understood well you are trying to create a substitute to the current horizontal scale (time) ?

Yes and no ... Just for the bars that are visible, I want to show the hours as labels, so the script is not touching the time scale.

I think it's kind of essential to show the correct time for my time zone to better realize when news might be hitting (to get out of the market in my case).

You would think that the MT5 developers would recognize that this is very important to users? It's really strange you can not adjust the time scale for time zone. 

 
marcovth:

Yes and no ... Just for the bars that are visible, I want to show the hours as labels, so the script is not touching the time scale.

I think it's kind of essential to show the correct time for my time zone to better realize when news might be hitting (to get out of the market in my case).

You would think that the MT5 developers would recognize that this is very important to users? It's really strange you can not adjust the time scale for time zone. 

I understand and I agree, however I don't think there is an easy way. It's not a trivial task and you are far from the result
 

Hi

you can use indicator