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

Calculate time in seconds between the close of two bars

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

    Calculate time in seconds between the close of two bars

    What is the NinjaScript code for printing the time in seconds between two bars? Thanks

    #2
    imported post

    Try something like:

    Print(Time[0].Subtract(Time[1]).Seconds.ToString());

    Ray
    RayNinjaTrader Customer Service

    Comment


      #3
      imported post

      I added the statement below but when I do PRINT stops working (nothing prints). If I comment outthe statement you suggested PRINT works okay.

      seconds = Time[
      0].Subtract(Time[1]).Seconds;



      Print(
      "Bar Time " + CurrentBar.ToString("00000"));



      I also tried using just the PRINT statement and nothing prints.

      Print(Time[
      0].Subtract(Time[1]).Seconds.ToString());

      Comment


        #4
        imported post

        When something does not work always check the Log tab to see what errors may have been generated.

        I suspect the issue is that Time[1] is throwing an exception since on the 1st bar, Time[1 bar ago] does not yet exist. Therefore, try something like:

        if (CurrentBar > 0)
        Print(Time[0].Subtract(Time[1]).Seconds.ToString());


        Ray
        RayNinjaTrader Customer Service

        Comment


          #5
          imported post

          That was it. Thanks for your help.

          I notice that by adding the following statement that some bars exceed 60 seconds and therefore have a value in the minutes variable. Is there a way to return the "total elapsed seconds" including minutes and seconds?

          Time[
          0].Subtract(Time[1]).Minutes.ToString() + " " +

          Comment


            #6
            imported post

            Try

            Time[0].Subtract(Time[1]).TotalSeconds.ToString()

            Comment


              #7
              imported post

              Works great. Thanks again

              Comment


                #8
                imported post

                After Placing a limit Order I want to wait for a period of time for a fill, then cancel if not filled. I'm thinking of something like;

                if ( Time[0] > OrderTime.AddSeconds( AverageBarInterval(5)) )
                {
                AsmStrategyCancelEntryOrder(OrderId)
                }

                Comment


                  #9
                  imported post

                  That looks like it should work.
                  RayNinjaTrader Customer Service

                  Comment


                    #10
                    imported post

                    Hi Ray,
                    How do I make the AverageBarInterval() method?

                    Comment


                      #11
                      imported post

                      Under variables:

                      private DataSeries barInterval = null;
                      WithinInitialize()

                      barInterval = new DataSeries(this);
                      Within OnBarUpdate()

                      barInterval.Set(CurrentBar > 0 ?(double) Time[0].Second - Time[1].Second : 0);

                      if (Time[0] > OrderTime.AddSeconds((int) SMA(barInterval, 5)[0])
                      // Do something....
                      RayNinjaTrader Customer Service

                      Comment


                        #12
                        turning this into a histogram?

                        Originally posted by OnePutt View Post
                        I added the statement below but when I do PRINT stops working (nothing prints). If I comment outthe statement you suggested PRINT works okay.

                        seconds = Time[0].Subtract(Time[1]).Seconds;



                        Print("Bar Time " + CurrentBar.ToString("00000"));



                        I also tried using just the PRINT statement and nothing prints.

                        Print(Time[0].Subtract(Time[1]).Seconds.ToString());
                        Still learning ninjascript but how would I turn this into a simple histogram?

                        So far I have this in void Initialize()

                        Add(new Plot(new Pen(Color.Red, 3), PlotStyle.Bar, "seconds"));

                        And this in void OnBarUpdate()

                        Value.Set(Time[0].Subtract(Time[1]).Seconds);

                        It compiles ok but nothing displays on the chart.

                        Comment


                          #13
                          I suggest you check the Control Center logs for errors when you run it. I suspect you may run into an index error outlined in this tip: http://www.ninjatrader-support.com/v...ead.php?t=3170
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by Josh View Post
                            I suggest you check the Control Center logs for errors when you run it. I suspect you may run into an index error outlined in this tip: http://www.ninjatrader-support.com/v...ead.php?t=3170
                            Thanks Josh. That did it.

                            if (CurrentBar < 1)
                            return;
                            if (CurrentBar >= 1)

                            Value.Set(Time[0].Subtract(Time[1]).Seconds);

                            Comment


                              #15
                              The second if-statement is unnecessary. If CurrentBar is less than 1 we return and stop processing the rest of the OnBarUpdate() method. Anything that comes after will only be processed if CurrentBar is >= 1 by default.
                              Josh P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by xiinteractive, 04-09-2024, 08:08 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post xiinteractive  
                              Started by Irukandji, Today, 09:34 AM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by RubenCazorla, Today, 09:07 AM
                              1 response
                              5 views
                              0 likes
                              Last Post RubenCazorla  
                              Started by TraderBCL, Today, 04:38 AM
                              3 responses
                              25 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by WeyldFalcon, 08-07-2020, 06:13 AM
                              11 responses
                              1,423 views
                              0 likes
                              Last Post jculp
                              by jculp
                               
                              Working...
                              X