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 trilliantrader, Today, 08:16 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by bill2023, Yesterday, 08:51 AM
        3 responses
        20 views
        0 likes
        Last Post bltdavid  
        Started by yertle, Today, 08:38 AM
        0 responses
        4 views
        0 likes
        Last Post yertle
        by yertle
         
        Started by Mestor, 03-10-2023, 01:50 AM
        15 responses
        379 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by samish18, Yesterday, 08:57 AM
        10 responses
        28 views
        0 likes
        Last Post samish18  
        Working...
        X