Announcement

Collapse
No announcement yet.

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 CarlTrading, 03-31-2026, 09:41 PM
                  1 response
                  152 views
                  1 like
                  Last Post NinjaTrader_ChelseaB  
                  Started by CarlTrading, 04-01-2026, 02:41 AM
                  0 responses
                  87 views
                  1 like
                  Last Post CarlTrading  
                  Started by CaptainJack, 03-31-2026, 11:44 PM
                  0 responses
                  131 views
                  2 likes
                  Last Post CaptainJack  
                  Started by CarlTrading, 03-30-2026, 11:51 AM
                  0 responses
                  127 views
                  1 like
                  Last Post CarlTrading  
                  Started by CarlTrading, 03-30-2026, 11:48 AM
                  0 responses
                  106 views
                  0 likes
                  Last Post CarlTrading  
                  Working...
                  X