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 CarlTrading, 03-31-2026, 09:41 PM
    1 response
    133 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    75 views
    1 like
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    117 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    113 views
    1 like
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    90 views
    0 likes
    Last Post CarlTrading  
    Working...
    X