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

Always getting 0 for GetProfitLoss

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

    Always getting 0 for GetProfitLoss

    Hello !

    In a strategy I have coded a EnterLong(1000) on EURUSD.

    The position is well displayed in the Positions tab of NT7, and the PnL is moving and does not equal 0.

    In my code I call the following method but I always have 0 as profit and also 0 in quantity :

    Code:
    			Print(Position.Instrument.FullName);
    			double profit = Position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
    			Print("Close[0]=" + Close[0]);	// Close[0] = GetCurrentBid()
    			Print("GetCurrentAsk=" + GetCurrentAsk());
    			Print("GetCurrentBid=" + GetCurrentBid());
    			Print(dt + " : profit=" + profit);
    			Print("Qty=" + Position.Quantity);
    What am I missing ?
    I would like to get the same PnL value as the one shown on the Positions tab...
    Thanks

    #2
    I think that I have understood.

    When a position is opened then the strategy that opened the position should not be disabled in order to get the profit and loss value for the opened position...

    A question that remains is : Is it possible to get the profit and loss (from a strategy) of a previously opened position when the strategy has been disabled then enabled again ?

    Comment


      #3
      Hello investdatasystems,

      When is this code called?

      Is this code in OnPositionUpdate()?

      Are you calling this a few bars after there is a position and then checking the unrealized?


      I'm not able to reproduce this behavior. Below is a link to a video demonstrating. Attached is the test script.
      Attached Files
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Thanks for your reply.

        Here is the source code :

        1- Attach it to an instrument (EURUSD)
        2- It will open a position
        3- It will correctly display the PnL
        4- Disable the strategy
        5- Comment the EnterLong() Line
        6- Enable the strategy
        7- It will only show PnL of 0 (even if the position is still listed and moving in the Positions tab)

        Code:
        protected override void Initialize()
                {
                    CalculateOnBarClose = false;
        			this.ClearOutputWindow();
        			//checkParamFile();
                }
        
        		double ask = 0;
        		double bid = 0;
        		double PnL = 0;
        		DateTime dt = new DateTime();
        		bool done = false;
        		bool disableOnBarUpdate = false;
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
        			if (disableOnBarUpdate) return;
        			
        			dt = DateTime.Now;						
        			PnL = Position.GetProfitLoss(GetCurrentBid(), PerformanceUnit.Currency);
        			Print(dt + " : PnL=" + PnL);
        			Print(dt + " : Qty=" + Position.Quantity);
        			
        			if (this.Historical == true) return;
        			
        			double ask = this.GetCurrentAsk();
        			double bid = this.GetCurrentBid();
        			//Print(dt.ToString() + " ask=" + ask);
        			if (done == false)
        			{
        				Print("Will enter long position");
        				IOrder iorder = EnterLong(10000);
        				done = true;
        			}			
                }
        Last edited by investdatasystems; 03-13-2018, 10:14 AM.

        Comment


          #5
          Hello investdatasystems,

          The line is checking the strategy position and unrealized pnl (not the account position and account unrealized pnl).

          If the EnterLong() code is commented out then no entry order is every placed. The strategy position will be flat. There will be no profit or loss if there is no entry.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Ok I'll try then with Account position functions.

            Thanks

            Comment


              #7
              Ok great, I tested with the following code and it is ok ! :

              Code:
              			Print("Account Positions Count = " + Account.Positions.Count);
              			Print("Account Position PnL = " + Account.Positions[0].GetProfitLoss(GetCurrentBid(), PerformanceUnit.Currency));
              Thank you again

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by TraderBCL, Today, 04:38 AM
              2 responses
              11 views
              0 likes
              Last Post TraderBCL  
              Started by martin70, 03-24-2023, 04:58 AM
              14 responses
              105 views
              0 likes
              Last Post martin70  
              Started by Radano, 06-10-2021, 01:40 AM
              19 responses
              606 views
              0 likes
              Last Post Radano
              by Radano
               
              Started by KenneGaray, Today, 03:48 AM
              0 responses
              4 views
              0 likes
              Last Post KenneGaray  
              Started by thanajo, 05-04-2021, 02:11 AM
              4 responses
              471 views
              0 likes
              Last Post tradingnasdaqprueba  
              Working...
              X