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 r68cervera, Today, 05:29 AM
        0 responses
        3 views
        0 likes
        Last Post r68cervera  
        Started by geddyisodin, Today, 05:20 AM
        0 responses
        6 views
        0 likes
        Last Post geddyisodin  
        Started by JonesJoker, 04-22-2024, 12:23 PM
        6 responses
        35 views
        0 likes
        Last Post JonesJoker  
        Started by GussJ, 03-04-2020, 03:11 PM
        12 responses
        3,240 views
        0 likes
        Last Post Leafcutter  
        Started by AveryFlynn, Today, 04:57 AM
        0 responses
        7 views
        0 likes
        Last Post AveryFlynn  
        Working...
        X