Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simplifying Code

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

    Simplifying Code

    Hello,
    Please help me find a method to minimize amount of code required to check the distance of a price bar to an EMA, 21 in this case. I'm checking the "closeness" of the High to the EMA in case I'm below it, then the Low if I'm above it, then the Close if my High and Lows are straddling the EMA, then checking which one is the least amount of distance to the EMA. (If I understand correctly, Math.Min only allows checking of two parameters)
    Code:
    [FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]
    [SIZE=2][FONT=Courier New][COLOR=#008000]//distanceTo21a = Math.Round(Math.Abs(High[0] - EMA(21)[0])*10);[/COLOR][/FONT][/SIZE]
    [SIZE=2][FONT=Courier New][COLOR=#008000]// distanceTo21b = Math.Round(Math.Abs(Low[0] - EMA(21)[0])*10);[/COLOR][/FONT][/SIZE]
    [SIZE=2][FONT=Courier New][COLOR=#008000]// distanceTo21c = Math.Round(Math.Abs(Close[0] - EMA(21)[0])*10);[/COLOR][/FONT][/SIZE]
    [SIZE=2][FONT=Courier New][COLOR=#008000]// distanceTo21MinAB = Math.Min(distanceTo21a,distanceTo21b);[/COLOR][/FONT][/SIZE]
    [SIZE=2][FONT=Courier New][COLOR=#008000]// distanceTo21Min = Math.Min(distanceTo21MinAB,distanceTo21c);[/COLOR][/FONT][/SIZE]
    [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
    thanks for your help.
    Kirk

    #2
    There is not much consolidation you can do. It takes you four sentences in english to say what you want to do. Your code is 5 lines.

    The only thing you can achieve is to reduce your code by one line by cascading your Math.Min() together.

    Code:
    Math.Min(a, Math.Min(b, c));
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      okay, thanks

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      593 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      343 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      103 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      556 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      554 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X