Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

What is "Close position" signal ?

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

    What is "Close position" signal ?

    Hi,

    I am having problems when I got into strategy development with multiple Position entry names with different properties (SL/PT) and handling within my strategy.

    I am observing some irregularities when backtesting this strategy and one of the issues is suddenly signal "Close position" started appearing at times when I am reversing positions.

    I am using Exit and then Enter but still there seems to be some problems with keeping track of how many contracts there currently are in the position and therefore how many need to be closed when Exit is called.

    Can you explain when the "Close position" signal is issued from NT platform?

    Best regards,
    Martin

    #2
    Hello Martin,
    Thanks for your note.

    The Enter() methods will first close any position in the opposite direction and then enter a trade.

    For example say you are long and a short signal is generated via the EnterShort() method. NinjaTrader will then first close the long position (and the order will be named as "Close position") and will then submit the short order.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Hi, thanks for the reply. I have assumed that this is the behaviour. What puzzles me is, what is this signal trying to Close and why. I am attaching an illustration of what is happening with comments.

      Can you suggest better way how to trace orders etc when testing a strategy so we can get to the bottom of this? At the moment I do not have much to go on, only my own debug output in the Output window and the names of the signals in the Chart window.

      Best regards,
      Martin
      Attached Files

      Comment


        #4
        Hello Martin,
        Please try using the below code in the initialize section of your code to trace the orders.

        Code:
        TraceOrders = true;


        For more debugging tips please refer to the posts here.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Hi Joydeep,

          thanks for suggesting the TraceOrders. I see now better output. Unfortunately I still do not see the reason for the "Close position" in the graph.

          1. There is no signal/order "Close position" in the orders trace

          2. Actually I found out, when I chart only last 30 days, I am not getting this problem. Only with longer history before this day (13.1.2011) I get this. I do not understand how that can differ, because we are always flat before the first Long Trade in the screenshot.

          We are flat because there is a test "(Position.MarketPosition == MarketPosition.Flat)" otherwise the entry could not be named Long XXXX from Flat ...

          Is there a way how to query from the strategy what positions are currently open and with how many contracts? I still do not see the reason why the platform is closing something by itself.

          Additionally I am having doubts whether I am maintaining my position properly. I am executing SetProfitTarget() method every bar (twice actually, to set both my profit targets for the positions) and also to maintain trailing stop loss to a certain level I amend stop loss with SetStopLoss(). It could happen that I try to SetStopLoss() for a position which is actually already closed because it hit PT, could that be a problem? If so, is there a way for me to to see from the code if my position with a certain EntryName was closed on SL or PT? That would help me a lot.

          Best regards,
          Martin

          Comment


            #6
            Hello Martin,
            Yes, you can do so by checking on the Positions class. Please refer to our help guide to know more about it


            Stop loss profit targets as submitted by the Set() methods has its own order name.


            If you have checked the market position (which is flat) then you should not be receiving the Close Position order. It will be generated only if you are not flat.

            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


              #7
              Hi Joydeep,

              thanks for the reply.

              1. In regards to Position Class, how do I get the information about the individual entries which were made to create a position? Names (EntrySignal), current quantity etc..

              2. In regards to Set() methods, I need to know what is the proper way to "maintain SL" ... is it ok to call SetStopLoss(fromEntrySignal,...) every bar? Will it just properly amend the order?

              Especially when e.g. 1 of 2 contracts from the position will be exited will the SL order contain updated quantity of only 1 contract?

              Best regards,
              Martin

              Comment


                #8
                Hello Martin,
                Unfortunately you wont be able to retrieve the position information for individual orders. You have to custom code it further.

                Yes, the Set() method will work fine in the OnBarUpdate event.

                In case of multiple orders please make sure to assign unique name to each order and make sure to refer that order name from the Set() methods.
                Last edited by NinjaTrader_Joydeep; 01-07-2013, 09:10 AM.
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Hi Joydeep,

                  I still do not understand why the "Close position" is not seen on the order trace. Does it mean there is no order associated with this? How will this behave in the live environment, will something be sent to the broker connection?

                  Best regards,

                  Martin

                  Comment


                    #10
                    Hello Martin,
                    TraceOrders, seems, will print out the strategy generated orders only and not the system orders.

                    You will however find the information about Close Position order in the Log tab.

                    Alternatively you can further custom code and append your own log in the say OnExecution event.

                    Code:
                    protected override void OnExecution(IExecution execution)
                    {
                    	if (execution.Order == null) return;
                    	Print(Position.MarketPosition.ToString() + "  " + execution.Order.Name);
                    }
                    JoydeepNinjaTrader Customer Service

                    Comment


                      #11
                      Hi Joydeep,

                      I see in the log this happening :

                      08/01/2013 09:21 Order Kinetick – End Of Day (Free), Order 'NT-00138' can't be submitted: Instrument type 'Future' is not supported by any provider connected. affected Order: BuyToCover 1 Limit @ 2297.25
                      08/01/2013 09:21 Order Order='8e5bd08792544f3daef227f2b5827412/Sim101' Name='Profit target' New state=Rejected Instrument='NQ 12-12' Action=BuyToCover Limit price=2297.25 Stop price=0 Quantity=1 Type=Limit Filled=0 Fill price=0 Error=OrderRejected Native error='Order 'NT-00138' can't be submitted: Instrument type 'Future' is not supported by any provider connected.'

                      Apparently for some reason one of the orders (profit target for short position) is rejected and somehow that creates the problems in my strategy.

                      This problem does not appear however when the backtest mechanism is used.

                      Unfortunately this got me sidetracked a bit because my main problem is that I have some odd long unclosed trades in my strategy when running backtest, I will have to start separate thread about those ones most probably.

                      Can you however explain what is the reason of this problem? I am running simmulation NT (no live trading).

                      Best regards,
                      Martin

                      Comment


                        #12
                        Hello Martin,
                        This is to confirm I have received your email and have replied to it.
                        JoydeepNinjaTrader Customer Service

                        Comment


                          #13
                          Similar problem with Close Position

                          Hi, I think I have a similar problem with Entry and Exit called 'Close position'. My strategy is pretty simple

                          if(ShortConditionTrue)
                          EnterShort(1,"Short");

                          if(LongPositionTrue)
                          EnterLong(1,"Long");

                          I do not even have ExitLong or ExitShort calls. But, "Close Position" is still being executed. Trades (sim account with live market) are shown In the attached file and you would see that they are executed with another Long order at the same time.
                          Any thoughts? I am pretty stumped with them.

                          I even have switched to Unmanaged in other cases and tried to cancel the "Close Position" orders but it does not work. Any help would be appreciated.
                          Attached Files

                          Comment


                            #14
                            gotet0, that would be expected - the Enter() methods will reverse for you, that's why you see a Close position and a new entry. As for example if you're long 1 contract and want to get to 1 short, you would have to effectively sell 2 (one to get flat 'close position', one to get net short 1).

                            Comment


                              #15
                              Thanks for replying, Bertrand. I understand that closing position is done to get the current trade flat.
                              But, should not getting flat with 'close position' close the trade right away instead of using the trade logic for the reverse position?

                              So, in your example, should not the trade to get flat close the current position right away and then only AFTER that, open the reverse position?

                              Another thing I notice is that "Close position" scenario does not occur consistently. Sometimes, my strategy would run fine with no Entry Names "Close position". But, sometimes, the scenario in your example shows up.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              656 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              370 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              578 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X