Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Edit Bar Timer Indicator

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

    Edit Bar Timer Indicator

    I wish to edit the Bar Timer Indicator. Basically, looking to change what is shown and how it's displayed... See attached screenshot.

    Where in the existing code...
    1. Edit the text shown, from 1 to 2 in the attachment?
    2. Edit/add font size?


    Click image for larger version

Name:	nt8-bar-timer.png
Views:	252
Size:	5.5 KB
ID:	1337707


    Code:
    Code:
    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")]
        public class BarTimer : Indicator
        {
            private string          timeLeft    = string.Empty;
            private DateTime        now         = Core.Globals.Now;
            private bool            connected,
                                    hasRealtimeData;
            private SessionIterator sessionIterator;
            private System.Windows.Threading.DispatcherTimer timer;
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description         = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionBarTimer;
                    Name                = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameBarTimer;
                    Calculate           = Calculate.OnEachTick;
                    DrawOnPricePanel    = false;
                    IsChartOnly         = true;
                    IsOverlay           = true;
                    DisplayInDataBox    = false;
                }
                else if (State == State.Realtime)
                {
                    if (timer == null && IsVisible)
                    {
                        if (Bars.BarsType.IsTimeBased && Bars.BarsType.IsIntraday)
                        {
                            lock (Connection.Connections)
                            {
                                if (Connection.Connections.ToList().FirstOrDefault(c => c.Status == ConnectionStatus.Connected && c.InstrumentTypes.Contains(Instrument.MasterInstrument.InstrumentType)) == null)
                                    Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerDisconnectedError, TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Transparent, Brushes.Transparent, 0);
                                else
                                {
                                    if (!SessionIterator.IsInSession(Now, false, true))
                                        Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerSessionTimeError, TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Transparent, Brushes.Transparent, 0);
                                    else
                                        Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerWaitingOnDataError, TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Transparent, Brushes.Transparent, 0);
                                }
                            }
                        }
                        else
                            Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerTimeBasedError, TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Transparent, Brushes.Transparent, 0);
                    }
                }
                else if (State == State.Terminated)
                {
                    if (timer == null)
                        return;
                    timer.IsEnabled = false;
                    timer = null;
                }
            }
            protected override void OnBarUpdate()
            {
                if (State == State.Realtime)
                {
                    hasRealtimeData = true;
                    connected = true;
                }
            }
            protected override void OnConnectionStatusUpdate(ConnectionStatusEventArgs connectionStatusUpdate)
            {
                if (connectionStatusUpdate.PriceStatus == ConnectionStatus.Connected
                    && connectionStatusUpdate.Connection.InstrumentTypes.Contains(Instrument.MasterInstrument.InstrumentType)
                    && Bars.BarsType.IsTimeBased
                    && Bars.BarsType.IsIntraday)
                {
                    connected = true;
                    if (DisplayTime() && timer == null)
                    {
                        ChartControl.Dispatcher.InvokeAsync(() =>
                        {
                            timer           = new System.Windows.Threading.DispatcherTimer { Interval = new TimeSpan(0, 0, 1), IsEnabled = true };
                            timer.Tick      += OnTimerTick;
                        });
                    }
                }
                else if (connectionStatusUpdate.PriceStatus == ConnectionStatus.Disconnected)
                    connected = false;
            }
            private bool DisplayTime()
            {
                return ChartControl != null
                        && Bars != null
                        && Bars.Instrument.MarketData != null
                        && IsVisible;
            }
            private void OnTimerTick(object sender, EventArgs e)
            {
                ForceRefresh();
                if (DisplayTime())
                {
                    if (timer != null && !timer.IsEnabled)
                        timer.IsEnabled = true;
                    if (connected)
                    {
                        if (SessionIterator.IsInSession(Now, false, true))
                        {
                            if (hasRealtimeData)
                            {
                                TimeSpan barTimeLeft = Bars.GetTime(Bars.Count - 1).Subtract(Now);
                                timeLeft = (barTimeLeft.Ticks < 0
                                    ? "00:00:00"
                                    : barTimeLeft.Hours.ToString("00") + ":" + barTimeLeft.Minutes.ToString("00") + ":" + barTimeLeft.Seconds.ToString("00"));
                                Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerTimeRemaining + timeLeft, TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Transparent, Brushes.Transparent, 0);
                            }
                            else
                                Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerWaitingOnDataError, TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Transparent, Brushes.Transparent, 0);
                        }
                        else
                            Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerSessionTimeError, TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Transparent, Brushes.Transparent, 0);
                    }
                    else
                    {
                        Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerDisconnectedError, TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Transparent, Brushes.Transparent, 0);
                        if (timer != null)
                            timer.IsEnabled = false;
                    }
                }
            }
            private SessionIterator SessionIterator
            {
                get
                {
                    if (sessionIterator == null)
                        sessionIterator = new SessionIterator(Bars);
                    return sessionIterator;
                }
            }
            private DateTime Now
            {
                get
                {
                    now = (Cbi.Connection.PlaybackConnection != null ? Cbi.Connection.PlaybackConnection.Now : Core.Globals.Now);
                    if (now.Millisecond > 0)
                        now = Core.Globals.MinDate.AddSeconds((long)Math.Floor(now.Subtract(Core.Globals.MinDate).TotalSeconds));
                    return now;
                }
            }
        }
    }
    #region NinjaScript generated code. Neither change nor remove.
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
        {
            private BarTimer[] cacheBarTimer;
            public BarTimer BarTimer()
            {
                return BarTimer(Input);
            }
            public BarTimer BarTimer(ISeries<double> input)
            {
                if (cacheBarTimer != null)
                    for (int idx = 0; idx < cacheBarTimer.Length; idx++)
                        if (cacheBarTimer[idx] != null &&  cacheBarTimer[idx].EqualsInput(input))
                            return cacheBarTimer[idx];
                return CacheIndicator<BarTimer>(new BarTimer(), input, ref cacheBarTimer);
            }
        }
    }
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
        public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
        {
            public Indicators.BarTimer BarTimer()
            {
                return indicator.BarTimer(Input);
            }
            public Indicators.BarTimer BarTimer(ISeries<double> input )
            {
                return indicator.BarTimer(input);
            }
        }
    }
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
        {
            public Indicators.BarTimer BarTimer()
            {
                return indicator.BarTimer(Input);
            }
            public Indicators.BarTimer BarTimer(ISeries<double> input )
            {
                return indicator.BarTimer(input);
            }
        }
    }​

    #2
    Hello sastrades,

    The code drawing the text is:
    Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerTimeRemaining + timeLeft, TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, Brushes.Transparent, Brushes.Transparent, 0);

    The 'Time remaining' string is the NinjaTrader.Custom.Resource.BarTimerTimeRemaining resource which you can remove.

    The ChartControl.Properties.LabelFont provides the SimpleFont object which contains the text size.
    You can replace this with your own SimpleFont object.
    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks. For the SimpleFont, must I create a string first, or could I add this inline? Would it then be something like this?

      Chart.SimpleFont timerFont = new Chart.SimpleFont("Arial", 12) { Size = 50, Bold = true };

      Comment


        #4
        Hello sastrades,

        You can generate a new object in the Draw method call or you can create a variable and assign the object to the variable and use the variable in the method call.

        This is C#. You can do either.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank you.

          Comment


            #6
            Here's my edited line...
            Code:
            Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.BarTimerDisconnectedError, TextPosition.BottomRight, ChartControl.Properties.ChartText, ChartControl.Properties.LabelFont, barTimerCountdown, Brushes.Transparent, Brushes.Transparent, 0);
            Where should I place the custom font code, assuming this is written correctly?
            Code:
            // BarTimerCountdown text
            NinjaTrader.Custom.Resource.SimpleFont barTimerCountdown = new NinjaTrader.Custom.Resource.SimpleFont("Arial", 15) { Size = 50, Bold = true };

            Comment


              #7
              Hello sastrades,

              Replace ChartControl.Properties.LabelFont with barTimerCountdown in the Draw.TextFixed() call.

              I would recommend that you declare the barTimerCountdown variable in the scope of the class, and instantiate the new SimpleFont object and assign this to the barTimerCountdown variable in State.DataLoaded of OnStateChange().
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

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