Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Displaying a Count of the Number of Range Bars within the last 30mins

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

    Displaying a Count of the Number of Range Bars within the last 30mins

    Hello!

    I'm trying to simplify an aspect of my trading by displaying a Range Bar Count in the top right of my Range chart that shows the Count of Range bars within the last 30mins (and 60mins) from the current bar.

    Finding the Count
    I've read through a number of functions, but can't seem to find a simple way to grab this count.
    I figure I could cycle through all the bars going backwards from the current bar and check the time on each one, but this seems like excessive work for something so simple.

    Anyone know an easy way to work this out?

    Happy to just get it working as an OnBarUpdate() for the latest candle, but if possible I'd quite like it to work when scrolling the chart as well. So that, whenever you are looking back through the data, it always shows the count for 30min behind the currently displayed bar.

    I've had a look at the OnRender() function for chart scrolling but not sure if this is appropriate due to all the warnings about not attempting to do too many calculations within the OnRender() call.

    Help?

    Display
    In terms of display: I've sorted out the Text positioning with declaring these as Fonts:

    private NinjaTrader.Gui.Tools.SimpleFont BTFont = new NinjaTrader.Gui.Tools.SimpleFont("Arial", 12) { Size = 30, Bold = false };
    private NinjaTrader.Gui.Tools.SimpleFont BTSmallFont = new NinjaTrader.Gui.Tools.SimpleFont("Arial", 12) { Size = 20, Bold = false };

    Then adding this to OnBarUpdate() (currently just using integers with preset values):

    rangeString = "| " + int30minCount + " |";
    range60String = int60minCount + "<padding spaces here - not shown in forums> .";

    Draw.TextFixed(this, "30MinCount", rangeString, TextPosition.TopRight, myAlertBrush, BTFont, Brushes.Transparent, Brushes.Transparent, 0);
    Draw.TextFixed(this, "60MinCount", range60String, TextPosition.TopRight, myAlertBrush, BTSmallFont, Brushes.Transparent , Brushes.Transparent, 0);

    Which creates a large value (BTFont) closest to the right side, then some padding spaces to give a smaller number (BTSmallFont) which seems fine and produces this in the top right:

    Click image for larger version

Name:	Display.jpg
Views:	893
Size:	5.4 KB
ID:	1175337

    Although I'm curious if these is a simpler way to do this display as well.

    Thanks to all, and sorry this is so long.

    WeLoveTheLibrary

    #2
    Hello,

    Thanks for your post.

    I'm not aware of an existing indicator that does this and you are welcome to search through the NT user apps section of the NinjaTrader Ecosystem for anything that might get you close or give you further ideas. Here is a link to the NinjaTrader8 indicators section: https://ninjatraderecosystem.com/use...r-8-indicators Please note: The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

    Using Draw.TextFixed() is a good and simple method to use here.

    The current live Range bar has a close time that is constantly changing until the range condition has been met and then once the bar is closed, the bars time is then fixed.

    What you might consider doing is taking the time of the previous bar (Time[1]) which is closed and does not change and then adding a - 30 minute and a -60 minutes to it and use the method GetBar() where you supply the time of the bar to get. The method will return the bar number of the closest bar to that time (which should be good enough for what you are doing). You can then simply subtract the returned number from the current bar number and that will be the number of bars in the 30/60 minute periods. (see the help guide guide example: https://ninjatrader.com/support/help...tml?getbar.htm)

    Here is a quick example:

    protected override void OnBarUpdate()
    {
    if (CurrentBar < 20) return;

    int barsAgo30 = (CurrentBar - Bars.GetBar(Time[1].AddMinutes(-30)))+1; // adding 1 for the current bar
    int barsAgo60 = (CurrentBar - Bars.GetBar(Time[1].AddMinutes(-60)))+1;

    Draw.TextFixed(this, "test", "60: "+barsAgo60+" 30: "+barsAgo30, TextPosition.TopRight);
    }


    CurrentBar is the systems bar counter and points to the active bar: https://ninjatrader.com/support/help...currentbar.htm

    Note that this will only get you the bars from the current bar.

    If you want to scroll back and have that change then you can try using ChartBars.ToIndex instead of CurrentBar: https://ninjatrader.com/support/help...rs_toindex.htm

    But note that you would also have to use that in the Time[] to get the bar you are referencing from because Time is a series and the index must be a bars ago relative to the Currentbar. For example, if you scrolled back 50 bars then you would use Bars.GetBar(Time[CurrentBar - ChartBars.ToIndex].AddMinutes(-30))


    Paul H.NinjaTrader Customer Service

    Comment


      #3
      That's lovely. I'll have a play with those functions and see if I can get it working.

      Thank you so much for your help.

      Comment


        #4
        Hi,

        i search a solution of an Range Counter.

        Actually i have an Counter but but it should work differently - look at the picture


        This range counter counts forward and backward, but now I want one that only counts forward. As you can see in the picture, the count would have to stop at 6 until a new price level is traded.

        Is it possible to program such an indicator?

        Greetings and sorry for the bad english
        Attached Files

        Comment


          #5
          Hello jnbart,

          Thank you for your note.

          That's not really the topic of this thread - that was regarding Range bars, and you appear to be wanting to count the levels in a Volumetric Bar. Those don't look like the Order Flow Volumetric bars, but this indicator that is intended for use with the Order Flow volumetric bars should give you a starting point. All you really need to know is the number of ticks in each price level.

          The attached indicator loops through from the high to the low of the bar by the tick size times the number of ticks per level. You could make this a user input, but for this example, since it relies on an Order Flow Volumetric Chart, we can just use the BarsPeriod.Value as that holds the ticks per level for Volumetric bars. If you're using a different volumetric chart, you'd want to create a user input where you can just input the number of ticks per level in use and use that in place of BarsPeriod.Value.

          If you run this On Price Change, you should see the current number of levels for the current bar update as new levels are reached.

          Please let us know if we may be of further assistance to you.
          Attached Files
          Kate W.NinjaTrader Customer Service

          Comment


            #6
            Hi,

            no I am not looking for that. Perhaps I should explain my intention in more detail.

            I use a 8 range chart and I want an indicator that either shows me a number in the top right of the window or directly above the current range candle.

            The number should count the price levels.
            I now assume from the example that a new range candle has formed and 3 price levels were traded. Now it should throw out a 3 and as soon as a new 4 price level is added - show 4, a new 5 price level is added - show 5, and so on. However, it should not count backwards. It should only add new price levels.

            Thus I can estimate at a glance, exactly when a range candle is over and must not always count the price levels manually.

            I hope this was now more understandable.

            Does this indicator already exist or do you have to program it yourself?

            Many thanks for your help.
            Attached Files

            Comment


              #7
              Hello jnbart,

              Thank you for your reply.

              The indicator above could be modified to use Draw.TextFixed to draw the current price level count in the lower right corner of the chart. You can then exclude checking BarsPeriod.Value to count if a Range bar is full.

              Code:
              namespace NinjaTrader.NinjaScript.Indicators
              {
              public class CountBarPriceLevelsExample : Indicator
              {
              private double count;
              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              Description = @"Enter the description for your new custom Indicator here.";
              Name = "CountBarPriceLevelsExample";
              Calculate = Calculate.OnPriceChange;
              IsOverlay = true;
              DisplayInDataBox = true;
              DrawOnPricePanel = true;
              DrawHorizontalGridLines = true;
              DrawVerticalGridLines = true;
              PaintPriceMarkers = true;
              ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
              //Disable this property if your indicator requires custom values that cumulate with each new market data event.
              //See Help Guide for additional information.
              IsSuspendedWhileInactive = true;
              }
              else if (State == State.Configure)
              {
              }
              }
              
              protected override void OnBarUpdate()
              {
              
              int count = 0;
              for (double i = High[0]; i > Low[0]; i-=TickSize)
              {
              count++;
              }
              Draw.TextFixed(this, "MyText", count.ToString(),TextPosition.BottomRight);
              }
              }
              }

              This would show "8" on 8 Range bars to signify it is waiting for the range to be broken.


              Please let us know if we may be of further assistance to you.
              Kate W.NinjaTrader Customer Service

              Comment


                #8
                Hi,

                thank you for your suggestions. I have found the Default "Range" Indicator. It shows me exactly what I was looking for.

                Code:
                //
                // Copyright (C) 2022, NinjaTrader LLC <www.ninjatrader.com>.
                // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
                //
                #region Using declarations
                using System;
                using System.Collections.Generic;
                using System.ComponentModel;
                using System.ComponentModel.DataAnnotations;
                using System.Linq;
                using System.Text;
                using System.Threading.Tasks;
                using System.Windows;
                using System.Windows.Input;
                using System.Windows.Media;
                using System.Xml.Serialization;
                using NinjaTrader.Cbi;
                using NinjaTrader.Gui;
                using NinjaTrader.Gui.Chart;
                using NinjaTrader.Gui.SuperDom;
                using NinjaTrader.Data;
                using NinjaTrader.NinjaScript;
                using NinjaTrader.Core.FloatingPoint;
                using NinjaTrader.NinjaScript.DrawingTools;
                #endregion
                
                // This namespace holds indicators in this folder and is required. Do not change it.
                namespace NinjaTrader.NinjaScript.Indicators
                {
                /// <summary>
                /// Calculates the range of a bar.
                /// </summary>
                public class Range : Indicator
                {
                protected override void OnStateChange()
                {
                if (State == State.SetDefaults)
                {
                Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionRange;
                Name = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meRange;
                BarsRequiredToPlot = 0;
                IsSuspendedWhileInactive = true;
                
                AddPlot(new Stroke(Brushes.Goldenrod, 2), PlotStyle.Bar, NinjaTrader.Custom.Resource.RangeValue);
                }
                }
                
                protected override void OnBarUpdate()
                {
                Value[0] = High[0] - Low[0];
                }
                }
                }
                
                #region NinjaScript generated code. Neither change nor remove.
                
                namespace NinjaTrader.NinjaScript.Indicators
                {
                public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
                {
                private Range[] cacheRange;
                public Range Range()
                {
                return Range(Input);
                }
                
                public Range Range(ISeries<double> input)
                {
                if (cacheRange != null)
                for (int idx = 0; idx < cacheRange.Length; idx++)
                if (cacheRange[idx] != null && cacheRange[idx].EqualsInput(input))
                return cacheRange[idx];
                return CacheIndicator<Range>(new Range(), input, ref cacheRange);
                }
                }
                }
                
                namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
                {
                public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
                {
                public Indicators.Range Range()
                {
                return indicator.Range(Input);
                }
                
                public Indicators.Range Range(ISeries<double> input )
                {
                return indicator.Range(input);
                }
                }
                }
                
                namespace NinjaTrader.NinjaScript.Strategies
                {
                public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
                {
                public Indicators.Range Range()
                {
                return indicator.Range(Input);
                }
                
                public Indicators.Range Range(ISeries<double> input )
                {
                return indicator.Range(input);
                }
                }
                }
                
                #endregion
                How do I get it to show me directly in the chart window and it should not count in 0.25 steps but in 1-2-3-4-5-6-7-8-9?
                It should only count at the current bar. Look at my Picture.


                Thanks
                Attached Files

                Comment


                  #9
                  Hello jnbart,

                  Draw.Text would be used to draw text on the chart.

                  Documentation can be found here:

                  Draw.Text() - https://ninjatrader.com/support/help...?draw_text.htm

                  If you are new to programming, I suggest using the Strategy Builder to create some simple code that draws text, and you can use the View Code button to view the resulting syntax so you can see how you may add it in your own custom modification of the Range indicator.

                  Drawing on a chart with the Strategy Builder - https://ninjatrader.com/support/help...ToDrawOnAChart

                  Indicator Tutorials - https://ninjatrader.com/support/help...indicators.htm
                  JimNinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_Kate View Post
                    Hello jnbart,

                    Thank you for your note.

                    That's not really the topic of this thread - that was regarding Range bars, and you appear to be wanting to count the levels in a Volumetric Bar. Those don't look like the Order Flow Volumetric bars, but this indicator that is intended for use with the Order Flow volumetric bars should give you a starting point. All you really need to know is the number of ticks in each price level.

                    The attached indicator loops through from the high to the low of the bar by the tick size times the number of ticks per level. You could make this a user input, but for this example, since it relies on an Order Flow Volumetric Chart, we can just use the BarsPeriod.Value as that holds the ticks per level for Volumetric bars. If you're using a different volumetric chart, you'd want to create a user input where you can just input the number of ticks per level in use and use that in place of BarsPeriod.Value.

                    If you run this On Price Change, you should see the current number of levels for the current bar update as new levels are reached.

                    Please let us know if we may be of further assistance to you.
                    This was really useful, thanks a lot.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by CommonWhale, Today, 09:55 AM
                    1 response
                    3 views
                    0 likes
                    Last Post NinjaTrader_Erick  
                    Started by Gerik, Today, 09:40 AM
                    2 responses
                    7 views
                    0 likes
                    Last Post Gerik
                    by Gerik
                     
                    Started by RookieTrader, Today, 09:37 AM
                    2 responses
                    12 views
                    0 likes
                    Last Post RookieTrader  
                    Started by alifarahani, Today, 09:40 AM
                    1 response
                    7 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Started by KennyK, 05-29-2017, 02:02 AM
                    3 responses
                    1,285 views
                    0 likes
                    Last Post NinjaTrader_Clayton  
                    Working...
                    X