Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Dynamic stop loss and profit target

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

    Dynamic stop loss and profit target

    Hello!
    I have a strategy developed in strategy builder, but it does not work very well.
    I want my stop loss and my profit target to be placed below/above the signal bar.
    When the conditions are met, the order is triggered, but I cannot have the stop loss and the profit target placed automatically.
    Since my stop loss and my profit target will be placed dynymically, I thought this would happen when the signal bar is mature and would be Bar[1] and not Bar[0].
    Anyway I can not have my stop loss and my profit target when my order is triggered.

    I have the following:

    Tab1
    HTML Code:
    TriggerState >= 2
    Position.MarketPosition = MarketPosition.Flat
    
    Do the following
    TriggerState = 0;


    Tab2
    HTML Code:
    myCondition
    Position.MarketPosition = MarketPosition.Flat
    
    Do the following
    TriggerState = 1;
    EnterLongStopMarket(DefaultQuantity, (High[0] + (1 * TickSize)) , @"Long");
    Tab3
    HTML Code:
    TriggerState = 1
    Position.MarketPosition = MarketPosition.Long
    
    Do the following
    TriggerState = 2;
    StopLoss = (Position.AveragePrice + ((Low[1] + (LongInitialStopLoss * TickSize)) )) ;
    Tab4:
    HTML Code:
    Position.MarketPosition = MarketPosition.Long
    Default input[0] >= (Position.AveragePrice + (LongTraget * TickSize))
    
    Do the following
    ExitLong(DefaultQuantity, @"LongProfitTarget", @"Long");
    I would appreciate any help.

    Thanks in advance.
    Last edited by Stanfillirenfro; 04-28-2022, 10:50 AM.

    #2
    Hello Stanfillirenfro,

    The code you are using seems to be similar to the builder break even sample. In that situation you would have to wait for the position to update so that you can dynamically calculate the targets. The builder does not have a way to have dynamic targets that submit with the entry when using the stops and targets page so what you have would essentially be the best way to control targets while in the builder.



    Comment


      #3
      Many thanks Jesse for your reply. Yes, the code I have is from the example you mentioned above.
      Please what do you mean with
      you would have to wait for the position to update so that you can dynamically calculate the targets.
      ?
      The builder does not have a way to have dynamic targets that submit with the entry when using the stops and targets page
      I haven't used the stops and targets page. I have declared and initialyzed these values in variables inputs.
      so what you have would essentially be the best way to control targets while in the builder.
      Since I am NOT having the profit target and stop loss printed on the chart, what could be the problem?
      Using Bar[1] instead of Bar[0] could be advisable here?

      Many thanks for your help.

      Comment


        #4
        Hello Stanfillirenfro,

        Please what do you mean with
        you would have to wait for the position to update so that you can dynamically calculate the targets.
        The way that script works is based on the position, that is why the targets cannot be submitted with the entry. The strategy builders stops and targets page normally submit the targets based on the fill of the entry but those can't be dynamic, the approach you are using with this sample would be the way to have dynamic targets but you do have to wait like you mentioned.


        Since I am NOT having the profit target and stop loss printed on the chart, what could be the problem?
        If you made any modifications to the sample I wouldn't be able to say, you would have to track down what part in the logic the script is getting to in order to better understand what the problem is.


        To quickly go over the sample and how it works:

        // Set 1
        resets the trigger state when flat

        // Set 2
        enters while flat and sets trigger state to 1

        // Set 3
        checks that a position exists and that we made it to trigger state 1, if so the stop and target price are calculated and we move to trigger state 2

        // Set 4
        checks that we made it to state 2 and that the break even price is met, then the break even happens and we move to state 3.

        // Set 5
        If the trigger state is greater than or equal to 2 meaning the target was at least calculated then we submit the exit order as long as the state remains 2 or more. This holds the order open so it does not expire after 1 bar

        Comment


          #5
          Many thanks Jesse for your reply!

          I am understanding the logic better. But here I do not have the strategy for break even. NO. I did not take this part into consideration. In my case the set 4 does not exist.
          Please Jesse is the following syntax correct with stop loss placed under the Bar[1]?
          HTML Code:
          StopLoss = (Position.AveragePrice + ((Low[1] + (LongInitialStopLoss * TickSize)) )) ;
          Entry is on the Bar[0], but could the stop loss be placed on Bar[1] instead of Bar[0]? Because when the order is triggered the signal bar becomes Bar[1].
          Another question. Is there a way to modify the stop loss when it is not placed dynamically? I have it on my others strategies, but everytime the order i striggered and the stop loss placed, I try to move the stop loss but it comes back automatically.

          Many thanks for your help.

          Comment


            #6
            Hello Stanfillirenfro,

            You don't need to do the breakeven I just included that for a complete overview. The last step is really the most important and making sure the state reached 2 or greater to hold that order open.

            Entry is on the Bar[0], but could the stop loss be placed on Bar[1] instead of Bar[0]? Because when the order is triggered the signal bar becomes Bar[1].
            The stop should be placed later or on Bar[1] in your example, if you are using OnBarClose processing that would be the next bar.

            Another question. Is there a way to modify the stop loss when it is not placed dynamically? I have it on my others strategies, but everytime the order i striggered and the stop loss placed, I try to move the stop loss but it comes back automatically.
            In the builder no, you can only dynamically make targets if you need to modify them later. The stops and targets page of the builder uses the Set methods for its targets which are static.

            If you mean manual modifications then that is also expected not to work based on how the builder holds open orders, on each bar it calls the order again and uses the same values so it would snap back.




            Comment


              #7
              Many thanks Jesse for your reply.
              The stop should be placed later or on Bar[1] in your example, if you are using OnBarClose processing that would be the next bar.
              I am using OnBarUpdate(). What would be please the difference here? I mean, because I am using OnBarUpdate()?

              I am not yet identifying the problem! Hummmm!!!!

              You mentioned above that
              The way that script works is based on the position
              . Pleaae what do you mean with it?

              Thanks!

              Comment


                #8
                Hello Stanfillirenfro,

                I am using OnBarUpdate(). What would be please the difference here? I mean, because I am using OnBarUpdate()?
                I am not sure I understand the question, was there a confusion between OnBarUpdate and the Calculate.OnBarClose? I mentioned Calculate.OnBarClose in the text you quoted.



                You mentioned above that
                The way that script works is based on the position
                . Pleaae what do you mean with it?
                If we look at the sample description for set 3:

                // Set 3
                checks that a position exists and that we made it to trigger state 1, if so the stop and target price are calculated and we move to trigger state 2

                The position is used to know that the entry was submitted. If we compare the Stops and Targets page to this sample, the Stops and Targets page will submit targets based on the fill of an entry and no position update is needed. Because you are dynamically calculating the targets we cant use the Stops and Targets page so that logic is now moved to OnBarUpdate. The only way we will know the entry happened is to check the position.

                Comment


                  #9
                  Many thanks Jesse!

                  I will deal with this problem these days and please maybe I will revert to you if I continue to stack somewhere.

                  Thanks again!

                  Comment


                    #10
                    Hello NinjaTrader_Jesse!
                    I am coming back to this topic because I did not have it closed yet. I am makig some improvement.
                    When the conditions are met, the order is submitted appropriately. At the close of the signal bar, the order gets filed corretly. And when the order is filed, the stop loss and the profit target are not placed yet. Only after the next bar is closed, that means the bar formed after the signal bar, the stop loss ist placed parfectly as stop order in the opposite direction to which the order is filed. But the target profit is not placed unfortunately.
                    Correct me please if I am wrong. When the signal bar is close, the bar is no longer bar[0], but bar[1], and the next bar is the bar[0]. I was expecting my stopLoss and profitTraget to be placed when the bar[0] is still in progress, that means the bar forming just after the signal bar. But in my case, the stop loss is placed at bar[2], which is wrong. How could I fix this problem?
                    Moreover, even when I am using Bar[0] for StopLoss instead of bar[1], it is placed ONLY when the bar after the signal bar has closed, making the signal bar the bar[2].
                    I would appreciate it if you could have a look on the code and advise me on this two issues please.

                    I have the following code until the set #3:

                    Set#1:
                    HTML Code:
                    TriggerState >= 2
                    Position.MarketPosition = MarketPosition.Flat
                    
                    TriggerState = 0;
                    Set#2:
                    HTML Code:
                    MyIndicator
                    Position.MarketPosition = MarketPosition.Flat
                    
                    TriggerState = 1;
                    EnterLongStopMarket(DefaultQuantity, (High[0]+(1*TickSize)), @LongEntry);
                    Set#3:
                    HTML Code:
                    TriggerState = 1
                    Position.MarketPosition = MarketPosition.Long​
                    
                    TriggerState = 2;
                    ProfitTarget = (High[1]+ (x*TickSize));
                    StopLoss = (Low[1]+(-1*TicSize));
                    I am not using
                    Position.AveragePrice
                    anymore.

                    Thanks in advance.
                    Last edited by Stanfillirenfro; 11-28-2022, 01:17 PM.

                    Comment


                      #11
                      Hello Stanfillirenfro,

                      This type of logic relies on bar events so you would need to wait for a bar close before the logic begins to work. The only solution would be to have more frequent bar events by changing to a faster timeframe if you needed to do something sooner.

                      Comment


                        #12
                        Thanks NinjaTrader_Jesse for your reply.
                        OK. But why do I have the stop loss placed even later and not the profit traget?

                        Comment


                          #13
                          Hello Stanfillirenfro,

                          The code you provided only places a stop, there is no target in that code. You would have to add another order for the target similar to the stop if you wanted a target. That logic can only work once you are in a position so it needs to wait for the OnbarUpdate event to be called after the entry filled before a stop is submitted.

                          Comment


                            #14
                            Hello NinjaTrader_Jesse and many thanks for your help.

                            I have overcomed some problems with this code. It is working fine. As you mentioned in your previous post, the stop loss and profit target are placed only when the Bar[0] on which the order is filed is closed. Since it is the logic, I am moving forward with it.

                            Here is another issue:
                            I was entering the trade with a stop order. In this cas, the bar needs to close and I enter one tick above/below the signal bar, using the following code for example:
                            HTML Code:
                            EnterLongStopMarket(DefaultQuantity, (High[0]+(1*TickSize)), @LongEntry);
                            With this code I am losing one to two ticks, which could be determinant in some cases. So I have decided to change the way to enter and to use limit orders. I want the prices to tick one tick above the high /below the low of the sigal bar and then enter the market one tick below the high/ above the low of the signal bar. I thought I could be using Bid/Ask prices, but the problem here is that Bid/Ask change on each tick.
                            What could be the best syntax with LimitEntry?

                            Many thanks in advance.

                            Comment


                              #15
                              Hello Stanfillirenfro,

                              You need to store the price you want to a variable and then use it with the last set where the order is submitted if you wanted a price from the signal bar. You can see the existing break even sample which shows how to use variables to control an orders price: https://ninjatrader.com/support/foru...rategy-builder

                              Using price series like High or ask/bid will change with each OnbarUpdate so the order will be moved on each OnBarUpdate. For a stationary order you need to save 1 price and then allow the strategy to resubmit the order on each OnBarUpdate to hold that price and keep the order open.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              54 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              130 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              72 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              44 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              49 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X