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

If statement and comparing double equality

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

  • Taddypole
    replied
    Not sure what happened but the stripped down version is now working. Didn't fix anything. Just trying some different things and moving stuff around.

    But I now have working code and will try to get it back into the full code and hopefully get it all working.

    Thanks for the support...

    regards,
    taddypole...

    Leave a comment:


  • Taddypole
    replied
    Jessica,

    Attached are the stripped down indicator and a picture of the chart showing an example of the multiple cases where many comparisons got through. These are the obvious ones. There are many others when you look closely.

    Regards,
    taddypole...
    Attached Files

    Leave a comment:


  • NinjaTrader_JessicaP
    replied
    Thank you Taddypole. This appears to be a bug. Would it be possible to provide us with a stripped down version of your complete source so we can investigate this further? With this type of bug, the code you wrote in the immediate area is likely not responsible for the behavior you are observing.

    Leave a comment:


  • Taddypole
    replied
    Thanks Jessica,

    Just had cataract surgery on one eye... and still adjusting.

    I used the "C" for currency and also tried the "e" for scientific.

    The results are in the attachment.

    regards,
    Attached Files

    Leave a comment:


  • NinjaTrader_JessicaP
    replied
    It looks like your format string has one more } than mine does.

    My format string : "{0:C}"

    The format string you are using : "{0:C}}"

    Leave a comment:


  • Taddypole
    replied
    Jessica,

    Here's how I entered your suggestions in the Print statement:

    Print("String.Format('{0:C}}', Math.Abs(oneTickLowerHigh-HighPivotTwo) = " + String.Format("{0:C}}",Math.Abs(oneTickLowerHigh - HighPivotTwo)) + " < " + " tolerance = " + tolerance);

    and here are the results:

    Error on calling 'OnBarUpdate' method for indicator 'aaaTriggerandKeltnerIndTwoA' on bar 416: Input string was not in a correct format.

    What does this mean?

    regards,
    taddypole...

    Leave a comment:


  • NinjaTrader_JessicaP
    replied
    I am having some trouble reproducing what you are seeing on my end. Would it be possible for you to replace

    Math.Abs(oneTickLowerHigh - HighPivotTwo).ToString("00.0000000000")

    by

    String.Format("{0:C}", Math.Abs(oneTickLowerHigh - HighPivotTwo))

    in your code, and send me those results?

    Leave a comment:


  • Taddypole
    replied
    Originally posted by marty087 View Post
    I had the same issue ages ago and was trying to find the code that I ended up solving this with. No doubt there is a more elegant solution however, I am pretty sure I ended up rounding both doubles before comparing them and this worked.

    Try making a simple bool func that rounds the 2 vals before comparing.

    eg.

    Code:
    		bool isSame(double val1, double val2)
    		{
    			int decPlaces = 9; //I have a func that auto finds my dec places 
    			
    			val1 = Math.Round(val1 , decPlaces, MidpointRounding.ToEven);
    			val2 = Math.Round(val2 , decPlaces, MidpointRounding.ToEven);
    			
    			if (val1 == val2) return(true);
    			return(false);
    		}
    Thank you marty087

    Not being a programmer by profession can you tell me where that code goes?
    I understand how to use it but by just inserting the code into my code, I get programming errors.
    sorry... no formal coding education...

    regards,
    taddypole...

    Leave a comment:


  • Taddypole
    replied
    oneTickLowerHigh = High[1] - 1 * TickSize;

    // One Tick Stop Out
    if(Math.Abs(oneTickLowerHigh - HighPivotTwo) < tolerance)
    {
    DrawDiamond("MyDiamondHighPvtTwoOneTickStopOut" + CurrentBar, true, 1, High[1] + offSetTrendDn * TickSize, Color.Gold);

    Print("");
    Print(" DiamondHighPvtTwoOneTickStopOut");
    Print(" Time[1] = " + Time[1] + " CurrentBar - 1 = " + (CurrentBar - 1) + " *** aaaTrgKeltIndyTwo");
    Print(" High[1] = " + High[1].ToString("00.0000000000") + " HighPivotTwo = " + HighPivotTwo.ToString("00.0000000000"));
    Print(" oneTickLowerHigh = " + oneTickLowerHigh.ToString("00.0000000000"));
    Print(" Math.Abs(oneTickLowerHigh-HighPivotTwo) = " + Math.Abs(oneTickLowerHigh - HighPivotTwo).ToString("00.0000000000") + " < " + " tolerance = " + tolerance);
    Print("");
    }

    Jessica,

    I tried your Math.Abs method but now I get too many passing through the "If" statement.
    Why is that?

    regards,
    taddypole...
    Attached Files

    Leave a comment:


  • NinjaTrader_JessicaP
    replied
    Hello Taddypole, and thank you for your question.

    The traditional software engineering solution to this problem is to assign a fault tolerance range. I can give an example.

    Code:
    double pi = 3.1415926;
    double reallyPi = 4 * Math.Atan(1.0);
    if (Math.Abs(pi - reallyPi) < 0.000001)
    {
        Print("Pi is " + pi);
    }
    A good rule of thumb is to use 1 less significant figure than your least precise measurement. In the example above, there were 7 numbers after the decimal point, and so I checked for a difference 6 numbers after the decimal point.

    Please let us know if there are any other ways we can help.

    Leave a comment:


  • marty087
    replied
    I had the same issue ages ago and was trying to find the code that I ended up solving this with. No doubt there is a more elegant solution however, I am pretty sure I ended up rounding both doubles before comparing them and this worked.

    Try making a simple bool func that rounds the 2 vals before comparing.

    eg.

    Code:
    		bool isSame(double val1, double val2)
    		{
    			int decPlaces = 9; //I have a func that auto finds my dec places 
    			
    			val1 = Math.Round(val1 , decPlaces, MidpointRounding.ToEven);
    			val2 = Math.Round(val2 , decPlaces, MidpointRounding.ToEven);
    			
    			if (val1 == val2) return(true);
    			return(false);
    		}

    Leave a comment:


  • Taddypole
    replied
    Attached is a output window showing some that worked and one that didn't.

    Any ideas anyone?
    Attached Files

    Leave a comment:


  • Taddypole
    replied
    if(High[1].Equals(HighPivotSeven))
    {
    DrawDot("MyDotHighTouchSeven" + CurrentBar, true, 1, High[1] + 1 * TickSize, Color.Magenta);
    }

    What's weird is in code above that doesn't look for a one tick offset, it seems to work fine. It's just when I use the one tick offset that It becomes random, working some times and not others.

    Regards,
    taddypole...

    Leave a comment:


  • Taddypole
    replied
    if((High[1] - (1 * TickSize)).Equals(HighPivotSeven))
    {
    DrawDiamond("MyDiamondHighPvtSevenOneTickStopOut" + CurrentBar, true, 1, High[1] + 1 * TickSize, Color.Gold);
    }


    Print("");
    Print(" All Pivots");
    Print(" Time[1] = " + Time[1]);
    Print(" CurrentBar - 1 = " + (CurrentBar - 1) + " *** aaaTrgKeltIndyTwo");
    Print(" High[1] = " + High[1].ToString("00.00000000"));
    Print(" High[1] - (1 * TickSize) = " + (High[1] - (1 * TickSize)).ToString("00.00000000"));
    Print(" HighPivotOne = " + HighPivotOne.ToString("00.000000000"));
    Print(" HighPivotTwo = " + HighPivotTwo.ToString("00.000000000"));
    Print(" HighPivotThree = " + HighPivotThree.ToString("00.000000000"));
    Print(" HighPivotFour = " + HighPivotFour.ToString("00.000000000"));
    Print(" HighPivotFIve = " + HighPivotFive.ToString("00.000000000"));
    Print(" HighPivotSix = " + HighPivotSix.ToString("00.000000000"));
    Print(" HighPivotSeven = " + HighPivotSeven.ToString("00.000000000"));
    Print(" TickSize = " + TickSize.ToString("00.000000000"));
    Print("");

    I'm using the double1.Equals(double2) method in the code above and it still doesn't work. In the above code I print out the values and it says it should have fired but it doesn't draw my Diamond.
    Attached Files

    Leave a comment:


  • Taddypole
    replied
    More research and found the :

    double1.Equals(double2));

    trying that....see if that works....

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by judysamnt7, 03-13-2023, 09:11 AM
4 responses
57 views
0 likes
Last Post DynamicTest  
Started by ScottWalsh, Today, 06:52 PM
4 responses
36 views
0 likes
Last Post ScottWalsh  
Started by olisav57, Today, 07:39 PM
0 responses
7 views
0 likes
Last Post olisav57  
Started by trilliantrader, Today, 03:01 PM
2 responses
20 views
0 likes
Last Post helpwanted  
Started by cre8able, Today, 07:24 PM
0 responses
9 views
0 likes
Last Post cre8able  
Working...
X