Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop Loss Method

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

    Stop Loss Method

    I programmed the following stop loss:

    double TrueRange = 1.5 * ATR(24)[0];

    // Stop Loss Long

    if (Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) < -TrueRange
    && Position.MarketPosition == MarketPosition.Long)
    ExitLong("LongStopLoss", "Long");

    In the backtest it was fine, but when I started to trade it live it created a lot of orders inside the same bar (30min).
    I don't understand why it happened, since the ATR was worth about 37 pips (less than the whole range of the bar where the trades took place), and the spread was tight (2 pips).

    1) Any suggestion why it happened?

    2) I guess I better switch to SetStopLoss() method. Is there any advantage in comparison to what I coded above?

    3) With the SetStopLoss() method, would it be correct to do the following?

    SetStopLoss(CalculationMode.Price, 1.5 * ATR(24)[0]);

    Thank you

    #2
    Hi stefy,

    One of the NinjaScript trainees will respond to you later today. Thank you for your patience.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      1) Are you using CalculateOnBarClose = false? If so your condition will evaluate to true over and over again in a single bar and thus multiple trades from within that bar. If you want to limit your trading to once per bar you can do something with a flag variable.

      Code:
      if (flagVariable == true)
      {
           EnterLong();
           flagVariable = false;
      }
      Take this concept and extrapolate it over into whichever one of your orders is firing multiple times. Don't forget you will need to reset the flagVariable when you want to be able to trade again.

      2) There is little difference between the two methods of creating a stop loss for your situation. User preference.

      3) It could be depending on if ATR(24)[0] * 1.5 will turn out to be a valid price. If it is then you will have no problem.
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        Hey Stefy,

        If you use SetStopLoss(CalculationMode.Price, 1.5 * ATR(24)[0]); you will use the ATR of that bar. It will not move as the ATR does throughout the trade.

        Just want to be sure thats your intention.
        mrlogik
        NinjaTrader Ecosystem Vendor - Purelogik Trading

        Comment


          #5
          3rd point is not clear yet - just to clarify: if I want to use a Stop Loss of 1 ATR(24), I will have to do the following:

          SetStopLoss(CalculationMode.Price, Position.AvgPrice + ATR(24)[0]);

          if I use

          SetStopLoss(CalculationMode.Price, 1.5 * ATR(24)[0]);

          I will get immediately stopped out, since my limit is the ATR only, which is very small in comparison with the price.

          Comment


            #6
            Hi mrlogik,

            I think it's better to use and ATR changing over time. So I should get rid of the [0] index?

            Thank you

            Comment


              #7
              Hey stefy,

              You can't get rid of the [0] or it wont work.

              If you want this to change over time, you can call this same line at the end of each bar while you're in the trade, rather than just once.

              Keep in mind you will have to ADD(+), or SUBTRACT(-) the ATR depending on which direction you are in the trade.
              mrlogik
              NinjaTrader Ecosystem Vendor - Purelogik Trading

              Comment


                #8
                mrlogik,

                1) To do so, I will have to write the code into OnBarUpdate() instead of Initialize().

                2) My question was, if I use the CalculationMode.Price, the Stop Loss will kick off on a price limit, so, for a Long, my limit will be AvgPrice + ATR, so the code will be:

                SetStopLoss(CalculationMode.Price, Position.AvgPrice + ATR(24)[0]);

                Can you please confirm on both?

                Thank you

                Comment


                  #9
                  1. Correct. You can see this reference sample for how to modify stop losses: http://www.ninjatrader-support.com/v...ead.php?t=3222

                  2. If you do that say your entry price was 100 and your ATR is 20 then your stop would be 120. That may not work because for a long you want your stop to be below your entry price. So instead switch it to minus ATR to get your stop at 80.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Josh is right.

                    You wouldn't want a stoploss to be > entry price for a long trade.

                    Also, keep this in mind (from the manual)
                    Should you call this method to dynamically change the stop loss price in the strategy
                    OnBarUpdate() method, you should always reset the stop loss price/offset value when your
                    strategy is flat otherwise, the last price/offset value set will be used to generate your stop
                    loss order on your next open position
                    mrlogik
                    NinjaTrader Ecosystem Vendor - Purelogik Trading

                    Comment


                      #11
                      Everything seems clear from your answers, but, using the reference provided below, I coded the following (Stop Loss Long):

                      if(Position.MarketPosition == MarketPosition.Flat)
                      {
                      SetStopLoss(CalculationMode.Price, Position.AvgPrice - ATR(24)[0]);
                      }

                      else if(Position.MarketPosition == MarketPosition.Long)

                      {
                      if(Close[0] > Position.AvgPrice + ATR(24)[0])

                      {
                      SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                      }

                      }

                      I run the backtest, but, as you can see in the chart attached, sometimes I get stopped out immediately (at the same entry price).

                      Thank you
                      Attached Files

                      Comment


                        #12
                        That is because of your this part:
                        else if(Position.MarketPosition == MarketPosition.Long)

                        {
                        if(Close[0] > Position.AvgPrice + ATR(24)[0])

                        {
                        SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                        }

                        Remember in backtesting you are running on CalculateOnBarClose = true. You can easily enter into a trade and on that bar the Close being greater than the price + ATR so it stops you out.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          How can I change the code to avoid this to happen in the backtest?

                          Thank you

                          Comment


                            #14
                            You would need to change your condition then. There is no way around it. Intrabar logic doesn't work in backtesting unless you start introducing multi-time frame strategies. Even then you will not be able to truly mimic how it would behave in real-time.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              I'm sorry, I don't understand.
                              As you can see in my attachment, the problem is exactly the opposite, i.e. that I get stopped out on the same bar.
                              I thought in backtests you could never go in and out a trade into the same bar, as it seems to happen in my picture.
                              Can you please clarify?

                              Thank you

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              578 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              334 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
                              553 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              551 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X