JPN_Time_Sub
Download scriptThis script is designed as a visual time-tracking and data labeling tool for the MetaTrader trading platform. Its primary purpose is to help traders identify specific points in time on their charts by converting raw timestamps into readable, customizable date and time markers.
Core Functionality
The logic functions by scanning the historical price data of your chart and identifying specific time intervals. Instead of showing just a generic price line, it places "objects" (text, arrows, or grid lines) directly onto the chart to act as visual landmarks.
Key Features and Logic:
1. Time Conversion and Offset The script includes a built-in time difference calculation. This allows it to adjust the displayed time based on your specific time zone requirements (for example, aligning the chart with a specific regional market's local time).
2. Customizable Data Display Users can choose exactly which components of the date and time they want to see visible on the screen. The logic allows you to toggle the following individually:
- Year
- Month
- Day
- Hour
- Minute
- Second
Depending on your settings, the script will dynamically construct a text string (e.g., "2023.10.27" or "14:30"). If you choose to display multiple components, it automatically handles the punctuation (like dots and colons) to ensure the text remains readable.
3. Visual Markers and Graphics The script supports several different ways to display this information:
- Text Labels: Displays the date and time as a clean string above or below the price candles.
- Multi-Line Text: An option to stack the date and time on two separate lines for better organization of the workspace.
- Arrows: The ability to place specific arrow symbols at certain time points to highlight significant moments.
- Grid Lines: A feature that draws vertical dotted lines to segment the chart, making it easier to navigate through different trading sessions or days.
4. Automatic Management and Cleanup The script is designed to be "clean." It includes logic to manage its own objects on the chart:
- Dynamic Creation: It checks if a marker already exists before creating one to prevent clutter.
- Automatic Deletion: When the indicator is removed or the timeframe is changed, it automatically cleans up all the text and lines it created, ensuring your chart remains tidy and free of "ghost" markers from previous sessions.
Summary for the User
Essentially, this tool acts as a customized "map" for your trading chart. It takes the complex, underlying data of time and converts it into a clear, user-defined visual format, allowing you to see exactly when specific events occurred while giving you full control over how much information is displayed on your screen.
//ì¬æÊÆobt@Ìwè
#property indicator_separate_window
#property indicator_minimum -1
#property indicator_maximum 2
//úÝèÏ
extern bool Grid = false;
extern bool YEAR = false;
extern bool MONTH = true;
extern bool DAY = true;
extern bool HOUR = true;
extern bool MINUTE = true;
extern bool SECOND = false;
extern bool ARROW = false;
extern bool NewLine = false;
extern color GridColor = LightSlateGray;
extern int Time_difference = 7;
extern int FontSize = 8;
extern int TimeInterval = 12;
extern color TextColor = White;
string ObjArray[300000];
datetime JPNT,StandardTime;
//string JPND,JPNM,WinName="JPN_Time_Sub";
string JPND,JPNM,WinName=" ";
int DefPeri,DefWin,ObjTotal;
//ú»
int init(){
IndicatorShortName(" ");
if(DefPeri != Period()){
DefPeri = Period();
DeleteTimeObject();
}
return(0);
}
//I¹@Ô\¦ðí
int deinit(){
DeleteTimeObject();
return(0);
}
int start(){
int limit=Bars-IndicatorCounted();
for(int i=limit; i>=0; i--){
//îÔ𴦽çì¬
if(StandardTime <= Time[i]){
//ú{Ô̶¬
JPNT = Time[i] + Time_difference * 3600;
SetTimeText(Time[i]);
//ÌîÔðÝè·é
datetime Period_Interval=DefPeri * 60 * TimeInterval;
for(StandardTime = Time[i] + Period_Interval;(TimeDayOfWeek(StandardTime) == 0 || TimeDayOfWeek(StandardTime) == 6) && DefPeri < PERIOD_H1;StandardTime += Period_Interval){}
}
}
return(0);
}
void SetTimeText(datetime settime){
//út¶ñÌì¬
string Y="";
string M="";
string D="";
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(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);
//¯¶IuWFNgª¶ÝµÈ¯êÎì¬
if(ObjectFind(StringConcatenate("JPNTS_Arrow",JPND,"_",JPNM)) == -1){
MakeTimeObject(StringConcatenate("JPNTS_Arrow",JPND,"_",JPNM),StringConcatenate("JPNTS_Text",JPND,"_",JPNM,"_1"),StringConcatenate("JPNTS_Text",JPND,"_",JPNM,"_2"),StringConcatenate("JPNTS_GridJPN",JPND,"_",JPNM),JPND,JPNM,settime);
}
}
void MakeTimeObject(string ArrowName,string TimeTextName1,string TimeTextName2,string GridName,string DATE,string TIME,datetime settime){
DefWin=WindowFind(WinName);//OÌ×
int Pos=2;
//îóÌì¬
if(ARROW){
ObjArray[ObjTotal]=ArrowName;
ObjTotal++;
ObjectCreate(ArrowName, OBJ_ARROW,DefWin,settime, Pos);
Pos--;
ObjectSet(ArrowName,OBJPROP_ARROWCODE,241);
ObjectSet(ArrowName,OBJPROP_COLOR,TextColor);
}
if(NewLine){
//2sÅ\¦
//útÌì¬
ObjArray[ObjTotal]=TimeTextName1;
ObjTotal++;
ObjectCreate(TimeTextName1, OBJ_TEXT,DefWin,settime, Pos);
Pos--;
ObjectSetText(TimeTextName1, DATE, FontSize, "Arial", TextColor);
//ÔÌì¬
ObjArray[ObjTotal]=TimeTextName2;
ObjTotal++;
ObjectCreate(TimeTextName2, OBJ_TEXT,DefWin,settime, Pos);
Pos--;
ObjectSetText(TimeTextName2, TIME, FontSize, "Arial", TextColor);
}else{
//1sÅ\¦
ObjArray[ObjTotal]=StringConcatenate(TimeTextName1,"_",TimeTextName2);
ObjectCreate(ObjArray[ObjTotal], OBJ_TEXT,DefWin,settime, Pos);
Pos--;
if(YEAR || MONTH || DAY){
if(HOUR || MINUTE || SECOND){
ObjectSetText(ObjArray[ObjTotal], StringConcatenate(DATE,"_",TIME), FontSize, "Arial", TextColor);
}else{
ObjectSetText(ObjArray[ObjTotal], DATE, FontSize, "Arial", TextColor);
}
}else if(HOUR || MINUTE || SECOND){
ObjectSetText(ObjArray[ObjTotal], TIME, FontSize, "Arial", TextColor);
}
ObjTotal++;
}
//ObhÌì¬
if(Grid){
ObjArray[ObjTotal]=GridName;
ObjTotal++;
ObjectCreate(GridName,OBJ_TREND, 0, settime,0,settime,WindowPriceMax(0));
ObjectSet(GridName,OBJPROP_COLOR,GridColor);
ObjectSet(GridName, OBJPROP_STYLE, STYLE_DOT);
}
ObjectsRedraw();
}
void DeleteTimeObject(){
DefWin=WindowFind(WinName);
//zñÉi[µ½¼OÅí
for(int i = 0 ; i < ObjTotal ;i++){
if(ObjArray[i] != ""){
ObjectDelete(ObjArray[i]);
ObjArray[i]="";
}
}
//\bh©ç·×ÄÌIuWFNgðæ¾µÄí
ObjTotal=ObjectsTotal();
for(i=ObjTotal;i>=0;i--){
string ObjName=ObjectName(i);
if(StringSubstr(ObjName,0,5) == "JPNTS"){
ObjectDelete(ObjName);
}
}
ObjTotal=0;
}
Comments