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

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 Jon17, Today, 04:33 PM
            0 responses
            1 view
            0 likes
            Last Post Jon17
            by Jon17
             
            Started by Javierw.ok, Today, 04:12 PM
            0 responses
            6 views
            0 likes
            Last Post Javierw.ok  
            Started by timmbbo, Today, 08:59 AM
            2 responses
            10 views
            0 likes
            Last Post bltdavid  
            Started by alifarahani, Today, 09:40 AM
            6 responses
            41 views
            0 likes
            Last Post alifarahani  
            Started by Waxavi, Today, 02:10 AM
            1 response
            20 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Working...
            X