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

Using startTime & endTime with DrawLine(0)

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

    Using startTime & endTime with DrawLine(0)

    I want to draw a line starting at 00:00:00 and ending at 09:30:00. I have not been able to figure out the correct syntax for the DrawLine(0) DateTime parameters. Any help would be appreciated.

    DrawLine("HighLine", false, <startTime>, HighestHigh, <endTime>, HighestHigh, Color.White, DashStyle.Solid, 2);

    #2
    Hello mlarocco,

    Thanks for your post.

    The syntax would require a datetime parameter so that it knows what day as well as what time to start/end with.

    One approach would be to use an if statement to check to see if the time equals 09:30 and if so then you would draw the line from the current bars time (Time[0]) to Time[0].AddHours(-9.5). This would work on time-based bars that have a bar close precisely at 9:30. If using non-time based bars then you may want to use a time filter to catch the first bar close after 9:30 to then draw the line.


    Paul H.NinjaTrader Customer Service

    Comment


      #3
      There isn't a way to just use the DateTime startTime parameter of the DrawLine() command?

      Comment


        #4
        Hello mlarocco,

        Thanks for your reply.

        You have to provide values for the start time and end time in a datetime structure. Time[0] is an example of a dateTime structure.

        Paul H.NinjaTrader Customer Service

        Comment


          #5
          I have tried different variations of Time[0] and ToTime(Time[0]) but could not get the indicator to compile correctly. I have something wrong with the syntax but I don't know what it is. Are there and code examples using the DateTime parameter with DrawLine? I looked but couldn't find any

          Comment


            #6
            Hello miarocco,

            Thanks for your reply.

            Here is a simple example:

            if (ToTime(Time[0]) == ToTime(09,30,00))
            {
            DrawLine("test"+CurrentBar, true, Time[0].AddHours(-9.5), High[0], Time[0], High[0], Color.Red, DashStyle.Dash, 3);
            }


            This is what that code produces on a 15 minute ES chart:

            Click image for larger version

Name:	miarocco-1.PNG
Views:	487
Size:	38.3 KB
ID:	1054200
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Thank you... I will give it a try

              Comment


                #8
                Hello Paul, I have a similar request.
                I'm trying get the starttime and endtime to occur at the end of each bar minus 10 seconds and minus 5 seconds.

                Something like
                startTime = EndOfEachBar - 10 seconds
                endTime = EndOfEachBar - 5 seconds

                I need the Line to draw only within that 5 seconds window, in order to avoid having too many changing drawings multiplied.
                I use the Calculate.OnEachTick and can't use the OnBarClose to also for the tick volume to be calculated.

                For example on a 1 minute chart, with something as this Draw.Line()
                PHP Code:
                Draw.Line(this"tag1"+CurrentBartrueEndOfEachBar 10 secondsHigh[0], EndOfEachBar 5 secondsHigh[0], Brushes.WhiteDashStyleHelper.Dash6); 

                I need the Dash to start drawing from the 50th second on of each 1 minute bar and stop at the 55th second (while still displaying afterward).
                But my problem currently is that it starts drawing right at the start of the new bar (the 0th second) on to the last bar's second (the 60th second), with the default syntax

                PHP Code:
                Draw.Line(this"tag1"+CurrentBartrue0High[0], 0High[0], Brushes.WhiteDashStyleHelper.Dash3); 

                which produces too many drawings.

                Is it possible to get the specific needed startTime and endTime? How would you formulate it?

                I tried applying Patrick's method with
                startTime = Time[1].AddSeconds(50)
                endTime = Time[1].AddSeconds(55)

                PHP Code:
                Draw.Line(this"tag1"+CurrentBartrueTime[1].AddSeconds(50), High[0], Time[1].AddSeconds(55), High[0], Brushes.WhiteDashStyleHelper.Dash6); 

                but it's not working (I had an error
                Time Category Message
                06/05/2022 20:48:27 Default Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 6767: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
                which I corrected using the CongestionBoxLite share CurrentBar check (which solves the indexing error)

                PHP Code:

                   
                if(CurrentBar ChartBars.FromIndex -30/// Only Calculate and paint bars on the screen + 30  bars
                   
                {
                      
                // DoX;
                   


                but my Draw.Line method is in another method outside the OnBarUpdate so with the CurrentBar check it doesn't plot anymore (i suspect it is the reason).

                So I would avoid if possible the Time[1].AddSeconds(50) / Time[1].AddSeconds(55) if there's a better way that does not require the CurrentBar Check.

                Thanks for your next insights!


                It seems to work now with your if statement to only fire the drawing within the 5 seconds window + Brett's DateTime feature (StartTime/EndTime format?)

                PHP Code:
                DateTime myStartTime = new DateTime(Time[0].YearTime[0].MonthTime[0].DayTime[0].HourTime[0].Minute50 );

                DateTime myEndTime = new DateTime(Time[0].YearTime[0].MonthTime[0].DayTime[0].HourTime[0].Minute55 );

                if (
                Time[0] >= myStartTime && Time[0] <= myEndTime)
                {
                   
                Draw.Line(this"tag1"+CurrentBartrue0High[0], 0High[0], Brushes.WhiteDashStyleHelper.Dash3);


                Many thanks again for the insight!
                Last edited by PaulMohn; 05-07-2022, 04:53 AM.

                Comment


                  #9
                  Hello Paul,

                  my previous code works for the minute granularity, but how would you make it work for the 15min timeframe for example?

                  Is there a way to make it execute between the 14min50 and 14min55? and 29min50 and 29min55? and 44min50 and 44min55? and 59min50 and 59min55? Of every hour?

                  Or simpler, how to do it for whatever the Bar interval on the chart is (i.e. 5min, 10min 240min etc.) with the least hardcoding possible so to be able to change the timeframe on the fly and it to pick the right interval? Something like "detect what the chart interval is and set the DateTime variable 10 and 5 seconds before the end of the chart interval Bar". Thanks!
                  Last edited by PaulMohn; 05-09-2022, 08:45 AM.

                  Comment


                    #10
                    Hello PaulMohn,

                    You can use the Bars object to get information about the bars, for example Bars.BarsPeriod.Value

                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Hello Jesse and thanks for the tip.

                      How would you get the last 10 seconds of each bar?

                      From https://ninjatrader.com/support/foru...951#post163951

                      I thought of a pseudocode like

                      myStartTime = Bars.BarsPeriod.Value.Subtract(11).Seconds);

                      myEndTime = Bars.BarsPeriod.Value.Subtract(1).Seconds);

                      But that's not enough and I can't find the answer from the doc

                      I would also need to formulate the substracton of 11 an 1 seconds from the end time of the Bar. Any tip on how to calculate from the end time of the bar? Thanks!

                      Comment


                        #12
                        Hello PaulMohn,

                        You could use the C# DateTime math functions like you have shown to add or subtract time from an existing time.

                        The Bar objects have specific times, for example 1 minute bars in historical don't have a last 10 seconds, its an even time allotment for each bar. For historical you would have to use a more granular secondary series if you wanted to do anything with those intrabar times. In realtime you can run the script OnEachTick and then calculate times which are within the bar.

                        You can Print out the times you are calculating to see how that is affecting the original time value.

                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Ah, ok, my end use is for realtime for now.

                          So you mean I could use as

                          DateTime myStartTime = new Bars.BarsPeriod.Value - DateTime(Time[0].Year, Time[0].Month, Time[0].Day, Time[0].Hour, Time[0].Minute, 49 );

                          DateTime myEndTime = new Bars.BarsPeriod.Value - DateTime(Time[0].Year, Time[0].Month, Time[0].Day, Time[0].Hour, Time[0].Minute, 58 );

                          ?

                          Will it work? I had some hesitations from the doc stating
                          BarsPeriod.Value -> Determines an integer value representing the period parameter.
                          Thanks!

                          Comment


                            #14
                            Hello PaulMohn,

                            No that wouldn't be valid, there is a problem with the placement of new and you cant subtract a value like that from a DateTime, you can use the methods you were previously if you wanted to add or subtract an amount. You can read about using DateTimes here:
                            https://ninjatrader.com/support/help...htsub=datetime
                            https://msdn.microsoft.com/en-us/lib...v=vs.113).aspx

                            An example would be:
                            Code:
                            DateTime myStartTime  = Time[0].Subtract(TimeSpan.FromSeconds(11));
                            You can use a Print to see how that affects the time.
                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              Hello Jesse and many thanks for the tips and helpful docs.

                              I'v thought of that for myStartTime

                              DateTime myStartTime = ((Time[0]-Time[1])) + Time[0]*100%?).Subtract(TimeSpan.FromSeconds(11));

                              Where:
                              (Time[0]-Time[1]) gives the current Bar's Starting time.

                              And something like
                              Time[0]*100%? would give the "real" end time/close of the Bar's time (not the current running time (Time[0])).

                              Or simpler (if it would work), just
                              DateTime myStartTime = (Time[0]*100%?).Subtract(TimeSpan.FromSeconds(11));

                              I've found those threads
                              Time of bar open and close

                              How to find the bars Opening time?

                              Time difference between current bar and previous 2 bars

                              Use TimeSpan structs. TimeSpan represents a period of time and has many helpful methods.


                              which are close but not enough to find the candle's end time formulation (the real end time i.e.
                              if the current time now is 2:10:07 PM, and my chart is a 1 minute chart, then the "real" close of the candle time is 2:10:59/2:11:00 PM;
                              if the current time now is 2:10:07 PM, and my chart is a 15min chart, then the "real" close of the candle time is 2:14:59/2:15:00 PM;
                              if the current time now is 2:10:07 PM, and my chart is a 1h chart, then the "real" close of the candle time is 2:59:59/3:00:00 PM;
                              etc. for all Bars period).

                              What would be the correct formulation for the real close (something like the 100% of the Time[0])? Thanks!


                              It seems i found a way with


                              PHP Code:
                              Print("2 : " Time[0]);
                              Print(
                              "3 : " Time[1]);
                              Print(
                              "4a : " BarsArray[0].GetTime(CurrentBars[0])); 
                              With output for 4a
                              2 : 17/05/2022 14:48:30
                              3 : 17/05/2022 14:48:26
                              4a : 17/05/2022 14:49:00
                              I have a 1min chart as primary series with a 1 tick secondary series.

                              is it right that .GetTime(CurrentBars[0]) return the "end time" of the Bar and not the running time?

                              The doc states
                              GetTime()
                              Definition


                              Returns the time stamp at the current bar index value.

                              It means:
                              Returns the time stamp at the current bar index value (from the End time of the Bar, not the current running second, nor the Start Time).


                              I tested 4b
                              PHP Code:
                              Print("2 : " Time[0]);
                              Print(
                              "3 : " Time[1]);
                              Print(
                              "4a : " BarsArray[0].GetTime(CurrentBars[0]));
                              Print(
                              "4b : " BarsArray[0].GetTime(CurrentBars[0]).Subtract(TimeSpan.FromSeconds(11))); 
                              And it seems to work as needed.

                              2 : 17/05/2022 14:55:12
                              3 : 17/05/2022 14:55:12
                              4a : 17/05/2022 14:56:00
                              4b : 17/05/2022 14:55:49
                              I'll test on other periods and'll be back. Thanks!

                              Also do you know of a more concise way to formulate it if possible? Thanks!
                              Last edited by PaulMohn; 05-17-2022, 07:00 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by yertle, Yesterday, 08:38 AM
                              7 responses
                              28 views
                              0 likes
                              Last Post yertle
                              by yertle
                               
                              Started by bmartz, 03-12-2024, 06:12 AM
                              2 responses
                              21 views
                              0 likes
                              Last Post bmartz
                              by bmartz
                               
                              Started by funk10101, Today, 12:02 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post funk10101  
                              Started by gravdigaz6, Yesterday, 11:40 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by MarianApalaghiei, Yesterday, 10:49 PM
                              3 responses
                              11 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Working...
                              X