Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need OnTimer

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

    #31
    Hello Marco,

    with Initialize() you don't have any control about how many instances of this indicator are "floating around"...and therefore you don't have any control about how many active timers there are. I would start the timers nowhere else than in OnStartUp(). and I would make sure to shut the timers down properly in Dispose() or in the other method called at the indicator's destruction (don't remember the name).

    Regards
    Ralph

    Comment


      #32
      hello Ralph,

      Please take a look at the code of my strategy, the Timer is initialized in OnStartUp() and shut down in OnTermination(). And still get the error after +/- 10 seconds. I really don't understand what else can be wrong....

      Code:
       
      #region Using declarations
      using System;
      using System.ComponentModel;
      using System.Diagnostics;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Data;
      using NinjaTrader.Indicator;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Strategy;
      #endregion
      // Add this to the declarations. It allows the use of a Timer object to trigger events at our own time.
      using System.Windows.Forms;
      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      /// <summary>
      /// Enter the description of your strategy here
      /// </summary>
      [Description("Enter the description of your strategy here")]
      public class dummy2 : Strategy
      {
      #region Variables
      // Wizard generated variables
      private int myInput0 = 0;
      private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer(); 
       
      // User defined variables (add any user defined variables below)
      #endregion
      /// <summary>
      /// This method is used to configure the strategy and is called once before any strategy method is called.
      /// </summary>
      protected override void Initialize()
      {
      CalculateOnBarClose = true;
      }
       
      protected override void OnStartUp()
      {
      // Initiate our Timer object with an interval of 100ms (0.1 second)
      myTimer.Tick += new EventHandler(TimerEventProcessor);
      myTimer.Interval = 100;
      myTimer.Start();
       
      }
       
      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // do something 
       
      }
       
      // Timer's tick event handler. Called at every tick of 1000ms.
      private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
      {
      /* Important to use the TriggerCustomEvent() to ensure that NinjaScript indexes and pointers are correctly set.
      Do not process your code here. Process your code in the MyCustomHandler method. */
      TriggerCustomEvent(MyCustomHandler, 0, myTimer.Interval);
      }
       
      private void MyCustomHandler(object state)
      {
      // Informs us of when the event was triggered and of our Timer settings
      Print("\tTime: " + DateTime.Now);
      Print("\tTimer Interval: " + state.ToString() + "ms");
       
      }
       
      // Important to clean up our resources
      protected override void OnTermination()
      {
      // Cleans up our Timer object
      myTimer.Dispose();
      }
      I found another thread on this issue, and still I can't seem to find a definitive solution...


      What I can confirm after testing is what Roonius suggested in post#18 in the thread linked above to limit the amount of "Timer-ticks" per bar.
      If I run the strategy above on a 1min bar graph the Timer (1 sec tick) keeps working because it can not reach 100+ in the same bar. Running it on a 3min graph, it throws the error because 100+ Timer ticks come in the same bar.

      Big problem is that I run my strategy on a minutes Kagi chart where sometimes a bar lasts 1 hour or more.....

      Do you have any other ideas how to get this working ?

      regards,
      Marco
      Last edited by marcow; 04-01-2011, 07:04 PM.

      Comment


        #33
        Hello Marco,

        Now I would check whether MyCustomHandler() is working as expected (do you find the desired print statements?). If not, try to find out what the reason is. In addition you could implement a static instance counter variable to keep control how many instances of the indicator with the active timer are generated ([1]private static int counter, [2] increment and print the counter in OnStartUp(), [3] decrement and print the counter in OnTermination()).

        Regards
        Ralph

        Comment


          #34
          thanks Ralph,

          Now I would check whether MyCustomHandler() is working as expected (do you find the desired print statements?)
          I'm not sure if I follow you here. Yes, I see the desired print statements in the ouput window. If I print out a variable which is increased at every timer tick, I get the error message ("More than 100 subsequent user events" ) after it reaches around 120.
          But again, only if it reaches 120 in the same bar. If the same test is done on a 1min bar chart, the Timer works fine, because its maximum in 1 bar is 60 (assuming that the timer interval is set at 1 sec.)

          In addition you could implement a static instance counter variable to keep control how many instances of the indicator with the active timer are generated ([1]private static int counter, [2] increment and print the counter in OnStartUp(), [3] decrement and print the counter in OnTermination()).
          I only get the More than 100 subsequent user events-error message when using the Timer in a strategy ( code is in previous post), in an indicator every is working as expected.

          I'm really amazed why a simple timer event is so problematic in a strategy....

          Comment


            #35
            Hello Marco,

            until now I was assuming that the error message "More than 100 subsequent user events" is caused by a pipeline of pending event requests but now I am wondering if that error message means something different. But that should be clarified with the NT support.

            Regards
            Ralph

            Comment


              #36
              thanks for your effort Ralph,

              and yes,I fully agree.
              All I have is a very basic stripped-down strategy which prints a variable to the output window every second, nothing else. Running it on a 1min chart works fine, running it on a 3Min chart will cause the "More than 100 subsequent user events" error, because now the 100 events happen in the same bar.

              The error is fully reproducible and I posted the code. Maybe someone from NinjaTrader can take a look at this ?

              Marco

              Comment


                #37
                Hello,

                I will have someone respond to you on Monday.

                Thanks for your patience.
                BrettNinjaTrader Product Management

                Comment


                  #38
                  Marco, I'll try reproducing your issue here and will look into - for now you can use the unsupported .MaxProcessedEvents property to set the # of events as high as needed in your script's Initialize().

                  Comment


                    #39
                    Thanks for the patience Marco, we reproduced here and the outcome seen is unfortunately expected as different code 'safeguards' apply comparing indicator vs strategy - we suggest therefore you work with the .MaxProcessedEvents property as needed to cover your scenario.

                    Thanks,

                    Comment


                      #40
                      hello Bertrand,

                      thank you for looking into this issue.

                      Because I'm not completely comfortable using the unsupported MaxProcessedEvents, I created a workaround for this problem in that I don't use the Timer anymore.
                      Instead I use the OnMarketData() method combined with a time filter; the code is executed on the first incoming tick which has to be more than 1 minute after the previous code execution. This way I have a reasonably accurate timer which also triggers intra bar on a trategy with CalculateOnBarClose= true.

                      Marco

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      582 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      338 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      103 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      554 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      552 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X