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 between bars

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

    Time between bars

    Hi everyone,

    I'm working with unirenko bars and am trying to build an indicator that shows the time
    elapsed between one bar and another (in seconds). For me, it should be as simple as a substraction between the opening time of a bar and its previous, but I can't get opening time, well I should say I don't know if it can be extracted as it is or calculated using other data/function. My programming level is 0.

    Thanks in advance for your help.

    #2
    Hello jmgnajar,

    Thanks for the note.

    You can access the timestamp of a processing bar with the Times[0][0] array for the primary series.

    Code:
    protected override void OnBarUpdate()
            {
                            Print(Times[0][0]);
    			if (FirstTickOfBar)
    			{
    				DateTime openTime = Times[0][0];
    			}
            }
    https://ninjatrader.com/support/help...nt7/?times.htm - Times[][] array.

    FirstTickOfBar can only be used if your script is calculating on each tick.

    With CalculateOnBarClose = true, using Times[0][0] will give you the closing timestamp of the bar.

    The Times array holds DateTime objects. For more information on working with DateTime objects see here:
    Represents an instant in time, typically expressed as a date and time of day.


    You can use the compare method provided by the .NET framework:
    Compares two instances of DateTime and returns an integer that indicates whether the first instance is earlier than, the same as, or later than the second instance.


    Or, to access the day, hour, minute, etc:

    Code:
    System.DateTime moment = new System.DateTime(
    								1999, 1, 13, 3, 57, 32, 11);
    // Year gets 1999.
    int year = moment.Year;
    
    // Month gets 1 (January).
    int month = moment.Month;
    
    // Day gets 13.
    int day = moment.Day;
    
    // Hour gets 3.
    int hour = moment.Hour;
    
    // Minute gets 57.
    int minute = moment.Minute;
    
    // Second gets 32.
    int second = moment.Second;
    
    // Millisecond gets 11.
    int millisecond = moment.Millisecond;


    Please let us know if we may be of any further assistance.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by jmgnajar View Post
      ... it should be as simple as a substraction between the opening time of a bar and its previous, but I can't get opening time...
      "Opening time of a bar" does not exist.

      Assuming CalculateOnBarClose=True, the general concept as used in NinjaTrader is 'Closing time of the bar', not opening.

      You're on the right track.

      "Time[0]" or "Times[0][0]" is the closing time of the most recently closed bar.

      "Time[1]" or "Times[0][1]" is the closing time of the bar previous to the most recently closed bar.

      I think you'll want to do "Time[0] - Time[1]", which returns a TimeSpan.
      Last edited by bltdavid; 04-02-2018, 08:45 PM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by PaulMohn, Today, 03:49 AM
      0 responses
      1 view
      0 likes
      Last Post PaulMohn  
      Started by inanazsocial, Today, 01:15 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Jason  
      Started by rocketman7, Today, 02:12 AM
      0 responses
      10 views
      0 likes
      Last Post rocketman7  
      Started by dustydbayer, Today, 01:59 AM
      0 responses
      2 views
      0 likes
      Last Post dustydbayer  
      Started by trilliantrader, 04-18-2024, 08:16 AM
      5 responses
      23 views
      0 likes
      Last Post trilliantrader  
      Working...
      X