Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Issues with SetStopLoss and SetProfitTarget not triggering

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

    Issues with SetStopLoss and SetProfitTarget not triggering

    Hi,

    I am having issues getting the SetStopLoss and SetProfitTarget to trigger exits. In this example, I have just used EMA with an ATR multiple for exits. When I print these values out, they are calculating correctly, but when the price moves above/below these, an exit is not triggered (either through Market Replay or backtest).

    Here is my logic.Thanks for any help you can give me.

    Kylie.

    Code:
     
    protectedoverridevoid Initialize()
    {
    Add(RSI(rsidays, 3));
    SetProfitTarget("JB", CalculationMode.Price, profittarget);
    SetStopLoss("JB", CalculationMode.Price, trailingstop, false);
    CalculateOnBarClose = true;
    }
    
    protectedoverridevoid OnBarUpdate()
    {
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Price, 0);
    SetProfitTarget(CalculationMode.Price, 0);
    }
     
    elseif (Position.MarketPosition == MarketPosition.Long)
    {
    profittarget = EMA(ptemadays)[0]+2*ATR(atrdays)[0];
    trailingstop = EMA(ptemadays)[0]-ATR(atrdays)[0];
     
    SetStopLoss(CalculationMode.Price,trailingstop);
    SetProfitTarget(CalculationMode.Price, profittarget);
    }
     
    if (MIN(RSI(rsidays, 3),20)[0] < rsitrigger
    && Close[0] > (MIN(Low, 20)[0]) + 2*ATR(atrdays)[0])
    {
    EnterLong(1, "JB");
    }
     
    }

    #2
    Is it just sometimes, or all the time that it doesnt work?

    Comment


      #3
      Hello,

      Thank you for your forum post.

      Please add TraceOrders=True to the Initialize() method and to track these orders make sure that they are submitting and at what values they are submitting.



      Let me know if I can be of further assistance.
      BrettNinjaTrader Product Management

      Comment


        #4
        Hi Brett,

        Thanks for the advice on TraceOrders. I haven't used this before and it is very useful.

        I am getting output as follows for the profit target and stop losses:

        9/07/2010 12:00:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=14.3567318132324 Currency=0 Simulated=False
        9/07/2010 12:00:00 AM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='' Mode=Price Value=11.4902026086365 Currency=0 Simulated=False

        These values are correct. I assume that if the price moves through these levels intra-bar, this should trigger the exit order, but this is not happening. Even when the entire bar is below the stop loss level, an exit is still not triggered. The only exit that triggers during backtesting/Market Replay is an exit on close.

        I apologise if I missing something really simple. I copied the logic approach from Reference Sample 5 (SamplePriceModification) and this strategy backtests successfully, so I'm not sure what I have incorrect in mine.

        Any advice is much appreciated.

        Regards,
        Kylie.

        Comment


          #5
          Kylie, thanks for the reply. We will get back to you tomorrow.
          AustinNinjaTrader Customer Service

          Comment


            #6
            kylie,

            The logic you are using would not be advised. You would want to set your levels before you enter into a position. Meaning you want to call Set() methods and then EnterLong(). Right now your code is entering a position with Set() methods set to price 0. This means immediately on going long you get 0 priced stop/targets. This is most likely not what you want. Instead, please set the stop/targets right before going EnterLong() and as you go EnterLong() it will put in stop/targets at the correct prices right away.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Hi Josh,

              Thanks for your reply. The way the logic was working, it was setting the stoploss/profit target to 0 when I had no market position on, then was updating the stoploss/profit target to the correct values once I was long. I can see these orders are being amended each day with the correct values through the TraceOrders (example shown below in my post on 28/8). However, for completeness, I have now updated the script to set the stop loss/profit target values just before I enter long so there will always be a non-zero value when I am in the market.

              So, the order amendments show that the stop loss and profit target values are correct, but they are not triggering. The only exit triggering is exit on close. I get the same results regardless of what market or timeframe I use.

              Any advice would be much appreciated.

              Regards,
              Kylie.

              Comment


                #8
                Kylie,

                I suggest using Print() to see what exactly those profit target/stop loss prices are being seen as. Please print out your profittarget and trailingstop variables and run manual comparisons against the Close[0] price to see if they should have ever been hit.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Josh,

                  Thanks for the reply. I have already printed out the values which was how I identified the problem when I originally posted the question, prior to Brett helping me use the TraceOrders. So both printing out the values and using the TraceOrders shows the correct values but the orders are not triggering.

                  I have now drawn dots on the chart to represent the stop loss and profit target and these match the order values. As you can see from my attached screenshot, the chart definitely goes below these (both intra-day and at close) but the exits are not triggered.

                  Here is the code segment that is generating the dots and the stop loss / profit targets so they should match.

                  Code:
                   
                  else if (Position.MarketPosition == MarketPosition.Long)
                  {
                  if ((High[0] - atrmult*ATR(atrdays)[0]) > trailingstop)
                  {
                  trailingstop = High[0] - atrmult*ATR(atrdays)[0];
                  SetStopLoss(CalculationMode.Price,trailingstop);
                  }
                  DrawDot("Trailing Stop" + CurrentBar,false,0, trailingstop, Color.Blue);
                  
                  profittarget = EMA(ptemadays)[0]+atrmult*ATR(atrdays)[0];
                  DrawDot("Profit Target" + CurrentBar,false,0, profittarget, Color.Black);
                  SetProfitTarget(CalculationMode.Price, profittarget);
                  
                  // Print("Time"+Time[0].ToString()+" RSI min "+MIN(RSI(7,3),20)+" Min Low "+MIN(Low, 20)[0]+" Profittarget "+profittarget+" TrailingStop "+trailingstop);
                  }
                  I have also attached the results from the Output Windows for April 2010 so that you can see that the stop loss order is sitting at 14.35 as at 6 April 2010 but then is not triggered mid-April when the stock definitely moves below this point.

                  Thanks again for your help and anything else you can suggest would be much appreciated as this is very confusing!!

                  Regards,
                  Kylie.
                  Attached Files

                  Comment


                    #10
                    Hi Josh,

                    I have been trying to work on this again tonight and threw together a simple script that changes the stop loss to the previous bar's low, just to see if that would work. It worked fine, but it highlighted to me that in the script below, I am getting the output lines:

                    " Entered internal SetStopTarget() method ...."

                    where the stop loss and profit target values are correct, but I am not getting the actual Amend Order lines that I believe should follow in the output window. For example, I think I should be seeing:

                    "4/08/2010 12:00:00 AM Entered internal SetStopTarget() method.....
                    4/08/2010 12:00:00 AM Amended stop order: Order='NT-00011/Back101' Name='Stop loss'....... "

                    I am only getting the first line. Do you have any ideas why it could be missing the Amended stop order lines?

                    Having said all of that, even if the amendment orders aren't going through I don't understand why I am not taken out when the stock goes below the initial stop loss price (ie. 14.73 in this example, which the stock definitely goes below).

                    Again, any help would be much appreciated.

                    Regards,
                    Kylie.

                    Comment


                      #11
                      kylie, this is likely due to your signal name tagging, for example if you change the code in your first post to the below snippet, the exits should trigger -

                      Code:
                       
                      SetStopLoss("JB", CalculationMode.Price,trailingstop, false);
                      SetProfitTarget("JB", CalculationMode.Price, profittarget);

                      Comment


                        #12
                        Hi Bertrand,

                        Thankyou for your response. I just managed to figure that out for myself after comparing to another script that worked (I wish I had seen your post earlier!!). I initially created the script through the wizard and didn't notice that I had included the tag in there.

                        Thank you to everyone who has provided input along the way. It has been an excellent exercise in learning how to use Ninjatrader and debug code.

                        In terms of concepts, I now appreciate that doing a trailing stop like this is going to create a lot of amended orders. I assume the other alternative is to just have an initial stop loss set and then code in an ExitLong order if the price goes below a trailing stop value you are maintaining in the code. The risk I see with this is if you lost connectivity (internet/power, etc), the only order your broker has is the initial stop loss. Do most users just accept all of the modified orders so that you have a true trailing stop?

                        Regards,
                        Kylie.

                        Comment


                          #13
                          You're welcome - great it works now.

                          This is correct, in case of an internet outage at this ExitLong point, only the last 'accepted' stop would be at your broker (if not a profit target for example would be working as well on a connection that would not support native OCO).

                          An 'on every x ticks amended' trail stop might then offer better risk control.

                          Comment


                            #14
                            Thanks again Bertrand for the advice.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            633 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            364 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            105 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            567 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            568 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X