Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

bars since exit not working

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

    bars since exit not working

    There are the EMA conditions to determine the main trend.
    If ema conditions are bullish,
    get long on the next RSI dip under the condition that super trend is bullish.
    (Get out if supertrend turns bearish.)

    When i enter if bar since exit > xx to reduce whipsaw for this trendfollower, i dont get any results. What did i do wrong?

    {
    if (BarsSinceExit() > 10

    && EMA(21)[0] > EMA(55)[0]
    && EMA(55)[0] > EMA(200)[0]
    && EMA(55)[0] > EMA(55)[1]
    && EMA(200)[0] > EMA(200)[1])
    if (SuperTrend(14, 2.618).Trend[0] //&& !SuperTrend(14, 2.618).Trend[1]
    && CrossBelow(RSI(4, 0), 70, 1))
    {
    EnterLong(DefaultQuantity, "");
    }

    #2
    Hello,

    This looks correct. Your not in multi series or anything like that are you?

    If stil having trouble would need you to send full code to support at ninjatrader dot com. Although I can't fully debug it I can take a quick look and offer input on where to look for where the issue is in your code.

    Let me know if I can be of further assistance.

    Comment


      #3
      No i am not using multi instruments, just a simple strategy!


      Here is the full code:

      #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>
      /// SuperTrendSimpleStrategy
      /// </summary>
      [Description("SuperTrendSimpleStrategy")]
      public class SuperTrendSimpleStrategy : Strategy
      {
      #region Variables
      // Wizard generated variables

      // 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(RSI(4, 0));
      Add(RSI(9, 0));
      Add(EMA(21));
      Add(EMA(55));
      Add(EMA(200));
      Add(ATR(14));
      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Condition set 1
      if (BarsSinceExit() > 10
      && EMA(21)[0] > EMA(55)[0]
      && EMA(55)[0] > EMA(200)[0]
      && EMA(55)[0] > EMA(55)[1]
      && EMA(200)[0] > EMA(200)[1])
      if (SuperTrend(14, 2.618).Trend[0]
      && CrossBelow(RSI(4, 0), 70, 1))
      {
      EnterLong(DefaultQuantity, "");
      }
      // Condition set 2
      if (!SuperTrend(14, 2.618).Trend[0] && SuperTrend(14, 2.618).Trend[1])
      {
      ExitLong("");
      }

      // Condition set 3
      if (BarsSinceExit() >10
      && EMA(21)[0] < EMA(55)[0]
      && EMA(55)[0] < EMA(200)[0]
      && EMA(55)[0] < EMA(55)[1]
      && EMA(200)[0] < EMA(200)[1])
      if (!SuperTrend(14, 2.618).Trend[0]
      && CrossAbove(RSI(4, 0), 30, 1))
      {
      EnterShort(DefaultQuantity, "");
      }
      // Condition set 4
      if (SuperTrend(14, 2.618).Trend[0] && !SuperTrend(14, 2.618).Trend[1])
      {
      ExitShort("");
      }
      }

      #region Properties
      #endregion
      }
      }

      Comment


        #4
        Hello,

        Thanks for posting code:

        Here is where the issue is. Please see below:

        Definition
        Returns the number of bars that have elapsed since the last specified exit.

        Method Return Value
        An int value that represents a number of bars. A value of -1 will be returned if a previous exit does not exist.


        Since when you start the strategy in historical it has not taken a trade yet. So BarsSinceEntry = -1 since there was no previous entry.

        Since your check needs greater then 10 it never executes a trade.

        You need to put an or clause in so that it will run on -1 as well so the first trade can get off the ground.

        if ((BarsSinceExit() > 10 || BarsSinceExit() == -1)
        && EMA(21)[0] > EMA(55)[0]
        && EMA(55)[0] > EMA(200)[0]
        && EMA(55)[0] > EMA(55)[1]
        && EMA(200)[0] > EMA(200)[1])
        if (SuperTrend(14, 2.618).Trend[0]
        && CrossBelow(RSI(4, 0), 70, 1))
        {
        EnterLong(DefaultQuantity, "");
        }

        Comment


          #5
          thanks Brett,
          now i get an error message overload on "setstoploss..." though i have not defined any stop loss in this script. When i click on that error message another script gets opened.

          Comment


            #6
            That might be the case sosMsos - NinjaTrader would always compile all your scripts, meaning they all need to be error free - you would need to look into the SetStopLoss call in the other script and ensure it compiles fine, too - or another options would be to comment it out completely so the compiler would not 'see' it.

            BertrandNinjaTrader Customer Service

            Comment


              #7
              Thanks Bertrand,
              another (BarsSinceEntry() == 1) problem.

              Setup:
              bars in progress is weekly, and trendrecognition with Emas are based on daily chart.

              Strategy:
              if the trend is up based on the daily chart and i have two downweeks in a row, enter long, exit on close end of the week.


              // Condition set 1
              if (EMA(BarsArray[1], 21)[0] > EMA(BarsArray[1], 55)[0]
              && EMA(BarsArray[1], 55)[0] > EMA(BarsArray[1], 130)[0]
              && EMA(BarsArray[1], 55)[0] > EMA(BarsArray[1], 55)[1]
              && EMA(BarsArray[1], 130)[0] > EMA(BarsArray[1], 130)[1]
              && Close[1] < Close[2]
              && Close[0] < Close[1])
              {
              if (BarsInProgress == 0)
              EnterLong(DefaultQuantity, "");
              }
              // Condition set 2
              //if (BarsInProgress == 0)
              if (BarsSinceEntry() == 1)
              {
              ExitLong("", "");
              }


              I know i would not get an exit on the close of the bar, have to figure that out.
              But i dont eeven get any results if i activate that bolded comand. Whats the mistake?
              Last edited by sosMsos; 05-19-2011, 07:18 AM.

              Comment


                #8
                Hello sosMsos,

                If you're working with BarsSinceEntry in multiseries context, you should use the advanced overload for this. This allows you to specify the specific series that it should look at for reference:

                BarsSinceEntry(int barsInProgressIndex, string signalName, int entriesAgo)
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_RyanM View Post
                  Hello sosMsos,

                  If you're working with BarsSinceEntry in multiseries context, you should use the advanced overload for this. This allows you to specify the specific series that it should look at for reference.
                  i dont understand the syntax.
                  if (BarsSinceEntry(int barsInProgress == 0 , string signalName, 1)

                  would it look like this? i have no name for the "string signal name"

                  Comment


                    #10
                    It should look like this:
                    if (BarsSinceEntry(0, "", 0) == 1)

                    For entriesAgo parameter you want to use 0 for the most recent entry.
                    Last edited by NinjaTrader_RyanM1; 05-19-2011, 08:11 AM.
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_RyanM View Post
                      It should look like this:
                      if (BarsSinceEntry(0, "", 0) == 1)

                      For entriesAgo parameter you want to use 0 for the most recent entry.
                      Thanks its working, would you please explain why you put that in the brackets, so that i can better understand?

                      Also can exit on bar close? Only found a command on session close.

                      Comment


                        #12
                        Which brackets do you mean? There are only parentheses - An open and close pair for the if statement and BarsSinceEntry.

                        There is no command to exit on bar close. If you use CalculateOnBarClose = true, then logic is processed on bar close and orders are submitted to the next bar following the signal.
                        Ryan M.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_RyanM View Post
                          Which brackets do you mean? There are only parentheses - An open and close pair for the if statement and BarsSinceEntry.

                          There is no command to exit on bar close. If you use CalculateOnBarClose = true, then logic is processed on bar close and orders are submitted to the next bar following the signal.
                          It always exit on the next bar open no matter what, how can i get an exit on the bar close?

                          Comment


                            #14
                            That is the timing of strategy submitted orders. You can work with CalculateOnBarClose = false to submit orders to the same bar that the condition occurred on, but submitting to bar close exactly is not possible.
                            Ryan M.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by geotrades1, Today, 10:02 AM
                            1 response
                            4 views
                            0 likes
                            Last Post NinjaTrader_BrandonH  
                            Started by ender_wiggum, Today, 09:50 AM
                            1 response
                            5 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Started by rajendrasubedi2023, Today, 09:50 AM
                            1 response
                            12 views
                            0 likes
                            Last Post NinjaTrader_BrandonH  
                            Started by bmartz, Today, 09:30 AM
                            1 response
                            9 views
                            0 likes
                            Last Post NinjaTrader_Erick  
                            Started by geddyisodin, Today, 05:20 AM
                            3 responses
                            26 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Working...
                            X