Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Run script every x seconds

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

    Run script every x seconds

    Hi,

    Is it possible to run my strategy every x seconds instead of OnBarUpdate(), OnExecution() or whatever else? If so, how would I code it?

    #2
    Welcome to our forums, you would need to work with a custom timer event then - http://www.ninjatrader-support2.com/...ead.php?t=5965
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thank you Bertrand.

      Comment


        #4
        Hello again,

        I'm trying to implement Timer in my strategy, but it doesn't work. How do I know? There's print instruction in the code, but no output is visible. Let me show you the important parts of my strategy:
        Code:
        using System.Windows.Forms;
        namespace NinjaTrader.Strategy
        {
        
            public class st : Strategy
            {
            private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
        
                protected override void Initialize()
                {
                    now = DateTime.Now;
                }
        
                protected override void OnBarUpdate()
                {
        			
        			// Initiate our Timer object with an interval of 1000ms (1 second)
        			myTimer.Tick += new EventHandler(TimerEventProcessor);
        			myTimer.Interval = 1000;
        			myTimer.Start();
        
                }
        
        		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");
        
        		public override void Dispose()
        		{
        			// Make sure you include base.Dispose() whenever you override the Dispose() method
        			base.Dispose();
        			
        			// Cleans up our Timer object
        			myTimer.Dispose();
        		}
        
            }
        }
        As you can see, the code is very simple, therefore I must miss something basic here. I would be very grateful for your help.
        Last edited by alkamid; 02-23-2010, 06:46 AM.

        Comment


          #5
          alkamid, any errors in the log tab? Are you expecting this to work in backtesting or realtime use?

          Have you tried for example with this call as in our reference sample?

          Code:
          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, myTimer.Interval);
          }
          BertrandNinjaTrader Customer Service

          Comment


            #6
            Bertrand,

            I am expecting it to work realtime. Using the sample code
            Code:
            TriggerCustomEvent(MyCustomHandler, myTimer.Interval);
            produces error:
            Code:
            No overload for method 'TriggerCustomEvent' takes '2' arguments
            In the log I found:
            Code:
            Error on triggering custom event for strategy 'nt': More than 100 subsequent user events
            Why is that? I thought the OnBarUpdate() function should only start the timer once and then let it work until the strategy is turned off. Instead, the strategy seems to execute myTimer.Start() many times on the first bar update.
            Last edited by alkamid; 02-25-2010, 02:16 AM.

            Comment


              #7
              alkamid, is this for NT 6.5 or 7?

              Thanks
              BertrandNinjaTrader Customer Service

              Comment


                #8
                It's NT 6.5

                Comment


                  #9
                  Hi alkamid, thanks for the strategy you had the correct overload in place, it needs to be the 3 parameter one...

                  Code:
                   
                  TriggerCustomEvent(CustomEvent customEvent, int barsInProgress, object state)
                  You also want to initiate your timer on the first OnBarUpdate() bar, like in the sample ... i.e. -

                  if (CurrentBar == 0)
                  {
                  initiate timer
                  }

                  This should take care of the too many events in the log entry...
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    OK, I changed my code to:
                    Code:
                            protected override void OnBarUpdate()
                            {
                    						
                    			if(CurrentBar == 0)
                    			{
                    			// Initiate our Timer object with an interval of 1000ms (1 second)
                    			myTimer.Tick += new EventHandler(TimerEventProcessor);
                    			myTimer.Interval = 10000;
                    			myTimer.Start();
                    			}
                            }
                    
                    		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, BarsInProgress, myTimer.Interval);
                    		}
                    Now there's no error in logs but I can't see any output either. Is there anything particular I have to put in Initialize() to make it work?

                    Comment


                      #11
                      Hi,

                      Bertrand is out for the rest of the day and will respond to your issue tomorrow morning.
                      Vince B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello,

                        I guess this topic has been lost in Bertrand's to-do list so I kindly ask him to look here once more :-)

                        Comment


                          #13
                          alkamid, thanks for ringing my bell on this - what event(s) are you then rising in your MyCustomHandler method?
                          BertrandNinjaTrader Customer Service

                          Comment


                            #14
                            Code:
                            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");
                            }
                            Only this for testing.

                            Comment


                              #15
                              alkamid,

                              Here is the deal. Since you are using a strategy and not an indicator you cannot use if (CurrentBar == 0) since the strategy doesn't process anything until BarsRequired is met. Instead you should do

                              if (CurrentBar == 20) or if (CurrentBar == BarsRequired). Default BarsRequired is 20.
                              Josh P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by cmtjoancolmenero, Yesterday, 03:58 PM
                              2 responses
                              19 views
                              0 likes
                              Last Post cmtjoancolmenero  
                              Started by Stanfillirenfro, 04-22-2024, 09:19 AM
                              7 responses
                              59 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by olisav57, Yesterday, 07:39 PM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by cocoescala, 10-12-2018, 11:02 PM
                              7 responses
                              942 views
                              0 likes
                              Last Post Jquiroz1975  
                              Started by oviejo, Today, 12:28 AM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X