Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bizarre Floating Point Comparison Results

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

    Bizarre Floating Point Comparison Results

    Greetings,

    I've written a strategy that is working well for most trades. However, for one trade in the back-test series, it is not behaving correctly. To test why this is happeneing, I embedded the following code into my Strategy:

    if( Position.MarketPosition == MarketPosition.Long && High[0] >= DonchianChannel(24).Upper[0] ) {
    bool isEqual = (High[0] >= firstLongTarget);
    bool does1equal1 = (92.59>=92.59);
    Print( Time[0].ToString() +", Donchian Upper Met: "+High[0]+", firstLongTarget: "+firstLongTarget+ ", isEqual?: " + isEqual+". 1=1: "+ does1equal1 );
    }

    Here's the result this code produces in the Output Window:

    10/1/2012 12:48:00 PM, Donchian Upper Met: 92.59, firstLongTarget: 92.59, isEqual?: False. 1=1: True

    The problem with this result is that apparently High[0] = 92.59 and firstLongTarget = 92.59, but the comparison of the variables for equality is returning false. As you can see, comparing the two hard-coded numbers returns true. High[0] should be returning a double, and firstLongTarget is defined as a double in my code.

    This is very confusing. Does printing double precision numbers in NT round them by default? This is the only explanation I can think of for this result. Any assistance anyone can provide would be greatly appreciated!

    #2
    Hello Jambo, welcome to our forums - those floating point challenges you've run into are being discussed in this tip here on the forums - http://www.ninjatrader.com/support/f...ead.php?t=3929

    It then presents two potential routes to handle via C#.

    Comment


      #3
      Re: Bizarre Floating Point Comparison Results

      Hi, NinjaTrader_Bertrand!

      Many thanks for your help! The suggested solutions worked and my strategy is now behaving correctly. I'm not a big fan of the readability of the Compare() function when embedded in my code, so I created the following utility functions that help a bit (though it's still not as readable as <, >, ==, <=, and >=). Other readers may want to adapt these to their own purposes.

      Regards,

      Jambo

      private bool isPriceGreaterThanEqualTo( double firstNum, double secondNum ) {

      if(Instrument.MasterInstrument.Compare(firstNum, secondNum) ==1 ||
      Instrument.MasterInstrument.Compare( firstNum, secondNum) == 0){
      return( true );
      } else {
      return(false);
      }

      }

      private bool isPriceLessThanEqualTo( double firstNum, double secondNum ) {

      if(Instrument.MasterInstrument.Compare(firstNum, secondNum) ==-1 ||
      Instrument.MasterInstrument.Compare( firstNum, secondNum) == 0){
      return( true );
      } else {
      return(false);
      }

      }

      private bool isPriceGreaterThan( double firstNum, double secondNum ) {

      if(Instrument.MasterInstrument.Compare(firstNum, secondNum) == 1 ){
      return( true );
      } else {
      return(false);
      }

      }

      private bool isPriceLessThan( double firstNum, double secondNum ) {

      if(Instrument.MasterInstrument.Compare(firstNum, secondNum) == -1 ){
      return( true );
      } else {
      return(false);
      }

      }

      private bool isPriceEqualTo( double firstNum, double secondNum ) {

      if( Instrument.MasterInstrument.Compare( firstNum, secondNum) == 0){
      return( true );
      } else {
      return(false);
      }

      }

      Comment


        #4
        We're glad to hear, thanks for sharing those utility functions with our forums, i'm sure other readers will find them helpful as well.

        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