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

Someone give me a formula

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

    Someone give me a formula

    I know, I know... NT support staff you can ignore this because you're not supposed to help with common C# programming.

    So if anyone ELSE could help me, I would sure appreciate it.

    I need a formula to tell me how many decimal places are in a number. Given the variable TickSize, how can I tell the number of decimal places are in that variable? I've tried a lot of things... logarithms, dividing, you name it. I cannot figure this out, and it's got to be simple. For now, this is what I'm doing, but it's crazy messy, and it's an amateur way of programming for this highly professional programmer.

    int decimals = 0;
    if (TickSize == .1) decimals = 1;
    if (TickSize == .25) decimals = 2;
    if (TickSize == .01) decimals = 2;
    if (TickSize == .025) decimals = 3;
    if (TickSize == .001) decimals = 3;
    if (TickSize == .0025) decimals = 4;
    if (TickSize == .0001) decimals = 4;


    Thanks!
    Bryan
    cassb
    NinjaTrader Ecosystem Vendor - Logical Forex

    #2
    Why would you need to know this? Try : double sizeofticks = 1 * ticksize;
    I don't have platform open to test but I think that would store .25 in the variable sizeofticks for the ES and .1 for the TF, etc. It would show the correct ticksize for the instrument in the chart that your indicator is on.
    eDanny
    NinjaTrader Ecosystem Vendor - Integrity Traders

    Comment


      #3
      Originally posted by cassb View Post
      I know, I know... NT support staff you can ignore this because you're not supposed to help with common C# programming.

      So if anyone ELSE could help me, I would sure appreciate it.

      I need a formula to tell me how many decimal places are in a number. Given the variable TickSize, how can I tell the number of decimal places are in that variable? I've tried a lot of things... logarithms, dividing, you name it. I cannot figure this out, and it's got to be simple. For now, this is what I'm doing, but it's crazy messy, and it's an amateur way of programming for this highly professional programmer.

      int decimals = 0;
      if (TickSize == .1) decimals = 1;
      if (TickSize == .25) decimals = 2;
      if (TickSize == .01) decimals = 2;
      if (TickSize == .025) decimals = 3;
      if (TickSize == .001) decimals = 3;
      if (TickSize == .0025) decimals = 4;
      if (TickSize == .0001) decimals = 4;


      Thanks!
      Bryan

      Hi CassB,

      I'm not sure what your planning on doing, but here's thought.

      Instead of having different length decimals, make them all the same.

      If you can redefine all your decmials to 4 digits.

      Ex.

      .1 = .1000
      .25 = .2500
      .0001 = .0001

      Would this help.

      I don't know if this is possible, just a thought.

      Good Luck,

      RJay
      RJay
      NinjaTrader Ecosystem Vendor - Innovative Trading Solutions

      Comment


        #4
        Well, maybe what I'm trying to do would help. I am using this 'decimals' variable in the Math.Round function.

        So, you would have a statement like:

        x = Math.Round((Open[0] - (y * z)),decimals);

        ... where y and z are double. Since I want x to be rounded to the same number of decimals that the TickSize is, I need to know how many decimals are in TickSize for the instrument this program is running for.

        See?
        cassb
        NinjaTrader Ecosystem Vendor - Logical Forex

        Comment


          #5
          Originally posted by cassb View Post
          Well, maybe what I'm trying to do would help. I am using this 'decimals' variable in the Math.Round function.

          So, you would have a statement like:

          x = Math.Round((Open[0] - (y * z)),decimals);

          ... where y and z are double. Since I want x to be rounded to the same number of decimals that the TickSize is, I need to know how many decimals are in TickSize for the instrument this program is running for.

          See?

          This is from Woodies Panel code maybe help. :-)

          // Set the stringformat parameter for decimal places to suit the instrument.
          // This ensures that trailing zeros are always displayed - maybe an easier way to do this?
          int tickLength;
          double temp1 = TickSize - (int)TickSize;// get fractional part of TickSize
          if (temp1 < 0.001) // c# will use scientific notation for small numbers. Attempt to cope with this here.
          {
          decimals = (temp1*1000).ToString(); // make it into a string, after removing 3 leading zeros
          tickLength = (decimals.Length)+3; // get the number of characters in the string(then add back the 3 previously removed)
          }
          else // no need to cope with scientific notation(hopefully)
          {
          decimals = temp1.ToString(); // make it into a string
          tickLength = decimals.Length; // get the number of characters in the string
          }
          switch (tickLength) // set format depending on length of string
          {
          case 1: decimals = "f0"; // eg YM 1t = 1 point
          break;
          case 3: decimals = "f1"; // eg ER2 1t = 0.1 point
          break;
          case 4: decimals = "f2"; // eg NQ 1t = 0.25 point
          break;
          case 5: decimals = "f3"; // eg 1t = 0.001 point
          break;
          case 6: decimals = "f4"; // eg 6E 1t = 0.0001 point
          break;
          case 7: decimals = "f5"; // eg 1t = 0.00001 point
          break;
          case 8: decimals = "f6"; // eg 6J 1t = 0.000001 point
          break;
          default: decimals = ""; // default is no fixed decimal format
          break;
          // more options can be added if neccessary......
          }
          Print(Time[0].ToString() + " decimals= " + decimals);

          Comment


            #6
            Originally posted by PrTester View Post
            This is from Woodies Panel code maybe help. :-)

            // Set the stringformat parameter for decimal places to suit the instrument.
            // This ensures that trailing zeros are always displayed - maybe an easier way to do this?
            int tickLength;
            double temp1 = TickSize - (int)TickSize;// get fractional part of TickSize
            if (temp1 < 0.001) // c# will use scientific notation for small numbers. Attempt to cope with this here.
            {
            decimals = (temp1*1000).ToString(); // make it into a string, after removing 3 leading zeros
            tickLength = (decimals.Length)+3; // get the number of characters in the string(then add back the 3 previously removed)
            }
            else // no need to cope with scientific notation(hopefully)
            {
            decimals = temp1.ToString(); // make it into a string
            tickLength = decimals.Length; // get the number of characters in the string
            }
            switch (tickLength) // set format depending on length of string
            {
            case 1: decimals = "f0"; // eg YM 1t = 1 point
            break;
            case 3: decimals = "f1"; // eg ER2 1t = 0.1 point
            break;
            case 4: decimals = "f2"; // eg NQ 1t = 0.25 point
            break;
            case 5: decimals = "f3"; // eg 1t = 0.001 point
            break;
            case 6: decimals = "f4"; // eg 6E 1t = 0.0001 point
            break;
            case 7: decimals = "f5"; // eg 1t = 0.00001 point
            break;
            case 8: decimals = "f6"; // eg 6J 1t = 0.000001 point
            break;
            default: decimals = ""; // default is no fixed decimal format
            break;
            // more options can be added if neccessary......
            }
            Print(Time[0].ToString() + " decimals= " + decimals);
            Aha... exactly! What a good idea to convert the TickSize to string and then count the characters. Why didn't I think of that? I was so hooked on a mathematical solution that I missed the obvious. Thank you!

            Here is what I ended up using -- as I didn't find that C# was converting a ticksize of 0.0001 to scientific notation:

            Code:
                        double temp = TickSize - (int)TickSize;                // get fractional part of TickSize
                        string a = temp.ToString();                         // make it into a string
                        if (a.IndexOf(".") > 0) decimals = (a.Length - 2);    // get the number of characters in the string minus the leading 0 and the decimal point
                        else decimals = 0;
            Bryan
            Last edited by cassb; 11-21-2008, 09:45 PM.
            cassb
            NinjaTrader Ecosystem Vendor - Logical Forex

            Comment


              #7
              Hi cassb,

              had a similar problem some time ago. I tried to define a string format for numbers according to the ticksize. Here is what I did:

              Code:
              [FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string [/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]PriceFormat([/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][SIZE=2][FONT=Courier New] TickSize)[/FONT][/SIZE]
              [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
              [/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  short[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] Precision = [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][SIZE=2][FONT=Courier New];[/FONT][/SIZE]
              [/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  while[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] (TickSize % [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] > [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0.0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][SIZE=2][FONT=Courier New])[/FONT][/SIZE]
              [SIZE=2][FONT=Courier New]  {[/FONT][/SIZE]
              [SIZE=2][FONT=Courier New]    TickSize *= [/FONT][/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]10[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][FONT=Courier New];[/FONT][/SIZE]
              [SIZE=2][FONT=Courier New]    Precision++;[/FONT][/SIZE]
              [SIZE=2][FONT=Courier New]  }[/FONT][/SIZE]
              [SIZE=2][FONT=Courier New]  return Precision == [/FONT][/SIZE][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] ? [/SIZE][/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"G"[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] : [/SIZE][/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"F"[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][SIZE=2][FONT=Courier New] + Precision;[/FONT][/SIZE]
              [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
              [/SIZE]
              Regards
              Ralph

              Comment


                #8
                Originally posted by Ralph View Post
                Hi cassb,

                had a similar problem some time ago. I tried to define a string format for numbers according to the ticksize. Here is what I did:

                Code:
                [FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string [/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]PriceFormat([/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][SIZE=2][FONT=Courier New] TickSize)[/FONT][/SIZE]
                [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
                [/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  short[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] Precision = [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][SIZE=2][FONT=Courier New];[/FONT][/SIZE]
                [/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]  while[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] (TickSize % [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] > [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0.0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][SIZE=2][FONT=Courier New])[/FONT][/SIZE]
                [SIZE=2][FONT=Courier New]  {[/FONT][/SIZE]
                [SIZE=2][FONT=Courier New]    TickSize *= [/FONT][/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]10[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][FONT=Courier New];[/FONT][/SIZE]
                [SIZE=2][FONT=Courier New]    Precision++;[/FONT][/SIZE]
                [SIZE=2][FONT=Courier New]  }[/FONT][/SIZE]
                [SIZE=2][FONT=Courier New]  return Precision == [/FONT][/SIZE][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] ? [/SIZE][/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"G"[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] : [/SIZE][/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"F"[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][SIZE=2][FONT=Courier New] + Precision;[/FONT][/SIZE]
                [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
                [/SIZE]
                Regards
                Ralph
                Thanks Ralph! It looks like you're a better C coder than I am. I come from a COBOL, Fortran, Basic background, so I tend to use that kind of syntax even in C. I need to learn some of the cool features of C, I think.
                cassb
                NinjaTrader Ecosystem Vendor - Logical Forex

                Comment


                  #9
                  Ralph,

                  Very nice. Could you decode for the 1st graders?

                  Thanks

                  Originally posted by Ralph View Post
                  Hi cassb,

                  had a similar problem some time ago. I tried to define a string format for numbers according to the ticksize. Here is what I did:

                  Code:
                  [FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string [/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2]PriceFormat([/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][SIZE=2][FONT=Courier New] TickSize)[/FONT][/SIZE]
                  [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
                  [/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] short[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] Precision = [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][SIZE=2][FONT=Courier New];[/FONT][/SIZE]
                  [/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff] while[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] (TickSize % [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] > [/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0.0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][SIZE=2][FONT=Courier New])[/FONT][/SIZE]
                  [SIZE=2][FONT=Courier New] {[/FONT][/SIZE]
                  [SIZE=2][FONT=Courier New]   TickSize *= [/FONT][/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]10[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][FONT=Courier New];[/FONT][/SIZE]
                  [SIZE=2][FONT=Courier New]   Precision++;[/FONT][/SIZE]
                  [SIZE=2][FONT=Courier New] }[/FONT][/SIZE]
                  [SIZE=2][FONT=Courier New] return Precision == [/FONT][/SIZE][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] ? [/SIZE][/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"G"[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2] : [/SIZE][/SIZE][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000][SIZE=2][COLOR=#800000]"F"[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/FONT][SIZE=2][SIZE=2][FONT=Courier New] + Precision;[/FONT][/SIZE]
                  [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
                  [/SIZE]
                  Regards
                  Ralph

                  Comment


                    #10
                    Originally posted by r2kTrader View Post
                    Ralph,

                    Very nice. Could you decode for the 1st graders?

                    Thanks

                    By the way, this is still working for me... I added the "E" check for scientific notation. It works pretty well and it's more understandable to this Visual Basic programmer.

                    Code:
                                double temp = TickSize - (int)TickSize;                // get fractional part of TickSize
                                string a = temp.ToString();                         // make it into a string
                                if (a.IndexOf(".") > 0) decimals = (a.Length - 2);    // get the number of characters in the string minus the leading 0 and the decimal point
                                if (a.IndexOf("E") > 0) decimals = Convert.ToInt16(a.Substring(3));    // If scientific notation, then extract the number of decimal places, e.g. 1E-06
                    cassb
                    NinjaTrader Ecosystem Vendor - Logical Forex

                    Comment


                      #11
                      The method returns a price-format-string usable to transform a double-value into a string according to the ticksize, i.e. ticksize: 0.25 -> value-string: 881.75 (and not 881.750000).
                      If the ticksize is > 0, then the returned format string is "G".
                      If the ticksize is 0.25, the returned format string is "F2".
                      If the ticksize is 0.0001, the returned format string is "F4".

                      Regards
                      Ralph

                      Comment


                        #12
                        There is that saying, at least here in Germany: Different roads lead to Rome. Great stuff, cassb.

                        Comment


                          #13
                          thank you very much.

                          Comment


                            #14
                            Glad we could help ... in an obtuse sort of way.
                            cassb
                            NinjaTrader Ecosystem Vendor - Logical Forex

                            Comment


                              #15
                              Round

                              cassb

                              I am a relative newbie but wouldn't it be simpler to use
                              Code:
                              public Double RoundPrice ( double price) 
                              			
                              {return base.Bars.Instrument.MasterInstrument.Round2TickSize(price);}
                              then
                              x = Math.Round((Open[0] - (y * z)),decimals);(Post #4)

                              becomes

                              x = RoundPrice((Open[0] - (y * z)));

                              I think it does anyway.
                              Last edited by Mindset; 11-23-2009, 02:58 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by kujista, Today, 06:23 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post kujista
                              by kujista
                               
                              Started by traderqz, Yesterday, 04:32 PM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by f.saeidi, Today, 05:56 AM
                              1 response
                              4 views
                              0 likes
                              Last Post Jltarrau  
                              Started by Jltarrau, Today, 05:57 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post Jltarrau  
                              Started by Stanfillirenfro, Yesterday, 09:19 AM
                              7 responses
                              52 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X