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 CarlTrading, 03-31-2026, 09:41 PM
      1 response
      152 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      87 views
      1 like
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      131 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      127 views
      1 like
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      106 views
      0 likes
      Last Post CarlTrading  
      Working...
      X