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
    BertrandNinjaTrader Customer Service

    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);
        BertrandNinjaTrader Customer Service

        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() -

            BertrandNinjaTrader Customer Service

            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
                    BertrandNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by usazencort, Today, 01:16 AM
                    0 responses
                    1 view
                    0 likes
                    Last Post usazencort  
                    Started by kaywai, 09-01-2023, 08:44 PM
                    5 responses
                    603 views
                    0 likes
                    Last Post NinjaTrader_Jason  
                    Started by xiinteractive, 04-09-2024, 08:08 AM
                    6 responses
                    23 views
                    0 likes
                    Last Post xiinteractive  
                    Started by Pattontje, Yesterday, 02:10 PM
                    2 responses
                    22 views
                    0 likes
                    Last Post Pattontje  
                    Started by flybuzz, 04-21-2024, 04:07 PM
                    17 responses
                    230 views
                    0 likes
                    Last Post TradingLoss  
                    Working...
                    X