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 PnL on Chart

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

    Unrealized PnL on Chart

    Hello,

    I know with NT8 now, you can pull up your real account (not strategy) PnL using the Position.Account function. I'm having a hard time coding a really simple indicator that will show my PnL on the chart, not just Chart Trader.

    Here's my code so far:

    protected override void OnBarUpdate()
    {

    if (PositionAccount.MarketPosition != MarketPosition.Flat)

    {
    Print("Open PnL: " + PositionAccount.GetUnrealizedProfitLoss(Performanc eUnit.Points, Close[0]));
    }

    Pretty straight forward, but I'm getting the error "The name 'PositionAccount' does not exist in the current context. What am I missing here? Is this not applicable to an indicator? Thanks for your help in advance!

    #2
    Originally posted by strongheart View Post
    Hello,

    I know with NT8 now, you can pull up your real account (not strategy) PnL using the Position.Account function. I'm having a hard time coding a really simple indicator that will show my PnL on the chart, not just Chart Trader.


    Pretty straight forward, but I'm getting the error "The name 'PositionAccount' does not exist in the current context. What am I missing here? Is this not applicable to an indicator? Thanks for your help in advance!
    It's only for Strategies?

    Comment


      #3
      Originally posted by strongheart View Post
      Hello,

      I know with NT8 now, you can pull up your real account (not strategy) PnL using the Position.Account function. I'm having a hard time coding a really simple indicator that will show my PnL on the chart, not just Chart Trader.


      Pretty straight forward, but I'm getting the error "The name 'PositionAccount' does not exist in the current context. What am I missing here? Is this not applicable to an indicator? Thanks for your help in advance!
      It's only for Strategies?



      Confirmed here:

      Comment


        #4
        Hello strongheart,

        Thank you for writing in.

        PositionAccount would only work within a strategy.

        However, what you can do is utilize the Account class: https://ninjatrader.com/support/help...ount_class.htm

        I have created a simple indicator that will print the total unrealized PnL for the Sim101 account to the bottom right corner of the chart to demonstrate.

        Please, let us know if we may be of further assistance.
        Attached Files
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          Use of the attached indicator

          Hi Zachary,
          i tried to use the uploaded by you UnrealizedPnLIndicatorTest indicator but it gives plenty of errors when imported in Ninjatrader 7.

          Can you please hava e look.

          Thanks


          Originally posted by NinjaTrader_ZacharyG View Post
          Hello strongheart,

          Thank you for writing in.

          PositionAccount would only work within a strategy.

          However, what you can do is utilize the Account class: https://ninjatrader.com/support/help...ount_class.htm

          I have created a simple indicator that will print the total unrealized PnL for the Sim101 account to the bottom right corner of the chart to demonstrate.

          Please, let us know if we may be of further assistance.

          Comment


            #6
            Thank you for your question manuil. The attached script was actually written for NT8. I have requested that this thread be moved to the NT8 page.

            NinjaTrader 7 does not have any events directly analagous to OnAccountItemUpdate. Instead, you may grab the Gross P&L for all your trades through the sample code here,



            and the unrealized PnL for a single position here,



            Please let us know if there are any other ways we can help.
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ZacharyG View Post
              Hello strongheart,

              Thank you for writing in.

              PositionAccount would only work within a strategy.

              However, what you can do is utilize the Account class: https://ninjatrader.com/support/help...ount_class.htm

              I have created a simple indicator that will print the total unrealized PnL for the Sim101 account to the bottom right corner of the chart to demonstrate.

              Please, let us know if we may be of further assistance.
              Hello,

              I replaced Accounts with All and compiled successfully the attached indicator, but it still doesn´t work as expected. Could you help me please?

              Comment


                #8
                Hello emuns, and thank you for your question.

                It will be easier to answer questions about strategies not producing expected results if we could answer as many of the following questions as possible. Could you take a few minutes to review these questions? I am happy to explain further any of the below questions and the rationale behind asking them.


                • Do you see expected results when running the same test environment on the SampleMaCrossOver strategy in NinjaTrader with a 3 and 14 period?
                  • By expected results, I mean that the SampleMACrossover places trades whenever a 3 and 14 period SMA cross
                • Who are you connected to? This is displayed in green on lower left corner of the Control Center window.
                • Are you connected to your data feed provider when running this test?
                • What instrument(s) (and expiry if applicable) have you selected?
                • What Data Series Type have you selected? Example: Tick, Minute, Day
                • What From and To date is selected?
                • Is your strategy a multi instrument or multi time frame strategy?
                • Do you receive an error on screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?
                Jessica P.NinjaTrader Customer Service

                Comment


                  #9
                  We understand your time is valuable. In lieu of the above, if you can briefly explain how this is working, and how it is expected to work, we will be happy to assist further.
                  Jessica P.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_JessicaP View Post
                    Hello emuns, and thank you for your question.

                    It will be easier to answer questions about strategies not producing expected results if we could answer as many of the following questions as possible. Could you take a few minutes to review these questions? I am happy to explain further any of the below questions and the rationale behind asking them.


                    • Do you see expected results when running the same test environment on the SampleMaCrossOver strategy in NinjaTrader with a 3 and 14 period?
                      • By expected results, I mean that the SampleMACrossover places trades whenever a 3 and 14 period SMA cross
                    • Who are you connected to? This is displayed in green on lower left corner of the Control Center window.
                    • Are you connected to your data feed provider when running this test?
                    • What instrument(s) (and expiry if applicable) have you selected?
                    • What Data Series Type have you selected? Example: Tick, Minute, Day
                    • What From and To date is selected?
                    • Is your strategy a multi instrument or multi time frame strategy?
                    • Do you receive an error on screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?
                    Hello Jessica,

                    it is not strategy but indicator (see Zachary´s contribution in this thread).
                    Now it works (it was just necessary to place it in the Same as input series panel).

                    But now I´m solving another problem. It reacts with big delay to price changes.

                    And second problem - I added realized PnL (see attachment), and I would like to ask if it is possible to display both, realized and unrealized PnL, in the same corner but without overlap.


                    Thank you
                    Attached Files

                    Comment


                      #11
                      I am glad you were able to resolve your query with regard to your indicator. To your new questions, the only real way to make your indicator more reactive to market conditions would be to use a more granular data series. You can add a secondary data series if you'd rather have most of your logic process on bar updates. You can find more support for this here,



                      You can review your Unrealized PnL from your Position object with this method



                      You can review your Realized PnL from your Account object through this advice,



                      Please let us know if there are any other ways we can help.
                      Jessica P.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello,

                        the indi in my previous entry (see below) displays P&L for unrealized and realized trades on a chart. Is there a way how to add up realized and unrealized P&L and display it as a one number?

                        Thank you

                        Comment


                          #13
                          Attached is a code sample that accomplishes this goal. Code samples we provide are for educational purposes, and are not intended for live trading, and are not guaranteed to accomplish any user goal or to be maintained.


                          Please let us know if there are any other ways we can help.
                          Attached Files
                          Jessica P.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello,

                            I tried unrealized PNL and it seems to work great for 1 instrument.
                            How can I accomplish getting unrealized PNL per instrument? Ex - I want to apply this indicator once per instrument and get unrealized PNL per instrument and not the global one.

                            Thanks

                            Comment


                              #15
                              Do want to attach the indicator to one instrument but see the PNL for multiple instruments on the chart?

                              If you just want the unrealized PnL you should comment out the other two 'Draw.TextFixed' lines.

                              Code:
                              // Draw.TextFixed(this, "RealizedPnL", LastRPnL.ToString("C"), TextPosition.BottomRight);
                              // Draw.TextFixed(this, "Total", (LastUPnL + LastRPnL).ToString("C"), TextPosition.BottomRight);
                              Last edited by NinjaTrader_JoshG; 09-06-2018, 01:02 PM.
                              Josh G.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by RubenCazorla, Today, 09:07 AM
                              2 responses
                              11 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by i019945nj, 12-14-2023, 06:41 AM
                              7 responses
                              81 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by timmbbo, 07-05-2023, 10:21 PM
                              4 responses
                              158 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by tkaboris, Today, 08:01 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by Lumbeezl, 01-11-2022, 06:50 PM
                              31 responses
                              819 views
                              1 like
                              Last Post NinjaTrader_Adrian  
                              Working...
                              X