• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time difference between current bar and previous 2 bars

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

    Time difference between current bar and previous 2 bars

    I want to write to a straemfile the time difference between the current bar and the previous bars.

    Time[0] - Time[2] does not work as it give me compile error.

    Time[0].subtract(Time[2]) also does not work. I have created DatetimeSeries to capture data for previous bars..

    Any help please?

    Thanks
    Yeskannan

    #2
    Hello Yeskannan,

    Thanks for your post and welcome to the NinjaTrader forums.

    Time[n] is a complete date time and you want to find a time difference so the output would need to be a timespan.

    There are a couple of ways to do this:

    Print (ToTime(Time[0]) - ToTime(Time[2]));

    Would provide an integer output representing time. ToTime() is a Ninjascript method: https://ninjatrader.com/support/help...t7/?totime.htm

    Alternatively, using standard C# methods:

    TimeSpan diff = Time[0] - Time[2];
    Print ("Time difference: "+diff);

    Another variation:

    TimeSpan diff = Time[0].Subtract(Time[2]);
    Print ("Time difference: "+diff);

    Reference: https://www.dotnetperls.com/timespan
    PaulNinjaTrader Customer Service

    Comment


      #3
      Time difference between current bar and previous 2 bars

      Thanks Paul,

      I am not able to use these methods to write to a streamfile. I would like to write the difference in seconds between the bars as a text file.

      Anytime I try to use the C# method, there is an error .

      Thanks
      yeskannan

      Comment


        #4
        Hello yeskannan,

        Thanks for your reply.

        You may want to try the examples I posted with just the print statements first to see how they work.

        Once you decide which one you prefer you can then output to a streamwriter created file. If you haven't already seen this, here is an example out using streamwriter: https://ninjatrader.com/support/foru...ead.php?t=3475
        PaulNinjaTrader Customer Service

        Comment


          #5
          Error on caling 'OnBarUpdate' method for Indicator 'TickTime' on bar 0

          Paul,

          I tried doing this an indicator and also Streamfile writer and I keep getting the following error message. What am i doing wrong?

          "Error on caling 'OnBarUpdate' method for Indicator 'TickTime' on bar 0: Bar Index needs to be greater/equal 0"

          Comment


            #6
            Hello yeskannan,

            Thank you for your response.

            Please attach your indicator to your response so we may investigate this matter further.

            You can attach your indicator to your response by going to File > Utilities > Export NinjaScript > Export selected source files > select your Indicator > select the right arrow > Export. The file will be located under (My) Documents\NinjaTrader 7\bin\Custom\ExportNinjaScript.

            I look forward to your response.
            Patrick H.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Paul View Post
              Hello Yeskannan,

              Thanks for your post and welcome to the NinjaTrader forums.
              [snip]
              Unfortunately, you were not told that when you use Time[2], you must make sure that CurrentBar > 2. That is, you can't subtract 2 bars ago from the current bar unless you have a minimum of 3 bars, right?

              Add this check to the examples above,

              Code:
              if (CurrentBar > 2)
                Print (ToTime(Time[0]) - ToTime(Time[2]));
              
              if (CurrentBar > 2)
              {
                  TimeSpan diff = Time[0] - Time[2];
                  Print ("Time difference: "+diff);
              }
              Make sense?

              Comment


                #8
                Originally posted by yeskannan View Post
                "Error on caling 'OnBarUpdate' method for Indicator 'TickTime' on bar 0: Bar Index needs to be greater/equal 0"
                This was my clue that your code was not checking for a minimum amount of bars needed to perform the required processing.

                Many programmers start their OnBarUpdate by checking if there is a minimum number of bars required for processing, in fact, "BarsRequired" is a variable that you set which can be used specifically for this purpose,

                Code:
                protected override void OnBarUpdate()
                {
                    if (CurrentBar < BarsRequired)
                        return;
                
                    ... all code below here should now have enough bars ...
                
                }
                Read about BarsRequired and you'll see the documentation says it the minimum numbers of bars for plotting, and not for minimum number of bars needed for calculations.

                However, for most of my simple indicator needs, these two values are CLOSE ENOUGH to each other (aka to the "minimum number of bars required to calculate the indicator values"), so I tend to adopt BarsRequired for both purposes.

                Also, very important, set BarsRequired in OnStartUp,

                Code:
                protected override void OnStartUp()
                {
                    BarsRequired = 3;    // your code may have different needs
                }
                Good luck!

                Comment


                  #9
                  Thanks a lot bltdavid,

                  I just saw your reply and tried out the code:

                  if (CurrentBar < BarsRequired)
                  return;

                  It worked . Really appreciate your help.
                  I am slowly grasping the ideas.

                  Regards
                  yeskannan

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by kiss987, 10-14-2018, 10:30 AM
                  3 responses
                  12 views
                  0 likes
                  Last Post music_p13  
                  Started by music_p13, Today, 02:13 PM
                  0 responses
                  2 views
                  0 likes
                  Last Post music_p13  
                  Started by music_p13, Today, 01:41 PM
                  2 responses
                  5 views
                  0 likes
                  Last Post music_p13  
                  Started by Johnny, Today, 12:27 PM
                  2 responses
                  17 views
                  0 likes
                  Last Post Johnny
                  by Johnny
                   
                  Started by Trader17, Today, 12:34 PM
                  4 responses
                  19 views
                  0 likes
                  Last Post Trader17  
                  Working...
                  X