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

Addition error

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

    Addition error

    Good Afternoon,

    I have run into this issue before and forget the work around.

    Trying to calculate were an arrow is drawn; half of the range above the candle. High[0] + ((High[0] - Low[0]) / 2)

    However when I print this sometimes the range is not a 2 digit decimal it is .05555555555555555555

    So the result is 12.50.05555555555555555555

    And it should be 12.55

    Any help would be appreciated.

    Thanks Ryan

    #2
    Hello Ryan,

    Thank you for your note.

    The syntax you are looking for is a more general C# concept for number formatting that is not specific to NinjaTrader, though we do have the following page about Formatting Numbers in the help guide:


    As mentioned on that page, other resources may be found through a Google search since this is a broader C# concept.

    Additionally, if you are using High[0] + ((High[0] - Low[0]) / 2) in a print statement it might print High[0] and then append the number that is a result of ((High[0] - Low[0]) / 2) to the High[0]. If you want only one number to come out of the calculation, you will need to include the entire statement in parenthesis as follows:
    Print((High[0] + ((High[0] - Low[0]) / 2)));

    If we were to follow the example for formatting to 2 decimal places, it would look like this:
    Code:
    Print((High[0] + ((High[0] - Low[0]) / 2)).ToString("N2"));
    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Emily, Thanks for the response, I can print is to a string but that doesn't solve the real issue. The Arrow is being placed at 12.50.05555555555 and it should be placed at 12.55. is there a way that I can convert or round ((High[0] - Low[0]) / 2) to a 2 digit decimal so I can then add it to High[0] without it causing a problem?

      Thanks Ryan

      Comment


        #4
        Hello Tonkingrf1551,

        Thank you for your reply.

        I am confused because 12.50.05555555555 has two decimal places and doesn't seem to be a valid number. Could you please provide an example of what you are referring to with where the arrow is being placed? Have you tried to add additional prints to better understand what two numbers you are trying to add together and confirm whether the outcome from the addition is the expected number or not?

        I appreciate your patience and look forward to your reply.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Emily, thanks for getting back to me.

          Here is the code and I have attached a picture of the output box.

          The result is the arrow is placed at ~12.75 instead of 12.82

          Print("candle high " + High[0]);
          Print("candle range" + (((High[0] - Low[0]) / 2)));
          Print("candle high + range " + High[0] + (((High[0] - Low[0]) / 2)));

          Draw.ArrowDown(this, "DownTier3Arrow" + CurrentBars[0], true, 0, High[0] + (((High[0] - Low[0]) / 2) * TickSize), Brushes.Red);



          Thank you Ryan

          Comment


            #6
            Hello Ryan,

            Thank you for your reply.

            While we do not offer hands-on debugging services, I did already make a comment about a small syntax item that is affecting your output. Here is what I mentioned in my initial reply to you:

            "Additionally, if you are using High[0] + ((High[0] - Low[0]) / 2) in a print statement it might print High[0] and then append the number that is a result of ((High[0] - Low[0]) / 2) to the High[0]. If you want only one number to come out of the calculation, you will need to include the entire statement in parentheses as follows:
            Print((High[0] + ((High[0] - Low[0]) / 2)));​"

            There is a difference between your current print, which is:
            Print("candle high + range " + High[0] + (((High[0] - Low[0]) / 2)));

            and then adding the parenthesis around the entire calculation and in the appropriate places, which would look like this where the bolded part is inside of the extra set of parenthesis that was added:
            Print("candle high + range " + (High[0] + (High[0] - Low[0]) / 2)));

            This is due to the way strings are concatenated. More details may be found at the following publicly available link:
            There are multiple ways to concatenate strings in C#. Learn the options and the reasons behind different choices.


            Something you could do in the future is save the values you are using to a variable, then print the variable's value. For example:

            private double candleHigh;
            private double candleRange;
            private double highPlusRange;

            candleHigh = High[0]
            candleRange = (High[0] - Low[0]) / 2;
            highPlusRange = candleHigh + candleRange;
            Print("candle high " + High[0] + "candleHigh " + candleHigh);
            Print("candle range" + (((High[0] - Low[0]) / 2)) + "candleRange " + candleRange);
            Print("candle high + range " + (High[0] + (High[0] - Low[0]) / 2)) + "highPlusRange " + highPlusRange);

            In the future, please keep parenthesis and syntax in mind. You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our NinjaTrader Ecosystem team to follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.

            Thank you for your time. Please don't hesitate to reach out with any additional questions or concerns.​
            Emily C.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by selu72, Today, 02:01 PM
            1 response
            4 views
            0 likes
            Last Post NinjaTrader_Zachary  
            Started by WHICKED, Today, 02:02 PM
            2 responses
            9 views
            0 likes
            Last Post WHICKED
            by WHICKED
             
            Started by f.saeidi, Today, 12:14 PM
            8 responses
            21 views
            0 likes
            Last Post f.saeidi  
            Started by Mikey_, 03-23-2024, 05:59 PM
            3 responses
            50 views
            0 likes
            Last Post Sam2515
            by Sam2515
             
            Started by Russ Moreland, Today, 12:54 PM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_Erick  
            Working...
            X