Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnPositionUpdate - MarketPosition parameter - what's it good for?

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

    OnPositionUpdate - MarketPosition parameter - what's it good for?

    Just wondering...

    Is it just to have a String of the MarketPosition?

    I can't access Flat/Long/Short in it, I have to use "Ninjatrader.Cbi.MarketPosition".



    Code:
    	protected override void OnPositionUpdate(Position position, double averagePrice, int quantity,
    									 MarketPosition marketPosition )

    #2
    I squeezed in a test...

    Code:
    Print ( "1 OnPositionUpdate.marketPosition=" + marketPosition.ToString() );
    
    if (position.MarketPosition == MarketPosition.Long)
    {
    	Print ( "2 OnPositionUpdate.MarketPosition=" + "LONG" );
    Result:

    1 OnPositionUpdate.marketPosition=Long
    2 OnPositionUpdate.MarketPosition=LONG

    Comment


      #3
      Interesting - my stoploss HIT, but OnPositionUpdate was clueless to the fact.

      The marketPosition parameter is correct, cbi.Ninjatrader.MarketPosition is wrong. (And that might be why Unrealized/Realized were both displaying information at the same time).

      So this feels more like a bug report now.


      1 OnPositionUpdate.marketPosition=Flat
      2 OnPositionUpdate.MarketPosition=LONG

      Comment


        #4
        Had to change my code to and suffer a ToString call.

        So I don't think the old way really works fully anymore:






        Code:
        			if (marketPosition.ToString()[0] == 'F' )
        			//if (position.MarketPosition == MarketPosition.Flat)
        			{
        				...
        
        			}
        			else if (marketPosition.ToString()[0] == 'L' )
        			//else if (position.MarketPosition == MarketPosition.Long)
        			{
        				...
        			}
        			else if (marketPosition.ToString()[0] == 'S' )
        			//else if (position.MarketPosition == MarketPosition.Short)
        			{
        				...
        			}

        Comment


          #5
          Hello,

          It sounds like the confusion is coming from the fact that you are making reference to the MarketPosition enum within the cbi class itself, rather than the instance passed into OnPositionUpdate. Within OnPositionUpdate, "marketPosition" will be a specific instance with relevant values, while "MarketPosition" will refer to the enum type itself.

          So, you will need to compare the enum value of the instance to one of the MarketPosition values, like so:

          Code:
          protected override void OnPositionUpdate(Position position, double averagePrice, int quantity, MarketPosition marketPosition)
                  {
                      base.OnPositionUpdate(position, averagePrice, quantity, marketPosition);
          
                      if (marketPosition == MarketPosition.Flat)
                          Print("position flat");
                  }
          Dave I.NinjaTrader Product Management

          Comment


            #6
            There is another nuance to keep in mind which has changed from the NinjaTrader 7 implementation of the IPosition interface. The Position object passed into OnPositionUpdate() does have a MarketPosition attached to it (position.MarketPosition), but this represents the state of the position before OnPositionUpdate() is called.

            OnPositionUpdate() is essentially updating the Position object passed into it, based on the MarketPosition object passed into it. So, at the moment that OnPositionUpdate() is triggered, the MarketPosition object represents the core position at that time, and the Position object has not yet been updated, so testing "position.MarketPosition" should always tell you what the position was before OnPositionUpdate() is called.
            Dave I.NinjaTrader Product Management

            Comment


              #7
              Originally posted by NinjaTrader_Dave View Post
              There is another nuance to keep in mind which has changed from the NinjaTrader 7 implementation of the IPosition interface. The Position object passed into OnPositionUpdate() does have a MarketPosition attached to it (position.MarketPosition), but this represents the state of the position before OnPositionUpdate() is called.

              OnPositionUpdate() is essentially updating the Position object passed into it, based on the MarketPosition object passed into it. So, at the moment that OnPositionUpdate() is triggered, the MarketPosition object represents the core position at that time, and the Position object has not yet been updated, so testing "position.MarketPosition" should always tell you what the position was before OnPositionUpdate() is called.
              Ok thanks. That makes me feel a little better that's is not a threading issue. I'll check it out tonight again.

              Comment


                #8
                Originally posted by NinjaTrader_Dave View Post
                There is another nuance to keep in mind which has changed from the NinjaTrader 7 implementation of the IPosition interface. The Position object passed into OnPositionUpdate() does have a MarketPosition attached to it (position.MarketPosition), but this represents the state of the position before OnPositionUpdate() is called.

                OnPositionUpdate() is essentially updating the Position object passed into it, based on the MarketPosition object passed into it. So, at the moment that OnPositionUpdate() is triggered, the MarketPosition object represents the core position at that time, and the Position object has not yet been updated, so testing "position.MarketPosition" should always tell you what the position was before OnPositionUpdate() is called.
                This is in Playback - Market Replay.

                The documentation definitely needs updated as the code is referencing NT7 shtako and no longer relevant as when you are inside OnPositionUpdate () position.MarketPosition is old when a StopLoss triggers.

                p.s. I think there's an issue here.



                Code:
                protected override void OnPositionUpdate(Position position, double averagePrice, int quantity,	 MarketPosition marketPosition )
                		{
                 
                			Print ( "1a marketPosition=" + marketPosition.ToString() );
                			Print ( "1b position.MarketPosition=" + position.MarketPosition.ToString() );
                Output On Long Position IN OnPositionUpdate:

                1a marketPosition=Long
                1b position.MarketPosition=Long

                Output on StopLoss triggered IN OnPositionUpdate:

                1a marketPosition=Flat
                1b position.MarketPosition=Long

                Comment


                  #9
                  I agree that could be documented more clearly. I've added a note to the help guide to clarify this point. There have been a wide range of updates to the help guide which should be published along with the next beta build.
                  Dave I.NinjaTrader Product Management

                  Comment


                    #10
                    I'm a little confused about this subject, so I'd like to clear my doubt.

                    Before I submit any order I have to check in reliable way, what's my actual market position, and I use to do this:

                    Code:
                    protected override void OnPositionUpdate(IPosition position)
                    		{
                    			if ( position.MarketPosition == MarketPosition.Long )
                    			{
                    				cp=1.0;
                    				
                    			}
                    			if ( position.MarketPosition == MarketPosition.Short )
                    			{
                    				cp=-1.0;
                    				
                    			}
                    		}
                    BUT after reading this post I have a doubt because it seems that position.MarketPosition could be the old position in case of triggering Stops orders. So I'd like to know what's is the ultimate and reliable best way to do it.

                    Thanks in advance

                    Comment


                      #11
                      Originally posted by pstrusi View Post
                      I'm a little confused about this subject, so I'd like to clear my doubt.

                      Before I submit any order I have to check in reliable way, what's my actual market position, and I use to do this:

                      Code:
                      protected override void OnPositionUpdate(IPosition position)
                      		{
                      			if ( position.MarketPosition == MarketPosition.Long )
                      			{
                      				cp=1.0;
                      				
                      			}
                      			if ( position.MarketPosition == MarketPosition.Short )
                      			{
                      				cp=-1.0;
                      				
                      			}
                      		}
                      BUT after reading this post I have a doubt because it seems that position.MarketPosition could be the old position in case of triggering Stops orders. So I'd like to know what's is the ultimate and reliable best way to do it.

                      Thanks in advance
                      Do it right before you submit the order, in OnBarUpdate() or wherever else you are submitting said order.

                      Comment


                        #12
                        Originally posted by koganam View Post
                        Do it right before you submit the order, in OnBarUpdate() or wherever else you are submitting said order.
                        But if it never fills, then order gets rejected....

                        Comment


                          #13
                          Thanks for your attention Koganam.

                          What I look for is checking in reliable way that old managed orders are already filled, and position updated before new ones. In a platform like NT, this should be done easily. So if you have more ideas to achieve this with another simple test, please share.

                          Thanks
                          Last edited by pstrusi; 10-17-2016, 04:08 AM.

                          Comment


                            #14
                            Originally posted by pstrusi View Post
                            Thanks for your attention Koganam.

                            What I look for is checking in reliable way that old managed orders are already filled, and position updated before new ones. In a platform like NT, this should be done easily. So if you have more ideas to achieve this with another simple test, please share.

                            Thanks
                            You said: "Before I submit an order ...", which leads to the presumption that you are talking about an entry order, because there is no need to make this check before you exit. You are either in a position to exit or you are not.

                            In which case, as I already said, you check right before you issue the order. What other idea are you looking for? After all, you are about to do something immediately, so check your position immediately before you take action, not check some unknown time back before your entry.

                            Even for an exit order, placed at some time after the order instead of in OnExecutionUpdate(), you would still check the position immediately before you make the order.

                            I do not see your objection to doing so, and there is nothing else that I can suggest other that what I do reliably.
                            Last edited by koganam; 10-17-2016, 08:10 AM. Reason: Corrected spelling.

                            Comment


                              #15
                              Thanks Koganam, the problem is that you presume that I'm exiting when actually I'm reversing positions. I'm sorry NOT to clear that before

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by cre8able, Today, 03:20 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post cre8able  
                              Started by Fran888, 02-16-2024, 10:48 AM
                              3 responses
                              47 views
                              0 likes
                              Last Post Sam2515
                              by Sam2515
                               
                              Started by martin70, 03-24-2023, 04:58 AM
                              15 responses
                              114 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by The_Sec, Today, 02:29 PM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by jeronymite, 04-12-2024, 04:26 PM
                              2 responses
                              31 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Working...
                              X