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

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#.
    BertrandNinjaTrader Customer Service

    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.
        BertrandNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DJ888, 04-16-2024, 06:09 PM
        6 responses
        18 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by Jon17, Today, 04:33 PM
        0 responses
        1 view
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        6 views
        0 likes
        Last Post Javierw.ok  
        Started by timmbbo, Today, 08:59 AM
        2 responses
        10 views
        0 likes
        Last Post bltdavid  
        Started by alifarahani, Today, 09:40 AM
        6 responses
        41 views
        0 likes
        Last Post alifarahani  
        Working...
        X