Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

built in bar time-span variable?

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

    built in bar time-span variable?

    The following 2 lines of code are in the OnBarUpdate region of my indicator, requiring manual update of BarsInADay to correspond to the current bar time-span I am using. Is there a built in bar variable providing the bar-time which I could use to make the BarsInADay calculation automatic?

    BarsInADay=24;//60 minute bars
    if (CurrentBar <= BarsInADay*2) return;//*2 because need 2 days

    Should I invoke the same test in the strategy OnBarUpdate region also?

    #2
    It sounds like you could get the same result by using the session end. Your bar will always be time stamped at the close.

    In your example you could use first bar of session and a custom counter to say once two days have passed, begin processing.

    LanceNinjaTrader Customer Service

    Comment


      #3
      Thanks very much for the help. I'm pretty sure I can figure out from here how to formulate the custom counter. However info on how to formulate the custom counter will be much appreciated.[ I realize I am unabashedly asking you to do my coding for me so will seriously fully understand if you do not respond ].

      Also, Would it make any sense for me to invoke the same test in the strategy OnBarUpdate region as I am invoking in the indicator OnBarUpdate region?

      Comment


        #4
        You could do something like this (where sessionCount is a custom variable) in your OnBarUpdate

        Code:
        OnBarUpdate()
        {
        if (Bars.FirstBarOfSession && sessionCount < 2)
        				sessionCount++;
        			
        if (sessionCount < 2) 
        
        //rest of your code that gets handled at least 2 sessions are loaded
        }
            return;
        I do think it would be a good idea to have this in both your strategy and indicator if this is required for the indicator and the strategy uses it.
        LanceNinjaTrader Customer Service

        Comment


          #5
          Many thanks again for your valuable help.

          Comment


            #6
            Regarding
            if (Bars.FirstBarOfSession && sessionCount < 2) sessionCount++;

            Bars.FirstBarOfSession is true if the bar is the first bar of a session; otherwise, false.

            If && means Boolean 'and' [so far I haven't been able to verify that] then must I not set a custom flag saying a first bar has been encountered because FirstBarOfSession is false after the 1st bar?

            Also I think the required number of bars encountered is not a constant value of 2, but depends on the bar-time [eg] one hour bars would require 48 bars to have lapsed for the required 2 days to have lapsed, not 2 bars?

            Comment


              #7
              Kindly let me know if what I said in my most recent reply [just previous to this reply] is correct or incorrect ... in which case I can then take it from there [ie] I don't want to be following and working on a solution based on incorrect assumptions.

              Comment


                #8
                "sessionCount < 2" means 2 sessions, regardless the number of bars within each session, not 2 bars.

                Comment


                  #9
                  Yep, I was confused alright.
                  Still not able to make your logic work even though I think I now know what the logic is doing ... nice little piece of code. I am beginning to understand and appreciate the richness of the NinjaScript function library. I have a lot with which to familiarize myself so am going to just use my code [ie]

                  BarsInADay=24;//60 minute bars
                  if (CurrentBar <= BarsInADay*2) return;//*2 because need 2 days


                  and will come back to streamlining it with your code later. Like most others, I'm in a time bind so don't want to mess with it any more at present.

                  Comment


                    #10
                    I am trying once again to make Lance's code work for me. Displayed below is my current attempt.
                    The two debug print statements tell me that sessionCount never changes [ie] always = 0. Therefore logic flow never goes beyond “if (sessionCount <= 3) return;”. Any thoughts or suggestions will be much appreciated.

                    static int sessionCount;
                    if (Bars.FirstBarOfSession) sessionCount++;
                    Print("time= "+Time);
                    Print("sessioncount="+sessionCount);
                    if (sessionCount <= 3) return;



                    Comment


                      #11
                      You'll want to make sure

                      int sessionCount;

                      is not defined within OnBarUpdate()

                      If you place it within the variables section above this should work for you.

                      As is your variable is being reset every bar update.

                      Let me know if I can further assist.
                      LanceNinjaTrader Customer Service

                      Comment


                        #12
                        I should have been clearer and more detailed in my post. sessionCount is defined in the Variables region as follows so the problem must be elsewhere?
                        #region Variables
                        // Wizard generated variables
                        private int myInput0 = 1; // Default setting for MyInput0
                        // User defined variables (add any user defined variables below)
                        ...
                        ...
                        ...
                        public double midptyestyest;
                        public int BarsInADay;
                        static int sessionCount;
                        #endregion

                        Comment


                          #13
                          Hello,

                          In this case could you provide me with a simplified version of a complete script that replicates the error?

                          Located in (MY)Documents\NinjaTrader 7\bin\Custom\Indicator
                          LanceNinjaTrader Customer Service

                          Comment


                            #14
                            script is attached

                            Unmodified script is attached. I can strip it down if you prefer.

                            Many thanks for the help.
                            Attached Files

                            Comment


                              #15
                              I've tested the script on my end and it appears to be working

                              I've attached the image I see

                              You may want to try deleting the script and then re-adding it. After re-adding it open any script and press F5 to compile.

                              If this doesn't resolve the issue please send mail to support[at]ninjatrader[dot]com and reference this thread
                              Attached Files
                              LanceNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              634 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              364 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              105 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              567 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              568 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X