JPN_Time_SubZero

Download script
Author: ver.2.4
JPN_Time_SubZero

This script is designed for the MetaTrader platform to provide a specialized visual time management tool. Its primary purpose is to automatically identify and display specific time intervals on a trading chart, specifically converting standard market times into a localized time format (such as Japan Standard Time).

Core Functionality

Automatic Time Synchronization The logic includes a smart detection system to determine the difference between your local computer time and the market server time. If it cannot automatically detect this gap, it allows for a manual input to ensure that the timestamps displayed on your chart remain accurate regardless of which broker's server you are connected to.

Dynamic Time Marking Rather than manually checking the clock, the script scans the historical data of your chart to find specific "anchor" times. It then places visual markers at regular intervals based on settings you choose (such as every X minutes or hours). This helps traders quickly identify significant sessions, news releases, or trading blocks.

Visual Customization The script provides several layers of visual feedback to help organize the chart:

  • Text Labels: It places clear text on the chart showing the date and time. You can choose exactly which components to show (e.g., just the hour, or a full date/time string).
  • Grid Lines: It can draw vertical "grid" lines at these specific time intervals. This makes it much easier to see how much price action occurs within a specific timeframe.
  • Color Coding: There is an option to highlight specific hours with different colors (e.g., marking certain high-volatility sessions in a distinct color), allowing for instant visual recognition of key market periods.
  • Arrows: An optional arrow marker can be toggled to provide a sharp visual point for specific time events.

How It Behaves on the Chart

The script works by "drawing" objects onto your chart. When you change your timeframe or refresh the data, the script cleans up its old markings and redraws them based on the current period. This ensures that the information remains consistent and doesn't clutter the screen with outdated data.

In summary, this is a visual utility for traders who need to keep track of time-sensitive sessions across different time zones without having to perform manual mental math.

Miscellaneous
It issuies visual alerts to the screen
14 Views
0 Downloads
0 Favorites
JPN_Time_SubZero
#property copyright "ver.2.4"
#property link "http://d.hatena.ne.jp/fai_fx/"
//ì¬‰æ–ʂƃoƒbƒtƒ@‚ÌŽw’è
#property indicator_separate_window
#property indicator_minimum -1
#property indicator_maximum 2

//‰ŠúÝ’è•ϐ”
// Ž©•ª‚̃fƒtƒHƒ‹ƒgÝ’è‚ɍ‡‚킹‚ăJƒXƒ^ƒ}ƒCƒY‚·‚é‚Ì‚ª‚æ‚¢B
extern int TimeInterval = 12;
extern bool UseLocalPCTime = true;
//UseLocalPCTime‚ªtrue ‚ÌŽž‚́A‰º‚ÌTime_difference@‚ðŽg‚í‚È‚¢‚̂ŒˆÓ 
extern int Time_difference = 9;
extern bool Grid   = true;
extern bool YEAR   = false;
extern bool MONTH  = false;
extern bool DAY    = false;
extern bool HOUR   = true;
extern bool Zero_H = false;
extern bool MINUTE = false;
extern bool SECOND = false;
extern bool ARROW  = false;
extern bool NewLine= false;
extern color GridColor = LightSlateGray;//C'32,32,32';
extern int FontSize = 8;
extern color TextColor = White;
extern bool UseColorGrid= false;
extern bool Use12Hour= false;
string  WinName="  ";//ƒXƒy[ƒX2•¶Žš
int DefPeri;

//‰Šú‰»
int init(){
   IndicatorShortName(WinName);
   IndicatorDigits(0);
   SetIndexLabel(0,NULL);
   if(DefPeri != Period()){
      DefPeri = Period();
      DeleteTimeObject();
   }
   if(!YEAR && !MONTH && !DAY) NewLine = false;
   return(0);
}


//I—¹Žž@ŽžŠÔ•\ަ‚ðíœ
int deinit(){
   DeleteTimeObject();
   return(0);
}


int start(){

   if(Bars < 50 ) return(0);
   if(Volume[0] < 2 ) return(0);
   int limit=Bars-IndicatorCounted()-1;
   if(limit < WindowBarsPerChart() ) limit = WindowBarsPerChart();
   static datetime PreBarsTime  = 0;
   static datetime StandardTime = 0;    
   
   //ƒ[ƒJƒ‹ŽžŠÔŒŸo  
   if(UseLocalPCTime){
      int Time_difference0 = (TimeLocal()-TimeCurrent()+15*60)/3600;
      if(Time_difference != Time_difference0){
         PreBarsTime = Time[Bars-1];
         DeleteTimeObject();
         StandardTime = 0;
      }      
      if(Time_difference0 > 10){
         static int checkcount = 0;
         checkcount++;
         if(checkcount>10){
           UseLocalPCTime = false;
           Alert(Symbol(),Period()," Can't detect LocalTime difference\n Now use Time_difference = ",Time_difference);
         }
      }else{
         Time_difference = Time_difference0;
         //Print("Time_difference =",Time_difference);
      } 
   }
        

   //ƒXƒNƒ[ƒ‹ƒoƒbƒN‚µ‚½‚çAì‚è’¼‚·
   if(PreBarsTime != Time[Bars-1]){
      PreBarsTime = Time[Bars-1];
      DeleteTimeObject();
      StandardTime = 0;
   }
   
   //Šî€ŽžŠÔ‚ÌŠJŽnŽž‚ð‹‚ß‚é   
   if(StandardTime == 0){
      for(int j=Bars-1; j>=0; j--){
         if(TimeMinute(Time[j])==0 && TimeHour(Time[j]+Time_difference * 3600)==0){
            StandardTime = Time[j]-24*60*60;//ŠJŽnŽž‚́A‚P“ú‘O‚©‚çB
            break;
         }
      }
   }
   datetime Period_Interval = DefPeri * 60 * TimeInterval;
   
   //Chart repaint ‚É”õ‚¦‚Ä10–{‘O‚©‚ç‚â‚è’¼‚¹‚邿‚¤‚ɁB
   StandardTime = StandardTime - 10 * Period_Interval;
   
   for(int i=limit; i>=0; i--){
      //Šî€ŽžŠÔ‚Ɉê’v‚µ‚½‚çì¬
      if(StandardTime == Time[i]  ){
         SetTimeText(Time[i],Time_difference);
      }
      //ŽŸ‚ÌŠî€ŽžŠÔ‚ðÝ’è‚·‚é
      while(StandardTime <= Time[i]) StandardTime = StandardTime + Period_Interval;      
   }
   return(0);
}

void SetTimeText(datetime settime,int Time_difference){
   //“ú–{ŽžŠÔ‚̐¶¬
   datetime JPNT = settime + Time_difference * 3600;
   //“ú•t•¶Žš—ñ‚̍쐬
   string Y="";
   string M="";
   string D="";
   string JPND,JPNM;
  
   JPND = StringSubstr(TimeToStr(JPNT, TIME_DATE),2);
  
   if(YEAR){
      Y=StringSubstr(JPND,0,2);
   }
   if(MONTH){
      if(YEAR){
         Y=StringConcatenate(Y,".");
      }
      M=StringSubstr(JPND,3,2);
   }
   if(DAY){
      if(YEAR || MONTH){
         M=StringConcatenate(M,".");
      }
      D=StringSubstr(JPND,6,2);
   }
   JPND=StringConcatenate(Y,M,D);
 

   //ŽžŠÔ•¶Žš—ñ‚̍쐬
   string h="";
   string m="";
   string s="";

   JPNM = TimeToStr(JPNT, TIME_SECONDS);
   if(HOUR){
      h=StringSubstr(JPNM,0,2);
      if(!Zero_H) h=TimeHour(JPNT);
   }
   if(MINUTE){
      if(HOUR){
         h=StringConcatenate(h,":");
      }
      m=StringSubstr(JPNM,3,2);
   }
   if(SECOND){
      if(HOUR || MINUTE){
         m=StringConcatenate(m,":");
      }
      s=StringSubstr(JPNM,6,2);
   }
   JPNM=StringConcatenate(h,m,s);
   string tm = TimeToStr(settime,TIME_DATE|TIME_SECONDS);
   //“¯‚¶ƒIƒuƒWƒFƒNƒg‚ª‘¶Ý‚µ‚È‚¯‚ê‚΍쐬
   if(ObjectFind(StringConcatenate("JPNTZ_",tm)) == -1){
      MakeTimeObject(StringConcatenate("JPNTZ_Arrow",tm),
      StringConcatenate("JPNTZ_",tm),
      StringConcatenate("JPNTZ_Text",tm,"_2"),StringConcatenate("JPNTZ_Grid_",tm),JPND,JPNM,settime);
   }
}

void MakeTimeObject(string ArrowName,string TimeTextName1,string TimeTextName2,string GridName,string DATE,string TIME,datetime settime){
   int DefWin=WindowFind(WinName);
   int Pos=2;

   //–îˆó‚̍쐬
   if(ARROW){
      ObjectCreate(ArrowName, OBJ_ARROW,DefWin,settime, Pos);
      Pos--;
      ObjectSet(ArrowName,OBJPROP_ARROWCODE,241);
      ObjectSet(ArrowName,OBJPROP_COLOR,TextColor);
   }
   
   if(NewLine){
      //2s‚Å•\ަ
      //“ú•t‚̍쐬
      ObjectCreate(TimeTextName1, OBJ_TEXT,DefWin,settime, Pos);
      Pos--;
      ObjectSetText(TimeTextName1, DATE, FontSize, "Arial", TextColor);

      //ŽžŠÔ‚̍쐬
      ObjectCreate(TimeTextName2, OBJ_TEXT,DefWin,settime, Pos);
      //Pos--;
      ObjectSetText(TimeTextName2, TIME, FontSize, "Arial", TextColor);

   }else{
      //1s‚Å•\ަ
      if(!ObjectCreate(TimeTextName1, OBJ_TEXT,DefWin,settime, Pos))
      {
         Print("error: can't create OBJ_TEXT! code #",GetLastError());
         return(0);
      }
      //Pos--;
      if(YEAR || MONTH || DAY){
         if(HOUR || MINUTE || SECOND){
            ObjectSetText(TimeTextName1, StringConcatenate(DATE,"_",TIME), FontSize, "Arial", TextColor);
         }else{
            ObjectSetText(TimeTextName1, DATE, FontSize, "Arial", TextColor);
         }
      }else if(HOUR || MINUTE || SECOND){
            //ObjectSetText(TimeTextName1, TIME, FontSize, "Arial", TextColor);
            color MyTextColor = TextColor;
            if(StrToInteger(TIME)>12 && Use12Hour){
               MyTextColor = DeepSkyBlue;
               TIME = StrToInteger(TIME)-12;
            }
            ObjectSetText(TimeTextName1, TIME, FontSize, "Arial", MyTextColor);
      }
   }
   //ƒOƒŠƒbƒh‚̍쐬
   if(Grid){
      int hr = TimeHour(settime+Time_difference * 3600);
      color MyGridColor = GridColor;
      // “Á’莞‚̃‰ƒCƒ“‚ɐF‚¯‚·‚é‚È‚çˆÈ‰º‚ð“K“–‚ɏ‘‚«Š·‚¦‚é
      if(UseColorGrid){
         if(hr == 16) MyGridColor = IndianRed;
         if(hr == 21 || hr == 0 || hr == 4) MyGridColor = SteelBlue;
      }
      ObjectCreate(GridName,OBJ_TREND, 0, settime,0,settime,0.1);
      ObjectSet(GridName,OBJPROP_COLOR,MyGridColor);
      ObjectSet(GridName, OBJPROP_STYLE, STYLE_DOT);
      ObjectSet(GridName, OBJPROP_BACK,true);
   }
   WindowRedraw();
}

void DeleteTimeObject(){
   //ƒƒ\ƒbƒh‚©‚ç‚·‚ׂẴIƒuƒWƒFƒNƒg‚ðŽæ“¾‚µ‚č폜
   for(int i=ObjectsTotal();i>=0;i--){
      string ObjName=ObjectName(i);
      if(StringSubstr(ObjName,0,5) == "JPNTZ"){
         ObjectDelete(ObjName);
      }
   }
}

Comments