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

How to determine when a position was just opened

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

    How to determine when a position was just opened

    I need to know when we go from MarketPosition.Flat to some other MarketPosition (Long or Short), ie I need to know the moment when we open a position.To do that I use this code:


    PHP Code:
    class MyStrategy Strategy {
        private 
    MarketPosition priorMarketPosition;
        private 
    MarketPosition currentMarketPosition;

        protected 
    override void OnPositionUpdate(Cbi.Position positiondouble averagePrice,
                
    int quantityMarketPosition marketPosition)
        {
            
    //
            // If we just opened a position, take appropriate action
            //
            
    this.priorMarketPosition this.currentMarketPosition;
            
    this.currentMarketPosition position.MarketPosition;
            if (
    this.currentMarketPosition != MarketPosition.Flat && this.priorMarketPosition == MarketPosition.Flat) {
                
    takeAppropriateAction();
            }
        }

        private 
    void takeAppropriateAction() {
            
    // do something here
        
    }


    However, the documentation for OnPositionUpdate says this:

    Its best practice to only work with the passed by value parameters and not reference parameters. This insures that you process each change of the underlying state.
    I am setting this.currentMarketPosition = position.MarketPosition;, ie I am using the position object. Is this going to cause problems? When can I use the position object and when can I not use the position object?

    #2
    Hello westofpluto,

    Thanks for your question.

    The strategy Position object is updated in Execution events and will be updated as early as OnExecutionUpdate. (Account positions like PositionAccount would be updated on PositionUpdate events and would be updated as early as OnPositionUpdate.) It is not necessary to manage your own Position object and the Position object can be referenced anywhere in your script.

    Position - https://ninjatrader.com/de/support/h.../?position.htm

    OnExecutionUpdate - https://ninjatrader.com/support/help...tionupdate.htm

    We look forward to assisting.


    JimNinjaTrader Customer Service

    Comment


      #3
      Jim, you totally missed the point of my question. I know how to get access to my current market position. What I need to know is the actual event, the moment when we enter a position. At the time at which my strategy enters a position, I need to set some variables in my strategy. Please re-read my question carefully, and please take another stab at answering. Thanks.

      Comment


        #4
        Hello westofpluto,

        I had informed you that the strategy position is updated in OnExecutionUpdate.

        Use OnExecutionUpdate to identify when the strategy position has changed, at the time at which your strategy enters or exits a position.

        Please clarify on withstanding issues if checking for a strategy position change here is not doing what you expect.

        JimNinjaTrader Customer Service

        Comment


          #5
          Ok, so in OnExecutionUpdate, you are saying it is possible to determine that the strategy just opened a new position. I assume that we can see this by checking the marketPosition value passed in?

          But the documentation says that marketPosition in this context can only be MarketPosition.Long or MarketPosition.Short. If I also wantto know when we havce closed a position, how will I know that in OnExecutionUpdate?

          Please explain.

          Comment


            #6
            Hello westofpluto,

            In OnExecutionUpdate, you can check the strategy's Position object and check the MarketPosition here to see if you have entered a position I.E. if (Position.MarketPosition == MarketPosition.Long) or if (Position.MarketPosition == MarketPosition.Short)

            To see if you have exited a position, you can check if the Strategy's Position object has a MarketPosition of flat. (Position.MarketPosition == MarketPosition.Flat)

            The marketPosition argument in OnExecutionUpdate lets us know if the the execution was a buy (Long operation) or a sell (Short operation)

            Please note that if you are scaling into an existing position, checking if (Position.MarketPosition == MarketPosition.Long) in OnExecutionUpdate would return as true and would not identify that you have entered a new position. You could create a class level Position object for the "LastSeenPosition" and check if (LastSeenPosition.MarketPosition == MarketPosition.Flat && (Position.MarketPosition == MarketPosition.Long || Position.MarketPosition == MarketPosition.Short)) to identify when a new position is entered for a strategy that scales into an existing position.

            For example:

            Code:
            private Position LastSeenPosition;
            
            protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
            Cbi.MarketPosition marketPosition, string orderId, DateTime time)
            {
                if (LastSeenPosition != null && LastSeenPosition.MarketPosition == MarketPosition.Flat && (Position.MarketPosition == MarketPosition.Long || Position.MarketPosition == MarketPosition.Short))
                    Print("New Position Entered.");
                else if (LastSeenPosition == null)
                    Print("New Position Entered.");
            
                LastSeenPosition = Position;
            }
            Please let us know if you have any questions.
            JimNinjaTrader Customer Service

            Comment


              #7
              Thanks, Jim, this makes sense except for this part:

              PHP Code:
              else if (LastSeenPosition == null)
                  Print(
              "New Position Entered."); 
              Is this a typo? Why would LastSeenPosition == null indicate a new position entered? And when would the strategy position object "Position" be null? Would it be null when we are flat?

              Comment


                #8
                Hello westofpluto,

                LastSeenPosition is our own property and would be initialized as null. We assign LastSeenPosition at the end of OnExecutionUpdate where it then is no longer null, so the first time that it is checked in the beginning of OnExecutionUpdate, it would be null. This would be the first execution made by the strategy which would open a strategy position.

                The strategy's Position object itself should never be null.

                Let us know if there is anything else we can do to help.
                JimNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by bmartz, 03-12-2024, 06:12 AM
                5 responses
                32 views
                0 likes
                Last Post NinjaTrader_Zachary  
                Started by Aviram Y, Today, 05:29 AM
                4 responses
                13 views
                0 likes
                Last Post Aviram Y  
                Started by algospoke, 04-17-2024, 06:40 PM
                3 responses
                28 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by gentlebenthebear, Today, 01:30 AM
                1 response
                8 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by cls71, Today, 04:45 AM
                1 response
                8 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Working...
                X