Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

its tricky to calculate times for ToTime(Time[0]) condition

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

    its tricky to calculate times for ToTime(Time[0]) condition

    Hi,

    I like to get alerted 20 min before news, with indicator, that draws text on chart.

    Here is what I have so far.

    Code:
                int news1 = 150000; string newsS1 = @"news";
               
                if (news1>0 && ToTime(Time[0]) >= [B]"calculated time"[/B] && ToTime(Time[0]) <= news1)
                    DrawText("News1", false, newsS1, -4,  Close[0]-0*TickSize, 0, Color.Orange, new Font ("Arial", 8),  StringAlignment.Center, Color.Empty, Color.Empty, 0);
                else RemoveDrawObject("News1");
    How would I calculate the time "calculated time" for the condition below?

    Because its a bit complicated it depends, what time one is subtracting the 20min from. If from full hour one has to subtract 6000, if from lower (like 17:59 one needs to subtract 2000 only, and when subtracting from 17:19 one has to use 6000 again. Tricky.

    e.g. for
    150000 news time "calculated time" has to be 144000 (difference 6000)
    153000 news time "calculated time" has to be 151000 (difference 2000)

    Thanks for replying

    Thomas

    #2
    Thomas, to simplify this I would look into working with C# DateTime objects, those offer an Add method which you could use to subtract a custom timespan as well that would take care internally of the 'tricky' parts you noted.

    So something along the lines of this snippet -

    Code:
    DateTime startTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 15, 0, 0);
    TimeSpan offset = new TimeSpan(0, 20, 0);
    DateTime triggerTime = startTime.Add(-offset);

    Comment


      #3
      Originally posted by td_910 View Post
      Hi,

      I like to get alerted 20 min before news, with indicator, that draws text on chart.

      Here is what I have so far.

      Code:
                  int news1 = 150000; string newsS1 = @"news";
       
                  if (news1>0 && ToTime(Time[0]) >= [B]"calculated time"[/B] && ToTime(Time[0]) <= news1)
                      DrawText("News1", false, newsS1, -4,  Close[0]-0*TickSize, 0, Color.Orange, new Font ("Arial", 8),  StringAlignment.Center, Color.Empty, Color.Empty, 0);
                  else RemoveDrawObject("News1");
      How would I calculate the time "calculated time" for the condition below?

      Because its a bit complicated it depends, what time one is subtracting the 20min from. If from full hour one has to subtract 6000, if from lower (like 17:59 one needs to subtract 2000 only, and when subtracting from 17:19 one has to use 6000 again. Tricky.

      e.g. for
      150000 news time "calculated time" has to be 144000 (difference 6000)
      153000 news time "calculated time" has to be 151000 (difference 2000)

      Thanks for replying

      Thomas
      That is unfortunately a consequence of trying to use integers for an entity, time, that is not a continuous integer sequence, but rather one that uses Modulo 60 and Modulo 24 or Modulo 12 arithmetic.

      You will be altogether better of using the standard C# DateTime structures. For simple filters, those ToTime() functions are usually just fine. The rub is when you want to do arithmetic with the returns.

      Comment


        #4
        Hi koganam,

        Thanks for your reply.

        I'm not so familiar with C# time structures, can you give a clue?

        Thanks

        Thomas

        Comment


          #5
          Hi Bertrand,

          Many thanks for that. And how would I compare the close time of the bar with such a DateTime structure?

          Originally posted by NinjaTrader_Bertrand View Post
          Thomas, to simplify this I would look into working with C# DateTime objects, those offer an Add method which you could use to subtract a custom timespan as well that would take care internally of the 'tricky' parts you noted.

          So something along the lines of this snippet -

          Code:
          DateTime startTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 15, 0, 0);
          TimeSpan offset = new TimeSpan(0, 20, 0);
          DateTime triggerTime = startTime.Add(-offset);

          Comment


            #6
            Thomas, you can for example use the CompareTo method offered here -

            if (Time[0].CompareTo(triggerTime) == 0) // if bar time equals your trigger time do something...

            More helpful tips on working with DateTime objects in NinjaScript can be reviewed with this sample's contents - http://www.ninjatrader.com/support/f...ad.php?t=19292

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            571 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            331 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            549 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            550 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X