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

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

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by chartchart, 05-19-2021, 04:14 PM
    3 responses
    577 views
    1 like
    Last Post NinjaTrader_Gaby  
    Started by bsbisme, Yesterday, 02:08 PM
    1 response
    15 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by prdecast, Today, 06:07 AM
    0 responses
    3 views
    0 likes
    Last Post prdecast  
    Started by i019945nj, 12-14-2023, 06:41 AM
    3 responses
    60 views
    0 likes
    Last Post i019945nj  
    Started by TraderBCL, Today, 04:38 AM
    2 responses
    18 views
    0 likes
    Last Post TraderBCL  
    Working...
    X