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

Time Series when Bar Closed

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

    Time Series when Bar Closed

    Trying to Compare the close time of a bar to another bar

    I wanting to state the when Low[0] equals ZigZag Last Low Pivot (I have a indicator that plots and hold the value of the Last 6 ZigZag Plots 3 most recent Highs and 3 Most Recent Lows

    so I want to compare the Time of Low[ Bar equals ZigZag Last Low Pivot to the Time of High Bar] equals ZigZag Last Low Pivot

    Using a Renko type bar look for Time between the bar closes to be greater than Xtime

    Usage is when multiple bars for in 20 seconds for example to block any trades with the condition set

    Thanks

    #2
    Hello DTSSTS,

    Thank you for the post.

    What part did you need help with?

    If you wanted to compare the time between two bars you would need to know how many BarsAgo each bar is as the first step. The Time series consists of DateTime objects so you can use any C# DateTime comparisons for two times.

    If you wanted the difference you could use Subtract: https://docs.microsoft.com/en-us/dot...ystem-datetime)

    If you know the bars ago for the Low then you could use the same BarsAgo with the Time series to get the time of that bar.
    JesseNinjaTrader Customer Service

    Comment


      #3
      thanks for the link, I have read thru 6-8 pages and of course none of the examples deal with bars or close[6] etc, so I have no idea where to begin

      I want to take the Close[0] time and subtract the time of the Close[5] and know how many seconds have passed

      thanks

      Comment


        #4
        Hello DTSSTS,

        The link i provided is the MSDN documentation for DateTime, thats a C# type. NinjaScript is built from C# so the base types used would match what the MSDN descriptions/samples. DateTime is a C# type so the functions of DateTime could be used.

        The Close[0] time is Time[0], each series has a BarsAgo. If you used 0 BarsAgo for the close price the 0 BarsAgo would be the same to get the time for that bar.

        If you wanted to subtract one time from another time you can use the C# DateTime function Subtract for that, Time[0] is a DateTime object.

        If we take your example using 5 bars ago, you could write that as:

        Code:
        DateTime firstTime = Time[0]; 
        DateTime secondTime = Time[5];
        TimeSpan diff = firstTime.Subtract(secondTime);
        That results in a C# TimeSpan object: https://docs.microsoft.com/en-us/dot...tframework-4.8

        You could use any timespan properties or methods to find out how long that period was.

        https://docs.microsoft.com/en-us/dot...tframework-4.8

        TotalSeconds represents the total time in seconds.


        JesseNinjaTrader Customer Service

        Comment


          #5
          Jesse TYVM

          Comment


            #6
            I guess the best method to apply to multiple conditions sets would be to create a rule based on the conditions

            public double TotalSeconds { get; }

            DateTime firstTime = Time[0];
            ateTime secondTime = Time[5];
            TimeSpan diff = firstTime.Subtract(secondTime);

            If(

            TimeSpan diff <= ? how many seconds?

            How does the code know we are measure diff in seconds

            TimeSpan.TotalSeconds diff = firstTime.Subtract(secondTime);

            or

            TotalSeconds diff = firstTime.Subtract(secondTime);

            or neither
            Last edited by DTSSTS; 01-29-2022, 08:31 AM.

            Comment


              #7
              Hello DTSSTS,

              How does the code know we are measure diff in seconds
              It doesn't, That is the second link I had provided, the result is a TimeSpan object which is another C# type. You can use any TimeSpan properties like TotalSeconds to get the total number in seconds. That could be less than 60 or greater than 60 because its a total seconds for the span of time.

              The code:
              Code:
              TimeSpan diff = firstTime.Subtract(secondTime);
              just gets you the base TimeSpan object, from the diff variable you can get TotalSeconds

              Code:
              Print(diff.TotalSeconds);
              JesseNinjaTrader Customer Service

              Comment


                #8
                Do I need the double in the code

                public double TotalSeconds { get; }

                Then as part of conditions


                && ((DateTime firstTime = Time[0])
                && (DateTime secondTime = Time[5])
                &&(TimeSpan diff = firstTime.Subtract(secondTime)))


                TimeSpan diff = firstTime.Subtract(secondTime);


                && (diff.TotalSeconds) >= 60)

                Comment


                  #9
                  Hello DTSSTS,

                  The public double TotalSeconds { get; } shown on the page is how that property is defined in TimeSpan, its a double type property. You don't need to add that you would just access the TotalSeconds like I had shown in the last post. It looks like in your code you have also used it but you have an extra( ) that is not needed.

                  Code:
                  && diff.TotalSeconds >= 60)
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Sorry to bother I still cannot get this to compile

                    I do not believe this is correct but I included in post because I only have 1 line of issues in my error log

                    // Below only 1 line of error for first line needing )
                    if (

                    DateTime firstTime = Time[0];
                    DateTime secondTime = Time[5];
                    TimeSpan diff = firstTime.Subtract(secondTime);


                    Print(diff.TotalSeconds) >= 60;

                    {

                    TradingTimeOK = true;
                    }


                    // below is what we discussed in my last post

                    && (DateTime firstTime = Time[0])
                    && (DateTime secondTime = Time[5])
                    &&(TimeSpan diff = firstTime.Subtract(secondTime))


                    TimeSpan diff = firstTime.Subtract(secondTime);


                    && (diff.TotalSeconds) >= 60)

                    // so i tried this

                    if (

                    (DateTime firstTime = Time[0])
                    && (DateTime secondTime = Time[5])
                    && (TimeSpan diff = firstTime.Subtract(secondTime))



                    && ((diff.TotalSeconds) >= 60))

                    {

                    TradingTimeOK = true;
                    }


                    WHICH IS LOADED WITH ERRORS

                    THANKS

                    Comment


                      #11
                      Likely a better approach would be to state

                      DateTime firstTime = Time[0];
                      DateTime secondTime = Time[5];
                      TimeSpan diff = firstTime.Subtract(secondTime);


                      I DO NOT KNOW WHERE TO PUT THAT MY GUESS IN IN OnBarUpdate area

                      and Next to just use the below in the area with other Trading Trigger Condition Set

                      && (diff.TotalSeconds) >= 60)

                      thanks again (Scared to try some of these items without guidance, as I have insert code that cause the entire platform to shutdown and corrupted the Strategy File)

                      Comment


                        #12
                        Hello DTSSTS,

                        In post 10 there are quite a few problems, 1 of which post 11 can solve. The definition of variables can go in OnBarUpdate, that would look like:


                        Code:
                        protected override void OnBarUpdate()
                        {
                        DateTime firstTime = Time[0];
                        DateTime secondTime = Time[5];
                        TimeSpan diff = firstTime.Subtract(secondTime);
                        That is defining 3 variables at the top of OnBarUpdate so the code following that in OnBarUpdate can use those variables.


                        Code:
                        && (diff.TotalSeconds) >= 60)
                        This would be valid, I would suggest to make your if conditions more simple by trying to avoid using ( ) on each part, you just need to use ( ) if you want to group something.

                        Code:
                        if(firstCondition && diff.TotalSeconds >= 60)
                        {
                        
                        }


                        JesseNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Chippy, Today, 02:00 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post Chippy
                        by Chippy
                         
                        Started by jimmy_NT, Today, 10:41 AM
                        1 response
                        6 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by JeffCO7, Today, 09:34 AM
                        1 response
                        4 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by AdeptistJune, Today, 08:53 AM
                        1 response
                        13 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by designer01, Today, 07:50 AM
                        1 response
                        14 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Working...
                        X