Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position object doesn't update after final close of position

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

    Position object doesn't update after final close of position

    An instance of a Position class can be obtained by going to a specific Account and finding the position of interest under Account.Positions. Further the same information can be obtained when the PositionUpdate event handler is fired. The expectation was that the quantity for said position would go to 0 when the position is finally zero. Instead, the Position instance will maintain the values of the last position held before going to 0.

    If for example, the current position is 40 units, and an execution for 40 units happen, the Position instance will still say 40 for quantity.

    When tracking a specific position for an account and instrument, one must subscribe to the PositionUpdate event and handle when the values coming in for the position are 0.

    Code:
    Position trackingPosition;
    
    private void Account_PositionUpdate(object sender, PositionEventArgs e)
    {
        if(e.Position.Instrument == desiredInstrument)
        {
            if(e.Quantity == 0)
            {
                // At this point e.Quantity != e.Position.Quantity
                trackingPosition = null;
            }
            else
            {
                trackingPosition = e.Position;
            }
        }
    }

    #2
    Hello ntbone,

    I am not able to reproduce this behavior.

    Below is a link to a video of the test.
    https://drive.google.com/file/d/1dc9...w?usp=drivesdk

    Attached is the test script.

    Using the test script are you finding the quantity is not 0 when the position is flat?
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      In my code above, trackingPosition which would be equal to e.Position from the position event handler reflects the problem. I have updated your sample code to reproduce the issue. Note: I switched it to use a playback account but that is not important to seeing the problem. Its the specific comparison of e.Position object for which the quantity and state is never updated once the position goes flat. This same object can be obtained from Account.Positions.

      Code:
      private void MyAccount_PositionUpdate(object sender, PositionEventArgs e)
      {
          Print(string.Format("Position: {0} {1}", e.MarketPosition, e.Quantity));
      
          if(e.Position.Quantity != e.Quantity)
          {
              Print(string.Format("The position sent in the event args was {0} but the position in the Position.Quantity is {1}",
                  e.Quantity, e.Position.Quantity));
          }
      }
      Attached Files

      Comment


        #4
        Hello ntbone,

        Ah I see you are focusing on e.Position.Quantity.

        The e.Position is the currently updating position. i.e. the e.Position.MarketPosition/e.Position.Quantity is the updating position which is having an operation of Remove.

        The e.MarketPosition/e.Quantity is the actual pointed position of the account. The e.Position updates the e.MarketPosition and causes the <account>.PositionUpdate to trigger.

        That's a mouthful of words. Let me know if I need to take another pass at explaining that.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I am not sure I follow. The e.Position (which is also int he accounts) has the same values as the e.Quantity except when the position is closed. When the position goes to 0, the Position object pointed to by e.Position (and also in Account.Positions never sets to 0. It does properly change otherwise.

          I understand that e.Position is likely being removed from Account.Positions but anyone still holding a reference won't realize that, and wont' realize it went to 0. I think it should be set to 0 on that object before it is removed as well as having the flatten state set..

          I started this thread to inform others who try to keep a reference to the Position object to let them know that when it is set to 0, they won't be alerted because the Position object is never set to 0.
          Last edited by ntbone; 06-18-2019, 02:18 PM.

          Comment


            #6
            Hello ntbone,

            The e.Position is the long position being removed. In the trace file we would also likely see the remove operation. This is not the position of the account. This is the last operation update. The operation was a Remove of a long position.

            The e.MarketPosition is the actual position of the account. The actual account position is flat because of the remove operation.

            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I understand. The object pointed to by e.Position has the position for the account (be it long/short) while a position is in place. When the account goes to 0/flatten, the Position object is being removed from the account at which point it retains the last value the account had for the position before the removal.

              For example, if the position of the account is 1000

              e.Position.Quantity == 1000 and e.Quantity == 1000, and the Position object in Account.Positions (same one as e.Position) also is 1000

              200 units are removed from the position.

              e.Position.Quantity == 800 and e.Quantity == 800.

              300 units are removed from the position.

              e.Possition.Quantity == 500 and e.Quantity == 500;

              The last 500 units are remove from the position.

              e.Position.Quantity == 500 and e.Quantity == 0.

              The object pointed to by e.Position never gets a quantity of 0. It just maintains the last known position of the account before it was removed. If one stores a reference to that object in an indicator, for example to display the current position, they will not be aware it went to 0 and will continue to show 500 unless they explicitly do something with a PositionUpdate event handler.

              I expected that as it is being removed

              e.Position.Quantity = 0 so that anyone holding a reference to that object will know it went to 0.

              Comment


                #8
                Hello ntbone,

                I think you are understanding.

                If you are wanting the actual position of the account, use the e.MarketPosition and e.Quantity.
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by yeikson89, Today, 02:53 PM
                2 responses
                9 views
                0 likes
                Last Post yeikson89  
                Started by sdauteuil, Today, 01:06 PM
                1 response
                11 views
                0 likes
                Last Post NinjaTrader_Jim  
                Started by trumpy, 03-09-2017, 02:11 PM
                30 responses
                3,224 views
                0 likes
                Last Post foxthorn  
                Started by shani77, Today, 09:58 AM
                1 response
                22 views
                0 likes
                Last Post NinjaTrader_Eleanore  
                Started by cjanau, Yesterday, 11:07 AM
                1 response
                31 views
                0 likes
                Last Post NinjaTrader_ThomasC  
                Working...
                X