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

Checking for time in OnStartUp

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

    Checking for time in OnStartUp

    Hello,

    I want to wait for a specific time to send only one message and then start a strategy, so I want to do this under OnStarUp only one time before all the strategy other methods. I try this but the platform hang up:

    protected override void OnStartUp()
    {
    while (DateTime.Now.TimeOfDay < triggerTime) // triggerTime is the time when I want to print the message
    {
    Print (" Waiting for this time: " + triggerTime.ToString());
    }
    Print (" Time Match!!!! ");
    }

    Thanks

    #2
    Hello federico,

    Thanks for opening the thread.

    I am noticing the platform crashes when I place a loop comparing the DateTime. As NinjaTrader 7 is in a freeze, I will not be able to submit this matter for review and a workaround must be used.

    I would suggest to create a return condition for OnBarUpdate() to prevent the code from executing unless a certain time of day is reached.

    Code:
    protected override void OnBarUpdate()
    {
    	if (DateTime.Now.TimeOfDay <= triggerTime.TimeOfDay)
    	{
    		Print("Message");
    		return;
    	}
    Please let me know if I may be of further help.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thank you,

      Your code is fine, but I need to get total exact time to trigger some logic, so, if you put this under "OnBarUpdate()", you will have to wait for the next tick to check time, and this could take even seconds to coming in a new tick, so this loss accuracy, specially if you need a 1 second or less of precision.

      BR,
      Federico

      Comment


        #4
        Hello federicoo,

        Thank you for your response.

        You could utilize a timer to perform the same check. Please refer to the BarTimer indicator code in the NinjaScript Editor for an example of using a timer.

        Please let me know if you have any questions.

        Comment


          #5
          Thanks,

          Having the same issue, because the calls are under OnBarUpdate() and this have not a specific time precision taking place...

          Comment


            #6
            Hello frederico,

            Thanks for writing back.

            OnBarUpdate() requires ticks to place new bars. If you need some code to execute based on time without the need for incoming ticks, you will have to use a timer.

            In addition to the BarTimer indicator, there is a sample provided on our forums that demonstrated how to apply a timer to a NinjaScript. The sample uses OnStartUp() to start the timer so it may by closer to what you are working for.

            SampleCustomEvent - http://ninjatrader.com/support/forum...ead.php?t=5965

            Please let me know if this does not resolve your inquiry.
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Barry Milan, Today, 10:35 PM
            0 responses
            2 views
            0 likes
            Last Post Barry Milan  
            Started by DJ888, Yesterday, 06:09 PM
            2 responses
            9 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by jeronymite, 04-12-2024, 04:26 PM
            3 responses
            40 views
            0 likes
            Last Post jeronymite  
            Started by bill2023, Today, 08:51 AM
            2 responses
            16 views
            0 likes
            Last Post bill2023  
            Started by sidlercom80, 10-28-2023, 08:49 AM
            167 responses
            2,260 views
            0 likes
            Last Post jeronymite  
            Working...
            X