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

Double to Int conversion

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

    Double to Int conversion

    Hello,

    I have a 101 question that I've wrestled with for a while now, without any success.

    When I compile this code
    Code:
    			private int Lst1BodySize = 0;
    			Lst1BodySize = ((Close[0]-Open[0])*10000);
    I get this message "Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists - CS0266 - click for info" which doesn't help.

    When Open=1.3620 and Close=1.3625, I would like to have 5 displayed in the output window instead of 4.99999999999945 !

    Any help is welcome,
    Gerard

    #2
    You need to preceed your calculation with the explicit type-cast (int).
    This type of conversion cuts your result down to the next valid integer. If that is not intended you could add a rounding function in addition.

    Regards
    Ralph

    Comment


      #3
      Hello,

      I tried this code but it does not work as my integer keeps the initial value.

      Code:
          {
              #region Variables
      			private double bdsz = 99;
      			private int BodySize = 99;
      		#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()
              {
                  CalculateOnBarClose	= false;
                  Overlay				= false;
                  PriceTypeSupported	= false;
      			BodySize = System.Convert.ToInt32(Math.Floor(bdsz));
      		}
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
      			Print("The tick count of the current bar is " + Bars.TickCount.ToString());
      			bdsz = (Close[0]-Open[0])*10000;
       			Print("Close-Open is " + bdsz.ToString() + " pips");
        			Print("  Bodysize is " + BodySize.ToString() + " pips");
            }
      I would appreciate a resolution sample.
      Gerard
      Attached Files

      Comment


        #4
        Resolution of double to int conversion

        Hi guys,

        I eventually found a solution as poking around :
        Code:
        			Print("The tick count of the current bar is " + Bars.TickCount.ToString());
        			bdsz = (Close[0]-Open[0])*10000;
         			Print("Close-Open is " + bdsz.ToString() + " pips");
        			bdsz = bdsz + 0.5;
        			BodySize = (int) bdsz;
        			Print("  Bodysize is " + BodySize.ToString() + " pips");
        Anyway, I welcome any smarter solution if you have one.

        Gerard

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by elirion, Today, 01:36 AM
        0 responses
        3 views
        0 likes
        Last Post elirion
        by elirion
         
        Started by gentlebenthebear, Today, 01:30 AM
        0 responses
        4 views
        0 likes
        Last Post gentlebenthebear  
        Started by samish18, Yesterday, 08:31 AM
        2 responses
        9 views
        0 likes
        Last Post elirion
        by elirion
         
        Started by Mestor, 03-10-2023, 01:50 AM
        16 responses
        390 views
        0 likes
        Last Post z.franck  
        Started by rtwave, 04-12-2024, 09:30 AM
        4 responses
        34 views
        0 likes
        Last Post rtwave
        by rtwave
         
        Working...
        X