Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculate High and Low during a session doesn't work?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Calculate High and Low during a session doesn't work?

    Hi,

    I'm trying to determine the High and Low during the Globex session.
    When the session starts, it resets the High and Low, this works fine.

    Now I want the High and Low to be updated as the session moves forward...
    In my code below it doesn't trigger when the time is between GlobexStart and GlobexEnd. (The bolded section doesn't run)

    Any ideas why this happens?

    Thanks

    Code:
    // Globex High and Low Level logic
    DateTime dt = Time[0];
    GlobexSessionStartTime = new DateTime(dt.Year, dt.Month, dt.Day, GlobexStartTime.Hour, GlobexStartTime.Minute, GlobexStartTime.Second);
    GlobexSessionEndTime = new DateTime(dt.Year, dt.Month, dt.Day, GlobexEndTime.Hour, GlobexEndTime.Minute, GlobexEndTime.Second);
    // Globex ends on a new day, so add 1 day to draw the lines correctly
    GlobexSessionEndTime = GlobexSessionEndTime.AddDays(1);
    // Initialize and Calculate the High and Low during the Globex Session
    if (Times[0][0] == GlobexSessionStartTime)
    {
      Print(string.Format("{0} Globex Session Starts Now.", Time[0]));
      Print(string.Format("{0} Globex Session Start Time for Today: {1} ", Time[0], GlobexSessionStartTime));
      Print(string.Format("{0} Globex Session Open Price for Today: {1} ", Time[0], Open[0]));
      Print(string.Format("{0} Globex Session End Time for Today: {1} ", Time[0], GlobexSessionEndTime));
      GlobexHigh = High[0];
      Print(string.Format("{0} Globex High set to first High of Today: {1} ", Time[0], GlobexHigh));
      GlobexLow = Low[0];
      Print(string.Format("{0} Globex Low set to first Low of Today: {1} ", Time[0], GlobexLow));
    }
    
    [B]// When to Globex session is active run the code below...
    if ((Times[0][0] >= GlobexSessionStartTime) && (Times[0][0] >= GlobexSessionEndTime))
    {
      Print(string.Format("{0} Globex Session is active...", Time[0]));
      if (High[0] > GlobexHigh)
      {
        GlobexHigh = High[0];
        Print(string.Format("{0} New Globex High found: {1} ", Time[0], GlobexHigh));
      }
      if (Low[0] > GlobexLow)
      {
         GlobexLow = Low[0];
         Print(string.Format("{0} New Globex Low found: {1} ", Time[0], GlobexLow));
      }
    }[/B]

    #2
    Hello ArdOfCrypto,

    Thank you for your post.

    I think you'll need to use DateTime.Compare so you're comparing the entire date time as I'm not sure this would work correctly when comparing them as you've got it. Try this:

    Code:
    [B]if ((DateTime.Compare(Times[0][0], GlobexSessionStartTime) > 0 ) && (DateTime.Compare(Times[0][0], GlobexSessionEndTime) == -1) {[/B]
    DateTime.Compare will return -1 if the first time is earlier than the second, 0 if the two times are the same, and greater than 0 if the first time is later than the second.

    You can read more about this in Microsoft's publicly available documentation here:

    Compares two instances of DateTime and returns an integer that indicates whether the first instance is earlier than, the same as, or later than the second instance.


    Does using DateTime.Compare seem to work as you'd expect?

    Thanks in advance; I look forward to assisting you further.

    Comment


      #3
      Hi Kate, thanks for your reply.

      I don't need the DateTime.Compare method because I know it works from other code I've created.

      It seems that I've been sleeping, because there are 2 typo's in my code, sorry about that.
      I used the >= twice, but it had to be >= and <=.
      This makes sure the codes is run when the time is between GlobexStart and GlobexEnd.

      Also I had:
      if (Low[0] > GlobexLow)
      and that had to be
      if (Low[0] < GlobexLow)
      to detect a new low that's lower than the current GlobexLow.

      I fixed them and now my code works like expected.
      I've bolded them for clarity.


      Code:
      if ((Times[0][0] >= GlobexSessionStartTime) && (Times[0][0][B]<=[/B]GlobexSessionEndTime))
      {
        Print(string.Format("{0} Globex Session is active...", Time[0]));
        Print(string.Format("{0} Globex High is: {1} ", Time[0], GlobexHigh));
        Print(string.Format("{0} Globex Low is: {1} ", Time[0], GlobexLow));
      if (High[0] > GlobexHigh)
      {
        GlobexHigh = High[0];
        Print(string.Format("{0} New Globex High found: {1} ", Time[0], GlobexHigh));
        string tag = GlobexSessionStartTime + TAG_SUFFIXHOR + "GlobexHigh";
        Draw.Line(this, tag, false, Time[0], GlobexHigh, GlobexSessionEndTime, GlobexHigh, GlobexLevelColor, PriceLevelLineDashStyle, PriceLevelLineThickness);
        Print(string.Format("{0} Print Horizontal: {1} ", Time[0], tag));
      }
      if (Low[0][B] < [/B]GlobexLow)
      {
        GlobexLow = Low[0];
        Print(string.Format("{0} New Globex Low found: {1} ", Time[0], GlobexLow));
        string tag = GlobexSessionStartTime + TAG_SUFFIXHOR + "GlobexLow";
        Draw.Line(this, tag, false, Time[0], GlobexLow, GlobexSessionEndTime, GlobexLow, GlobexLevelColor, PriceLevelLineDashStyle, PriceLevelLineThickness);
        Print(string.Format("{0} Print Horizontal: {1} ", Time[0], tag));
      }
      }
      Click image for larger version

Name:	2022-05-22 23_02_18-Chart - ES 06-22-GlobexLow.png
Views:	141
Size:	2.6 KB
ID:	1202431

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      633 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      364 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      567 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      568 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X