Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Converting double to integer

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

    Converting double to integer

    I am trying to move a profit target to bring even if unrealized profit exceeds half the profit target. I am having trouble converting the results of the calculation : 0.5 * profit to an integer value so I can substitute it for the old stop value.

    Here is the code I am using:
    [/CODE]
    if ((Position.MarketPosition == MarketPosition.Long)
    && (Position.GetUnrealizedProfitLoss(PerformanceUnit. Ticks, Close[0]) >= int(0.5 * Profit))
    && (Position.GetUnrealizedProfitLoss(PerformanceUnit. Ticks, Close[0]) > Position.GetUnrealizedProfitLoss(PerformanceUnit.T icks, Close[1])))
    {
    Stop = int(0.5 * Profit);
    SetStopLoss("@BSDV_L", CalculationMode.Ticks, Stop, false);
    }
    [/QUOTE]

    How can I convert the calculated new stop value to an integer so I can substitute it into the old Stop Value?

    Thanks for your help.
    DaveN

    #2
    Hello Daven,

    If trying to cast a type double as a type int, you would need to wrap the type in parentheses.

    Code:
    Stop = (int)(0.5 * Profit);
    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Thank you, I looked and looked for the proper expression for int, and just couldn't find it.
      So simple.
      Thanks again.
      DaveN

      Comment


        #4
        Use this instead
        Code:
        var stop = Convert.ToInt32(0.5 * profit)
        which is safer. Casting by parenthesis will always compile but could fail at runtime.

        Furthermore casting will truncate the number rather than round it. Therefore by the parenthesis method 8.99999999 will result in an int value of 8. Convert.ToIn32(value) will result in an int value of 9.
        Last edited by reach4thelasers; 06-17-2017, 02:10 AM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        38 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        124 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        64 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        41 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X