Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

calculating time past 50 percent

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

    calculating time past 50 percent

    I need to figure out ha time stamp is past 50 percent past a bar for example

    I drew a line on a 15 minute time frame and the time is 8:23.15

    what i need to know if the 8:15 bar is half way done.

    I need it for all time frames

    1 min, 2 min 5 min etc

    #2
    Hello ballboy11,

    Thanks for your inquiry.

    You could do so using CalculateOnBarClose = false;

    This way your strategy will iterate on each tick. You can still reference each bar close by using a barsAgo of 1 instead of 0 when a tick iterates FirstTickOfBar.

    As your strategy iterates each tick, you can get the time of the bar close using Time[0] and the current time with DateTime.Now. Depending on the time frame used, you can subtract half of that interval from Time[0] and see if DateTime.Now is greater than it.

    Here is some sample code demonstrating how the logic could be set up for second bars:

    Code:
    TimeSpan TimeToSubtract;
    protected override void OnBarUpdate()
    {
    	if(BarsPeriod.BasePeriodType == PeriodType.Second)
    		TimeToSubtract = new TimeSpan(0,0,0,(int)BarsPeriod.Value/2);
    	if(FirstTickOfBar)
    	{
    		Print(Close[1]); // Prints the same as Close[0] with CalculateOnBarClose = true;
    	}
    	if(ToTime(Time[0].Subtract(TimeToSubtract)) < ToTime(DateTime.Now))
    		Print("Halfway done with second type bar");	
    }
    I will include documentation links below on the items used so they may be referenced to expand the logic for minutes, etc.

    BarsPeriod - https://ninjatrader.com/support/help...barsperiod.htm

    FirstTickOfBar - https://ninjatrader.com/support/help...ttickofbar.htm

    Time - https://ninjatrader.com/support/helpGuides/nt7/time.htm

    ToTime() - https://ninjatrader.com/support/help...nt7/totime.htm

    Additional reading on general C# DateTime and TimeSpan can be found below.

    DateTime - https://msdn.microsoft.com/en-us/lib....datetime.aspx

    TimeSpan - https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

    Please let me know if I can be of further help.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    566 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    330 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
    547 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    548 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X