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

Move Stoploss and Takeprofit in strategy once trade is open.

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

    Move Stoploss and Takeprofit in strategy once trade is open.

    Hello,

    I have created a strategy that includes Stoploss and Takeprofit.
    I have put the lines in the OnStateChange() state.

    Everything works fine, but I have a question.

    I want to move the SL and TP once the trade is open.

    I see that if I do it automatically returns to the initial values as soon as it updates the bar.

    How can I do to be able to move those levels when the operation is already open?

    Thanks for your help and have a nice day.​

    #2
    Hello Mestor,

    Thank you for your post.

    When using the Set() methods, like SetStopLoss() and SetProfitTarget(), the method can be called during OnStateChange() if you want the price to be static and not move. If you want to dynamically change/modify the price, the method should be called from within OnBarUpdate() and the price should always be reset when the strategy becomes flat again. This information is noted in the "Tips" section on the following help guide pages:The reference sample script SamplePriceModification demonstrates how to change the price of stop loss and profit target orders:


    Please let me know if I may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your help.

      I have imported the example from the link to test and I have the same problem.

      When a position is opened, SL and TP are well defined.

      But if I manually modify those levels, they change again when updating the bar.

      I would like them not to move.

      Thanks​

      Comment


        #4
        Hello Mestor,

        Thank you for your reply.

        I apologize for any miscommunication or confusion. The reason I shared that strategy is to demonstrate how to programmatically move a stop loss or profit target order. If you try to adjust orders manually, they will update to the programmed value on the next call of OnBarUpdate(). This is expected behavior because automated strategies are designed to be left running on their own without manual intervention.

        What are you looking to achieve with your strategy? When you manually move your stop loss and profit target, what is the reason why and how do you decide which price to move them to? Is this something you might be able to define programmatically in your script so that you do not need to manually move the orders? If not, you might want to look into using ATM strategies which do allow you to modify the stop loss and profit target:


        You may even use NinjaScript to generate an entry that executes an ATM strategy. It is important to review the information here that describes the differences between orders generated by a NinjaScript strategy and orders generated by ATM strategies:


        With that information, you may choose to incorporate ATM strategy methods in your strategy to get the desired result:


        Please let me know if I may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          When using the Set() methods, like SetStopLoss() and SetProfitTarget(), the method can be called during OnStateChange() if you want the price to be static and not move. If you want to dynamically change/modify the price, the method should be called from within OnBarUpdate() and the price should always be reset when the strategy becomes flat again. This information is noted in the "Tips" section on the following help guide pages:The reference sample script SamplePriceModification demonstrates how to change the price of stop loss and profit target orders:

          NinjaScript > Educational Resources > Reference Samples > Strategy > Modifying the price of stop loss and profit target orders
          https://ninjatrader.com/support/help...of_stop_lo.htm
          I'm running into a similar issue, so I have an additional question:
          I'm calling SetStopLoss() and SetProfitTarget() from another method called within OnBarUpdate() but the StopLoss and Target both move back to their initial position at each bar.
          Do SetStopLoss() and SetProfitTarget() have to be called "on their own" within OnBarUpdate() (as shown in the SamplePriceModification example) or can they be called from another method (in a cascading way)?
          Thanks for your help.

          Comment


            #6
            Hello z.franck,

            Welcome to the NinjaTrader forums!

            You can call Set methods from any method, but it's important to call and reset these before calling an entry method.

            These will revert to the strategy assigned price on each bar update, which means these won't be manually modifiable.

            To have a stop and target that can be manually modified, use exit orders with isLiveUntilCancelled as true, and submit these only once without calling or changing these again from the code.
            Hi I would like to be able to move the stop manually in a strategy created in strategy builder, is there any line of code that could help to be able to move the SL and target once the order is placed?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thanks for your reply Chelsea,
              So what you're saying is that there should be exit orders done once (as soon as) the entry position has been triggered regardless of how the trade is going (as shown in the ExitMethodsSample exemple), right?

              Comment


                #8
                Hello z.franck,

                You could submit the stop and limit immediately when the entry fills in OnExecutionUpdate(), or you can wait to submit these sometime later in OnBarUpdate().

                The important thing is to use Exit order methods with isLiveUntilCancelled true, and only submit them once.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks Chelsea,
                  But if I don't use the SetStopLoss() and SetProfitTarget() but use instead ExitLongLimit() and ExitLongStopLimit() (for target and stop loss) with isLiveUntilCancelled as true, the Stop and Targets don't show on the graph.
                  The strategy triggers the order as it should but Stop and Targets are not visible.
                  Any suggestion?
                  Thank you

                  Comment


                    #10
                    Just to clarify, this is what I do:

                    private void OpenLongTrade()
                    {
                    if (Position.MarketPosition == MarketPosition.Flat)
                    {
                    EnterLong(Target_1_Qty + Target_2_Qty, "LongTrade");
                    inLongTrade = true;
                    }

                    if ((Position.MarketPosition == MarketPosition.Long) && inLongTrade)
                    {
                    // ---- Stop Loss
                    ExitLongStopLimit(0, true, Target_1_Qty + Target_2_Qty, 0, Position.AveragePrice - Stop_Loss * TickSize, "LongTradeStop","LongTrade");

                    // ---- Targets
                    ExitLongLimit(0, true, Target_1_Qty, Position.AveragePrice + Target_1 * TickSize, "LongTradeTarget1", "LongTrade");
                    ExitLongLimit(0, true, Target_2_Qty, Position.AveragePrice + Target_2 * TickSize, "LongTradeTarget2", "LongTrade");

                    inLongTrade = false;
                    }
                    }​

                    Comment


                      #11
                      Hello z.franck,

                      Thank you for your reply.

                      You mentioned that when you submit your protective orders with the Exit() methods, they are not showing on the chart. Do you see these orders on the Orders tab of the Control Center after the entry is filled and the order is submitted? I suggest enabling Trace Orders to get information sent to the NinjaScript Output window when orders are submitted, changed, canceled, ignored, or rejected. This should give you more info about which orders are submitted and at what price they are submitted at, or if any orders are being ignored or causing errors. For more information about using Trace Orders (and prints) for debugging:


                      You can use Trace Orders to verify the prices that the orders are submitted at and ensure that price is visible within your chart's scaling. You could also add Print() statements to double-check the values you are using for the price of the exit orders, such as:
                      Code:
                      Print("Submitting Stop loss with limit price: " + 0 + " and stop price: " + Position.AveragePrice - Stop_Loss * TickSize);
                      ExitLongStopLimit(0, true, Target_1_Qty + Target_2_Qty, 0, Position.AveragePrice - Stop_Loss * TickSize, "LongTradeStop","LongTrade");
                      The syntax you are using for the ExitLongStopLimit() is as follows, and you will need to decide if the limitPrice and stopPrice are the desired values:
                      ExitLongStopLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, double stopPrice, string signalName, string fromEntrySignal)

                      You could also print the values for the limit price of your ExitLongLimit() orders and determine if those are teh desired price or not.

                      Please let us know if we may be of further assistance.
                      Emily C.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks Emily,

                        When the order is triggered the Stop and Limit don't show in the Orders tab (they're both at 0) while the Avg. Price is coherent.
                        So, it looks like the EnterLong() method is doing its job, but both the ExitLongStopLimit() and ExitLongLimit() are not.

                        Can you clarify what the limitPrice refers to in ExitLongStopLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, double stopPrice, string signalName, string fromEntrySignal)? I get that the stopPrice is where my Stop loss should be but I'm not sure what the limitPrice refers to.

                        Thankyou

                        Comment


                          #13
                          Thanks for all your help!
                          I figured out what was wrong with your guidance. You're all so very helpful.

                          I have 1 more question though: let's say the trade has 3 contracts, 1 stop loss (for the 3 contracts) and 2 targets (1st one with 1 contract & 2nd one with 2 contracts)
                          When the price hits the first target, the trade is still on with the 2 remaining contracts, but of course, the ExitLongStopLimit(), which was configured with 3 contracts still holds a stop limit for those 3 initial contracts. This means that if the price changes direction and hits the stop loss, I'm out of the original trade but back into an unwanted trade with 1 contract in the opposite direction.

                          The most obvious solution that comes to mind would be to enter the trade not with 1 EnterLong() but with 2 (basically, triggering the trade with as many EnterLong() as there are targets)
                          Does that sound like the adequate way to do this or do you have better suggestions?

                          Thanks again.

                          Comment


                            #14
                            Originally posted by z.franck View Post
                            Thanks for all your help!
                            I figured out what was wrong with your guidance. You're all so very helpful.

                            I have 1 more question though: let's say the trade has 3 contracts, 1 stop loss (for the 3 contracts) and 2 targets (1st one with 1 contract & 2nd one with 2 contracts)
                            When the price hits the first target, the trade is still on with the 2 remaining contracts, but of course, the ExitLongStopLimit(), which was configured with 3 contracts still holds a stop limit for those 3 initial contracts. This means that if the price changes direction and hits the stop loss, I'm out of the original trade but back into an unwanted trade with 1 contract in the opposite direction.

                            The most obvious solution that comes to mind would be to enter the trade not with 1 EnterLong() but with 2 (basically, triggering the trade with as many EnterLong() as there are targets)
                            Does that sound like the adequate way to do this or do you have better suggestions?

                            Thanks again.
                            Hello z.franck,

                            Glad to hear you were able to figure out what was wrong.

                            When it comes to separate stops and targets for multiple contracts, yes the most appropriate way to handle this is typically separating the entry order into separate quantities. In this case, if you want 2 targets, that means you should have two separate entry orders (one with 1 contract and another with 2 contracts). It is also a best practice to utilize the entrySignal parameter in the entry orders so you can tie the exit orders to the entry via fromEntrySignal.

                            Please let us know if we may be of further assistance.
                            Emily C.NinjaTrader Customer Service

                            Comment


                              #15
                              Hi Emily,
                              I'm following up on my previous post as I ran into some issues: sometimes (not always), when I the EnterLong() kicks off, the order is properly triggered but the MarketPosition remains Flat.
                              It means that the following test : if (Position.MarketPosition == MarketPosition.Long) is negative as it's still flat and therefore the following ExitLongLimit() is never executed.

                              Hope the question is clear.
                              Do you have any idea how to address this issue.

                              Thanks

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DJ888, Today, 10:57 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              158 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Today, 09:29 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post Belfortbucks  
                              Started by zstheorist, Today, 07:52 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post zstheorist  
                              Started by pmachiraju, 11-01-2023, 04:46 AM
                              8 responses
                              151 views
                              0 likes
                              Last Post rehmans
                              by rehmans
                               
                              Working...
                              X