• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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, 03: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, 04: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, 04:24 PM.

                      Comment


                        #12
                        Thanks a lot for your help. best regards

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Newtrader101, Today, 08:01 AM
                        0 responses
                        4 views
                        0 likes
                        Last Post Newtrader101  
                        Started by Johnny Santiago, 10-11-2019, 08:21 AM
                        6 responses
                        101 views
                        0 likes
                        Last Post 4x777
                        by 4x777
                         
                        Started by pjsmith, Today, 02:42 AM
                        2 responses
                        16 views
                        0 likes
                        Last Post pjsmith
                        by pjsmith
                         
                        Started by moyerchopra, Yesterday, 11:43 PM
                        0 responses
                        5 views
                        0 likes
                        Last Post moyerchopra  
                        Started by ttodua, Yesterday, 11:40 PM
                        0 responses
                        6 views
                        0 likes
                        Last Post ttodua
                        by ttodua
                         
                        Working...
                        X