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

Alert at certain time minus 5 minutes

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

    Alert at certain time minus 5 minutes

    Hi all. It's me. Again.

    I want to build a sound alert into my strategy that goes off 5 minutes before a chosen time (say a scheduled news release at 10:00 am).

    My first idea was this:
    if (Times[0][0].TimeOfDay > new TimeSpan(9, 54, 00) && Times[0][0].TimeOfDay < new TimeSpan(10, 00, 00))
    This way I would get a sound signal 5 times from 9:55 to 9:59. I like this, but in order to maintain it I need to type two times per alert. Not handy and error prone.

    My second idea was this:
    if (ToTime(Time[0]) >= NewsTime1Warning && ToTime(Time[0]) < NewsTime1))
    I would define NewsTime1 to be 100000 and NewsTime1Warning would be NewsTime1 minus 5 minutes.
    I like this better because I only have to type one time instead of two.

    Unfortunately, I cannot find an elegant solution here. While I would need to subtract 500 from say 93000 (to become 92500) I would need to subtract 4500 from 100000 (to become 95500). I can certainly write this in a long if statement, but it does not seem good coding and I am sure there is a function that does what I wish to do.

    Would anyone be so kind and give me a hint, please?

    P

    #2
    Hello pennywise,

    Thanks for your post.

    You could use the time span subtract method to subtract one timespan from another. For example, you can create a time span for your event and then create another timespan for your "warning" and then check to see of the bar close time is equal to the event time - the warning time. If you have several events you could use the same warning time span on each. Here is an example:



    Click image for larger version

Name:	Pennywise-1.PNG
Views:	141
Size:	104.1 KB
ID:	1060346


    There are probably better ways to do this and perhaps other community members will contribute to this as well.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thank you, PaulH, this is much better than what I had and works perfectly fine. Thank you.

      Just one question: Times[0][0].TimeOfDay accesses the time of my computer while ToTime(Time[0]) accesses the time of a bar within NinjaTrader, correct?

      Is my understanding correct? If yes, do you see an issue with using computer time? What if it happens to be off? Can NinjaTrader time be off?

      P

      Comment


        #4
        Hello pennywise,

        Thanks for your reply.

        The time series Time[0] and Times[0][0] are actually the same thing. In a single data series script, Time[0] is typically used as it points to the bar close time of the current bar of the primary data series. Times[0][0] is typically used in a multi-series script (and can be used in a single series script) when the first [] would point to the number of the data series and the second[] would point to the bar to reference.

        Both Time[0] and Times[0][0] are DateTime data types meaning they hold both a date and the time value (The date is the date of the bar and the time is the close time of the bar). Using Times[0][0] with the .TimeOfDay returns a timespan value that is just the time only without the date. (Time[0].TimeOfDay would also return the same thing)
        References:



        The ToTime() method returns an integer value representing time. Reference: https://ninjatrader.com/support/help...us/?totime.htm

        Here is an educational reference sample you may want to review/use for further understanding of date/time: https://ninjatrader.com/support/help...me_objects.htm

        DateTime and TimeSpan structures are C# and you can find further information on how to use them from various sites on the internet by searching for C# Date Time and or timeSpan. Here are a couple of links that may also be helpful:
        Compute date and time values. Get relative dates and call DateTime constructors and properties.

        Use TimeSpan structs. TimeSpan represents a period of time and has many helpful methods.

        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Thank you, PaulH, fully clear now.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by andrewtrades, Today, 04:57 PM
          1 response
          6 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by chbruno, Today, 04:10 PM
          0 responses
          5 views
          0 likes
          Last Post chbruno
          by chbruno
           
          Started by josh18955, 03-25-2023, 11:16 AM
          6 responses
          436 views
          0 likes
          Last Post Delerium  
          Started by FAQtrader, Today, 03:35 PM
          0 responses
          7 views
          0 likes
          Last Post FAQtrader  
          Started by rocketman7, Today, 09:41 AM
          5 responses
          19 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Working...
          X