Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to set stop loss on a particular value

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

    How to set stop loss on a particular value

    Hi i have this code :

    if (CrossAbove(Close, SupportAndResistanceMTF(1, PeriodType.Day).R1, 1))
    {
    EnterShort(DefaultQuantity, "short2");
    }



    if (Close[0] > SupportAndResistanceMTF(1, PeriodType.Day).R3[0])


    ExitShort("", "");

    However my problem is that if my trade is held until the indicator draws a new line the next day then i would like to still have my stop loss on the r3 from the day of entry.

    Any help on how to do this would be really appreciated!

    Thanks

    #2
    Hello freddy250,

    Thank you for your inquiry.

    What I would suggest is creating a variable and assigning its value to the R3[0] value upon entry. You can then use either ExitShortStop() or ExistShortStopLimit() to generate your stop losses.

    Example:
    Code:
    // stop order
    ExitShortStop(stopPrice, "short2");
    
    // stop limit order
    ExitShortStopLimit(limitPrice, stopPrice, "short2");
    Please take a look at the NinjaTrader help guide at these links for more information about both ExitShortStop() and ExitShortStopLimit():



    If you ever wish to amend your stop loss, you can use the same method call with the same entry signal name with your different stop price.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ZacharyG View Post

      What I would suggest is creating a variable and assigning its value to the R3[0] value upon entry. You can then use either ExitShortStop() or ExistShortStopLimit() to generate your stop losses.
      Hi There ZacharyG,

      Could you please elaborate on how one would specifically assign this variable, UPON ENTRY. This is the part that is confusing me.

      Many Thanks

      Muddy

      Comment


        #4
        Hello Muddy,

        After entering your short, you would want to create a variable to hold the R3[0] value.

        Example:
        Code:
        EnterShort(DefaultQuantity, "short2");
        double stopPrice = SupportAndResistanceMTF(1, PeriodType.Day).R3[0];
        Or, you could just call ExitShortStop immediately:
        Code:
        EnterShort(Default, "short2");
        ExitShortStop(SupportAndResistanceMTF(1, PeriodType.Day).R3[0], "short2");
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          This is what i did,

          #region Using declarations
          using System;
          using System.ComponentModel;
          using System.Diagnostics;
          using System.Drawing;
          using System.Drawing.Drawing2D;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Data;
          using NinjaTrader.Indicator;
          using NinjaTrader.Gui.Chart;
          using NinjaTrader.Strategy;
          #endregion

          // This namespace holds all strategies and is required. Do not change it.
          namespace NinjaTrader.Strategy
          {
          /// <summary>
          /// Enter the description of your strategy here
          /// </summary>
          [Description("Enter the description of your strategy here")]
          public class Resistance : Strategy
          {
          #region Variables
          // Wizard generated variables
          private int takeProfit2 = 10; // Default setting for TakeProfit2
          private int stopLoss2 = 100; // Default setting for StopLoss2
          private int stopLoss3 = 30; // Default setting for StopLoss3
          private int takeProfit3 = 10; // Default setting for TakeProfit3

          // User defined variables (add any user defined variables below)
          #endregion

          /// <summary>
          /// This method is used to configure the strategy and is called once before any strategy method is called.
          /// </summary>
          protected override void Initialize()
          {
          Add(SupportAndResistanceMTF(1, NinjaTrader.Data.PeriodType.Day));
          SetProfitTarget("short2", CalculationMode.Ticks, TakeProfit2);
          SetProfitTarget("short1", CalculationMode.Ticks, TakeProfit2);

          CalculateOnBarClose = true;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {
          // Condition set 1
          if (CrossAbove(Close, SupportAndResistanceMTF(1, PeriodType.Day).R3, 1))
          {
          EnterShort(DefaultQuantity, "short2");

          double stopPrice = SupportAndResistanceMTF(1, PeriodType.Day).R4[0];

          ExitShortStop(stopPrice, "short2");



          }

          if (CrossAbove(Close, SupportAndResistanceMTF(1, PeriodType.Day).R2, 1))
          {
          EnterShort(DefaultQuantity, "short1");

          double stopPrice = SupportAndResistanceMTF(1, PeriodType.Day).R4[0];

          ExitShortStop(stopPrice, "short1");



          }









          }





          #region Properties
          [Description("")]
          [GridCategory("Parameters")]
          public int TakeProfit2
          {
          get { return takeProfit2; }
          set { takeProfit2 = Math.Max(1, value); }
          }

          [Description("")]
          [GridCategory("Parameters")]
          public int StopLoss2
          {
          get { return stopLoss2; }
          set { stopLoss2 = Math.Max(1, value); }
          }

          [Description("")]
          [GridCategory("Parameters")]
          public int StopLoss3
          {
          get { return stopLoss3; }
          set { stopLoss3 = Math.Max(1, value); }
          }

          [Description("")]
          [GridCategory("Parameters")]
          public int TakeProfit3
          {
          get { return takeProfit3; }
          set { takeProfit3 = Math.Max(1, value); }
          }
          #endregion
          }
          }


          However this does not exit, i attach a screenshot of the backtest..

          Thanks for helping a newbie!
          Attached Files

          Comment


            #6
            Hello Muddy,

            The screenshot you have provided appears not to work.

            What I can tell you from the code you have provided is that your stop price is going to change to the current R4[0] value of the SupportAndResistanceMTF indicator if any of the CrossAbove conditional statements are true.

            I would suggest assigning your orders to IOrder objects as well. Please take a look at the NinjaTrader help guide at this link for more information and sample syntax: http://ninjatrader.com/support/helpG...t7/?iorder.htm

            In the Executions tab of your backtest results, are any Buy orders executed?

            Please debug the values of stopPrice, SupportAndResistanceMTF.R3[0], and SupportAndResistanceMTF.R2[0]. You can do this by placing them in Print statements, like so:
            Code:
            Print("stopPrice: " + stopPrice);
            Print("R3: " + SupportAndResistanceMTF.R3[0]);
            Print("R2: " + SupportAndResistanceMTF.R2[0]);
            We want to see what the indicator values are compared to Close[0] and if stopPrice is being assigned to the value you desire. You'll be able to see these values in the Tools -> Output Window menu selection in the NinjaTrader Control Center.
            Zachary G.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

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