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

Problem doing comparisons with fractions

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

    Problem doing comparisons with fractions

    Hi Ninjas,

    This is simple but I still can't see why.

    I'm doing a very simple logic comparison, a double variable against a fraction. For example:

    Let's suppose a=0.5, then I do

    if ( a>0.4 && a<0.6 )
    {
    EnterLong(100000, "BUY");
    }

    under this way the code sends a BUY order, BUT if I do the same comparison like this:

    if ( a>4/10 && a<6/10 )
    {
    EnterLong(100000, "BUY");
    }

    The code does nothing. Why? and how can I solve it? Despite this problem is easy to solve writing directly, I use a lot fractions, so I´d like to know how.

    Thanks

    #2
    I found the solution. Obviously cause the type of variable.

    If I do this change, then it does work perfect

    if ( a>4.0/10.0 && a<6.0/10.0 )
    {
    EnterLong(100000, "BUY");
    }

    Comment


      #3
      Originally posted by pstrusi View Post
      I found the solution. Obviously cause the type of variable.

      If I do this change, then it does work perfect

      if ( a>4.0/10.0 && a<6.0/10.0 )
      {
      EnterLong(100000, "BUY");
      }

      Correct. Also see the C# Reference for the division operator.

      http://msdn.microsoft.com/en-us/library/3b1ff23f.aspx

      "When you divide two integers, the result is always an integer. For example, the result of 7 / 3 is 2. To determine the remainder of 7 / 3, use the remainder operator (%). To obtain a quotient as a rational number or fraction, give the dividend or divisor type float or type double
      ."

      Sample results:

      7 / 3 = 2
      -7 / 3 = -2
      7 % 3 = 1
      7 / 3.0 = 2.33333333333333
      7.0 / 3 = 2.33333333333333
      8 / 5 = 1
      8 / -5 = -1
      8 % 5 = 3
      8 / 5.0 = 1.6
      8.0 / 5 = 1.6
      Last edited by Harry; 07-30-2013, 09:09 AM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by fx.practic, 10-15-2013, 12:53 AM
      5 responses
      5,404 views
      0 likes
      Last Post Bidder
      by Bidder
       
      Started by Shai Samuel, 07-02-2022, 02:46 PM
      4 responses
      95 views
      0 likes
      Last Post Bidder
      by Bidder
       
      Started by DJ888, Yesterday, 10:57 PM
      0 responses
      8 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by MacDad, 02-25-2024, 11:48 PM
      7 responses
      159 views
      0 likes
      Last Post loganjarosz123  
      Started by Belfortbucks, Yesterday, 09:29 PM
      0 responses
      8 views
      0 likes
      Last Post Belfortbucks  
      Working...
      X