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

Converting double to int

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

    Converting double to int

    Hi. I cannot figure out how to make an int type variable take the value of an integer which is held in a double type variable.

    Here is some sample code:


    #region Variables
    private double tickAltPeriodPro = 0.20;

    // User defined variables (add any user defined variables below)
    private int tickAltPeriod;
    #endregion

    protected override void Initialize()
    {
    tickAltPeriod = Math.Floor(Bars.Period.Value * tickAltPeriodPro);

    Add(PeriodType.Tick, tickAltPeriod);
    }


    For example, let's say the variable tickAltPeriodPro takes a value of 0.20. Using a tick chart with a period of 266, I would like tickAltPeriod to be equal to 53 (Floor function return value of the expression [266 * 0.20]).

    If I make tickAltPeriod an int variable then I get an error when the Floor function returns a double. If I make tickAltPeriod a double then the Add function returns an error (it's expecting a int input).

    Hopefully there's a simple workaround that I'm overlooking. Thank you.


    Regards,

    #2
    After poking around on the MSDN site I think I just answered my question:
    tickAltPeriod = System.Convert.ToInt32(Math.Floor(Bars.Period.Valu e * tickAltPeriodPro));
    Thanks again.


    Regards,

    Comment


      #3
      This shoud also work:

      tickAltPeriod = (int) Math.Floor(Bars.Period.Valu e * tickAltPeriodPro);
      RayNinjaTrader Customer Service

      Comment


        #4
        Thanks for the prompt reply, Ray. After solving my first problem, I encountered a second one. I received the following error message in my log window:

        Strategy Failed to call method 'Initialize' for strategy 'ATRTrade_v8': 'Bars' property can not be accessed from within 'Initialize' method

        Is there any way to access the period value in the Initialize block?


        Regards,

        Comment


          #5
          Try "BarsPeriod"

          Comment


            #6
            Originally posted by NinjaTrader_Dierk View Post
            Try "BarsPeriod"
            Dierk,

            Can you please use that in context? I tried the following:

            tickAltPeriod = Math.Floor(BarsPeriod * tickAltPeriodPro);

            I received an error that I can't use the "*" operator on the BarsPeriod object.

            Thank you.


            Regards,

            Comment


              #7
              Try BarsPeriod.Value. Just use the intellisense and you'll see.

              Comment


                #8
                Originally posted by NinjaTrader_Dierk View Post
                Try BarsPeriod.Value. Just use the intellisense and you'll see.
                tickAltPeriod = System.Convert.ToInt32(Math.Floor(BarsPeriod.Value * tickAltPeriodPro));


                I get an error when I choose to run a backtest. I can ignore past the dialogue to run a backtest, but the period comes through as 0.
                Attached Files

                Comment


                  #9
                  As the error you got suggests the value can not be 0.

                  You need to check that tickAltPeriod does not become 0.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Jon17, Today, 04:33 PM
                  0 responses
                  1 view
                  0 likes
                  Last Post Jon17
                  by Jon17
                   
                  Started by Javierw.ok, Today, 04:12 PM
                  0 responses
                  6 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
                  41 views
                  0 likes
                  Last Post alifarahani  
                  Started by Waxavi, Today, 02:10 AM
                  1 response
                  20 views
                  0 likes
                  Last Post NinjaTrader_LuisH  
                  Working...
                  X