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 ScottWalsh, 04-16-2024, 04:29 PM
    6 responses
    27 views
    0 likes
    Last Post ScottWalsh  
    Started by frankthearm, Today, 09:08 AM
    10 responses
    35 views
    0 likes
    Last Post frankthearm  
    Started by GwFutures1988, Today, 02:48 PM
    0 responses
    3 views
    0 likes
    Last Post GwFutures1988  
    Started by mmenigma, Today, 02:22 PM
    1 response
    3 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by NRITV, Today, 01:15 PM
    2 responses
    9 views
    0 likes
    Last Post NRITV
    by NRITV
     
    Working...
    X