Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetStopLoss not triggering

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

    SetStopLoss not triggering

    I have set my StopLoss as follow but it is not triggering

    Code:
    if (Position.MarketPosition == MarketPosition.Long)
                    {                  
                            SetStopLoss(CalculationMode.Price, Close[0]-2*ATR(3)[0];                           
                    }
                    if (Position.MarketPosition == MarketPosition.Flat)
                    {                
                        if (//conditions)
                        {                        
                            EnterLong();
                        }                
                    }
    
    if (Position.MarketPosition == MarketPosition.Short)
                    {             
                            SetStopLoss(CalculationMode.Price, Close[0]+2*ATR(3)[0]);
                        
                    }
                    if (Position.MarketPosition == MarketPosition.Flat)
                    {                
                        if (//Conditions )
                        {                        
                            EnterShort();
                        }
                    }
    Thanks.

    #2
    Hello,

    Thank you for the post.

    Generally you would need to call SetStopLoss prior to the entry to set up the order. If this has not been called earlier in OnBarUpdate or in State.Configure, you likely need to add in a call to SetStopLoss prior to the entries.

    Can you confirm if you change the logic to the following, do you see it place the stop?



    Code:
                    if (Position.MarketPosition == MarketPosition.Flat)
                    {                
                        if (//conditions)
                        {              
                            SetStopLoss(CalculationMode.Price, Close[0]-2*ATR(3)[0];           
                            EnterLong();
                        }                              
                        else if (//Conditions )
                        {         
                            SetStopLoss(CalculationMode.Price, Close[0]+2*ATR(3)[0]);               
                            EnterShort();
                        }
                    }
    I look forward to being of further assistance.

    Comment


      #3
      Hi Jesse,
      I placed the ATR Trailing Stop prior to the entry as suggested, but it's still not working. At best I get an exit prior to the close/end of day, else a couple of randoms exits.

      Comment


        #4
        Hello,

        Thank you for testing that.

        It sounds like the stop is working for you if you got some stops placed, it just seems they are not at the price or time you had expected, is this correct?

        Would you be able to provide a more complete sample of the logic?

        Without seeing your script I can only guess what may be happening, I tried the following and can see that the stop is both placed consistently. Could you try a very basic test like the following to see if the stop is placed for you after entering?

        If the result is different than the current script, this is likely to do with the logic being used and in that case a more complete sample would be helpful.

        Code:
        protected override void OnBarUpdate()
        {
              if (Position.MarketPosition == MarketPosition.Flat)
             {                         
                        SetStopLoss(CalculationMode.Price, Close[0]-2*ATR(3)[0]);           
                        EnterLong();
             }
        }

        I look forward to being of further assistance.

        Comment


          #5
          Jesse,
          Thanks for the detailed explanation. I tried the SetStopLoss on the following simple script without getting it to execute.
          Code:
          {                                            
                          if (Position.MarketPosition == MarketPosition.Flat)
                          {                
                              if (BarsSinceExit()!=0  && SMA(12)[0]>SMA(20)[0])
                              {                        
                                  SetStopLoss(CalculationMode.Price, Close[0]-2*ATR(3)[0]);
                                  EnterLong();
                              }                
                          }
          
                          if (Position.MarketPosition == MarketPosition.Flat)
                          {                
                              if (BarsSinceExit()!=0 && SMA(12)[0]<SMA(20)[0] )
                              {                        
                                  SetStopLoss(CalculationMode.Price, Close[0]+2*ATR(3)[0]);
                                  EnterShort();
                              }
                          }
                      }
          Also, where in the script can I add a "DrawText" so it will print after the SetStopLoss executes?
          Code:
          DrawText(""+CurrentBar, Math.Round(TradeProfit,2).ToString(), 0, Low[0] - (TickSize *5), Color.Cyan);

          Comment


            #6
            Hello,

            Before I continue to test this code, I noted this is NT7 syntax but we are in the NT8 forum.
            BarsSinceExit and DrawText have changed in NT8.

            I have been providing help/samples for this question in respect to NT8, are you using NT7?

            Could you confirm which version you are using?

            Comment


              #7
              Oh, I'm sorry, my mistake. Yes I'm still using NT7. Please let me know if I need to repost it. Thanks.

              Comment


                #8
                Hello,

                Not a problem I went ahead and moved this into the correct forum, I just wanted to make sure we were talking about the same thing.

                I tried the code in NT7, I do see this placing stops or placing a stop that is not filled and exiting on close. Are you seeing no stops placed at all? http://screencast.com/t/XyHVF0CS

                If you are seeing no Filled stops, potentially they are being placed at prices that will not fill, are you only seeing exit on close and entries? Can you provide an image of the chart you are seeing?

                I look forward to being of further assistance.

                Comment


                  #9
                  Thanks Jesse, I'm attaching the script. For better visual, the candles are colored and I added DrawText.
                  Attached Files

                  Comment


                    #10
                    Hello,

                    It appears in the image there were stops placed and filled, I can also see that this is the case when running the script.

                    Are you expecting different placement of the stops or different fills? Could you tell me what you are expecting in this case because it appears the stop is in fact working.

                    For the question related to DrawText, you could check the Execution information from OnExecution:


                    I look forward to being of further assistance,

                    Comment


                      #11
                      In the script I have the Stop Loss that's supposed to exit the long position when the price crosses below the ATR trailing stop line or to exit the short position when the price crosses above the ATR trailing stop line. This is not occurring as the stops when executed, are mostly random or at the end of day.

                      I was expecting Stops every time the price crosses the ATR trailing stop line. Did I go wrong with the logic or the location of the SetStopLoss? Thanks again for your time.

                      Comment


                        #12
                        Hello,

                        Thank you for the reply.

                        I believe this may be a confusion of the methods needed for the concept.

                        You said:
                        exit the long position when the price crosses below the ATR trailing stop line or to exit the short position when the price crosses above the ATR trailing stop line
                        If you want to exit at a specific condition you could call ExitLong or ExitShort. The SetStopLoss would need to be set before the entry and it would be some amount of Price, Percent or Ticks away from the price. That means depending on the market price, it could fill now or later.

                        I believe to correct the logic, rather than using SetStopLoss, use ExitLong or ExitShort in the condition for exit. You could leave the other entry logic alone, mainly as long as Exit is not called in the same OnBarUpdate as an Enter it should just exit the position at that point.

                        I look forward to being of further assistance.

                        Comment


                          #13
                          If you refer to something like the following, then my ExitLong condition is not consistent with the ATR trailing stop because it isn't executing. What would be the proper syntax for both the ExitLong and ExitShort?
                          Code:
                          if (Position.MarketPosition == MarketPosition.Flat)
                                          {                
                                              if (Condition )
                                              {                        
                                                  EnterLong();
                                              }                
                                          }
                                          if (Position.MarketPosition == MarketPosition.Long)
                                          {
                                              if(Close[0]<(Close[0]-2*ATR(3)[0]))
                                              {
                                                  ExitLong();
                                              }                    
                                          }

                          Comment


                            #14
                            Hello,

                            Try using a Print to see whats going on:

                            Code:
                            if (Position.MarketPosition == MarketPosition.Long)
                            {
                            Print("Is Long");
                            Print(Close[0] + " < " + (Close[0]-2*ATR(3)[0]));
                                if(Close[0]<(Close[0]-2*ATR(3)[0]))
                                {
                                     ExitLong();
                                }                    
                            }
                            If no exit is happening at all, it is likely the value is not equating as true. This type of print would just show if the Position is long, and if so there should be a print right after "Is Long" showing the Close price < Close[0]-2*ATR(3)[0]

                            If the value of the close is less than the calculated value and still no exit happens, I would suggest trying TraceOrders: http://ninjatrader.com/support/helpG...ub=traceorders

                            I look forward to being of further assistance.

                            Comment


                              #15
                              The two print statements to the output window are consistent with the values, yet still no exit happens.

                              The TraceOrders = true; will printout a buy order consistent with the strategy but no exits based on Close[0]<(Close[0]-2*ATR(3)[0]). The only exit is at the close of the day as shown in TraceOrders.
                              HTML Code:
                              If no exit is happening at all, it is likely the value is not equating as true
                              I agree with your statement, but why is that? I also tried the logic as part of an indicator instead of a strategy and tried BackColor whenever it was true, but nothing happened either.

                              Comment

                              Latest Posts

                              Collapse

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