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

Time-delay issue

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

    Time-delay issue

    Hi,

    I'm trying to use a time-delay in my strategy. I want to simply do some actions "on bar close", then wait 2 minutes, and perform some more actions. I'm using the following code to attempt to "wait 2 minutes":

    if(Time[0].Subtract(Time[0]).Minutes > 2){Print( "2 Minutes... ");}

    This compiles fine but doesn't seem functional, the message never prints to the Output Window. I have changed the "calculateonbarclose" to "false" to enable tick-by-tick processing (I'm using "Firsttickofbar" for the "CalculateonBarclose" stuff) .
    I also thought about manipulating this code from the Help:

    // Checks if current time is greater than the bar time stamp
    if (DateTime.Now.Ticks > Time[0].Ticks)
    Print("Do something");


    I'm not sure how to go about correcting this code to have it perform as I wish...any suggestions appreciated.

    #2
    For sure that will not work since you are subtracting the same time instance from itself.

    You will want to do:

    if(Time[0].Subtract(Time[1]).Minutes > 2){Print( "2 Minutes... ");}
    RayNinjaTrader Customer Service

    Comment


      #3
      Oh, thanks, nice catch. That works however it seems to be printing upon every tick of the bar, regardless of whether the tick occurs within the 2 minutes or not...

      Is there a way (within the "FirstTickofBar" code) to set the current time of that "first" tick and then use this to determine if 2 minutes have elapsed from that point? From there I suppose I could use a variable to prevent additional processing of the code upon each tick...

      Comment


        #4
        You could store local DateTime variables and on first condition, assign DateTime.Now, then check against DateTime.Now later etc...
        RayNinjaTrader Customer Service

        Comment


          #5
          Thank you Ray. So I should do something like this?:

          if(FirstTickofBar)
          {
          double Start = DateTime.Now;
          }

          if(Start.Subtract(Time[1]).Minutes > 2){Print( "2 Minutes... ");}

          I'm not sure if that syntax is correct?

          Comment


            #6
            No

            DateTime start = DateTime.Now

            then later

            DateTime.Now.Subtract(start).Minutes > X
            RayNinjaTrader Customer Service

            Comment


              #7
              OK thank you, I'll try it out.

              Comment


                #8
                Sorry for my confusion, it still prints on each tick regardless of what time it is, I'm doing:

                #region Variables
                DateTime start;
                #endregion

                protectedoverridevoid OnBarUpdate()
                {
                if (FirstTickOfBar)
                {
                start = DateTime.Now;
                }

                if(DateTime.Now.Subtract(start).Minutes > 2){Print( "2 Minutes... ");}
                }

                Comment


                  #9
                  Then the condition must always be true, you would have to debug to see what values you are comparing etc...
                  RayNinjaTrader Customer Service

                  Comment


                    #10
                    Thanks again, Ray. I think I've got that worked out but I notice another issue. Since the running of the code is dependent on the updates of each tick, this leads to a condition where the desired code will be run only at or (almost always) some time after the "2 minute condition" has elapsed...depending on when the ticks happen.

                    My question is if there is some way to "force" the "2 minute condition" without relying on the randomness of ticks...perhaps NT has an internal clock or can access the Windows system clock in order to ensure an "absolute" condition of 2 minutes of elapsed time?

                    Comment


                      #11
                      You certainly could code that in NinjaScript. Please check out the timer reference sample I referred you to before: http://www.ninjatrader-support.com/v...ead.php?t=5965

                      Comment


                        #12
                        Thanks Dierk, that helps.

                        Comment


                          #13
                          Hello again,

                          Regarding that code in the Reference sample. It seems to be creating a "constant" time delay/event.

                          if (CurrentBar > 0)
                          {
                          // Initiate our Timer object with an interval of 120000ms (2 minutes)
                          myTimer.Tick += new EventHandler(TimerEventProcessor);
                          myTimer.Interval = 120000;
                          myTimer.Start();
                          }

                          I'm trying to "reset" it upon the Close of each bar. Thus on each new bar on the chart I'd like the Timer to begin counting from that point until 2 minutes have elapsed. The way it is now is that it just constantly begins a new event every 2 minutes, regardless of whether there are new bars or not...I've tried changing "(CurrentBar > 0)" to "(FirstTickofBar)" but saw no difference in results. It must be possible to accomplish that?

                          Comment


                            #14
                            Yes it is for sure but this you are really are into general programming now and not NinjaScript. Apart from the guidance we have so far provided we are unable to provide any more help in this area.
                            RayNinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by rajendrasubedi2023, Today, 09:50 AM
                            0 responses
                            2 views
                            0 likes
                            Last Post rajendrasubedi2023  
                            Started by ender_wiggum, Today, 09:50 AM
                            0 responses
                            1 view
                            0 likes
                            Last Post ender_wiggum  
                            Started by bmartz, Today, 09:30 AM
                            1 response
                            7 views
                            0 likes
                            Last Post NinjaTrader_Erick  
                            Started by geddyisodin, Today, 05:20 AM
                            3 responses
                            21 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Started by lorem, Today, 09:18 AM
                            1 response
                            5 views
                            0 likes
                            Last Post lorem
                            by lorem
                             
                            Working...
                            X