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 leojimenezp, 04-20-2024, 05:49 PM
        4 responses
        47 views
        0 likes
        Last Post leojimenezp  
        Started by nicthe, Today, 09:24 AM
        1 response
        5 views
        0 likes
        Last Post nicthe
        by nicthe
         
        Started by samish18, Today, 10:13 AM
        0 responses
        6 views
        0 likes
        Last Post samish18  
        Started by kenz987, Yesterday, 10:20 AM
        2 responses
        13 views
        0 likes
        Last Post kenz987
        by kenz987
         
        Started by nicthe, 08-23-2023, 07:53 AM
        7 responses
        197 views
        0 likes
        Last Post nicthe
        by nicthe
         
        Working...
        X