Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    Kate W.NinjaTrader Customer Service

    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:	105
Size:	2.6 KB
ID:	1202431

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Jonker, 04-27-2024, 01:19 PM
      3 responses
      23 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by businessman1929, 04-29-2024, 01:28 PM
      2 responses
      21 views
      0 likes
      Last Post businessman1929  
      Started by bltdavid, 03-27-2023, 05:32 AM
      18 responses
      347 views
      0 likes
      Last Post ETFVoyageur  
      Started by NM_eFe, Today, 05:15 PM
      0 responses
      5 views
      0 likes
      Last Post NM_eFe
      by NM_eFe
       
      Started by vitaly_p, Today, 05:09 PM
      0 responses
      6 views
      0 likes
      Last Post vitaly_p  
      Working...
      X