Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position detection problem

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

    Position detection problem

    Hi,
    I'm working with CalculateOnBarClose = true on 60 minutes bars.
    Te idea is to send orders only if the strategy is flat, positions are closed with market orders sent when a condition is met.
    Theoretically if I'm currently long and at the end of current bar I send a market order to close my position, next bar I should do nothing as at the end of this bar I'm still long (I'm flat only on the open of next bar), yet my strategy sends orders also in the bars where positions are closed with MKT orders.
    I detect positions with variable IsFlat as get return Positions[0].MarketPosition == MarketPosition.Flat
    Am I considering in some wrong way the moment the position is closed or am I mistaking something else?
    Thansk, bye
    Smodato

    #2
    Hello smodato,
    The MarketPosition property will get updated whenever you get flat, and not at the end of the bar as you perceive.

    NinjaTrader is a event driven application and the positions get updated whenever you have an execution and not at the end of the bar.

    You can update the IsFlat variable in the OnExectution event;
    Code:
    protected override void OnExecution(IExecution execution)
    {
    	if (Position.MarketPosition == MarketPosition.Flat)
    		isFlat = true;
    	else isFlat = false;
    }


    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Joydeep View Post
      Hello smodato,
      The MarketPosition property will get updated whenever you get flat, and not at the end of the bar as you perceive.

      NinjaTrader is a event driven application and the positions get updated whenever you have an execution and not at the end of the bar.

      You can update the IsFlat variable in the OnExectution event;
      Code:
      protected override void OnExecution(IExecution execution)
      {
      	if (Position.MarketPosition == MarketPosition.Flat)
      		isFlat = true;
      	else isFlat = false;
      }


      Please let me know if I can assist you any further.
      Thanks, ye I think your solution is what I wanted to avoid, if I'm in position at the end of this bar I DON'T want to place an order next bar regardless of the fact my position could be closed at the very start of next bar (Tradestation logic which looks at the end of bar ), your solution acts after an execution and tells me I'm flat at the open of the bar which is what I was trying to avoid, or am I misinterpreting it?
      Smodato

      Comment


        #4
        Hello smodato,
        In such scenario you can further filter your code with the BarsSinceExit() function. Please refer to our help guide to know more about it.

        Code:
        if (conditions && (this.BarsSinceExit() == -1 || this.BarsSinceExit() > 1))
        {
        	//do things
        }
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Joydeep View Post
          Hello smodato,
          In such scenario you can further filter your code with the BarsSinceExit() function. Please refer to our help guide to know more about it.

          Code:
          if (conditions && (this.BarsSinceExit() == -1 || this.BarsSinceExit() > 1))
          {
          	//do things
          }
          http://www.ninjatrader.com/support/h...ssinceexit.htm

          Thanks I read the guide and I was wondering if in the case I mention it should not be barssinceexit > 0 instead of >1 (I don't want orders to be placed in the bar where I exited on the open so the bar has not elapsed yet).
          Smodato

          Comment


            #6
            Hello smodato,
            Yes, the 0 (zero) bar will be the bar where the exit took place. If you want to place order on the next bar then it will be BarsSicenExit() > 0.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Joydeep View Post
              Hello smodato,
              Yes, the 0 (zero) bar will be the bar where the exit took place. If you want to place order on the next bar then it will be BarsSicenExit() > 0.
              I tried both with 1 and 0 but on backtest I keep on getting entries on bar of close positions, I attach a screenshot with an example.
              Smodato
              Attached Files

              Comment


                #8
                Hello smodato,
                In backtest orders will be submitted on the close of the bar only (at the opening of the new bar).
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Joydeep View Post
                  Hello smodato,
                  In backtest orders will be submitted on the close of the bar only (at the opening of the new bar).
                  So sorry but I don't understand, in this case theoretically the IsFlat check should be OK (but I added the BarsSinceExit as well), how could those positions be opened on the bars if I sent orders only with IsFlat = true?
                  Smodato

                  Comment


                    #10
                    Hello smodato,
                    To assist you further can you please send a toy NinjaScript code* replicating the behavior to support[AT]ninjatrader[DOT]com

                    Please append Attn:Joydeep in the subject line of the email and give a reference of this thread in the body of the email.

                    I look forward to assisting you further.

                    *The "toy" just means something that is a stripped down version that isn't necessarily the whole logic. It makes things easier to rout out.
                    JoydeepNinjaTrader Customer Service

                    Comment


                      #11
                      I think I found the mistake, the problem was not related to a new Entry order but to a double exit (depending on different rules) which was actually reversing the position, now I took care to separate the action due to different rules and the issue disappeared.
                      Thanks for your help
                      Smodato

                      Comment


                        #12
                        Hello smodato,
                        Glad to know you could figure it out.

                        Please let me know if I can assist you any further.
                        JoydeepNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by ninza33, Today, 12:31 PM
                        1 response
                        4 views
                        0 likes
                        Last Post NinjaTrader_LuisH  
                        Started by AlgoDreamer, Today, 12:39 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post AlgoDreamer  
                        Started by nleitman, Yesterday, 11:46 AM
                        17 responses
                        45 views
                        0 likes
                        Last Post nleitman  
                        Started by tradingnasdaqprueba, Today, 03:42 AM
                        7 responses
                        31 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by kaywai, Today, 11:59 AM
                        1 response
                        6 views
                        0 likes
                        Last Post NinjaTrader_Erick  
                        Working...
                        X