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 giulyko00, 04-24-2024, 12:03 PM
        9 responses
        43 views
        0 likes
        Last Post giulyko00  
        Started by memonic, Yesterday, 01:23 PM
        2 responses
        24 views
        0 likes
        Last Post memonic
        by memonic
         
        Started by merc410, Today, 03:41 AM
        2 responses
        15 views
        0 likes
        Last Post merc410
        by merc410
         
        Started by sugalt, 04-30-2024, 04:02 AM
        2 responses
        13 views
        0 likes
        Last Post sugalt
        by sugalt
         
        Started by Ndakotan1313, 03-14-2024, 05:02 PM
        2 responses
        64 views
        0 likes
        Last Post blaise_code  
        Working...
        X