Announcement

Collapse
No announcement yet.

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 Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      649 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      370 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      109 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      574 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      576 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X