Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SampleCustomEvents and timer

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

    SampleCustomEvents and timer

    Hi Ninja team,

    This may be more a C# question, but since the code is from you I have to ask.

    I'm using a variant of the SampleCustomEvents script for NT7 to have a timer print to the output window every 5 seconds if something is true. It works as expected unless run it for a long time. I get this error:

    **NT** Error on triggering custom event for strategy 'MyStrategy/9951653031314b6f968db4264edd1653': More than 100 subsequent user events

    I can get that error to happen faster if I change the timer interval to a lower figure.

    Any idea how I can prevent that error and how I can code around it if it happens again? Try/catch doesn't seem to be "catching" it.

    Thanks!

    #2
    Hi MXASJ, when / where are you initiating the timer? Would you mind sharing a snippet we could look into? You want to make for example you don't start this on each bar...
    BertrandNinjaTrader Customer Service

    Comment


      #3
      In Variables:

      PHP Code:
      private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
      private 
      bool useTimer false
      In OnBarUpdate:

      PHP Code:
      if (!useTimer && pingConnection)// Run timer
      {
      myTimer.Tick += new EventHandler(TimerEventProcessor);
      myTimer.Interval pingFrequency;
      myTimer.Start();
      useTimer true;
      }
       
      After OnBarUpdate:
      private 
      void TimerEventProcessor(Object myObjectEventArgs myEventArgs// Timer's tick event handler. Called at every tick of pingFrequency.
      {
      /* 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. */
      try
      {
      TriggerCustomEvent(MyCustomHandler0myTimer.Interval);
      }
      catch (
      Exception e)
      {
      Print(
      DateTime.Now " ERROR!: Exception in ConnectionMonitor/TimerEventProcessor"); 
      }
      }

      private 
      void MyCustomHandler(object state)
      {
      try
      {
      MyPing();
      }
      catch (
      Exception e)
      {
      Print(
      DateTime.Now " ERROR!: Exception in ConnectionMonitor/MyCustomHandler"); 
      }

      And I have myTimer.Dispose() happening in OnTermination().

      The only real departure in the Timer logic from the SampleCustomEvents example is the addition of the useTimer=false in Variables and useTimer=true in OnBarUpdate.

      That way whatever is in the MyCustomHandler method only happens once per timer period.

      If nothing obvious stands out, I'll code a simple one that just prints to the output window and let it run for a day.

      Comment


        #4
        Thanks MXASJ, don't see anything really 'breaking' ... could you please post the simple one that would exhibit this issue, so we can look into?
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Will do, Bertrand. I got the same error again today about 12 hours into running that script. It might take a few days (it being Friday) but I will follow up.

          Comment


            #6
            Here you go. An NT7b16 compile of a strat that recreates this error is attached.

            A peek at the code:

            PHP Code:
            //Variables

            private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();

            private 
            bool useTimer false//used as flag so the timer only fires once per interval. If you remove
            // this here and its counterpart in OnBarUpdate the error will reproduce faster (typically within minutes)

            protected override void Initialize()
            {
            CalculateOnBarClose false;
            }

            protected 
            override void OnBarUpdate()
            {
            if (
            Historical) return;
            //if (CurrentBar == 0) // Stratgey will not print if I use this
            //if (BarsInProgress == 0) // If you delete the useTimer try this instead 
            if (!useTimer
            {
            // Initiate our Timer object with an interval of 1000ms (1 second)
            myTimer.Tick += new EventHandler(TimerEventProcessor);
            myTimer.Interval 250;
            myTimer.Start();
            useTimer true//used as flag so the timer only fires once per interval




            And

            PHP Code:
            // Timer's tick event handler. Called at every tick of 1000ms.
            private void TimerEventProcessor(Object myObjectEventArgs 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(MyCustomHandler0myTimer.Interval);
            }

            private 
            void MyCustomHandler(object state)
            {
            // Informs us of when the event was triggered and of our Timer settings
            Print(DateTime.Now " myTimer Test");



            }

            // Important to clean up our resources
            protected override void OnTermination()
            {
            // Cleans up our Timer object
            myTimer.Dispose();

            Some caveats;

            1. If you delete or comment out the useTimer logic, multiple Print() events will happen per second (using 1000 as interval) and you get the error in a few minutes.
            2. If you leave the useTimer logic as-is above and use 1000 as the interval, it takes 8-12 hours for the error to pop up.
            3. If you leave the useTimer as-is above and use 250 as the interval, the error pops up in a few minutes. It looks like that was the state I compiled and exported this with.
            Attached Files

            Comment


              #7
              MXASJ,

              I've ran your strategy unmodified for 10 minutes and nothing came up while using it on the ES. What steps am I missing? This was ran on a 1min chart.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Thanks Josh,

                I guess all I can ask is that you let it run for more than 10 minutes. I don't run it on a chart, I just open it via the strategies tab and let it run while I do other things. I'm using ES 1 sec bars, CalculateOnBarClose=false.

                It popped up again at 4:15pm EST. Interestingly, over the past three days, the error only occured between 4:15pm and 5:00pm.

                I'm guessing that error is one generated by NT itself and not dotNet. It appears to be some sort of buffer overflow exception. My goal is to code around it either by writing code that doesn't create that exception, or better handling it when it does. The strategy stays green and enabled even if it has stopped processing events in OnBarUpdate after that exception is thrown.

                Writing code that doesn't create that exception requires me to understand what causes it. The logic of that strat is "print something once every second, and think about it on every incoming tick." What bothers me is that if I change that to "think about it every second" or "think about it every minute", I'll still get an error, over time.

                Anyway I understand you have some higher priority stuff to look at first. I'll work on this some more next week.

                Comment


                  #9
                  We can try again when markets are open.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    MXASJ,

                    I have just ran your strategy for 30 minutes just now to no avail. Will have to try around market close. What session template are you applying to your strategy?
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      I still got that error at 16:15:50 in the production version of this that I run 23/5. I'll run the MOD version I posted in this thread in parrallel with that and see what happens.

                      Is there a simple way to code an error check or alert if the OnBarUpdate logic has effectively halted but the Strategy remains green and enabled in the Strategies tab?

                      Thanks for your time on this.

                      Comment


                        #12
                        Josh I've been running the strat I posted using b16 and its is extremely boring to watch. Sorry about that. Changing the 250ms to 50ms seems to lessen the time it takes to raise that exception. I've changed that setting in the attached strat.

                        I'll email you my production strat after the close. While the "more than 100 events" excepton is the same, I can a guess a print buffer overflow in the attached strat is the cause... but the production version isn't printing and I see nothing in my code or the logs that would generate 100 unprocessed events.

                        Anyway I'll email it later.

                        Thanks.
                        Attached Files

                        Comment


                          #13
                          Why don't you set

                          MaxProcessedEvents=25;

                          in Initialize() ?

                          to trigger the error already after 25 events or similar?

                          Then the problem shows up quicker

                          andreas

                          Comment


                            #14
                            Thanks Andreas,

                            Good thought. But where I am now is... I know the exception gets thrown and I can reproduce it , but how can I handle it? try/catch doesn't seem to do it.

                            The strat logic effectively halts but the strat remains green and enabled in the Strategies tab, which I don't want.

                            Comment


                              #15
                              Actually I get the same exception also sometimes in my PairsStrategy code.
                              Usually it happens when CurrentBar is still Historical . I suspect that my strategy order logic does fire to many orders/cancellations/etc for the NinjaTrader queue.
                              This happens only in Historical, probably because then from CurrentBar = 0 to GetBar(DateTime.Now) the code execution time is quenched from the actual for example 10000 Bars == 10000 Minutes to a few seconds until Historical becomes false.
                              This in my view will overflow NinjaTrader internals queue.

                              Had no time for further testing as this PairsStrategy was just a spin - off exercise.


                              Maybe your testing will find out ...


                              regards
                              Andreas

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by carnitron, 04-27-2024, 08:42 PM
                              1 response
                              15 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by WHICKED, 04-26-2024, 12:56 PM
                              3 responses
                              24 views
                              0 likes
                              Last Post WHICKED
                              by WHICKED
                               
                              Started by zstheorist, 04-26-2024, 07:52 PM
                              1 response
                              13 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by Touch-Ups, 04-27-2024, 10:36 AM
                              2 responses
                              22 views
                              0 likes
                              Last Post Touch-Ups  
                              Started by needsomehelp147, Today, 06:43 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post needsomehelp147  
                              Working...
                              X