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 Rapine Heihei, 04-23-2024, 07:51 PM
        2 responses
        30 views
        0 likes
        Last Post Max238
        by Max238
         
        Started by Shansen, 08-30-2019, 10:18 PM
        24 responses
        943 views
        0 likes
        Last Post spwizard  
        Started by Max238, Today, 01:28 AM
        0 responses
        9 views
        0 likes
        Last Post Max238
        by Max238
         
        Started by rocketman7, Today, 01:00 AM
        0 responses
        4 views
        0 likes
        Last Post rocketman7  
        Started by wzgy0920, 04-20-2024, 06:09 PM
        2 responses
        28 views
        0 likes
        Last Post wzgy0920  
        Working...
        X