Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Is DispatcherTimer preferred in NT8?

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

    Is DispatcherTimer preferred in NT8?

    Is DispatcherTimer preferred in NT 8?
    If not, what is?
    If it is, Can anybody point out why this code is not resulting in my timer method being called?

    Code:
    dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
    dispatcherTimer.Tick += timer_Tick;
    dispatcherTimer.Interval = new TimeSpan(0,0,1);
    dispatcherTimer.Start();
    See attachment for runnable sample strategy code with print statements to show not working.

    Thanks,
    Gary

    #2
    Here is attachment

    Attachment shows timer never starts.
    Attached Files

    Comment


      #3
      Originally posted by GaryAlbers View Post
      Attachment shows timer never starts.
      It's working here so well, it didn't stop!!! I paused Playback-MR and it didn't stop.
      I even disabled the strategy, and it keeps on plugging away like the Energize Bunny!!!!



      Enabling NinjaScript strategy 'GTestTimer/70428016' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
      Timer created and started
      ----- timer_Tick
      ----- timer_Tick
      ----- timer_Tick
      ----- timer_Tick
      ----- timer_Tick
      ----- timer_Tick
      ----- timer_Tick
      Disabling NinjaScript strategy 'GTestTimer/70428016'
      ----- timer_Tick
      ----- timer_Tick
      ----- timer_Tick
      ----- timer_Tick
      ----- timer_Tick
      ----- timer_Tick
      ----- timer_Tick

      Comment


        #4
        Originally posted by GaryAlbers View Post
        Is DispatcherTimer preferred in NT 8?
        If not, what is?
        I used this in NT7 and it is in the NT8 documentation, along with System.Timers.Timer.

        There should be examples floating around here.

        *I have not tried in NT8.


        Comment


          #5
          Oops

          Sorry sledge,

          I forgot to add the Termination state in the sample.

          When I retried my own example on a new chart (and with a fresh nights sleep) it is working for me too.

          Sorry for the false alarm.

          Gary

          Comment


            #6
            Getting weird

            I can only get it to run when the timer is created in the Configure state.
            But that seems wrong because configure state is called again when strategy dialog is opened in order to stop the strategy (this surprised me).

            When I start the timer in historical or realtime state, I don't see the Tick event fire.
            I attached an updated example.

            Any ideas on why the timer would not fire>

            Gary
            Attached Files
            Last edited by GaryAlbers; 03-26-2016, 01:28 PM.

            Comment


              #7
              Originally posted by GaryAlbers View Post
              I can only get it to run when the timer is created in the Configure state.
              But that seems wrong because configure state is called again when strategy dialog is opened in order to stop the strategy (this surprised me).

              When I start the timer in historical or realtime state, I don't see the Tick event fire.
              I attached an updated example.

              Any ideas on why the timer would not fire>

              Gary
              I could NOT get it to fire when turning on Enabled in the Configure screen. No idea why. Probably NT8B10 bug of some sorts.

              It would only enable/run when I enabled it in the Strategies tab screen.

              Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.

              Comment


                #8
                Interesting distinction to discover sledge. It does sound like a bug.

                I also discovered that by using System.Timers.Timer instead of System.Windows.Threading.DispatcherTimer it works as expected.

                Comment


                  #9
                  Hello sledge and GaryAlbers,

                  I will be testing this code momentarily. First, though, I did notice several changes in the code you provided GaryAlbers that were not directly related to either Timer or DispatcherTimer. I am going to provide a summary of the differences. Lines starting with a - are in the first sample, lines starting with a + are in the second sample, and all other lines are in both samples. I will use ... to mean that I am skipping lines of code.

                  Code:
                  [FONT=Courier New]
                  ...
                  
                               {
                                   ClearOutputWindow();
                               }
                  -            else if (State == State.Realtime)
                  +            else if (State == State.Historical)
                               {
                                   dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
                                   dispatcherTimer.Tick += timer_Tick;
                  
                  ...
                  
                                   dispatcherTimer.Start();
                   Print("Timer created and started");
                               }
                  +            else if (State == State.Realtime)
                  +            {
                  +            }
                  +            else if (State == State.Terminated)
                  +            {
                  +                disableTimer();
                  +            }
                           }
                   
                           private void disableTimer()
                  
                  ...
                   
                           private void timer_Tick(object sender, EventArgs e)
                           {
                  -            Print("----- timer_Tick");
                  +            TriggerCustomEvent(o =>
                  +            {
                  +                Print("----- timer_Tick");
                  +            }, this);
                  +
                           }
                  [/FONT]
                  For the purpose of clean testing, I will make the following choices :

                  Code:
                  [FONT=Courier New]
                  ...
                  
                               {
                                   ClearOutputWindow();
                               }
                               [B]else if (State == State.Realtime)[/B]
                               {
                                   dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
                                   dispatcherTimer.Tick += timer_Tick;
                  
                  ...
                  
                                   dispatcherTimer.Start();
                   Print("Timer created and started");
                               }[/FONT]
                  [FONT=Courier New][FONT=Courier New]             else if (State == State.Terminated)
                               {
                                   disableTimer();
                               }[/FONT]
                           }
                   
                           private void disableTimer()
                  
                  ...
                   
                           private void timer_Tick(object sender, EventArgs e)
                           {
                               Print("----- timer_Tick");
                           }[/FONT]
                  I will test in each of the states you mentioned where I have bolded above.

                  I will reply again when I have determined whether DispatcherTimer is working properly.
                  Jessica P.NinjaTrader Customer Service

                  Comment


                    #10
                    Hello,

                    I was able to confirm, using the attached C# file, that System.Windows.Threading.DispatcherTimer will only fire from State.Configure. Thank you for your report. The QA team has been made aware of this issue. I will update this thread when there is more information available.
                    Attached Files
                    Jessica P.NinjaTrader Customer Service

                    Comment


                      #11
                      I have some feedback as to why the Dispatcher timer will not load in this context. I am providing a quote from the publicly available link to Microsoft's documentation for the DispatcherTimer class.

                      Originally posted by https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx
                      If a System.Timers.Timer is used in a WPF application, it is worth noting that the System.Timers.Timer runs on a different thread then the user interface (UI) thread. In order to access objects on the user interface (UI) thread, it is necessary to post the operation onto the Dispatcher of the user interface (UI) thread using Invoke or BeginInvoke. Reasons for using a DispatcherTimer opposed to a System.Timers.Timer are that the DispatcherTimer runs on the same thread as the Dispatcher and a DispatcherPriority can be set on the DispatcherTimer.
                      In this case, the Dispatcher thread was not made available to user strategies. It is recommended that you use System.Timers.Timer, which does have support available. I am providing a publicly available link to Microsoft's documentation for System.Timers.Timer class :

                      Generates an event after a set interval, with an option to generate recurring events.
                      Jessica P.NinjaTrader Customer Service

                      Comment


                        #12
                        Thank you for the follow up and documentation!

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by judysamnt7, 03-13-2023, 09:11 AM
                        4 responses
                        57 views
                        0 likes
                        Last Post DynamicTest  
                        Started by ScottWalsh, Today, 06:52 PM
                        4 responses
                        36 views
                        0 likes
                        Last Post ScottWalsh  
                        Started by olisav57, Today, 07:39 PM
                        0 responses
                        7 views
                        0 likes
                        Last Post olisav57  
                        Started by trilliantrader, Today, 03:01 PM
                        2 responses
                        19 views
                        0 likes
                        Last Post helpwanted  
                        Started by cre8able, Today, 07:24 PM
                        0 responses
                        9 views
                        0 likes
                        Last Post cre8able  
                        Working...
                        X