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

Scaling out of Position

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

    Scaling out of Position

    Unfortunately I need to ask this question that's been asked before. I wouldn't ask it but most of the knowledge base that discusses this in the forums all the links are broken that reference the solutions. So I will ask it again.

    Position Size is 4 contracts
    Primary StopLoss = 12 points
    Target 1 = 20 points Exit 1 contract Move stop to 2 points below the 20 point target
    Target 2 = 30 points Exit 1 contract Move stop to 2 points below the 20 point target
    Target 3 = 40 points Exit 1 contract Move stop to 2 points below the 20 point target
    Target 4 = 50 points Exit 1 contract Move stop to 2 points below the 20 point target

    If SL1 is hit, close full order
    When TP1 is hit sell 1 contract from LOTSIZE and now make SL = TP1 - 2 points $118 (example: price was 100 at execution, now price made it to 120, TP1 hit, 1 contract sold, now 3 contracts remaining with SL of 118 - if 118 is hit all three contracts will be sold)
    When TP2 is hit sell 1 contract from LOTSIZE and now make SL = TP2 - LADDER $128 (example: from above, 3 contracts were left, 1 we just now sold at 130, now 2 are left, set SL to 128)
    When TP3 is hit sell 1 contract from LOTSIZE and now make SL = TP3 - LADDER $138 (example continuing from above, we just sold 1 contract at 140, now 1 contract is left and set the SL to 138)
    TP4 is hit at 150, final contract is sold, nothing left to do​

    Can this be done using the managed orders? Can I do it without manually monitoring market data and issuing ExitLong or ExitShort for each 1 contract to scale out?

    So if this can't be done with managed orders, how do you do it unmanaged?
    Last edited by dmisselhorn; 03-11-2024, 06:13 PM.

    #2
    Hello dmisselhorn,

    Thank you for your post.

    This can be done using the managed approach. You can tie your targets together with an entry using the signal name.

    I recommend you take a look at the SampleScaleOut script that demonstrates scaling out of a position using stops and targets:



    SamplePriceModification also demonstrates the modification of the stop loss order to a break even price once a desired level of profit has been reached.



    Please let us know if you have any further questions.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      I think my take away here is what I expected, to target scale out you must enter with as many "named" entries as you want to scale out of. I actually knew that I was just wondering if that's the only alernative to the the scaled target approach.

      Is to do the
      EnterLong("Long 1");
      EnterLong("Long 2");
      ​EnterLong("Long 3");
      EnterLong("Long 4");

      SetProfitTarget("Long 1", Calculation.Ticks, 20);
      SetProfitTarget("Long 2", Calculation.Ticks, 30);
      SetProfitTarget("Long 3", Calculation.Ticks, 40);​​
      SetProfitTarget("Long 4", Calculation.Ticks, 50);

      And than slide the stop manually as the trade progresses

      Comment


        #4
        Where's the best place to slide the stop the when I see position quantity drop?

        The OnPositionUpdate:
        protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
        int quantity, Cbi.MarketPosition marketPosition)
        {}​

        Or OnOrderUpdate:
        protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
        int quantity, int filled, double averageFillPrice,
        Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
        {}​

        Comment


          #5
          Hello,

          If you are scaling out using profit targets, then using the signal names to scale out would be the usual approach.

          You can reset a stop loss in OnBarUpdate(). SamplePriceModification (from my previous post) demonstrates resetting the stop loss.

          Please let me know if you have any further questions.
          Gaby V.NinjaTrader Customer Service

          Comment


            #6
            You didn't answer my question. The Problem with using OnBarUpdate is that when the reduction in the position occurs you might have to wait 5, 10, or 15 minutes to get the OnBarUpdate to make the StopLoss change. If I used OnOrderUpdate or OnPositionUpdate I could do it immediately and protect the stop from sliding deeper into the loss. Do you understand my question? Can I not call SetStopLoss(CalculationMode.Price, Positions[0].AveragePrice + 20 - 2) from the OnPositionUpdate or OnOrderUpdate when I see the Position Quantity reduced? To indicate I hit Target 1?

            Comment


              #7
              Hello dmisselhorn,

              Apologies for not being more clear.

              It is possible to call SetStopLoss() and SetProfitTarget() in OnPositionUpdate() or OnOrderUpdate() if you are not wanting to do this from OnBarUpdate() - but we would recommend using Exit methods and following the SampleOnOrderUpdate reference sample that demonstrates using OnOrderUpdate() and OnExecution() to enter protective orders.



              Please let me know if you have any other questions.
              Gaby V.NinjaTrader Customer Service

              Comment


                #8
                If I run 2 time frames, by possibly going down to 1 minute for doing the shifting of the SetStopLoss() will that cause an issue if I enter on the 10min and shift my stop as the positions target down on the 1 minute? Will this cause any issues?

                AddDataSeries(Data.BarsPeriodType.Minute, 1);

                Than if I watch BarsInProgess == 1 change it like this:

                if ((Position.MarketPosition == MarketPosition.Long) && (BarsInProgress == 1))
                SetStopLoss(CalculationMode.Price, Position.AveragePrice + (TickSize * Target1) - (TickSize * StopLossTarget1));

                Comment


                  #9
                  Hello dmisselhorn,

                  Thank you for your response.

                  Submitting orders for to the secondary time frame when using Set() methods can cause issues. You should only submit orders to the primary time frame.

                  From the Help Guide:

                  "Should you have multiple Bars objects of the same instrument while using Set() methods in your strategy, you should only submit orders for this instrument to the first Bars context of that instrument. This is to ensure your order logic is processed correctly and any necessary order amendments are done properly."

                  This is from the Help Guide page below under the section titled "Entering, Exiting and Retrieving Position Information"​



                  Please let me know if you have any further questions.
                  Gaby V.NinjaTrader Customer Service

                  Comment


                    #10
                    The entry orders were done on a 10min timeframe I'm changing the stop orders on a 1 minute timeframe since they could be targeting within the 10 min timeframe. If I only set the stop orders on the 1 minute timeframe does that properly handle the "can cause issues" problem?

                    Comment


                      #11
                      Hello,

                      This could still cause issues because you should only use SetStopLoss() on the primary bar series the script is running on. Since the 1 minute timeframe is an added Bars object, this would not be the first bars context/primary bar series.

                      If you would like to submit orders to a secondary series, you could use Exit methods for your stops/targets and pass in the BarsInProgressIndex you want to submit orders to into the Exit methods.

                      For example, ExitLongStopMarket() could be used to submit a stop for a long position and you would use BarsInProcessIndex 1 to submit the exit order to the first added secondary series.



                      ExitLongStopMarket(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double stopPrice, string signalName, string fromEntrySignal)

                      Please let me know if you have any further questions.
                      Gaby V.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Jonker, Today, 01:19 PM
                      0 responses
                      1 view
                      0 likes
                      Last Post Jonker
                      by Jonker
                       
                      Started by futtrader, Today, 01:16 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post futtrader  
                      Started by Segwin, 05-07-2018, 02:15 PM
                      14 responses
                      1,791 views
                      0 likes
                      Last Post aligator  
                      Started by Jimmyk, 01-26-2018, 05:19 AM
                      6 responses
                      844 views
                      0 likes
                      Last Post emuns
                      by emuns
                       
                      Started by jxs_xrj, 01-12-2020, 09:49 AM
                      6 responses
                      3,294 views
                      1 like
                      Last Post jgualdronc  
                      Working...
                      X