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

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);
    BertrandNinjaTrader Customer Service

    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
            BertrandNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by zstheorist, Today, 07:52 PM
            0 responses
            3 views
            0 likes
            Last Post zstheorist  
            Started by pmachiraju, 11-01-2023, 04:46 AM
            8 responses
            149 views
            0 likes
            Last Post rehmans
            by rehmans
             
            Started by mattbsea, Today, 05:44 PM
            0 responses
            5 views
            0 likes
            Last Post mattbsea  
            Started by RideMe, 04-07-2024, 04:54 PM
            6 responses
            33 views
            0 likes
            Last Post RideMe
            by RideMe
             
            Started by tkaboris, Today, 05:13 PM
            0 responses
            5 views
            0 likes
            Last Post tkaboris  
            Working...
            X