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

Multiple Timers and TriggerCustomEvent?

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

    Multiple Timers and TriggerCustomEvent?

    I recently stumbled upon the timer functionality, and it is helped simplify some components of my strategies.

    Is if possible to have multiple timers? I want to run one timer fast - something on the order of 10 seconds, or so. The second timer I want to run slow - several hours.

    I can kick off one timer fine. But, how do I use a second timer. Declaring and starting the second timer is not the problem. The problem is how do I mediate TriggerCustomEvent? How do I put a switch or it/then statement in Trigger CustomEvent?

    In pseudocode, I want to do this:

    private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
    {
    if (myBool)
    {
    TriggerCustomEvent(myFastTimerEvent, 0, fastTimer.Internal);
    }
    else
    {
    TriggerCustomEvent(mySlowTimerEvent, 0, slowTimer.Internal);
    }

    It is probably clear I am not understanding the timer mechanism in NinjaTrader.

    Suggestions?

    Thanks!

    #2
    Hello,

    Thank you for the question.

    It is possible to have multiple timers in NinjaTrader running at once. If you would like to post the actual code you have for the timer you have now, I can see what you are doing and advise what you would need to update to make multiple timers.

    To make it simple, you would essentially just duplicate the code that you are using now with a different naming convention to make it unique.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Jesse,

      Thanks for the reply. Attached is the essence of what I am trying to do. I've removed at lot of the non-relevant code in the strategy (I'd rather not share the entirety, and it is fairly long and complex). I don't think it will compile as is.

      I want to run two timer events that are not related, and each timer has a different period. I'm confused on how to mediate TriggerCustomEvent in Ninjatrader. The attached code has TriggerCustomEvent duplicated.

      Thanks!
      Attached Files

      Comment


        #4
        Hello,

        So looking at the code it seems you have two timers already defined but they are pointing to the same event handler.

        Rather than doing what you are now, I will provide a simple example that shows two seperate timers.

        You can then choose which timer to enable or disable, you can also set the interval at any time you want based on a boolean value.

        I would recommend just creating both of the timers in your OnStartUp but only enable the timers that you need and set the interval to what you would like based on the bool value in either a timers tick event handler or in OnBarUpdate.


        Code:
        private System.Windows.Forms.Timer limitOrderTimer = new System.Windows.Forms.Timer();
        private System.Windows.Forms.Timer selfOptTimer = new System.Windows.Forms.Timer();
        private bool someBool = false;
        
        
        
        protected override void OnStartUp()
        {
             limitOrderTimer.Tick += TimerEventProcessor;
             limitOrderTimer.Interval = 5000;
        
             selfOptTimer.Tick += TimerEventProcessor1;
             selfOptTimer.Interval = 1000;
        }
        
        protected override void OnBarUpdate()
        {
        	if (someBool == true)
        	{
        	//to switch the interval for a timer based on a bool you can actually do that at any time
        	
        	selfOptTimer.Stop();
        	selfOptTimer.Interval = 10000;
        	selfOptTimer.Start();
        	}
        }
        
        private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
        {
        	try
        	{
        		//TriggerCustomEvent(.....
        	}
        	catch (Exception ex)
        	{
        		Print("Fatal error in TimerEventProcessor: " + ex.Message + ".");
        	}
        }
        
        private void TimerEventProcessor1(Object myObject, EventArgs myEventArgs)
        {
        	try
        	{
        		//TriggerCustomEvent(.....
        	}
        	catch (Exception ex)
        	{
        		Print("Fatal error in TimerEventProcessor: " + ex.Message + ".");
        	}
        }
        This would result in two totally separate timers, two event handlers for the Tick of the timer and the ability to change the timers Interval at any time, just remember to stop the timer and then start it again.

        Also you will need to initially start the timers which I did not include in the above example, you can do this at any time by calling the timers variable name .Start();

        Also please ensure to end the timers when the script ends otherwise they will keep going!

        Code:
        protected override void OnTermination()
        {
        	selfOptTimer.Stop();
        	selfOptTimer.Tick -= TimerEventProcessor1;
        	limitOrderTimer.Stop();
        	limitOrderTimer.Tick -= TimerEventProcessor;
        }
        This is called at the end of the script and will stop both timers and remove the handler from memory.

        Finally I would recommend re working the ManagerOrders you were using into two separate methods, one for each TriggerCustomEvent call to make things less confusing.
        Last edited by NinjaTrader_Jesse; 03-03-2015, 04:24 PM.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Jesse,

          Completely clear now. Somehow I hadn't got the connection with adding the new EventHandler with my own function. Got it.

          Thanks for your help.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by inanazsocial, Today, 01:15 AM
          0 responses
          2 views
          0 likes
          Last Post inanazsocial  
          Started by trilliantrader, 04-18-2024, 08:16 AM
          5 responses
          22 views
          0 likes
          Last Post trilliantrader  
          Started by Davidtowleii, Today, 12:15 AM
          0 responses
          3 views
          0 likes
          Last Post Davidtowleii  
          Started by guillembm, Yesterday, 11:25 AM
          2 responses
          9 views
          0 likes
          Last Post guillembm  
          Started by junkone, 04-21-2024, 07:17 AM
          9 responses
          71 views
          0 likes
          Last Post jeronymite  
          Working...
          X