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

Unrealized Profit for different strategies

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

    Unrealized Profit for different strategies

    Afternoon,

    I am currently trying to get the unrealized profit for my active strategies in an add on, and the number that I get isn't consistent with the Strategies Tab in the Control Center window. This is the code that I am using to get the unrealized profit.

    What do you recommend I use so the number is consistent with the Control Center?


    This is what I am doing.

    double CurrentUnrealizedPNL = 0;

    foreach (StrategyBase strategy in accountSelector.SelectedAccount.Strategies)
    {
    CurrentUnrealizedPNL = strategy.Position.GetUnrealizedProfitLoss(Performa nceUnit.Currency, strategy.Close[0])
    }

    What should I be doing for the number to be accurate?

    #2
    I am using this logic to help determine what todo if my risk ratios are exceeded as a way of automatically turning off the most profitable strategy I have running at the time. So getting an accurate figure is pretty important.

    Comment


      #3
      Hello parkinsonbr,

      Thank you for your reply.

      I believe the issues you are seeing are likely two-fold - first, your use of Close[0] as the price in GetUnrealizedProfitLoss, and secondly, the Calculate setting of your strategies.

      I've created an example indicator that may be of help. It's important to note that within a dispatcher like you'd use to get the account, Close[0] is unreliable at best and using .GetValueAt(idx) where idx is the absolute bar index of the bar will be accurate.

      Next, depending on the Calculate setting of the strategies, you'd only be getting updates using the Close price depending on what that's set to - so for example, if the strategy is set to calculate OnBarClose using the close price of the previous bar may be far off of what the current PnL is.

      I've attached the aforementioned example indicator that will check for strategies on the selected account in chart trader and print their current unrealized pnl to an Output window. The same approach could be used in an add-on.

      Please let us know if we may be of further assistance to you
      Attached Files
      Kate W.NinjaTrader Customer Service

      Comment


        #4
        Thanks Kate. I added the line of code to the AddOn and it had a compile which we fixed by adding strategy. to the last close. I have bolded where I updated the line.


        strategy.Position.GetUnrealizedProfitLoss(Performa nceUnit.Currency, strategy.Close.GetValueAt(strategy.CurrentBar)),


        I am testing this today and will let you know if anything else pops up. Thanks again for your help Kate, this appears to have done the trick.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by yertle, Yesterday, 08:38 AM
        7 responses
        28 views
        0 likes
        Last Post yertle
        by yertle
         
        Started by bmartz, 03-12-2024, 06:12 AM
        2 responses
        21 views
        0 likes
        Last Post bmartz
        by bmartz
         
        Started by funk10101, Today, 12:02 AM
        0 responses
        5 views
        0 likes
        Last Post funk10101  
        Started by gravdigaz6, Yesterday, 11:40 PM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by MarianApalaghiei, Yesterday, 10:49 PM
        3 responses
        11 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Working...
        X