Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Seconds counter

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

    Seconds counter

    Hi,

    Is there any way to programm a "seconds counter" for a current bar?. I mean, just from the opening of a bar the counter must start in ZERO and start counting the seconds until the bar closes.

    Thanks

    #2
    Hello,

    Thank you for the post.

    I wanted to check, are you trying to count Up rather than counting Down like the included BarTimer indicator does?

    If you want to Count down once a bar closes, there is already an indicator that counts Time based bar times. This is called BarTimer.

    If you are instead wanting to count Up, this could also be accomplished using a similar approach that the BarTimer indicator uses.

    I look forward to being of further assistance.

    Comment


      #3
      Hi Jesse,

      I need to count up and only counting the elapsed seconds, like this:

      Elapsed Seconds: 67 (for example of 1:07 elapsed time from the opening of current bar)

      Do you know what I mean?

      Comment


        #4
        Hello,

        Thank you for clairifing.

        In this case, you could make a duplicate of the existing BarTimer indicator and then change the way it counts time.

        To count time up, you could use a StopWatch: https://www.dotnetperls.com/stopwatch

        One simple example would be the following, please note that this would only update on each Tick which may or may not update quick enough to display exactly the bar time. Likely you would see less than 10 seconds if used on a 10 second chart due to update frequency.

        To have a more frequently updated text on the chart that is not dependent on Ticks, you would likely need to review how the existing BarTimer works and uses a DispatcherTimer to update. This also uses ForceRefresh which aids in making a quicker update. You could implement a similar use of a Timer to check the StopWatch more frequently or when there is no live data.



        Code:
        private System.Diagnostics.Stopwatch myStopWatch;
        
        protected override void OnStateChange()
        {
        	if (State == State.SetDefaults)
        	{				
        		Calculate			= Calculate.OnEachTick;
        	}
        	else if(State == State.Configure)
        	{
        		 myStopWatch = new System.Diagnostics.Stopwatch();
        	}
        	else if (State == State.Terminated)
        	{
        		if (myStopWatch == null)
        			return;
        
        		myStopWatch.Stop();
        		myStopWatch = null;
        	}
        }
        
        protected override void OnBarUpdate()
        {
        	if (State == State.Realtime && IsFirstTickOfBar)
        	{
        		myStopWatch.Stop();
        		myStopWatch.Reset();
        		myStopWatch.Start();
        	}
        		Draw.TextFixed(this, "NinjaScriptInfo", Math.Round(myStopWatch.Elapsed.TotalSeconds).ToString(), TextPosition.BottomRight);
        }

        I look forward to being of further assistance.

        Comment


          #5
          I will work with this information and will comeback to you in case of not being able to do it

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          579 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          334 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          554 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          551 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X