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 DJ888, 04-16-2024, 06:09 PM
      6 responses
      18 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by Jon17, Today, 04:33 PM
      0 responses
      1 view
      0 likes
      Last Post Jon17
      by Jon17
       
      Started by Javierw.ok, Today, 04:12 PM
      0 responses
      6 views
      0 likes
      Last Post Javierw.ok  
      Started by timmbbo, Today, 08:59 AM
      2 responses
      10 views
      0 likes
      Last Post bltdavid  
      Started by alifarahani, Today, 09:40 AM
      6 responses
      41 views
      0 likes
      Last Post alifarahani  
      Working...
      X