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

adding a timer that starts to count on bar close

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

    adding a timer that starts to count on bar close

    I am playing around with the code in the SampleCustomEvents file, provided here:




    As long as I leave the lines of code like this:

    if (State == State.Configure)
    {
    myTimer.Tick += new EventHandler(TimerEventProcessor);
    myTimer.Interval = 1000;
    myTimer.Start();
    }

    everything works fine and it prints out just as expected

    But because I need my timer to start counting right on the open of a bar (isfirsttick is not precisely enough for me), I tried to put the correlating line of code into bthe onbarupdate section like this:

    protected override void OnBarUpdate()
    {
    if (myTimer.Enabled)
    {
    return;
    }

    myTimer.Start();
    Print("Timer started");
    }

    It prints out that the timer has been started, but the timer itself seems to not do anything since there are no timer events printed from the MyCustomHandler Method on the output window anymore.

    #2

    You found the Close of each bar... If you search the forum long enough you will find posts that say in NT8 first tick of bar effectively is the close of the previous bar.

    This page gets into some detail on how, tick by tick, bars are updated. https://ninjatrader.com/support/help...ick_replay.htm

    I am not seeing an immediate answer to help you with the rest of the questions.

    "I need my timer to start counting right on the open of a bar (isfirsttick is not precisely enough for me),"

    Why? What are you trying to do?

    HedgePlay



    Comment


      #3
      Hello ravendo,

      The specific issue is that you are seeing the print after the timer is started by you are not seeing the prints from the MyCustomHandler, is this correct?

      Is the TimerEventProcessor method still calling TriggerCustomEvent to trigger MyCustomHandler?

      Is myTimer.Start() still being called in State.Configure?

      Unfortunately, a bar will not open until at least one tick has been received to open the bar. If the bar is not open, no ticks have been received. If the bar is open, at least one tick has been received.
      With Calculate set to OnEachTick, IsFirstTickOfBar will be true, specifically when the bar opens as the first tick is received. There is no other way to know when a bar has opened, other than knowing a tick was received to open the bar. Without that tick, the bar does not open.

      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Thank you hedgeplay and thank you ChelseaB.

        hedgeplay unfortunately first tick of bar is not the close of the previous bar especially not when you have a very thin market like for example at night. You can compare time[1] with the first timestamp on the time & sales window of the currently forming bar and see the difference. Time[1] might look like 12:30:00:000 but the first tick of the next bar on the time and sales window might show something like 12:30:00:997 meaning that this first tick happened with almost a second delay to the bars open. So what I want to achieve is my timer to start at the exact time when the previous bar closes. For example at 00:00:000 of every new minute and then I want to calculate my logic every certain time the predefined interval expires. And with the start of every new bar or close of the last bar, which from my point of view is exactly the same, the process has to start itself.


        Yes ChelseaB the specific issue is that I am seeing the print after the timer is started but I am not seeing the prints from the MyCustomHandler.

        And no, myTimer.Start() is not called in State.Configure, because I have this if statement

        if (myTimer.Enabled)
        {
        return;
        }

        in OnBarUpdate(). So if myTimer.Start() was already called in State.Configure, this would always be true and therefore useless.


        As long as I leave this codeblock

        myTimer.Tick += new EventHandler(TimerEventProcessor);
        myTimer.Interval = 1000;
        myTimer.Start();
        Print("Timer started");

        in State.Configure, I am seeing on the output window that the timer is startet and also the prints from the MyCustomHandler for every Interval.

        But when I move myTimer.Start(); or even the whole codeblock in OnBarUpdate() only "Timer started" appears.










        Comment


          #5
          I am testing with replay data. And today I found out that it is working if I start replay mode on the very first bar of a day, where there is no previous replay data available and then add the indicator to the chart. Once this first bar closes the timer starts and begins to count. But when I start replay mode on the second bar, or later somehow only the timer starts, but there are no counts.

          Comment


            #6
            Hello ravendo,

            I am not able to reproduce this behavior.

            Below is a link to a video of the test.
            https://drive.google.com/file/d/1p9o...w?usp=drivesdk

            Attached is the test script (reduced from 'Using custom events to output the current Level II data book' a.k.a. SampleCustomEvents).

            Using the exact steps shown in the video with this provided test script, are you experiencing different results than what is shown in the video?
            Attached Files
            Last edited by NinjaTrader_ChelseaB; 04-27-2021, 12:28 PM.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi ravendo,

              First, Chelsea is very good. I recommend paying close attention to his advice.

              I am testing with replay data. And today I found out that it is working if I start replay mode on the very first bar of a day, where there is no previous replay data available and then add the indicator to the chart. Once this first bar closes the timer starts and begins to count. But when I start replay mode on the second bar, or later somehow only the timer starts, but there are no counts.
              Second, I will just offer up the practical habits I use to smooth out bumps in reply, afterhours and work while the market is not open (weekends and holidays).

              Simple habits for working with tick charts, or tick reply when not live:

              Practical Use Tips / A few things to try:
              • NT8 tick bars orient themselves via a new session that starts at the time specified in the trading hours template.
              • So even if I only need a few tick bars or hours of tick always request at least "1 day" of tick history.
              • On normal weekends always request 3 days so you will have Friday's ticks to work with on Sunday.
              • On long weekends ask for 4-5 days of tick.
              • For off-hour development and testing of Strategies, be sure to change the Trading Hours template to "Default 24x7" to avoid unexpected stalls in strategy execution.
              • Sometimes I find "Default 24x7" is the right answer while using Tick Reply as well.

              Best of Luck!
              HedgePlay

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by r68cervera, Today, 05:29 AM
              0 responses
              2 views
              0 likes
              Last Post r68cervera  
              Started by geddyisodin, Today, 05:20 AM
              0 responses
              3 views
              0 likes
              Last Post geddyisodin  
              Started by JonesJoker, 04-22-2024, 12:23 PM
              6 responses
              35 views
              0 likes
              Last Post JonesJoker  
              Started by GussJ, 03-04-2020, 03:11 PM
              12 responses
              3,239 views
              0 likes
              Last Post Leafcutter  
              Started by AveryFlynn, Today, 04:57 AM
              0 responses
              6 views
              0 likes
              Last Post AveryFlynn  
              Working...
              X