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

Math.Pow Problems

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

    Math.Pow Problems

    Developing a trend indicating SMA, but one line of code isn't working in line 63:

    if(BarsPeriod.Id == PeriodType.Minute)
    slopescale=Math.Pow(390/BarsPeriod.Value,.5);
    else slopescale=Math.Pow(1/BarsPeriod.Value,.5);

    If the else condition is triggered and the BarsPeriod.Value is anything besides one, my slopescale value is returned as zero.

    I'm not a C# coder, so it might be something obvious that I'm missing.
    Attached Files

    #2


    I set up a 3 minute ES 09-13 chart, and added your indicator (and added some prints in initialize).. looks fine here.

    You might want to reboot your machine?!!

    Code:
    BarsPeriod.Value=3
    slopescale=11.4017542509914
    BarsPeriod.Value=3
    slopescale=11.4017542509914
    BarsPeriod.Value=3
    slopescale=11.4017542509914
    BarsPeriod.Value=3
    slopescale=11.4017542509914
    BarsPeriod.IdMinute BarsPeriod.Value=3 slopescale=11.4017542509914 calcthreshold=-0.00877058019307029 calcupdntriggerband=0.030697030675746
    BarsPeriod.IdMinute BarsPeriod.Value=3 slopescale=11.4017542509914 calcthreshold=-0.00877058019307029 calcupdntriggerband=0.030697030675746
    BarsPeriod.IdMinute BarsPeriod.Value=3 slopescale=11.4017542509914 calcthreshold=-0.00877058019307029 calcupdntriggerband=0.030697030675746
    BarsPeriod.IdMinute BarsPeriod.Value=

    Originally posted by betahoser View Post
    Developing a trend indicating SMA, but one line of code isn't working in line 63:

    if(BarsPeriod.Id == PeriodType.Minute)
    slopescale=Math.Pow(390/BarsPeriod.Value,.5);
    else slopescale=Math.Pow(1/BarsPeriod.Value,.5);

    If the else condition is triggered and the BarsPeriod.Value is anything besides one, my slopescale value is returned as zero.

    I'm not a C# coder, so it might be something obvious that I'm missing.

    Comment


      #3
      Originally posted by betahoser View Post
      Developing a trend indicating SMA, but one line of code isn't working in line 63:

      if(BarsPeriod.Id == PeriodType.Minute)
      slopescale=Math.Pow(390/BarsPeriod.Value,.5);
      else slopescale=Math.Pow(1/BarsPeriod.Value,.5);

      If the else condition is triggered and the BarsPeriod.Value is anything besides one, my slopescale value is returned as zero.

      I'm not a C# coder, so it might be something obvious that I'm missing.
      BarsPeriod.Value is an integer. If you divide an integer by another integer, the result will also be an integer. In this case you obtain

      Code:
      1/BarsPeriod.Value = 0
      Therefore slopescale also returns the value 0.

      All you need to do is to divide a double by an integer

      Code:
      [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] [COLOR=Black]slopescale=Math.Pow([/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][COLOR=Black][SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]1.0[/SIZE][/FONT][/SIZE][/FONT][/SIZE][/COLOR][/FONT][FONT=Courier New][COLOR=Black][SIZE=2][FONT=Courier New][SIZE=2]/BarsPeriod.Value,[/SIZE][/FONT][/SIZE][/COLOR][/FONT][FONT=Courier New][COLOR=Black][SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].5[/SIZE][/FONT][/SIZE][/FONT][/SIZE][/COLOR][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[COLOR=Black];[/COLOR][/SIZE][/FONT][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
      and the problem should be fixed.

      Comment


        #4
        Thank you so much for taking the time to reply. I had a feeling it was something obvious that I was missing. I've gotta remember C#'s implicit calcs...

        Comment


          #5
          OK - so why didn't I get that issue?

          Results were posted previous post.



          Print ( "BarsPeriod.Value="+BarsPeriod.Value);

          if(BarsPeriod.Id == PeriodType.Minute)
          slopescale=Math.Pow(390/BarsPeriod.Value,.5);
          else
          slopescale=Math.Pow(1/BarsPeriod.Value,.5);
          Print ("slopescale=" + slopescale );



          Originally posted by Harry View Post
          BarsPeriod.Value is an integer. If you divide an integer by another integer, the result will also be an integer. In this case you obtain

          Code:
          1/BarsPeriod.Value = 0
          Therefore slopescale also returns the value 0.

          All you need to do is to divide a double by an integer

          Code:
          [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] [COLOR=Black]slopescale=Math.Pow([/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][COLOR=Black][SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]1.0[/SIZE][/FONT][/SIZE][/FONT][/SIZE][/COLOR][/FONT][FONT=Courier New][COLOR=Black][SIZE=2][FONT=Courier New][SIZE=2]/BarsPeriod.Value,[/SIZE][/FONT][/SIZE][/COLOR][/FONT][FONT=Courier New][COLOR=Black][SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].5[/SIZE][/FONT][/SIZE][/FONT][/SIZE][/COLOR][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[COLOR=Black];[/COLOR][/SIZE][/FONT][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
          and the problem should be fixed.

          Comment


            #6
            Originally posted by sledge View Post
            OK - so why didn't I get that issue?

            Results were posted previous post.

            Print ( "BarsPeriod.Value="+BarsPeriod.Value);

            if(BarsPeriod.Id == PeriodType.Minute)
            slopescale=Math.Pow(390/BarsPeriod.Value,.5);
            else
            slopescale=Math.Pow(1/BarsPeriod.Value,.5);
            Print ("slopescale=" + slopescale );

            You did not get that issue, because it only occurs with the "else" - branch.

            Your test was performed with minute charts, which were not affected by the bug.

            Comment


              #7
              Originally posted by Harry View Post
              You did not get that issue, because it only occurs with the "else" - branch.

              Your test was performed with minute charts, which were not affected by the bug.
              I see it now.

              I didn't get past his statement and didn't try another bar type, hence thinking 3 was fine as a period since it is anything besides one.

              Edit - and after reading it again, I would have had to figure out how to trigger the else, then use something other than one. Man I'm slipping. Long week.

              "If the else condition is triggered and the BarsPeriod.Value is anything besides one, my slopescale value is returned as zero. "

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Tim-c, Today, 02:10 PM
              1 response
              7 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by Taddypole, Today, 02:47 PM
              0 responses
              2 views
              0 likes
              Last Post Taddypole  
              Started by chbruno, 04-24-2024, 04:10 PM
              4 responses
              50 views
              0 likes
              Last Post chbruno
              by chbruno
               
              Started by TraderG23, 12-08-2023, 07:56 AM
              10 responses
              399 views
              1 like
              Last Post beobast
              by beobast
               
              Started by lorem, Yesterday, 09:18 AM
              5 responses
              25 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Working...
              X