Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using computer time clock

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

    Using computer time clock

    Dear support,

    I try various way but I cannot find solution.
    I need to synchronize my strategy with Computer Clock.
    I use DataTime.Now and it work fine but I have 2 questions:

    1: When there is no Tick Variation the strategy do not work. Example if I need "do something" at 15:30:00 and there is no price (Tick) variation the strategy do not work.
    Is possible to solve it, is possible to do something although no tick movement?

    2: DateTime.Now give Data+ Hour ... how can i get only hour, minutes, second from Pc Clock please? The same of (ToTime(Time[0]) but getting Pc Clock?

    Many thanks!

    #2
    Hello ClauTrade, and thank you for your questions.

    NinjaTrader is event driven software. This means that, as you have observed, its methods will only be called on price updates. There are two strategies which can allow you to work around this.

    First, the sample code in this forums post demonstrates how to set up a custom timer event.


    Second, you can use programming logic to execute during a window of time, rather than a single time. You can ensure this only executes once per day with a boolean flag that is reset when FirstBarOfSession and FirstTickOfBar are both true.

    Please let us know if there are any other ways we can help.


    To your second question, the NinjaTrader installation itself is actually pulling its time from your PC clock, and so DateTime.Now should match your PC clock's time at the time of execution. ToTime(DateTime.Now) should retrieve this time.
    Last edited by NinjaTrader_JessicaP; 11-15-2016, 12:50 PM.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hello Jessica,

      many thanks ...your suggests are interesting but too much advanced for me (... new in NT).

      I think solve my strategy with TimeSpan, is not so fast because depend from "Tick update" but I think it can work.

      Anyway I have an error, I cannot compare two variables, can you help me please?

      This is the input variable:
      Code:
      private TimeSpan timenews = new TimeSpan(8,30,00);
      Then the new variable about the actual Time from PC Clock (only HH,MM,SS):
      Code:
      protected override void OnBarUpdate()
                  
              
              {            
              if (Position.MarketPosition == MarketPosition.Flat);
              {
                  TimeSpan myActSpan = new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
                  Print ("Time var " + timenews);
                  Print ("Time act " + myActSpan);
              }
      I print the output data and it work fine:

      Time var 08:30:00
      Time act 12:06:14
      Time var 08:30:00
      Time act 12:06:17
      Time var 08:30:00
      Time act 12:06:17

      When I try to compare the 2 variable I get error due the name "myActSpan" do not exist.

      Code:
      [COLOR=SlateGray][COLOR=Black]                            
              if (myActSpan >= timenews);
              
              {
                  DrawDiamond("My diamond" + CurrentBar, false, 0, Close[0], Color.Red);
              }[/COLOR][/COLOR]


      Can you suggest how can I compare the two Times?
      Thanks
      Last edited by ClauTrade; 11-16-2016, 07:16 AM.

      Comment


        #4
        The recipe code I had in mind does not use the TimeSpan object, but rather two DateTime objects and some comparator objects. I am providing a code sample, I will be happy to answer any clarification questions that arise.

        Code:
        [FONT=Courier New]private boolean hasExecutedToday = false;
        protected override void OnBarUpdate()
        {
            // reset at the beginning of the trading day
            if (FirstBarOfSession && FirstTickOfBar)
            {
                hasExecutedToday = false;
            }
        
            // We close at 5 PM, start checking for a close at 4:30
            if (ToTime(Time[0]) > 163000 && ! hasExecutedToday)
            {
                hasExecutedToday = true;
        
                // just as an example, this will flatten us at the end of the day
                ExitLong();
                ExitShort();
            }
        }
        [/FONT]
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Hi Jessica,

          many thanks, I'll try your suggest .. really it would be better to set the time value with format "04:00:00" in input variable. With ToTime I must use "int" in format"040000".

          Jessica please ... have you an idea about the reason I get error to compare two similar variable (see question in my last post).

          if (myActSpan >= timenews);

          Have you see? In the output print they have same format and are TimeSpan.

          Thanks

          Comment


            #6
            To clarify TimeSpans, a TimeSpan object is the difference between 2 DateTime objects. For instance, both of these will return TimeSpan(8,30,00) :

            Set up :
            Code:
            [FONT=Courier New]int year = 2016;
            int month = 1; // January
            int day = 1;
            int eightAM = 8;
            int thirtyMinutes = 30;
            int midnight = 0;
            int onTheHour = 0;
            int fourPM = 16;
            int zeroSeconds = 0;
            int zeroMilliseconds = 0;[/FONT]
            Example 1 :
            Code:
            [FONT=Courier New]new TimeSpan(8, 30, 00) == (new DateTime(year, month, day, eightAM, thirtyMinutes, zeroSeconds, zeroMilliseconds) - new DateTime(year, month, day, midnight, onTheHour, zeroSeconds, zeroMilliseconds))[/FONT]
            Example 2:
            Code:
            [FONT=Courier New]new TimeSpan(8, 30, 00) == (new DateTime(year, month, day, fourPM, thirtyMinutes, zeroSeconds, zeroMilliseconds) - new DateTime(year, month, day, eightAM, onTheHour, zeroSeconds, zeroMilliseconds))[/FONT]
            If you would like to work with TimeSpan objects, then, you will probably want to rename myActSpan to myActTime, and then initialize myActTime with a new DateTime object as I have done in my above code examples. You will then be able to compare myActTime to other DateTime objects directly to generate TimeSpan objects.
            Last edited by NinjaTrader_JessicaP; 11-17-2016, 09:35 AM.
            Jessica P.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            558 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            324 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
            545 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            547 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X