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 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 Max238, Today, 01:28 AM
        2 responses
        26 views
        0 likes
        Last Post NinjaTrader_ChristopherJ  
        Started by Shansen, 08-30-2019, 10:18 PM
        25 responses
        949 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by JonesJoker, 04-22-2024, 12:23 PM
        8 responses
        41 views
        0 likes
        Last Post JonesJoker  
        Started by timko, Today, 06:45 AM
        0 responses
        4 views
        0 likes
        Last Post timko
        by timko
         
        Started by Waxavi, 04-19-2024, 02:10 AM
        2 responses
        39 views
        0 likes
        Last Post poeds
        by poeds
         
        Working...
        X