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

Question on @MAX and @MIN. What is the role of "CurrentBar < thisBar"?

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

    Question on @MAX and @MIN. What is the role of "CurrentBar < thisBar"?


    Hi,

    I would like to better understand the role of this condition statement included in both @MAX.cs and @MIN.cs.
    CurrentBar < thisBar




    What are a few examples of situations that demonstrate when CurrentBar < thisBar will equate to 'True'?


    Thanks!
    HedgePlay





    Below is unmodified default NT8 code for @MAX.cs pasted in just to provide a handy context reference.


    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
    /// <summary>
    /// The Maximum shows the maximum of the last n bars.
    /// </summary>
    public class MAX : Indicator
    {
    private int lastBar;
    private double lastMax;
    private double runningMax;
    private int runningBar;
    private int thisBar;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionMAX;
    Name = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meMAX;
    IsOverlay = true;
    IsSuspendedWhileInactive = true;
    Period = 14;
    
    AddPlot(Brushes.DarkCyan, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meMAX);
    }
    else if (State == State.Configure)
    {
    lastBar = 0;
    lastMax = 0;
    runningMax = 0;
    runningBar = 0;
    thisBar = 0;
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (CurrentBar == 0)
    {
    runningMax = Input[0];
    lastMax = Input[0];
    runningBar = 0;
    lastBar = 0;
    thisBar = 0;
    Value[0] = Input[0];
    return;
    }
    
    if (CurrentBar - runningBar >= Period || [SIZE=14px][B]CurrentBar < thisBar[/B][/SIZE])
    {
    runningMax = double.MinValue;
    for (int barsBack = Math.Min(CurrentBar, Period - 1); barsBack > 0; barsBack--)
    if (Input[barsBack] >= runningMax)
    {
    runningMax = Input[barsBack];
    runningBar = CurrentBar - barsBack;
    }
    }
    
    if (thisBar != CurrentBar)
    {
    lastMax = runningMax;
    lastBar = runningBar;
    thisBar = CurrentBar;
    }
    
    if (Input[0] >= lastMax)
    {
    runningMax = Input[0];
    runningBar = CurrentBar;
    }
    else
    {
    runningMax = lastMax;
    runningBar = lastBar;
    }
    
    Value[0] = runningMax;
    }
    
    #region Properties
    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
    public int Period
    { get; set; }
    #endregion
    }
    }

    #2
    Hello hedgeplay,

    Thanks for your post.

    thisBar is an int variable that refers to the current bar. The way the script works is on each update, if thisBar is not equal to CurrentBar then it gets set to CurrentBar. This means that thisBar would always be less than or equal to CurrentBar.

    That said, this would mean that the CurrentBar < thisBar condition check would never return true and does nothing in the script. It is likely something that was overlooked when the script was developed.

    If you would like to have the condition removed from the script, you could make a copy of the script in the NinjaScript Edtior and omit CurrentBar < thisBar. To make a copy of the script, open the MAX indicator script in a NinjaScript Editor window, right-click in the script, click 'Save as', and name the copy of the script. Once a copy is created, changes could be made.

    Let us know if we may assist further.


    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_BrandonH View Post

      ...

      That said, this would mean that the CurrentBar < thisBar condition check would never return true and does nothing in the script. It is likely something that was overlooked when the script was developed.

      ...

      "condition check would never return true and does nothing in the script."

      That's what it looked like to me, hence the question.


      Thanks Brandon!

      Comment


        #4
        Originally posted by NinjaTrader_BrandonH View Post
        That said, this would mean that the CurrentBar < thisBar condition check would never return true and does nothing in the script. It is likely something that was overlooked when the script was developed.
        Interestingly, NT7 files @MAX.cs and @MIN.cs do not have this check.

        You would think it was added to NT8 for a reason ...

        Comment


          #5
          Originally posted by bltdavid View Post

          You would think it was added to NT8 for a reason
          Hi David,

          Totally guessing...
          1) I expect it was added because it was needed.

          2) In later Development the ~~"if(not enough CurrentBars yet) return;" work then eliminated the need for " CurrentBar < thisBar"

          3) The Developer just forgot to cleanup and delete the now superfluous " CurrentBar < thisBar " line from the shipping code.


          HedgePlay

          Comment


            #6
            Originally posted by hedgeplay View Post
            Totally guessing...
            "Doubtful", says my engineering detective mind.
            Why? The NT7 --> NT8 code transition doesn't support those guesses.

            Take a look at the NT7 vs NT8 code, side by side.
            I've attached a screenshot below.

            I actually agree with you and BrandonH that the code is useless.

            But ..
            Considering the heritage of @MAX.cs already existing in practically
            identical form in NT7, it begs the 'why' question. I mean, the finer
            point is to realize it was added to NT8, it was not there in NT7
            because obviously it was not needed -- so, why was it added?

            That's the curious part.

            Remember, NT7 was the starting point for NT8, and NT7 didn't
            have that code -- so it must have been added to NT8, right?

            What was that reason?
            Is there a corner case it's trying to catch?
            Is it leftover code? Leftover from what?

            I don't know the reason. Thus my rhetorical comment.

            So, yeah, it looks useless (and benign) to me, too.

            I don't imagine we'll ever really know.
            My money is on some intern did it.


            Attached Files

            Comment


              #7
              Originally posted by bltdavid View Post

              My money is on some intern did it.


              That's funny.

              Comment


                #8
                In NT 8 new bar types are supported. Some bar types support RemoveLastBar, so after removing the last bar CurrentBar can be lower than thisBar.

                The special handling of these bars was added in many indicators.

                Comment


                  #9
                  Originally posted by mithnadril View Post
                  In NT 8 new bar types are supported. Some bar types support RemoveLastBar, so after removing the last bar CurrentBar can be lower than thisBar.

                  The special handling of these bars was added in many indicators.

                  Mithnadril, Thank you for the post!

                  If I read your reply correct you are saying that all of the chat above is wrong and in fact "|| CurrentBar < thisBar)" is accurate and important code.


                  Thank you! Your reply is why I posted, to learn something more about NT8.

                  HedgePlay

                  Comment


                    #10
                    the code is optimized, the max of bar [n-1] to bar [1] is constant, So only the current bar ist compared to the previous bars.

                    The special handling is more obvious in the SMA indicator, Here you have 2 clear paths, one if IsRemoveLastBarIsSupported is true and one for the common bar types.

                    Comment


                      #11
                      Very interesting ... thanks Mithnadril!

                      Yeah, as you can tell ..
                      I was kinda wondering if there was a reason.
                      Twas a corner case all along.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by maybeimnotrader, Today, 05:46 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post maybeimnotrader  
                      Started by quantismo, Today, 05:13 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post quantismo  
                      Started by AttiM, 02-14-2024, 05:20 PM
                      8 responses
                      166 views
                      0 likes
                      Last Post jeronymite  
                      Started by cre8able, Today, 04:22 PM
                      0 responses
                      8 views
                      0 likes
                      Last Post cre8able  
                      Started by RichStudent, Today, 04:21 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post RichStudent  
                      Working...
                      X