Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

using decimals

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

    using decimals

    Hi,

    I have a problem with an indicator I'm working on.

    Basically, my indicator formula looks like following :

    Myindicator.Set(varA*varB)

    where varA and varB are double types(very basic). The thing is when I'm running it , it does not plot anything. I've checked the output window to point out the problem and it appears that it comes from my varA which is always equal to 0.

    I think the problem comes from the type of varA .Since the formula looks like Constant/(Period*(Period-1)*(Period+1)), it obviously returns 0.0 when Period is set to 20 for example. The correct value is 0.00000xxx.

    So I tried to change the value type to decimal in order to get the precision I need but when I do it, I'm forced to change the type of varB (also to decimal) and of course my indicator won't compile since Dataseries only holds double values.

    I don't know what to do anymore. Could anyone help me ?

    By the way ,why can't I print the values in the output window with my variables defined as decimals ?

    #2
    kuroro13,

    You should be able to print them.

    Print(var1.ToString());

    Please check your variables through these prints first to see what values they are really at. Would have imagined doubles to work with your values to begin with. If you are using such a small decimal value you may consider normalizing it by removing some zeros.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thank you Josh for your answer.

      I've checked all the code through the prints and i still have the same problem.
      Here is the first part of the code :

      public class filtre1 : Indicator
      {
      #region Variables
      // Wizard generated variables
      private int length = 20; // Default setting for Length
      // User defined variables (add any user defined variables below)
      private double f18=0.00;
      private double f20;
      private int f48=0;
      private int f38=0;
      #endregion

      /// <summary>
      /// This method is used to configure the indicator and is called once before any bar data is loaded.
      /// </summary>
      protected override void Initialize()
      {
      Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "Plot0"));
      Add(new Line(Color.FromKnownColor(KnownColor.ControlDark), 0, "Zero"));
      CalculateOnBarClose = true;
      Overlay = false;
      PriceTypeSupported = false;
      arr0 = new double [301];
      arr1 = new double [301];
      arr2 = new int [301];
      arr3 = new int [301];
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>

      protected override void OnBarUpdate()
      {




      if (f38 == 0)
      {
      f38=1;
      f40=0;
      if ((Length-1)>=2)
      {
      f30=Length-1;
      }
      else {f30=2;}

      f48=f30+1;

      f10=Close[0];

      f18=12/(f48*(f48-1)*(f48+1));
      Print(f18.ToString());
      double f20=(f48+1)*0.5;
      Print(f20.ToString());
      }


      When Length is set to 20 the output window shows me :
      0
      10.5

      my f18 is always equal to 0 . I tried to normalize it by multipying it by a 1000 factor but then it returns me 1.

      I'm a little bit lost there....

      Comment


        #4
        You could try this: f18=12.0/(f48*(f48-1)*(f48+1));

        Comment


          #5
          Thank you Ralph !!!

          It works smoothly now...but now you should give me a little explanation on why ?!!! :-)

          Comment


            #6
            Hi kuroro13,

            12 is an integer and if divided by a double, the double is converted to integer obviously, so your result is: 12/7980.0 -> 0.
            12.0 is a double and so is the result: 12.0/7980.0 -> 0.0015

            Regards
            Ralph

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            633 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            364 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            105 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            567 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            568 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X