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

Comparing two times with ToTime(Time[0]) while avoiding overruns

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

    Comparing two times with ToTime(Time[0]) while avoiding overruns

    Hi all,

    I think I know the answer to this question in advance, but I'm going to ask it anyway.

    I've been using ToTime to do a lot of time comparisons in my strategy. A well known use of it is by restricting the time a script executes a bit of code, based on the time of day. Here's what I use:

    Code:
    //  Run the script if the time is between 7:15 AM and 2:57 PM
    			if (ToTime(Time[0]) >= 71500 && ToTime(Time[0]) <= 145700)
    The problem I'm having now is comparing two time values at each bar update. The first time is a variable I set when any trade occurs. The second variable is the time of the bar update. I keep track of the difference between these two for reporting purposes and to measure how long I've been in a trade.

    The snag occurs when I make a trade at 8:56 AM, and the bar update time value is 9:01. The math goes like this:

    At the 8:58 AM mark:
    85800 - 85600 = 200 (which is what I'm looking for)

    At the 9:01 AM mark:
    90100 - 85600 = 4500 (which is the overrun gap I run into using ToTime

    ------------------

    My instinct is telling me that I have to convert all of my double variables holding times to DateTime variables, and using TimeSpan to figure out the difference.

    Would this be correct, or is there a workaround within Time or ToTime that I can leverage? Mind you, the trade time is set with ToTime(Time[0]) at the time of the trade, and the same method is used to compare that time with OnBarUpdate in a COBC = false environment.

    Thanks in advance for any answers or advice!

    Yours,
    Spider

    #2
    Got it!

    Nevermind!

    I figured it out on my own. The following code compares two time structures using TotalMinutes. Just had to scour StackOverflow for about 30 minutes and found a solution.

    Code:
    #region Variables
            
    		DateTime FirstTime = new DateTime();
    		DateTime SecondTime = new DateTime();
    		double Diff;
    		
            #endregion
    
     protected override void OnBarUpdate()
            {
    			if (ToTime(Time[0]) == 83500)	{FirstTime = Time[0];}
    			SecondTime = Time[0];
    			Diff = (SecondTime - FirstTime).TotalMinutes;
    			Print("Diff is " + Diff);	
    			
            }
    Hope that helps someone else in the same predicament!

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by rjbtrade1, 11-30-2023, 04:38 PM
    2 responses
    72 views
    0 likes
    Last Post DavidHP
    by DavidHP
     
    Started by suroot, 04-10-2017, 02:18 AM
    5 responses
    3,021 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by Stanfillirenfro, Today, 07:23 AM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by cmtjoancolmenero, Yesterday, 03:58 PM
    3 responses
    22 views
    0 likes
    Last Post cmtjoancolmenero  
    Started by olisav57, Yesterday, 07:39 PM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Working...
    X