Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Flatten Position

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

    Flatten Position

    Is there a way to Flatten all the position of the strategy before disabling the strategy? I have a working strategy using an unmanaged approach and find it hard to close all the position when the strategy getting disabled?

    #2
    Hello edward_bell,

    Thanks for your note.

    This is possible using OnTermination().

    Please take a look a the attached sample I have created to demonstrate this.

    When you enable the script it will submit a buy market order. You can confirm this in the Positions tab of the Control Center.

    Then when you disable the script, the position will close. Again, confirm this in the Positions tab of the Control Center.

    Below is a link to the help guide on OnTermination().
    http://www.ninjatrader.com/support/h...rmination2.htm
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Yeah I have tried it with managed approach but how about unmanaged approach type of entries?

      Comment


        #4
        Hi edward_bell,

        Thanks for pointing that out.

        I've remade the sample. It will have the same name so when you import you will want to overwrite the files when prompted.

        This sample uses the unmanaged approach.
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks for that. Basically submit a market order on it so that will close the position.

          Comment


            #6
            Hello edward_bell,

            Thats correct. The code here submits a market order in the opposite direction for the position quantity.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Ok what about this situation, I have submitted 3 stops on my long entry got filled and then in the next 3 bars I want to exit that 3 stops? How I would do that on the unmanaged approach?

              submitted stops:

              if(stop1Order == null)
              {
              stop1Order = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, target1Contracts, 0, Position.AvgPrice - (20* TickSize), "st1L", "stop1");
              }

              if(stop2Order == null )
              {
              stop2Order = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, target2Contracts, 0, Position.AvgPrice - (20* TickSize), "st2L", "stop2");
              }

              if(stop3Order == null)
              {
              stop3Order = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, target3Contracts, 0, Position.AvgPrice - (20 * TickSize), "st3L", "stop3");
              }

              For example I have submitted 3 stops right after the entry order is filled. So if in the next 3 bars my unrealized profit got below with my expected loss then I would like to exit on that trade? So what would I do in that case?

              Comment


                #8
                Hello edward_bell,

                As I understand your question, you have three entries, each with an associated stop market order. After 3 bars, you would like to check to see if you unrealized profit is below a certain amount you would like to have the stop losses immediately exit. Is this correct?

                You could, after three bars check the unrealized profit and loss. If this is below a certain point change the orders you have to market orders.

                You would want to do this with OnExecution so that you know the order action and can get the Position.AvgPrice which won't be available until the entry order fills.

                I've continued adding this to the original script I made for you. Again you will want to overwrite when importing.
                Attached Files
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Yes I have submitted 3 stops in the OnExecution() when my entry got filled and after 3 bars since my entry I want to check my unrealized profit. And if my unrealized profit is already hit my loss cut off then I want to exit those 3 stops. So are you saying that stop1, stop2, and stop3 can be exit by submitting a market order of each stops? Like using ATM there is a method that ATMStrategyClose() where it will submit a market order to exit a current position.

                  Comment


                    #10
                    Hi edward_bell,

                    Please take a look at the sample I have added to and attached to my last post.

                    This sample does not check the unrealized profit, you can add any logic you would like for that.

                    Instead this waits 10 bars and then resubmits the stops as market orders so they fill immediately.

                    You would only want to add conditions to the if statement on line 10.
                    if (BarsSinceEntry() == 10)
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      :Okay thanks with that.

                      Comment


                        #12
                        Now I got a problem on the OnExecution() in submitting stops. I entered market order with 5 contracts (target1 = 2, target2 = 2, target3 = 1)and in the first it filled only 3 contracts and in the few seconds later on it filled the remaining 2 contracts and that's now 5 position. Basically the filling of 5 contracts does not happened on the same time.

                        Now in the first filled I have this logic:

                        stop1 = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 2, 0, Position.AvgPrice - 10 * TickSize, "", "stop1");
                        stop2 = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0, Position.AvgPrice - 15 * TickSize, "", "stop2");

                        because only 3 contracts filled

                        and now in the second filled which is the remaining 2 contract I am wondering how will be the stop submitted. Can I amend stop2 so that it would be

                        stop1 = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 2, 0, Position.AvgPrice - 10 * TickSize, "", "stop1");
                        stop2 = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 2, 0, Position.AvgPrice - 15 * TickSize, "", "stop2");
                        stop3 = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0, Position.AvgPrice - 20 * TickSize, "", "stop3");

                        so that it could accommodate the 5 contracts I entered?

                        I have no idea on how to handle on this one. Please help me on this one on how to submit and handle stops on this situation.

                        Comment


                          #13
                          Hello edward_bell,

                          I would like to make sure I am understanding correctly.

                          You entered the market with a quantity of 5. When then entry fills you submit 2 stop orders with a quantity of 3.

                          When these 3 fill, and your position quantity is 2, you would like to submit stops for the remaining quantity of 2. Is this correct?

                          In this case since your active position quantity is 2, you will want to submit an order for the remaining 2.

                          For example:
                          Code:
                          stop3 = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, [B]2[/B], 0, Position.AvgPrice - 20 * TickSize, "", "stop3");
                          Please let me know if I did not understand that right.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            I have submit a long entry with 5 contracts. Now the market filled 3 contracts at the moment then few seconds later the market filled the remaining 2 contracts. I have these on setup to stops (2 contracts for stop1, 2 contracts for stop2, and 1 contract for stop3) so a total of 5 contracts.

                            The first filled 3 contracts I have submitted 2 contracts for stop1 and 1 contract for stop2. Now in the second filled of 2 remaining contracts I am confused with it. Because I want to follow my setup for my stop which is 2 for stop1, 2 for stop2 and 1 for stop3.

                            Now my question is how to amend the stop2 that I have submitted in the first filled to make it a quantity of 2 since in the first filled I have submitted only 1 quantity of stop2?

                            Comment


                              #15
                              Hi edward_bell,

                              I am a little confused by your inquiry.

                              You say that you submit 3 stops. The first with 2 contracts, the second with 2 contracts, and the third with 1 contract.

                              Initially your entry of 5 had a partial fill of 3 contracts, then the remaining 2 contracts fill shortly after.

                              This means that the OnExecution is run twice for the entry order fills.

                              When the first 3 fill, are you specifically submitting two stop orders with quantities of 2 and 1? Or are you submitting 3 stop orders for the entire quantity of 5?
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              88 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              48 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              30 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              34 views
                              0 likes
                              Last Post TheRealMorford  
                              Started by Mindset, 02-28-2026, 06:16 AM
                              0 responses
                              68 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Working...
                              X