• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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.
      https://drive.google.com/file/d/16dJ...w?usp=drivesdk
      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, 09: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 pstrusi, Today, 03:11 PM
              0 responses
              12 views
              0 likes
              Last Post pstrusi
              by pstrusi
               
              Started by godric, Today, 01:29 PM
              1 response
              8 views
              0 likes
              Last Post NinjaTrader_Jim  
              Started by blar58, Today, 02:06 PM
              1 response
              10 views
              0 likes
              Last Post NinjaTrader_JoshG  
              Started by woodyfox, Yesterday, 08:06 AM
              3 responses
              16 views
              0 likes
              Last Post NinjaTrader_PatrickH  
              Started by julifro, Today, 11:58 AM
              1 response
              5 views
              0 likes
              Last Post NinjaTrader_PaulH  
              Working...
              X