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

Accessing chart time value in code

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

    Accessing chart time value in code

    Hi Ninjas All

    I'm working on something where a parameter depends on the time value of a chart.

    Is there a way in code of accessing this value (whether it be 20 mins, 55 mins, 1729 seconds or any time value) so I can use it within an indicator?

    Thanks in advance for any advice.

    Added on edit:

    I think I've found a way of doing this:

    Code:
    Time[0].Subtract(Time[1]).TotalSeconds;
    gives the time in seconds between each bar.

    Trouble is, I'm experiencing this problem: I've added the following in OnBarUpdate:

    Code:
    if (CurrentBar < 60)
            		return;
    
    int BarValueSeconds = (int) Time[0].Subtract(Time[1]).TotalSeconds; // ***
    int BarValueMinutes = (int) BarValueSeconds / 60;
    	
    Print(BarValueMinutes);
    But in the Output Window, I'm getting the correct values but then an error message as follows

    5
    5
    5
    Error on calling 'OnBarUpdate' method for indicator 'BarNumbersCounting02c' on bar 1436: Object reference not set to an instance of an object.
    Even if I set line *** to:

    Code:
    int BarValueSeconds = (int) Time[[B][COLOR="Blue"]1[/COLOR][/B]].Subtract(Time[[B][COLOR="blue"]2[/COLOR][/B]]).TotalSeconds;
    I still have the problem.

    Any advice much appreciated and I'm wondering if there may be a more direct way of achieving this.
    Last edited by arbuthnot; 05-02-2015, 02:40 PM.

    #2
    Hello arbuthnot,

    Thank you for your post.

    In testing your code I did not find the same error. Could you provide the instrument, bar type and interval tested on?

    I look forward to your response.

    Comment


      #3
      Thanks very much, Patrick, for getting back to me on this at the weekend.

      Those lines of code are incorporated within a 'bigger' indicator. When used independently, there's no problem. When used within the other code, the problem sometimes recurs.

      However, I've found I can get the information I need using the BarsPeriod.Value method, which solves the problem almost completely, so the situation as I initially described in this thread is no longer relevant.

      But I do have this question: looking through Help on BarsPeriod, I couldn't find a way within NinjaScript of distinguishing between minute bars and second bars. It would help in my code if this were possible.

      I've a feeling this might not be possible but thanks for advising me on this.

      Cheers.

      Comment


        #4
        Originally posted by arbuthnot View Post
        Thanks very much, Patrick, for getting back to me on this at the weekend.

        Those lines of code are incorporated within a 'bigger' indicator. When used independently, there's no problem. When used within the other code, the problem sometimes recurs.

        However, I've found I can get the information I need using the BarsPeriod.Value method, which solves the problem almost completely, so the situation as I initially described in this thread is no longer relevant.

        But I do have this question: looking through Help on BarsPeriod, I couldn't find a way within NinjaScript of distinguishing between minute bars and second bars. It would help in my code if this were possible.

        I've a feeling this might not be possible but thanks for advising me on this.

        Cheers.
        BarsPeriod.Id ?

        Comment


          #5
          Much obliged to you for your suggestion, Koganam.

          Looking through Help, I did indeed try BarsPeriod.Id, but I obviously didn't do it right.

          What I'd like in the code would be on this basis:

          If ( Bar Type = Minutes )

          { ...actions... }

          If ( Bar Type = Seconds )

          { ...other actions... }

          According to Help, PeriodType.Minute is

          Only relevant for Kagi, LineBreak, and PointAndFigure Bars objects
          but if there is a workaround, I'd appreciate any advice as to how to do this.

          Cheers.

          Comment


            #6
            Originally posted by arbuthnot View Post
            Much obliged to you for your suggestion, Koganam.

            Looking through Help, I did indeed try BarsPeriod.Id, but I obviously didn't do it right.

            What I'd like in the code would be on this basis:

            If ( Bar Type = Minutes )

            { ...actions... }

            If ( Bar Type = Seconds )

            { ...other actions... }

            According to Help, PeriodType.Minute is



            but if there is a workaround, I'd appreciate any advice as to how to do this.

            Cheers.
            The example is right on the description page.
            Code:
            if (BarsPeriod.Id == PeriodType.Second) {...}

            Comment


              #7
              Originally posted by koganam View Post
              The example is right on the description page.
              Code:
              if (BarsPeriod.Id == PeriodType.Second) {...}
              As always, Koganam, you've come up trumps!

              I did this simple test:

              Code:
              if (BarsPeriod.Id == PeriodType.Second) BarColor = Color.Blue;
              if (BarsPeriod.Id == PeriodType.Minute) BarColor = Color.Red;
              and it worked.

              The trouble is, the Help guide does not give enough examples and is sometimes misleading. You do get the impression that this will only work for Kagi, etc. I now know better!

              I really hope they improve Help in NT8.

              Much obliged, Koganam.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by cmtjoancolmenero, Yesterday, 03:58 PM
              1 response
              17 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by benmarkal, Yesterday, 12:52 PM
              3 responses
              23 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by helpwanted, Today, 03:06 AM
              1 response
              19 views
              0 likes
              Last Post sarafuenonly123  
              Started by Brevo, Today, 01:45 AM
              0 responses
              11 views
              0 likes
              Last Post Brevo
              by Brevo
               
              Started by pvincent, 06-23-2022, 12:53 PM
              14 responses
              244 views
              0 likes
              Last Post Nyman
              by Nyman
               
              Working...
              X