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

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 bmartz, 03-12-2024, 06:12 AM
      3 responses
      27 views
      0 likes
      Last Post NinjaTrader_Zachary  
      Started by Aviram Y, Today, 05:29 AM
      2 responses
      8 views
      0 likes
      Last Post Aviram Y  
      Started by gentlebenthebear, Today, 01:30 AM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by cls71, Today, 04:45 AM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by TradeForge, Today, 02:09 AM
      1 response
      24 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Working...
      X