Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calling the time of the day

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

    Calling the time of the day

    Back to NT7, if you wanted to know the exact time of the day, one easy way was to use something like this:

    "ToTime(Time[0]) >= 170000" this meant "if actual time is later than 5.00pm then...."

    I imagine that it should be easy as well in NT8 but I can't find any quick sample, so I've had to code a little procedure, here below:

    variables
    private TimeSpan start;
    private TimeSpan end;
    private TimeSpan now;

    Initialize
    start = new TimeSpan(17, 0, 10);
    end = new TimeSpan(17, 14, 55);
    now = DateTime.Now.TimeOfDay;

    Code
    if ((now > start) && (now < end))
    {
    // Reset some variables
    }


    But is it another simpler way to call time of day in NT8 ?

    Thanks in advance
    Last edited by pstrusi; 09-25-2017, 05:17 AM.

    #2
    Hello,

    Thank you for the question.

    In NT8 ToTime is still used and is likely the most simple way to compare times in the platform as it converts to an integer.



    The code you posted that you have used in NT7 should be able to copy/paste into an NT8 script with no issue. Here is a simple example from the help guide modified to your posts syntax:

    if (ToTime(Time[0]) >= 170000)
    {
    // logic goes here
    }


    .
    Some items have changed, you can find a list of items that have changed at the following link: http://ninjatrader.com/support/helpG...ng_changes.htm


    Please let me know if I may be of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      I need to call and evaluate the time of the day constantly BUT out of the OnBarUpdate, because I need it regardless market activity. So, I have this extract to assemble the snippet:

      Code:
      // setting variables
      private TimeSpan start;
      private TimeSpan end;
      private TimeSpan now;
      
      //Initialize them
      start = new TimeSpan(13, 0, 10);
      end	= new TimeSpan(13, 14, 55);
      now = DateTime.Now.TimeOfDay;
      
      //Code to use
      if ( (now > start) && (now < end) )
      {
      // Flatten or Reset some variables
      }
      Which time event-driven method would fit it the best to do this simple function or how to create a simple method to perform this basic activity independently?

      Perhaps something like this:

      Code:
      private DoSomething(TimeSpan start, TimeSpan end)
      {
          if (  (DateTime.Now.TimeOfDay > start) && (DateTime.Now.TimeOfDay < end)  )
          {
              specialflag=true;
              
          }
          else
          {
             specialflag=false;
          }
          return specialflag;
      }
      Thanks in advance
      Last edited by pstrusi; 09-26-2017, 04:47 AM.

      Comment


        #4
        Hello,

        Thank you for the reply.

        I wanted to check, are you trying to get the most recent bars time or the computers time? I would never suggest using DateTime.Now unless you are specifically trying to get the computers time. DateTime.Now has no relationship with the bars on your chart as the script makes progress.

        If you are instead trying to get the most recent bars time or last ticks time, you could either store the Time object as a variable from OnBarUpdate or use the GetTime method where your logic is.



        One example would be in OnRender, because this is called frequently for data events or can also be forced to render you could place logic in this override. OnRender does require using the Get methods like GetClose or GetTime to accurately get bars as this is out of the event context.

        Alternatively, you could look into using a Timer in the case you need to "poll" for something. Timers are just a C# concept, we do have an example here: https://ninjatrader.com/support/help...lightsub=timer

        The method you made would be a good way of isolating common code, but you would need to determine if the computers time is what you need or the most recent bars time and adjust as needed.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi Jesse, thanks for the replay; useful, I'll figure it out some workaround.

          Wouldn't be great that NT8 had an Event-driven override method that was based only in Datetime ? This will increase flexibility to do many things.

          Regards

          Comment


            #6
            hello Jesse,

            I would like know at what time high price occur in 5 minutes bar chart. (HH:mm:ss)

            thank you

            Comment


              #7
              Hello oceanis400,

              Thanks for your question.

              You could store your Highest High and the Bar Index for your Highest High whenever the current High[0] is greater than your Highest High. With the Highest High tracked and the Bar Index noted, you can use Bars.GetTime as Jesse has posted in post #4 to get the time of that bar.

              For example:

              Code:
              private int HighestHighIndex = 0;
              private double HighestHighPrice = 0;
              
              protected override void OnBarUpdate()
              {
                  if (High[0] > HighestHighPrice)
                  {
                      HighestHighPrice = High[0];
                      HighestHighIndex = CurrentBar;
                  }
              
                  Print("Highest High Bar Time: " + Bars.GetTime(HighestHighIndex));
              }
              Please let me know if you have any questions.
              JimNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by bmartz, 03-12-2024, 06:12 AM
              5 responses
              32 views
              0 likes
              Last Post NinjaTrader_Zachary  
              Started by Aviram Y, Today, 05:29 AM
              4 responses
              13 views
              0 likes
              Last Post Aviram Y  
              Started by algospoke, 04-17-2024, 06:40 PM
              3 responses
              28 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by gentlebenthebear, Today, 01:30 AM
              1 response
              8 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by cls71, Today, 04:45 AM
              1 response
              8 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Working...
              X