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

Problem Custom Trigger

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

    Problem Custom Trigger

    I want to trigger some code based on time. I have setup the following simple test:

    protectedoverridevoid OnStartUp()
    {
    myTimer.Tick += new EventHandler (TimerEventProcessor);
    myTimer.Interval = TimerInterval;
    myTimer.Start();
    }

    privatevoid TimerEventProcessor(Object myObject, EventArgs myEventArgs)
    {

    TriggerCustomEvent(TestHandler, 0, myTimer.Interval);
    }

    privatevoid TestHandler(object state)
    {
    Print(DateTime.Now+
    ": FiboConnector "+myTimer.Interval);
    }

    The interval has been set to 500 msec (so execute twice per second). After enabling the strategy, it prints the line each half a second in the output window. So far so good. After 100 lines I do get the following erro message in the log: "Error on trigger custom event: More than 100 subsequent user events" and the strategy stops running.

    I know there is a hidden variable MaxProcessedEvents, but I would like to know why I get this message and how to prvent this. I can raise the MaxProcesseEvenets but than I will get the same problem only later in time. It seems that the call to the customer event is not cleared from memory after finishing? Do you have any idea how to solve this.

    Kind regards,
    Rene

    #2
    Hi Rene, this issue would usually be seen in Strategies when a high # of NS method calls is queued up / stacked before control would be returned to NinjaTrader. Are you running the timer historically as well and potentially more than 100 times per bar?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      This ia all the code I am testing. I am testting this simple piece of code on a 1 minute bar of EURUSD (loaded 5 days). I noticed that exactly at the moment that the print statement is executed 100 times that i get this error (I am using NT V7). So I don't think it has to with histrorical bars.

      Comment


        #4
        Thanks - are you disposing your timer resource then as in this example?

        BertrandNinjaTrader Customer Service

        Comment


          #5
          Yes, I dispose in the Onterminition section. I used the same example for building the code:

          protectedoverridevoid OnTermination()
          {

          // Cleans up our Timer object
          myTimer.Dispose();

          }

          Comment


            #6
            This is the complete simple code. This gives the error message after 100 output lines and stops running. What do I miss here.


            #region Variables
            private int TimerInterval = 500;
            private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
            #end region
            protected override void Initialize()
            {
            BarsRequired =
            1;
            }

            protected override void OnStartUp()
            {
            // Initiate our Timer object with an interval of XX ms
            myTimer.Tick += new EventHandler(TimerEventProcessor);
            myTimer.Interval = TimerInterval;
            myTimer.Start();
            }

            private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
            {TriggerCustomEvent(TestHandler,
            0, "");}

            private void TestHandler(object state)
            {Print(DateTime.Now+
            ": Connector "+myTimer.Interval);}

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

            }

            }

            Comment


              #7
              Renejm, the issue to my understand and experience would likely be that too many custom events then trigger within the same bar for you with your code, if you can space those out or run with a higher allowed # of events you should be fine.
              BertrandNinjaTrader Customer Service

              Comment


                #8
                Unfortunately that is not a solution to the problem. In my test I use a bar of 1 minute of the EURUSD = 60 sec or 60000 msec. I run the test with the timer interval of 500. That makes 120 events per 1 minute bar. This generates the error. Now I increase the timerinterval to 1000. That makes only 60 events for 1 minute bar. After 100 seconds (the graph is already on a new bar) I still get the same error. Now I increase the MaxProcessedEvents to 200 instead of the default 100. Now I get the error after 200 seconds (already 2 bars further). So the problem is not related to the amount of events triggered in 1 bar.

                What could it be else?

                Kind regards,
                Rene Kreileman

                Comment


                  #9
                  Rene, would the suggest you thrown in some other (non timer based) event into this hypothetical example as well, for example an OBU print every bar, so the custom events q count could be reset. This way and with an increased Max Events you should be able to handle any scenario.
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Hi Bertrand, thanks for the suggestion. I have added OnBarUpdate section without any code and as long as a new tick comes in, triggering OnBarUpdate before the MaxProcessedEvents has been reached, the counter for ProcesseEvents will be reset and it will keep on running. Leaves me with one issue, if there is no connection to the datafeed, no other section will be executed except for the timer based user event. If the disconnection will take long enough the code still gevies an error when reaching the MaxProcessedEvents. Three questions in that regard:

                    Question 1: Is there another section that can be included that will keep execute when there is no connection (the OnConnectionStatus is only executed once, when the disconect occurs, so that will not be the solution) and that will reset the Processes Event counter?
                    Question 2: What is the maximum value for MaxProcessedEvents?
                    Question 3: Does increasing the MaxProcessedEvents does have any effect on the performance, because this will stack more events?

                    Kind regards,
                    Rene

                    Comment


                      #11
                      Hello,

                      This is Chelsea responding for Bertrand who will be out for the rest of the week.

                      I am currently working with the development department to find the answers about this custom timer event issue with strategies.

                      So far I can answer one question.
                      Question 2: What is the maximum value for MaxProcessedEvents?
                      The MaxProcessedEvents variable does not have a limit and can accept the largest integer possible using int (int.MaxValue).

                      Regarding your other questions we are researching this issue and I will keep you updated when we have any new information.

                      Thank you for your patience with this matter.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hell Renejm,

                        Thank you for your patience with this matter.

                        I have had a chance to discuss this with Bertrand and the development department.

                        I have been made aware that NinjaTrader was designed with this to be a safeguard for strategies so that scripts are not allowed to become uncontrolled/unsupervised etc.

                        Without market data there is not away to trigger an NT event that would reset the queue of custom events.

                        Also, it is expected that NinjaTrader would take a slight performance hit for each queued event. This will be specific to the event and the calls in the event. Something like using a Print() statement would not cause much increase resources consumption.


                        Please let me know if this does not resolve your inquiry.
                        Chelsea B.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by algospoke, Yesterday, 06:40 PM
                        2 responses
                        19 views
                        0 likes
                        Last Post algospoke  
                        Started by ghoul, Today, 06:02 PM
                        3 responses
                        14 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Started by jeronymite, 04-12-2024, 04:26 PM
                        3 responses
                        44 views
                        0 likes
                        Last Post jeronymite  
                        Started by Barry Milan, Yesterday, 10:35 PM
                        7 responses
                        20 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Started by AttiM, 02-14-2024, 05:20 PM
                        10 responses
                        180 views
                        0 likes
                        Last Post jeronymite  
                        Working...
                        X