Announcement

Collapse
No announcement yet.

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()
            DierkNinjaTrader Customer Service

            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 M_ichel, Today, 08:38 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post M_ichel
                              by M_ichel
                               
                              Started by soon123, Today, 12:18 AM
                              1 response
                              20 views
                              0 likes
                              Last Post NinjaTrader_PaulH  
                              Started by space_trader, Today, 12:52 AM
                              1 response
                              20 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by ybhx0315, Yesterday, 09:06 PM
                              0 responses
                              21 views
                              0 likes
                              Last Post ybhx0315  
                              Started by parkinsonbr, Yesterday, 08:23 PM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X