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

A very simple question for programmers

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

    A very simple question for programmers

    I apologize for my programming ignorance but I'm stuck in something that it shouldn't happen

    I'm testing a very simple condition:

    if ( (Close[0]-(Close[0]+Open[0])/2) < -0.000175)
    {
    EnterShort(100000, "SELL");

    }

    I work with Range bars, onBarUpdate=true....ect etc

    The problem comes whenever I set "==" instead of ", or >" , there's not any result, when I've previously checked that actually there are a lot of results with that condition...so when I use < or > no problem, but if I use == I got NONE results....

    how is this possible?
    How could I fix it?

    Thanks in advance

    #2
    Originally posted by pstrusi View Post
    I apologize for my programming ignorance but I'm stuck in something that it shouldn't happen

    I'm testing a very simple condition:

    if ( (Close[0]-(Close[0]+Open[0])/2) < -0.000175)
    {
    EnterShort(100000, "SELL");

    }

    I work with Range bars, onBarUpdate=true....ect etc

    The problem comes whenever I set "==" instead of ", or >" , there's not any result, when I've previously checked that actually there are a lot of results with that condition...so when I use < or > no problem, but if I use == I got NONE results....

    how is this possible?
    How could I fix it?

    Thanks in advance
    Try rounding your calculation off; either mathematically, or to tickSize. ref: http://www.ninjatrader.com/support/h...trument_ro.htm

    Comment


      #3
      Turn on output window from tools menu item.

      Then use a print statement before your if condition.

      Print("result="+(Close[0]-(Close[0]+Open[0])/2));

      You will see if you ever get -0.000175.
      Last edited by sledge; 06-03-2012, 04:34 PM. Reason: removed * typo

      Comment


        #4
        Thanks Koganan but I'd need to work with those values, so what others ways are there?

        Comment


          #5
          Originally posted by pstrusi View Post
          Thanks Koganan but I'd need to work with those values, so what others ways are there?
          I do not understand what you just wrote. Use any values you want; just round off before/when you do the comparison. Comparison of doubles requires exactitude. IOW, leave the values intact; just round off the result of your calculation at the time that you do the comparison. Your values remain. So what is the problem again?

          Comment


            #6
            I meant those format values ( 6 digits after 0 ), I didn't understand how the round off will work out with it

            Thanks Koganan, I'll try it that way; this is embarrassing cause it should be pretty easy to solve

            Comment


              #7
              Originally posted by pstrusi View Post
              I meant those format values ( 6 digits after 0 ), I didn't understand how the round off will work out with it

              Thanks Koganan, I'll try it that way; this is embarrassing cause it should be pretty easy to solve
              Code:
               
              double ComparisonValue = Close[0]-(Close[0]+Open[0])/2;
              if (Instrument.MasterInstrument.Round2TickSize(ComparisonValue) == 0.000175)
              {
              //MakeMegaBucks();
              }
              That, of course assumes that your tickSize accuracy matches the value that you are comparing. I do not know any instrument that has a tickSize that goes to 6 digits. Therefore, given that you are using Close and Open, which are both exactly sized to tickSize, there is only one very limited exact condition under which your equation will be satisfied.

              It seems to me that you may be better off multiplying both sides by a suitable factor, such as 1000, so that you are comparing values of like accuracy, or else mathematically rounding to 6 decimal places, which will allow a match if, and only if, the instrument has a tickSize to 5 decimal places, and the range of the candle is exactly 0.00035.

              Edit: As a matter of fact, writing
              Code:
               
              if ((Close[0] - Open[0]) == 0.00035) ...
              might be the better condition to write, as on an instrument with a 5 decimal place tickSize, that equation is already exactly matched to ticSize, and so avoids all rounding errors.
              Last edited by koganam; 06-03-2012, 05:17 PM.

              Comment


                #8
                Koganan in my backtesting I'd need to compare 2 values like differences between average, therefore I have two questions in order to find the best solution:

                1. The best way could be using for example:
                if (Instrument.MasterInstrument.Compare(Price1, Price2) == 1)

                2. Or can I format double values to a fix format, for example: round to 0.000000 format but not necessarily divisible exactly by ticksize ?

                Thanks for all inputs my friends

                Comment


                  #9
                  Originally posted by koganam View Post
                  Code:
                   
                  double ComparisonValue = Close[0]-(Close[0]+Open[0])/2;
                  if (Instrument.MasterInstrument.Round2TickSize(ComparisonValue) == 0.000175)
                  {
                  //MakeMegaBucks();
                  }
                  This isn't the first time Koganam has left out the MakeMegaBucks function.

                  One day there will be a slip up.

                  Comment


                    #10
                    Originally posted by koganam View Post
                    Code:
                     
                    double ComparisonValue = Close[0]-(Close[0]+Open[0])/2;
                    if (Instrument.MasterInstrument.Round2TickSize(ComparisonValue) == 0.000175)
                    {
                    //MakeMegaBucks();
                    }
                    That, of course assumes that your tickSize accuracy matches the value that you are comparing. I do not know any instrument that has a tickSize that goes to 6 digits. Therefore, given that you are using Close and Open, which are both exactly sized to tickSize, there is only one very limited exact condition under which your equation will be satisfied.

                    It seems to me that you may be better off multiplying both sides by a suitable factor, such as 1000, so that you are comparing values of like accuracy, or else mathematically rounding to 6 decimal places, which will allow a match if, and only if, the instrument has a tickSize to 5 decimal places, and the range of the candle is exactly 0.00035.

                    Edit: As a matter of fact, writing
                    Code:
                     
                    if ((Close[0] - Open[0]) == 0.00035) ...
                    might be the better condition to write, as on an instrument with a 5 decimal place tickSize, that equation is already exactly matched to ticSize, and so avoids all rounding errors.
                    How could I round to 6 decimal places?

                    Comment


                      #11
                      Originally posted by pstrusi View Post
                      Koganan in my backtesting I'd need to compare 2 values like differences between average, therefore I have two questions in order to find the best solution:

                      1. The best way could be using for example:
                      if (Instrument.MasterInstrument.Compare(Price1, Price2) == 1)

                      2. Or can I format double values to a fix format, for example: round to 0.000000 format but not necessarily divisible exactly by ticksize ?

                      Thanks for all inputs my friends
                      To start off with, anything that attempts to calculate an output to a greater accuracy than its inputs is mathematically meaningless. Hence my use of Round2TickSize().

                      That mathematical puritanism aside, yes you can round to any value you want. There is the C# Math.Round() function. ref: http://msdn.microsoft.com/en-us/library/75ks3aby.aspx
                      Last edited by koganam; 06-03-2012, 05:24 PM.

                      Comment


                        #12
                        Thanks a lot for your help. best regards

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Javierw.ok, Today, 04:12 PM
                        0 responses
                        4 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
                        40 views
                        0 likes
                        Last Post alifarahani  
                        Started by Waxavi, Today, 02:10 AM
                        1 response
                        18 views
                        0 likes
                        Last Post NinjaTrader_LuisH  
                        Started by Kaledus, Today, 01:29 PM
                        5 responses
                        15 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Working...
                        X