Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Smallest 2-Bar Range in Last 20 Bars

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

    Smallest 2-Bar Range in Last 20 Bars

    I am trying to find a way to figure out the smallest 2-bar range over the past 20 bars and plot a dot over it and/or change the color of the bar.

    Here is my code:
    __________________________________________________ ___

    double Two_Bar_High = Math.Max(High[1], High[0]); // Calculate 2-bar hi
    double Two_Bar_Low = Math.Min(Low[1], Low[0]); // Calculate 2-bar lo
    Two_Bar_Range.Set(Two_Bar_High - Two_Bar_Low); // 2-bar range
    double Smallest_Range = MIN(Two_Bar_Range, 20)[0];

    if (Two_Bar_Range[0] == Smallest_Range)
    {
    BarColorSeries[0] = Color.Black;
    BarColorSeries[1] = Color.Black;
    DrawDot("0"+CurrentBar, true, 0, High[0] + Offset, Color.Black);
    DrawDot("1"+CurrentBar, true, 1, High[1] + Offset, Color.Black);
    }


    else if (Two_Bar_Range[1] == Smallest_Range)
    {
    BarColorSeries[1] = Color.Black;
    BarColorSeries[2] = Color.Black;
    DrawDot("1a"+CurrentBar, true, 1, High[1] + Offset, Color.Black);
    DrawDot("2"+CurrentBar, true, 2, High[2] + Offset, Color.Black);
    }

    else if (Two_Bar_Range[2] == Smallest_Range)
    {
    BarColorSeries[2] = Color.Black;
    BarColorSeries[3] = Color.Black;
    DrawDot("2a"+CurrentBar, true, 2, High[2] + Offset, Color.Black);
    DrawDot("3"+CurrentBar, true, 3, High[3] + Offset, Color.Black);
    }

    ..... and so on up to [20].
    __________________________________________________ ___

    This code almost works, but not quite. It sometimes plots signals on 3 bars in a row when the condition is satisfied on back-to-back bars. It also fails to plot any signals at all for over 20 bars on some occasions. I need it to be able to "repaint" bars and signals correctly.

    Any suggestions?

    #2
    Hello Zachariah21,

    Thank you for your post.

    Please attach your indicator to your response so I may investigate this matter further. Please also advise what instrument, period and interval you are testing this on. Example: ES 06-14 1 Minute chart.

    You will find your indicator in the following directory on your PC: (My) Documents\NinjaTrader 7\bin\Custom\Indicator

    Comment


      #3
      There are a few logical problems with this indicator.

      First of all it cannot be used with CalculateOnBarClose = false, because it will produce many false signals. This is because the developing bar starts out as a 1-tick doji with a zero range and gradually grows into its final shape. Therefore with CalculateOnBarClose = false the check whether a two bar narrow range pattern has developed, can only be performed with the first tick of the subsequent bar.

      This was not your question, as apparently your indicator is designed to work with CalculateOnBarClose = true only. But also with this setting there is a similar problem. Let us assume that a double inside bar pattern (last bar containted within range of second but the last bar and this bar contained within the range of the third but the last bar) has occurred, and that the first bar of the 3-bar pattern is a 2BNR20 bar itself. In that case OnBarUpdate() will identify the third but the last bar as a narrow range bar, then it will identify the second but the last bar as a narrow range bar, then it will identify the last bar as a narrow range bar. Therefore all of them will show dots.

      If you do not want to show the dots for the prior bars, you need to remove those dots that have already been drawn, and which - with new hindsight knowledge - are no longer wanted. This can be achieved with the NinjaScript method RemoveDrawObject().

      Comment


        #4
        Hi Zachariah21,

        I wanted to confirm that I am seeing the same issue as Harry.

        If the trigger is detected on two bars in a row, the first detection draws the dot a bar ago and the current bar, the second detection draws over the dot from a bar ago and then the current bar.

        You will need to remove the dot from three bars ago when drawing for the current bars trigger with RemoveDrawObject().

        Below is a link to the help guide on RemoveDrawObject().
        http://www.ninjatrader.com/support/h...drawobject.htm
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Easy enough

          Hi Zach,

          You are not thinking logically. This can all be done with a few lines of code and be totally variable of 1 to 100 or whatever NR bar. It took me all of 3 mins to create the code. Another 20mins would have as an indicator.
          What you need to do is start with the NR indicator, I have one here. http://www.raefontrading.com/indicators/rcnr12/

          My indicator can be called with ZStrategyuse true.
          This would be the code for a buy signal
          if (AAARCNR12(NRbackcount, 40, true).Plot0[1] == 11
          && Range()[0] <= Range()[1])
          {
          DrawDot(
          "My dot" + CurrentBar, false, 0, Low[0] + -20 * TickSize, Color.Fuchsia);
          }
          The Plot0[1] == 11 needs to be changed to Plot0[1] == 12 for a sell signal.
          this can all be done with the use of the strategy wizard then view code edit to your specific requirements as an indicator.
          Attached the pic of strategy.
          The code prints the Pink dot in the picture A when the requirements are met.

          Hope this helps Raef

          Attached Files

          Comment


            #6
            Originally posted by Zachariah21 View Post
            I am trying to find a way to figure out the smallest 2-bar range over the past 20 bars and plot a dot over it and/or change the color of the bar.

            Here is my code:
            __________________________________________________ ___

            double Two_Bar_High = Math.Max(High[1], High[0]); // Calculate 2-bar hi
            double Two_Bar_Low = Math.Min(Low[1], Low[0]); // Calculate 2-bar lo
            Two_Bar_Range.Set(Two_Bar_High - Two_Bar_Low); // 2-bar range
            double Smallest_Range = MIN(Two_Bar_Range, 20)[0];

            if (Two_Bar_Range[0] == Smallest_Range)
            {
            BarColorSeries[0] = Color.Black;
            BarColorSeries[1] = Color.Black;
            DrawDot("0"+CurrentBar, true, 0, High[0] + Offset, Color.Black);
            DrawDot("1"+CurrentBar, true, 1, High[1] + Offset, Color.Black);
            }


            else if (Two_Bar_Range[1] == Smallest_Range)
            {
            BarColorSeries[1] = Color.Black;
            BarColorSeries[2] = Color.Black;
            DrawDot("1a"+CurrentBar, true, 1, High[1] + Offset, Color.Black);
            DrawDot("2"+CurrentBar, true, 2, High[2] + Offset, Color.Black);
            }

            else if (Two_Bar_Range[2] == Smallest_Range)
            {
            BarColorSeries[2] = Color.Black;
            BarColorSeries[3] = Color.Black;
            DrawDot("2a"+CurrentBar, true, 2, High[2] + Offset, Color.Black);
            DrawDot("3"+CurrentBar, true, 3, High[3] + Offset, Color.Black);
            }

            ..... and so on up to [20].
            __________________________________________________ ___

            This code almost works, but not quite. It sometimes plots signals on 3 bars in a row when the condition is satisfied on back-to-back bars. It also fails to plot any signals at all for over 20 bars on some occasions. I need it to be able to "repaint" bars and signals correctly.

            Any suggestions?
            HI Zach,

            Just curious. Were you able to get your indicator to calculate narrowest 2-bar range within previous 20 bars ?

            Thanks

            TT

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Mongo, Today, 11:05 AM
            4 responses
            14 views
            0 likes
            Last Post Mongo
            by Mongo
             
            Started by traderqz, Today, 12:06 AM
            7 responses
            14 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by Skifree, Today, 03:41 AM
            5 responses
            13 views
            0 likes
            Last Post Skifree
            by Skifree
             
            Started by traderqz, Yesterday, 09:06 AM
            5 responses
            35 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by guillembm, Today, 11:25 AM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X