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

Previous Bar Time

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

    Previous Bar Time

    I am trying to figure out how call the time of the Previous bar. More specifically I want to get the difference of the current bar and previous bar in seconds.

    I have tried Time[1] - Time[0] and of course the ToTime int method but it says I cannot use array data which makes absolutely no sense because I have to store it as a value.

    Any help is appreciated thanks.

    #2
    Welcome to our forums - have you tried something like this?

    Code:
     
    int myTimeDiff = ToTime(Time[0]) - ToTime(Time[1]);
    This would give you the time different as an integer...

    i.e. on a 5 min chart you see 500 being returned.

    Forgot to add, you could also work with DateTime structures directly -

    Code:
     
    TimeSpan myTSDiff = Time[0] - Time[1];
    Last edited by NinjaTrader_Bertrand; 11-23-2009, 06:10 AM.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Appreciate the input Bertrand but these are the examples I am receiving errors for. More specifically I can enter your exact code and I get:

      CS0021: Cannot Apply indexing with [] to type 'int'

      I tried making it a double, a DateTime variable, and several other suggestions from the MSDN site. I can't figure out exactly why it will not accept the statement.

      Comment


        #4
        Hmm, please post the exact code you used - the first is an Int as ToTime would return this, the second a TimeSpan, the difference between two DateTime's.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Here is the the section with the error. I am not calling any special functions in any other part of the indicator, just a custom dataseries.

          Code:
           protected override void OnBarUpdate()
                  {
                      if (CurrentBar == 0)
                          Value.Set(Input[0]);
                      else
                      {    
                      double distance = (Close[1] - Close[0]) * (-1);
                      double volume = VOL()[0];
                      int myTimeDiff = ToTime(Time[0]) - ToTime(Time[1]);
                    
                      myDataSeries.Set(volume);
                          double DVTT = SMA(myDataSeries, fastMAPeriod)[0];
                      myDataSeries2.Set(distance);
                          double DVTT2 = SMMA(myDataSeries2, slowMAPeriod)[0];
                      
                      DVTPlot.Set(DVTT2);

          Comment


            #6
            jhtumblin,

            Which is the exact line it is complaining about?
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              This line is specifically throwing the error message:
              CS0021: Cannot Apply indexing with [] to type 'int'

              Code:
              int myTimeDiff = ToTime(Time[0]) - ToTime(Time[1]);

              Comment


                #8
                I put your line above into a strategy and it compiled for me at least, with no issue.

                As the line is, it is correct - you are not making an array access except where appropriate so not clear why the error is showing up for you.

                Could you perhaps put that line in a different file - like a new clean wizard generated template and try to compile and see if the error is local?

                Otherwise, it could something else in your code prior to that line that is really causing the error but not bubbling up until later in the compilation.

                Hope that helps a bit.

                Comment


                  #9
                  The snippet works well here, too - is it compiling if you comment this line out? From the code you posted you miss a few closing braces and also a check like this at the OnBarUpdate() start of the indicator code -

                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    OK, well I just scratched my whole indicator and started from a fresh wizard, magically it is compiling now. I didn't make any changes to the code at all so I still don't know what was causing the problem, but it's working nevertheless. I appreciate all the input from you guys, at least it's documented if anyone else runs across this issue. Thanks again.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Aviram Y, Today, 05:29 AM
                    0 responses
                    1 view
                    0 likes
                    Last Post Aviram Y  
                    Started by quantismo, 04-17-2024, 05:13 PM
                    3 responses
                    25 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by ScottWalsh, 04-16-2024, 04:29 PM
                    7 responses
                    34 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by cls71, Today, 04:45 AM
                    0 responses
                    6 views
                    0 likes
                    Last Post cls71
                    by cls71
                     
                    Started by mjairg, 07-20-2023, 11:57 PM
                    3 responses
                    216 views
                    1 like
                    Last Post PaulMohn  
                    Working...
                    X