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 cmoran13, 04-16-2026, 01:02 PM
      0 responses
      42 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      25 views
      0 likes
      Last Post PaulMohn  
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      162 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      98 views
      1 like
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      157 views
      2 likes
      Last Post CaptainJack  
      Working...
      X