Login Page - Create Account

Support Board


Date/Time: Wed, 12 Mar 2025 21:42:28 +0000



[Programming Help] - Compare times, so conditional is only triggered outside of certain time segment

View Count: 643

[2022-05-24 15:57:35]
BenjFlame - Posts: 335
Here is a code that will run the conditional only between 11:30 and 11:45.

However, it seems to be triggered outside of this time segment.
Why?



SCDateTime startTime;
  startTime.SetTimeHMS(11, 30, 0);

  SCDateTime endTime;
  endTime.SetTimeHMS(11, 45, 0);


  SCDateTime currentTime;
  currentTime.SetTimeHMS(sc.BaseDateTimeIn[sc.Index].GetHour(), sc.BaseDateTimeIn[sc.Index].GetMinute(), sc.BaseDateTimeIn[sc.Index].GetSecond());

  if (currentTime > startTime && currentTime < endTime)
  {
    SubGraph_Ref_Data.DataColor[sc.Index] = RGB(146,76,112);
  } else
  {
    SubGraph_Ref_Data.DataColor[sc.Index] = RGB(255,255,255);
  }

[2022-05-24 21:47:47]
Tony - Posts: 553
startTime variable needs to be initialized with year/month/day, otherwise it just has the init. values: 1899 Dec. 30th.

It doesn't matter if you just want compare time portion only, but your code is comparing the two sc.DateTime variables as a whole, which including date and time

You have access to current date and time with variable sc.CurrentSystemDateTime, sc.BaseDateTimeIn[sc.Index] won't give you the current time, most of the times (sc.BaseDataEndDateTime[sc.Index] would be a much better choice, but still does not 100% match the current date and time)
Date Time Of Last Edit: 2022-05-25 05:53:13
[2022-05-25 08:19:28]
User431178 - Posts: 613
Try this instead, using GetTime to only compare the time portion of date/time variable.


SCDateTime startTime(11, 30, 0, 0);
SCDateTime endTime(11, 45, 0, 0);

if (sc.BaseDateTimeIn[sc.Index].GetTime() > startTime.GetTime()
&& sc.BaseDateTimeIn[sc.Index].GetTime() < endTime.GetTime())
{
SubGraph_Ref_Data.DataColor[sc.Index] = RGB(146,76,112);
}
else
{
SubGraph_Ref_Data.DataColor[sc.Index] = RGB(255,255,255);
}

[2022-05-25 08:36:55]
BenjFlame - Posts: 335
Thank you for your answers.
To the first gentleman: SetTimeHMS is specifically to set hour/Minute/seconds hence the HMS...
Not sure how to reformat the code with the rest of your suggestions.

To the second gentleman: thank you for that code. Unfortunately, it won't work, it is always false.
[2022-05-25 08:53:19]
User431178 - Posts: 613
Unfortunately, it won't work, it is always false.

Sorry, but how do you reach that conclusion?

Image attached showing coloring bars using that exaxt code block
imagebar coloring.png / V - Attached On 2022-05-25 08:53:13 UTC - Size: 37.64 KB - 122 views
[2022-05-25 14:19:27]
Tony - Posts: 553
Unless you need to process the bars one by one,


SCDateTime startTime(11, 30, 0, 0);

SCDateTime endTime(11, 45, 0, 0);


for (int BarCount {sc.Index}; BarCount>=sc.IndexOfFirstVisibleBar; BarCount--) {
if (sc.BaseDateTimeIn[sc.Index].GetTimeInSeconds() > startTime.GetTimeInSeconds()
&& sc.BaseDateTimeIn[sc.Index].GetTimeInSeconds() < endTime.GetTimeInSeconds())
{
SubGraph_Ref_Data.DataColor[sc.Index] = RGB(146,76,112);
}
else
{
SubGraph_Ref_Data.DataColor[sc.Index] = RGB(255,255,255);
}
}

Date Time Of Last Edit: 2022-05-25 14:22:47
[2022-05-25 14:51:26]
User431178 - Posts: 613
Unless you need to process the bars one by one,

In which case you would also want to replace sc.Index with BarCount in the for loop.
Otherwise, you are looping through many bars but only ever testing/coloring the bar at sc.Index, therefore rendering the loop completely pointless....
Date Time Of Last Edit: 2022-05-25 14:51:50
[2022-05-25 15:10:51]
BenjFlame - Posts: 335
Thanks guys. Got it working.

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

Login

Login Page - Create Account