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

PnL Question

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

    PnL Question

    I'm Stumped,

    What I am trying to is keep track of a trades Profit and Loss per round trade, not per fill. For example If I buy 3 contracts and when they are sold 2 are filled on one lot and 1 at another price. I want to store the PnL in a DataSeries for this trade as one trade (the 3 contracts).

    My code is recording PnL for each lot not the round trade. HELP!

    Code:
    protected override void OnExecution(IExecution execution)
    
    		{
    			// Check to make sure there is at least one trade in the collection
    			if (Performance.RealtimeTrades.Count > 0)
    			{
    				// Get the last completed real-time trade (at index 0)
    				Trade Trade1 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
    				
    				// Calculate the PnL for the last completed real-time trade
    				double lastProfit = Trade1.ProfitCurrency * Trade1.Quantity;
    				Print("Last PnL*" + PnL[1] + " Current PnL: " + PnL[0] + " LP " + lastProfit);
    				
    				if(lastProfit != PnL[1]) {PnL[0] = lastProfit; Print("PnL " +PnL[0]);}
    				Print("The last trade's profit is " + lastProfit);
    And what it is printing is:

    Last PnL*0 Current PnL: 0 LP -379.999999999973
    PnL -379.999999999973
    The last trade's profit is -379.999999999973

    Last PnL*0 Current PnL: -379.999999999973 LP -379.999999999973
    PnL -379.999999999973
    The last trade's profit is -379.999999999973

    Last PnL*0 Current PnL: 0 LP -170.000000000005
    PnL -170.000000000005
    The last trade's profit is -170.000000000005

    Last PnL*0 Current PnL: -170.000000000005 LP -179.999999999995
    PnL -179.999999999995
    The last trade's profit is -179.999999999995

    Any help pointing me in the right direction would be very helpful!!

    Cheers,

    SodyTexas

    #2
    Hello SodyTexas,

    Thank you for your post.

    Can you provide the code used to set the Pnl DataSeries?

    I look forward to your response.

    Comment


      #3
      PnL Question

      Patrick,

      First, thank you for taking the time to help!

      I thought I did include the code,

      This brings in the PnL:

      Code:
      double lastProfit = Trade1.ProfitCurrency * Trade1.Quantity;
      Then: I record it in a data series here:

      Code:
      if(lastProfit != PnL[1]) {PnL[0] = lastProfit; Print("PnL " +PnL[0]);}
      I do have a snippet of code that makes PnL zero at if I am on the first bar and also the basics to add a dataseries.

      But I am thinking I am headed the wrong direction because it is bringing in just the PnL for the lots and not the entire trade.

      Regards,

      SodyTexas

      Comment


        #4
        SodyTexas,

        Thank you for the info.

        What is the code that creates the PnL[0] dataseries? Not necessarily the lastprofit but the variable PnL
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Code:
          #region Variables
          private DataSeries PnL;
          
          protected override void Initialize()
          {
          PnL = new DataSeries(this);
          }
          
          protected override void OnBarUpdate()
          {
          //No PnL information in this 
          }
          
          protected override void OnExecution(IExecution execution)
          {
          if (Performance.RealtimeTrades.Count > 0)
          {
          	// Get the last completed real-time trade (at index 0)
          Trade Trade1 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
          				
          	// Calculate the PnL for the last completed real-time trade
          double lastProfit = Trade1.ProfitCurrency * Trade1.Quantity;
          				
          if(lastProfit != PnL[1]) {PnL[0] = lastProfit; Print("PnL " +PnL[0]);}
          Print("The last trade's profit is " + lastProfit);
          }
          This is all the code pertaining to PnL, Again, I gave you the code already that creates the PnL Data Series. If you guys can't see how I am doing this I am asking the wrong people. But, I don't want to be mean so I apologize if I am confusing you and I do appreciate the help.

          Cheers,

          Comment


            #6
            Hello SodyTexas,

            Thank you for your response.

            I see you are not setting the DataSeries but using '=' for the value. First item I notice is that you print the PnL of the current index value (0) before you set this value:
            Code:
            	Print("Last PnL*" + PnL[1] + " Current PnL: " + [B]PnL[0] [/B]+ " LP " + lastProfit);
            				
            				if(lastProfit != PnL[1]) {PnL[0] = lastProfit; Print("PnL " +PnL[0]);}
            You could instead do the following before printing the values:
            Code:
            if(lastProfit != PnL[1])
            PnL.Set(lastProfit);
            And then print out the values. I would also include Time[0] with these prints in order get a timestamp of when these values are set, you can then compare these to find out if your calculations are correct.

            Please let me know if I may be of further assistance.

            Comment


              #7
              Originally posted by NinjaTrader_PatrickH View Post
              Hello SodyTexas,

              Thank you for your response.

              I see you are not setting the DataSeries but using '=' for the value. First item I notice is that you print the PnL of the current index value (0) before you set this value:
              Code:
              	Print("Last PnL*" + PnL[1] + " Current PnL: " + [B]PnL[0] [/B]+ " LP " + lastProfit);
              				
              				if(lastProfit != PnL[1]) {PnL[0] = lastProfit; Print("PnL " +PnL[0]);}
              You could instead do the following before printing the values:
              Code:
              if(lastProfit != PnL[1])
              PnL.Set(lastProfit);
              And then print out the values. I would also include Time[0] with these prints in order get a timestamp of when these values are set, you can then compare these to find out if your calculations are correct.

              Please let me know if I may be of further assistance.
              Hey Patrick,

              Thanks for taking a look, but I don't think this will resolve my issue. I am sorry if I am not getting my point across.

              The issue with what you posted there is the same issue I am having. If I buy three contracts with one EnterLong signal; when I sell them 2 fill with a PnL of 200.00 and the third 199.00, our math would treat them as two separate PnL entries. I would like to treat them as one PnL entry. I been exploring asigning them as tokens in order to look at the PnL per token, but I can't find the syntax for PnL on tokens.

              Does that help explain my situation?

              Thanks,

              SodyTexas

              Comment


                #8
                Hello SodyTexas,

                Thank you for your response.

                Correct, using IOrder objects you could call the specific orders and tie their PnL together using your own calculation. You would need to compare the fill price of each entry to it's exit and then calculate the PnL based on that for the whole of the "trade".

                For information on IOrder objects please visit the following link: http://www.ninjatrader.com/support/h...nt7/iorder.htm

                Please let me know if you have any questions.

                Comment


                  #9
                  Thanks Patrick,

                  I will give it a try..

                  SodyTexas

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by sidlercom80, 10-28-2023, 08:49 AM
                  167 responses
                  2,260 views
                  0 likes
                  Last Post jeronymite  
                  Started by warreng86, 11-10-2020, 02:04 PM
                  7 responses
                  1,361 views
                  0 likes
                  Last Post NinjaTrader_Manfred  
                  Started by Perr0Grande, Today, 08:16 PM
                  0 responses
                  5 views
                  0 likes
                  Last Post Perr0Grande  
                  Started by elderan, Today, 08:03 PM
                  0 responses
                  9 views
                  0 likes
                  Last Post elderan
                  by elderan
                   
                  Started by algospoke, Today, 06:40 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post algospoke  
                  Working...
                  X