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

Is there a better way?

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

    Is there a better way?

    Hello,

    I'm having a great deal of success coding up various things, but having no formal programming training I'm wondering if there is a better way to approach how I do certain coding related to checking conditions where some number of conditions must be met out of some total number of conditions.

    For instance:

    Code:
    (BODY[1] < BODY[2] && BODY[1] < BODY[3])
     || (BODY[1] < BODY[2] && BODY[1] < BODY[4]) ||
    (BODY[1] < BODY[3] && BODY[1] < BODY[4])
    In this example, BODY is a custom data series where the syntax is supposed to return TRUE if the body of bar 1 is less than two of the prior three bodies.

    While this code works, it lacks any sophistication. Is there a more concise way to approach these sort of problems? Or is this the best way to do it? This particular case wasn't too bad, but if I was dealing with more bars and/or possible combinations I could quickly end up with a huge amount of code.

    Thanks in advance for any suggestions/advice.

    #2
    Originally posted by coolmoss View Post
    Hello,

    I'm having a great deal of success coding up various things, but having no formal programming training I'm wondering if there is a better way to approach how I do certain coding related to checking conditions where some number of conditions must be met out of some total number of conditions.

    For instance:

    Code:
    (BODY[1] < BODY[2] && BODY[1] < BODY[3])
     || (BODY[1] < BODY[2] && BODY[1] < BODY[4]) ||
    (BODY[1] < BODY[3] && BODY[1] < BODY[4])
    In this example, BODY is a custom data series where the syntax is supposed to return TRUE if the body of bar 1 is less than two of the prior three bodies.

    While this code works, it lacks any sophistication. Is there a more concise way to approach these sort of problems? Or is this the best way to do it? This particular case wasn't too bad, but if I was dealing with more bars and/or possible combinations I could quickly end up with a huge amount of code.

    Thanks in advance for any suggestions/advice.
    Each case is different, but for all cases, it helps to examine the logic of the code to determine if it may be coded more succinctly. In the particular case that you have, take, for example the first condition:
    (BODY[1] < BODY[2] && BODY[1] < BODY[3])
    Examination shows that in that case, all you are saying is the BODY[1] is the least of the values, as it is smaller than either of the others, so more compact/readable code becomes:
    Code:
    (BODY[1] < Math.Min(BODY[2], BODY[3]))

    Comment


      #3
      koganam,

      Thanks much! That's a trick a wasn't aware of.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Brevo, Today, 01:45 AM
      0 responses
      3 views
      0 likes
      Last Post Brevo
      by Brevo
       
      Started by aussugardefender, Today, 01:07 AM
      0 responses
      3 views
      0 likes
      Last Post aussugardefender  
      Started by pvincent, 06-23-2022, 12:53 PM
      14 responses
      239 views
      0 likes
      Last Post Nyman
      by Nyman
       
      Started by TraderG23, 12-08-2023, 07:56 AM
      9 responses
      384 views
      1 like
      Last Post Gavini
      by Gavini
       
      Started by oviejo, Today, 12:28 AM
      0 responses
      6 views
      0 likes
      Last Post oviejo
      by oviejo
       
      Working...
      X