Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calling Methods ... what in the world is this command doing?

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

    Calling Methods ... what in the world is this command doing?

    Hi Ninja.

    I am learning NinjaScript.
    I refer to calling method from within OnBarUpdate().
    I came across this call from one of the Ninja Samples:

    OnBarUpdate()
    {
    ...
    DetermineHL_BarRange(); // A ... OK, I get this ... call Method A

    if ( ! StuffHits_BarRange() ) // B ...??? ... what in the world is this command doing?
    return;
    ...
    }

    DetermineHL_BarRange() // Method A
    {
    ...
    }

    StuffHits_BarRange() // Method B
    {
    ...
    }

    For B, my understanding is ==> call Method B, UNTIL Method B does not work, then return.(I am just speculating)

    Can anyone enlighten?

    Thanks

    #2
    Originally posted by Apollo11 View Post
    For B, my understanding is ==> call Method B, UNTIL Method B does not work, then return.(I am just speculating)
    Pretty close.

    Say it like a programmer:
    "call Method B, return from OnBarUpdate only if Method B returns false"

    [More experienced programmers say "if not Method B then return" -- see below]

    The point is: if Method B return trues, OnBarUpdate continues normally.
    Method B is serving as some kind of guard, or filter -- if for any reason it
    returns false, then it found something wrong, and this means OnBarUpdate
    should bail, er, I mean, immediately return.

    The '!' character negates the boolean expression, so, yes, you are basically
    correct. The '!' character is known as the logical negation operator, which
    just means it changes true values to false, and vice versa. When reading the
    code, your mind should say 'not' when you see the '!' character as a logical
    negation operator -- ie, "if not Method B then return". (Saying 'not' in your
    mind when reading the '!' operator is quite common for C/C++ and Java
    programmers, as well C# programmers. C# evolved from Java, which
    itself has roots in C and C++. But I digress.)

    -=o=-

    Btw, this specific question is not NinjaScript -- it's just a C# question.

    Make sure you augment your NinjaScript exposure with some serious
    study
    of the C# programming language.

    -=o=-

    Although you don't show it, we know that Method B must be returning
    a 'bool' value -- otherwise it cannot be called in that if expression in
    the manner shown. I mean, experienced programmers can easily infer
    that Method B return type is not 'void' -- we can even go further and infer
    that if Method B is of any type other than 'bool', the compiler would have
    produced an error message. Thus, assuming the sample code you're
    reading is correct code, then we know Method B return type is 'bool'
    before we ever see the Method B definition further down in the code.

    -=o=-

    One more thing.

    You're still learning, so this is not your fault. But we should fix your
    young brain now, and set you on the right path.

    You stated,
    "call Method B, UNTIL Method B does not work"

    That is very cringe-worthy, don't use 'UNTIL' like that. Why? Because
    using 'UNTIL' implies a loop -- like keep doing something UNTIL that
    something reaches 10, or do something UNTIL that something fails.
    In some programming languages (but not C#), the word until is a
    reserved word, because it is a keyword in that language. (C# is closely
    associated with C/C++, where until is a keyword.)

    Now, in a much larger context, if you were able to see that OnBarUpdate
    is being called repeatedly (like, ya know, in a loop) then you're partially
    forgiven. The internal NinjaTrader code has what is called an 'event
    driven loop' and calls your OnBarUpdate each time a bar(*) closes.

    My point is: old school programmers who are familiar with do-until loops
    associate any use of until with loop structures, not with if statements.
    Thus, since the code with which you are describing does not itself deal
    with a loop (ignoring the larger event driven loop), you should use if and
    then style keywords, and only use until when discussing loops.

    In fact,
    Describing code in quasi English-like statements is actually a technique
    well-known by many programmers, and is called 'pseudo code'. Soo,
    in that larger world of programming, using until in pseudo code when
    describing an if statement would be frowned upon. [Ok, I admit this
    is my humble opinion. I mean, there are few (if any) rules for pseudo
    code, but I'm saying something gleaned from a 30+ year career as a
    professional software engineer: only use until when discussing loops.]

    (*) OnBarUpdate is actually called according to the frequency dictated by
    the Calculate setting.

    Just my 2˘.

    Last edited by bltdavid; 02-24-2022, 05:51 PM.

    Comment


      #3
      Wow.

      Thanks for the interesting reply.

      Very helpful.

      I feel like I have a long way to go (sweating emoji)

      Best 2cents ever.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      647 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      369 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      108 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      572 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      573 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X