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 kinfxhk, 07-14-2026, 09:39 AM
      0 responses
      127 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 07-13-2026, 10:18 AM
      0 responses
      105 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 07-13-2026, 09:50 AM
      0 responses
      85 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 07-13-2026, 07:21 AM
      0 responses
      105 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 07-11-2026, 02:11 AM
      0 responses
      86 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Working...
      X