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

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.
    JesseNinjaTrader Customer Service

    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.
        JesseNinjaTrader Customer Service

        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 pkefal, 04-11-2024, 07:39 AM
          11 responses
          36 views
          0 likes
          Last Post jeronymite  
          Started by bill2023, Yesterday, 08:51 AM
          8 responses
          44 views
          0 likes
          Last Post bill2023  
          Started by yertle, Today, 08:38 AM
          6 responses
          25 views
          0 likes
          Last Post ryjoga
          by ryjoga
           
          Started by algospoke, Yesterday, 06:40 PM
          2 responses
          24 views
          0 likes
          Last Post algospoke  
          Started by ghoul, Today, 06:02 PM
          3 responses
          16 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Working...
          X