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

How to calc how many seconds before bar ends?

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

    How to calc how many seconds before bar ends?

    I am running a custom indicator on 3 minute bars with CalculateOnBarClose = false, and using the Alarm function to play a sound when a condition is met.
    How do I calculate the remaining seconds in the current bar, so I can pass that number to the Alarm function preventing it from rearming until the next bar starts?

    Alert("Alert", NinjaTrader.Cbi.Priority.High, "", @"C:\Program Files\NinjaTrader 6.5\sounds\kramgolf.wav", (remaining seconds), Color.White, Color.Black);

    #2
    Hi Zac, I believe PercentComplete would do the trick. It is returned as a double value though, so you would need to convert that to seconds.
    AustinNinjaTrader Customer Service

    Comment


      #3
      Is there a more precise way to do this by subtracting datetime structures, because the bigger the time period the less accurate PercentComplete will be?
      Is there something like a Bars.Complete.Seconds function?

      I tried to use the following code from the BarTimer indicator, but I get error CS0103 "The name 'Now' does not exist in the current context.

      TimeSpan barTimeLeft = Bars.Get(Bars.Count - 1).Time.Subtract(Now);
      Last edited by zacharydw00; 07-22-2009, 04:05 PM.

      Comment


        #4
        Zac, you can do this however you want. I suppose using DateTime structures could be more precise, but you'd have to test that on your end to be sure.

        Try changing Now to DateTime.Now, but keep in mind that will return the time from your PC clock, not the bar objects.

        Time[] holds an array of DateTime structures for each bar, so you could try working directly with those. Maybe by using Time[0] for the current bar, and then referencing the timestamps on the tick data arriving in OnMarketData() to determine how much time is left.
        AustinNinjaTrader Customer Service

        Comment


          #5
          Thanks! Substituting DateTime.Now works.

          Here's the code for anyone else who may need this .

          TimeSpan barTimeLeft = Bars.Get(Bars.Count - 1).Time.Subtract(DateTime.Now);
          Alert("Alert", NinjaTrader.Cbi.Priority.Medium, "", @"C:\Program Files\NinjaTrader 6.5\sounds\alarm.wav", 60 * barTimeLeft.Minutes + barTimeLeft.Seconds, Color.White, Color.Black);

          Comment


            #6
            Hi,

            How to get the timestamps on the tick data arriving in OnMarketData() in order to determine how much time is left?

            Thanks,
            Eric

            Comment


              #7
              Eric, just record the time when the OnMarketData() is called for a change in 'Last' price - http://www.ninjatrader-support.com/H...arketData.html
              BertrandNinjaTrader Customer Service

              Comment


                #8
                Thanks. I found that the data time is not updated at chart. However I saw the data time is updated at Output screen. The following is my script. Please assist.


                protected override void OnMarketData(MarketDataEventArgs e)
                {
                Print("Time :" + e.Time.ToLongTimeString());

                DrawTextFixed("Banner", e.Time.ToLongTimeString(), TextPosition.BottomLeft, Color.Black, font, Color.White, Color.White, 10);

                }

                Comment


                  #9
                  Please retest your script with the 'display update interval' set to 0 for your chart - http://www.ninjatrader-support.com/H...roperties.html
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    All,

                    Another question on this line of logic...

                    Where a strategy specification is CalculateOnBarClose = false and the strategy contains the below time-filter code (followed by subsequent code):
                    // Only process in the last three seconds of each bar
                    TimeSpan barTimeLeft = Bars.Get(Bars.Count - 1).Time.Subtract(DateTime.Now);
                    if (!(barTimeLeft.Seconds <=3))
                    return;
                    In the event where no ticks (i.e. BarUpdates) occur in the last three seconds of a particular bar, will the code following the above time-filter code run at the close of the bar? Essentially, I guess, is the close of a bar considered a BarUpdate event?

                    If not the case, any guidance on what code would be required to run on a BarUpdate event in the last three seconds and the close of the bar would be appreciated.

                    As always, thanks
                    Shannon

                    Comment


                      #11
                      The closing of a bar is the same event as the start of a new bar. Only when a new bar starts does the previous bar close, so on the event that closes a bar, your values will all already be relative to the new bar that is being built now.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        Josh,

                        Thanks for the quick reply.

                        To confirm my understanding where "CalculateOnBarClose" = false, the OnBarUpdate() method is called whenever a bar is updated (i.e. an incoming tick). While the end of one bar (aka. start of the next bar) is not an incoming tick, it is considered as a "BarUpdate" event?

                        Given, 1) I am attempting to have the strategy run in the last 3 seconds of each bar while guaranteeing it will run at least once (at the close of the bar if necessary), and
                        2) The start of a new bar is considered as a "BarUpdate" event,
                        to achieve my goal, would require the running the below code amended to include a scenario where with 60secs (in the case of the strategy running on a 1min chart) remaining in the current bar all subsequently accessed information would need to be for the prior bar (e.g. High[1]).

                        I'm sure I'm attacking this from the wrong angle!

                        As always, your input would be appreciated.
                        Regards
                        Shannon

                        Comment


                          #13
                          Originally posted by Shansen View Post
                          While the end of one bar (aka. start of the next bar) is not an incoming tick, it is considered as a "BarUpdate" event?
                          An incoming tick is required to start a new bar, and thus closing the previous bar.

                          An issue you may run into, as an exaggeration. If you are using a 20 second chart, and no incoming tick is received for 22 seconds, a whole bar will be skipped by this code. Watch the ES or YM late in the evening with a 1 minute chart and the BarTimer indicator loaded. Sometimes the BarTimer skips a couple of bars before it restarts, because there is no incoming tick for over a minute. If the instrument you are trading has sufficient volume this will not be a problem.
                          Last edited by zacharydw00; 08-18-2009, 10:53 PM.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by aussugardefender, Today, 01:07 AM
                          0 responses
                          3 views
                          0 likes
                          Last Post aussugardefender  
                          Started by pvincent, 06-23-2022, 12:53 PM
                          14 responses
                          238 views
                          0 likes
                          Last Post Nyman
                          by Nyman
                           
                          Started by TraderG23, 12-08-2023, 07:56 AM
                          9 responses
                          384 views
                          1 like
                          Last Post Gavini
                          by Gavini
                           
                          Started by oviejo, Today, 12:28 AM
                          0 responses
                          4 views
                          0 likes
                          Last Post oviejo
                          by oviejo
                           
                          Started by pechtri, 06-22-2023, 02:31 AM
                          10 responses
                          125 views
                          0 likes
                          Last Post Leeroy_Jenkins  
                          Working...
                          X