Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Basic Stop Loss Question

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

    Basic Stop Loss Question

    I'm new (again) to NT and trying to use the Wizard to create a basic strategy. Enter Long if the RSI 28 is below 20. Create a trailing stop loss of 10%. When I do this using the Wizard, the long positions get entered but the stop loss never triggers for any position. Here is what the Wizard created:

    {
    #region Variables
    // Wizard generated variables
    private int myInput0 = 1; // Default setting for MyInput0
    // 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()
    {
    SetTrailStop("", CalculationMode.Percent, 10);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (RSI(28, 0).Avg[0] <= MyInput0)
    {
    EnterLong(DefaultQuantity, "");
    }
    }

    #2
    oyz79, please try 0.1 to enter a stop of 10 percent...then see if they get triggered as you would expect.

    Thanks

    Comment


      #3
      Thanks for the reply - I just tried reentering the strategy using the wizard with .1 (and 10) and I now get an error CS1501:

      {
      #region Variables
      // Wizard generated variables
      private int myInput0 = 28; // Default setting for MyInput0
      // 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()
      {
      SetTrailStop(".1", CalculationMode.Percent, 0, false);

      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Condition set 1
      if (RSI(28, 1).Avg[0] <= MyInput0)
      {
      EnterLong(DefaultQuantity, "");
      }
      }

      Comment


        #4
        You seem to have entered the value in the wrong field of the SetTrailStop call - it should be something like this -

        SetTrailStop("", CalculationMode.Percent, 0.1, false);

        Comment


          #5
          Thanks, that fixed it.

          Where do I find information on how to code a holding period in addition to a stop loss - for example, set a stop loss and if not executed sell after 5,10,20, etc. days?

          Comment


            #6
            A Time Exit?

            You could realize this with BarsSinceEntry() -

            Comment


              #7
              Thanks, I've changed the script but when I run backtests I'm still getting stopped out as opposed to holding for 20 days. Here is the updated script, not sure how it is still reading a trailing stop loss as the exit rule when I backtest stocks?

              protected override void Initialize()
              {CalculateOnBarClose = true;
              if (BarsSinceEntry() > 20)
              ExitLong();
              }

              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
              // Condition set 1
              if (RSI(28, 1).Avg[0] <= Variable)
              {
              EnterLong(DefaultQuantity, "");
              }
              }

              Comment


                #8
                oyz79, have you hit the F5 key after editing the code to re-compile the script? That is an essential step in order for the changes to take effect.
                AustinNinjaTrader Customer Service

                Comment


                  #9
                  Thanks for the replies and patience - F5 has been hit. now when I go to File>New>Strategy Analyzer and select a stock or group of stocks to backtest, I select my RSI 28 script. I run the backtest and it runs a backtest for a different strategy I previously created, not the RSI 28 strategy that I selected.

                  Is my database possibly corrupt?

                  Here again is my script

                  /// <summary>
                  /// This method is used to configure the strategy and is called once before any strategy method is called.
                  /// </summary>
                  protected override void Initialize()
                  {CalculateOnBarClose = true;
                  if (BarsSinceEntry() > 20)
                  ExitLong();
                  }

                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                  // Condition set 1
                  if (RSI(28, 1).Avg[0] <= Variable)
                  {
                  EnterLong(DefaultQuantity, "");

                  Comment


                    #10
                    You're welcome, the script to backtest selection would not be code related - any errors in the logs you spot? How do you determine the incorrect script was run? If you like contact us via Help > Mail to Support and we take a look at your logs and traces generated for further clues as to why this might happen.

                    Thanks

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    88 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    48 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    30 views
                    0 likes
                    Last Post Deep42
                    by Deep42
                     
                    Started by TheRealMorford, 03-05-2026, 06:15 PM
                    0 responses
                    34 views
                    0 likes
                    Last Post TheRealMorford  
                    Started by Mindset, 02-28-2026, 06:16 AM
                    0 responses
                    68 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Working...
                    X