Login Page - Create Account

Support Board


Date/Time: Sun, 22 Jun 2025 15:04:10 +0000



[Programming Help] - ConvertDateTimeToChartTimeZone broken on daylight savings day when using SetTimeHMS

View Count: 853

[2022-03-14 04:06:20]
User719512 - Posts: 316
See the code and output below. The RTH_Open_Time_ChartTime should be 8:30 but is converting to 9:30.
This breaks studies that are comparing a CurrentBarDateTime against the RTH_Open_Time_PT which has been converted to chart time in the variable RTH_Open_Time_ChartTime using ConvertDateTimeToChartTimeZone.


The scenario: a study wants to detect a particular time of day. So the study gets the current bar time, converts it to a known time zone, sets the time to the desired time in that time zone, and calls ConvertDateTimeToChartTimeZone to store the desired time (in chart time) in a variable. That variable is getting a time that is 1 hour off. On the Globex open today 3/13, that desired time was 15:00 PT or 17:00 CT. My study did not detect that time till 18:00 CT and caused all further calculations to be off.

See below for the output and the code I ran on a particular bar to try to identify the issue. It appears if you have a valid DateTime and call SetTimeHMS, then despite that time looking good when you print it, calling ConvertDateTimeToChartTimeZone on that time will give an invalid result. This may only be an issue on the DST time change day, but we have studies that are anchored to the Globex open on Sunday, and will give bad results for an entire week because of this. A fix and/or workaround would be appreciated.


OUTPUT
======
Chart: F.US.ENQM22[M] 30 Sec #1 | Study: MGI | !!! This bar is sc.Index 15684 and is on a Chicago Timezone Chart at 18:00 on 3/13/2022 (daylight savings day) !!! | 2022-03-13 19:11:54.140 *
Chart: F.US.ENQM22[M] 30 Sec #1 | Study: MGI | CurrentBarDateTime: 2022-03-13 18:00:00 | 2022-03-13 19:11:54.140 *
Chart: F.US.ENQM22[M] 30 Sec #1 | Study: MGI | CurrentBarDateTime_PT: 2022-03-13 16:00:00 | 2022-03-13 19:11:54.140 *
Chart: F.US.ENQM22[M] 30 Sec #1 | Study: MGI | CurrentBarDateTime_PT --> CurrentBarDateTime_ChartTime: 2022-03-13 18:00:00 | 2022-03-13 19:11:54.140 *
Chart: F.US.ENQM22[M] 30 Sec #1 | Study: MGI | RTH_Open_Time_PT: 2022-03-13 06:30:00 | 2022-03-13 19:11:54.140 *
Chart: F.US.ENQM22[M] 30 Sec #1 | Study: MGI | RTH_Open_Time_PT --> RTH_Open_Time_ChartTime: 2022-03-13 09:30:00 | 2022-03-13 19:11:54.140 *

CODE
====

if (sc.Index == 15684) {
SCDateTime CurrentBarDateTime = sc.BaseDateTimeIn[sc.Index];
SCDateTime CurrentBarDateTime_PT = sc.ConvertDateTimeFromChartTimeZone(CurrentBarDateTime, TIMEZONE_LOS_ANGELES);
SCDateTime RTH_Open_Time_PT = CurrentBarDateTime_PT;
RTH_Open_Time_PT.SetTimeHMS(6,30,0);

msg.Format("!!! This bar is sc.Index %d and is on a Chicago Timezone Chart at 18:00 on 3/13/2022 (daylight savings day) !!!", sc.Index);
sc.AddMessageToLog(msg, 1);
msg.Format("CurrentBarDateTime: %s", GetDateTimeString(CurrentBarDateTime).GetChars());
sc.AddMessageToLog(msg, 1);
msg.Format("CurrentBarDateTime_PT: %s", GetDateTimeString(CurrentBarDateTime_PT).GetChars());
sc.AddMessageToLog(msg, 1);
SCDateTime CurrentBarDateTime_ChartTime = sc.ConvertDateTimeToChartTimeZone(CurrentBarDateTime_PT, TIMEZONE_LOS_ANGELES);
msg.Format("CurrentBarDateTime_PT --> CurrentBarDateTime_ChartTime: %s", GetDateTimeString(CurrentBarDateTime_ChartTime).GetChars());
sc.AddMessageToLog(msg, 1);

msg.Format("RTH_Open_Time_PT: %s", GetDateTimeString(RTH_Open_Time_PT).GetChars());
sc.AddMessageToLog(msg, 1);
SCDateTime RTH_Open_Time_ChartTime = sc.ConvertDateTimeToChartTimeZone(RTH_Open_Time_PT, TIMEZONE_LOS_ANGELES);
msg.Format("RTH_Open_Time_PT --> RTH_Open_Time_ChartTime: %s", GetDateTimeString(RTH_Open_Time_ChartTime).GetChars());
sc.AddMessageToLog(msg, 1);
}
[2022-03-15 01:04:07]
User719512 - Posts: 316
Here is a clean repro that you can run on this issue.
Sierra Chart Engineering, please take a look and advise.


Pacific Time/PT is 2 hours behind Chicago/Central Time/CT.
So on a Chicago chart, 9am PT is 11 am CT.

Using SetTime() for times before 10:00am give bad results on the DST day 3/13/2022.
Using SetTime() for times on/after 10:00am give good results on the DST day 3/13/2022.

This is running the same code.

Sierra appears to be broken when using the pattern:
sc.BaseDateTimeIn[sc.Index];
sc.ConvertDateTimeFromChartTimeZone(CurrentBarDateTime, TIMEZONE_LOS_ANGELES);
SetTime() -> SetTime(Input_TimeOfDay.GetTime());
sc.ConvertDateTimeToChartTimeZone(PT_Time, TIMEZONE_LOS_ANGELES);

The conversion FROM chart time and back TO chart time is not giving the same result on the DST day!

One would expect a conversion FROM Central Time (chart time), TO Pacific Time, and back TO Central Time (Chart Time) to be equal.

See output and a sample code below. You can repro this on a NQ 60 min chart set to Chicago time zone.


--------------------------
Output
--------------------------

Setting: Input_TimeOfDay -> 9:59
The time expected should all be "11:59:00". You can see for several bars, the time is "12:59:00". This is the issue.

Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0049 @ 2022-03-11 14:30:00, PT_Time 2022-03-11 09:59:00 -> PT_to_CT_ChartTime 2022-03-11 11:59:00 | 2022-03-14 17:43:38.767 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0050 @ 2022-03-11 15:15:00, PT_Time 2022-03-11 09:59:00 -> PT_to_CT_ChartTime 2022-03-11 11:59:00 | 2022-03-14 17:43:38.767 *
--> error below
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0051 @ 2022-03-13 16:15:00, PT_Time 2022-03-13 09:59:00 -> PT_to_CT_ChartTime 2022-03-13 12:59:00 | 2022-03-14 17:43:38.767 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0052 @ 2022-03-13 17:15:00, PT_Time 2022-03-13 09:59:00 -> PT_to_CT_ChartTime 2022-03-13 12:59:00 | 2022-03-14 17:43:38.767 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0053 @ 2022-03-13 18:15:00, PT_Time 2022-03-13 09:59:00 -> PT_to_CT_ChartTime 2022-03-13 12:59:00 | 2022-03-14 17:43:38.767 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0054 @ 2022-03-13 19:15:00, PT_Time 2022-03-13 09:59:00 -> PT_to_CT_ChartTime 2022-03-13 12:59:00 | 2022-03-14 17:43:38.767 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0055 @ 2022-03-13 20:15:00, PT_Time 2022-03-13 09:59:00 -> PT_to_CT_ChartTime 2022-03-13 12:59:00 | 2022-03-14 17:43:38.767 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0056 @ 2022-03-13 21:15:00, PT_Time 2022-03-13 09:59:00 -> PT_to_CT_ChartTime 2022-03-13 12:59:00 | 2022-03-14 17:43:38.767 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0057 @ 2022-03-13 22:15:00, PT_Time 2022-03-13 09:59:00 -> PT_to_CT_ChartTime 2022-03-13 12:59:00 | 2022-03-14 17:43:38.767 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0058 @ 2022-03-13 23:15:00, PT_Time 2022-03-13 09:59:00 -> PT_to_CT_ChartTime 2022-03-13 12:59:00 | 2022-03-14 17:43:38.767 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0059 @ 2022-03-14 00:15:00, PT_Time 2022-03-13 09:59:00 -> PT_to_CT_ChartTime 2022-03-13 12:59:00 | 2022-03-14 17:43:38.767 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0060 @ 2022-03-14 01:15:00, PT_Time 2022-03-13 09:59:00 -> PT_to_CT_ChartTime 2022-03-13 12:59:00 | 2022-03-14 17:43:38.767 *
--> error above
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0061 @ 2022-03-14 02:15:00, PT_Time 2022-03-14 09:59:00 -> PT_to_CT_ChartTime 2022-03-14 11:59:00 | 2022-03-14 17:43:38.768 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0062 @ 2022-03-14 03:15:00, PT_Time 2022-03-14 09:59:00 -> PT_to_CT_ChartTime 2022-03-14 11:59:00 | 2022-03-14 17:43:38.768 *


Setting: Input_TimeOfDay -> 10:00

Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0049 @ 2022-03-11 14:30:00, PT_Time 2022-03-11 10:00:00 -> PT_to_CT_ChartTime 2022-03-11 12:00:00 | 2022-03-14 17:45:23.345 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0050 @ 2022-03-11 15:15:00, PT_Time 2022-03-11 10:00:00 -> PT_to_CT_ChartTime 2022-03-11 12:00:00 | 2022-03-14 17:45:23.345 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0051 @ 2022-03-13 16:15:00, PT_Time 2022-03-13 10:00:00 -> PT_to_CT_ChartTime 2022-03-13 12:00:00 | 2022-03-14 17:45:23.345 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0052 @ 2022-03-13 17:15:00, PT_Time 2022-03-13 10:00:00 -> PT_to_CT_ChartTime 2022-03-13 12:00:00 | 2022-03-14 17:45:23.345 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0053 @ 2022-03-13 18:15:00, PT_Time 2022-03-13 10:00:00 -> PT_to_CT_ChartTime 2022-03-13 12:00:00 | 2022-03-14 17:45:23.345 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0054 @ 2022-03-13 19:15:00, PT_Time 2022-03-13 10:00:00 -> PT_to_CT_ChartTime 2022-03-13 12:00:00 | 2022-03-14 17:45:23.345 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0055 @ 2022-03-13 20:15:00, PT_Time 2022-03-13 10:00:00 -> PT_to_CT_ChartTime 2022-03-13 12:00:00 | 2022-03-14 17:45:23.345 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0056 @ 2022-03-13 21:15:00, PT_Time 2022-03-13 10:00:00 -> PT_to_CT_ChartTime 2022-03-13 12:00:00 | 2022-03-14 17:45:23.346 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0057 @ 2022-03-13 22:15:00, PT_Time 2022-03-13 10:00:00 -> PT_to_CT_ChartTime 2022-03-13 12:00:00 | 2022-03-14 17:45:23.346 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0058 @ 2022-03-13 23:15:00, PT_Time 2022-03-13 10:00:00 -> PT_to_CT_ChartTime 2022-03-13 12:00:00 | 2022-03-14 17:45:23.346 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0059 @ 2022-03-14 00:15:00, PT_Time 2022-03-13 10:00:00 -> PT_to_CT_ChartTime 2022-03-13 12:00:00 | 2022-03-14 17:45:23.346 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0060 @ 2022-03-14 01:15:00, PT_Time 2022-03-13 10:00:00 -> PT_to_CT_ChartTime 2022-03-13 12:00:00 | 2022-03-14 17:45:23.346 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0061 @ 2022-03-14 02:15:00, PT_Time 2022-03-14 10:00:00 -> PT_to_CT_ChartTime 2022-03-14 12:00:00 | 2022-03-14 17:45:23.346 *
Chart: F.US.ENQM22 [CBV][M] 60 Min #1 | Study: zzz_DST_Test | sc.Index: 0062 @ 2022-03-14 03:15:00, PT_Time 2022-03-14 10:00:00 -> PT_to_CT_ChartTime 2022-03-14 12:00:00 | 2022-03-14 17:45:23.346 *


--------------------------
Code
--------------------------

#include "sierrachart.h"


//
// scsf_DST_Test
//
SCSFExport scsf_DST_Test(SCStudyInterfaceRef sc)
{
SCInputRef Input_TimeOfDay = sc.Input[0];


if (sc.SetDefaults)
{
sc.GraphName = "zzz_DST_Test";
sc.AutoLoop = 1;
  sc.DrawZeros = 1;
sc.GraphRegion = 1;
sc.DrawStudyUnderneathMainPriceGraph = 0;

Input_TimeOfDay.Name = "Time of Day PT";
Input_TimeOfDay.SetTime(HMS_TIME(6,30,0));

return;
}

if (sc.GetBarHasClosedStatus() != BHCS_BAR_HAS_CLOSED) {
return;
}

  SCDateTime CurrentBarDateTime = sc.BaseDateTimeIn[sc.Index];
SCDateTime CurrentBarDateTime_PT = sc.ConvertDateTimeFromChartTimeZone(CurrentBarDateTime, TIMEZONE_LOS_ANGELES);

SCDateTime PT_Time = CurrentBarDateTime_PT;
PT_Time.SetTime(Input_TimeOfDay.GetTime());

SCDateTime PT_to_CT_ChartTime = sc.ConvertDateTimeToChartTimeZone(PT_Time, TIMEZONE_LOS_ANGELES);

SCString msg;

msg.Format("sc.Index: %0.4d @ %s, PT_Time %s -> PT_to_CT_ChartTime %s",
sc.Index,
sc.FormatDateTime(CurrentBarDateTime).GetChars(),
sc.FormatDateTime(PT_Time).GetChars(),
sc.FormatDateTime(PT_to_CT_ChartTime).GetChars()
);
sc.AddMessageToLog(msg, 1);
}
Date Time Of Last Edit: 2022-03-15 02:50:52
[2022-03-24 22:47:54]
User719512 - Posts: 316
NOTE: with good repro provided in another request, Sierra has fixed this issue in ConvertDateTimeToChartTimeZone broken on daylight savings day | Post: 301191 as of v2375.

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account