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

Stop strategy at a specific time

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

    Stop strategy at a specific time

    Hello all,

    I was wondering how I can make my strategy stop at a specific time of day given the following parameters my strategy is using too:

    - My strategy runs on a chart with Renko bars only. So there is no given time period on a bar close.
    - My settings are "update on bar close".
    - It runs every day, from Monday till Friday.

    So question is: How can I make my strategy stop and close all positions and orders at a specific time reliably, (For instance, 9:30PM), and start up again the next day at the beginning of the day (For instance, 09:00AM)?


    Thanks in advance!
    Dennis

    #2
    Hello Dennis, and thank you for your question.

    Here is a modified version of some code from the help guide which should accomplish your goal. Please let us know if there are any other ways we can help.




    Code:
    [FONT=Courier New]protected override void OnBarUpdate()
    {
        // close all trades at 09:30 PM
        if (ToTime(Time[0]) >= 213000)
        {
            // [/FONT][FONT=Courier New]If you do not specify a quantity the entire position is exited rendering your strategy flat[/FONT][FONT=Courier New][FONT=Courier New]
            // This method is ignored if a short position does not exist
    [/FONT]        ExitShort();
    
    [/FONT][FONT=Courier New][FONT=Courier New]        // This method is ignored if a long position does not exist
    [/FONT]        ExitLong();
    
            return;
        }
    
        // we can't trade before 9 AM
        if (ToTime(Time[0]) < 90000)
        {
            return;
        }
    
        // the rest of your OnBarUpdate code here
    }
    [/FONT]
    Last edited by NinjaTrader_JessicaP; 09-30-2016, 12:08 PM.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Okay, thanks for your reply and help.

      What happens if the market doesn't move enough anymore to print another renko bar from 8PM till 10PM for example and I'm still in a position?
      Cause there will be no more 'OnBarUpdate' and I do want to close the position at 9:30PM

      Comment


        #4
        In this case you will want to examine and adopt some of the custom event trigger code available from this link,



        We are happy to help should any questions come up implementing any information from that link.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Hello Jessica,

          Thanks for the info. I don't fully understand the logic how to set this up. I've tried some of it and came up with the following:

          Code:
          public class Test2: Strategy											
          	{											
          		private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
          						
          		protected override void OnStateChange()										
          		{										
          			myTimer.Interval = 1000;
          		}										
          				
          		private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
          		{
          			TriggerCustomEvent(MyCustomHandler, 0, myTimer.Interval);
          		}
          		
          		private void MyCustomHandler(object state)
          		{
           			if(ToTime(Time[0]) > 213000)
          			{
          				ExitLong();
          				ExitShort();
          			}
          		}
          	}
          It doesn't work yet but this is what i've got so far.

          Comment


            #6
            Hello Aqua-Life,

            I noticed you are missing any hook methods NinjaTrader knows about. Because of this fact, NinjaTrader has no entry point into your class and, as such, no way to trigger anything within it including your timer. I would like to suggest avoiding timers if at all possible. NinjaTrader is truly event driven, and so every price change will cause NinjaTrader's hook methods to activate. If you need a timer so that something is absolutely guaranteed to happen regularly, it should still be in addition to an OnBarUpdate method.

            There is some example code in this thread which can help you get started with implementing a 1 second timer should you decide OnBarUpdate's ability to trigger every price update is not suitable to your trading style.

            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              Thanks, i'll look into it

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by JonesJoker, 04-22-2024, 12:23 PM
              7 responses
              40 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by Waxavi, 04-19-2024, 02:10 AM
              2 responses
              37 views
              0 likes
              Last Post poeds
              by poeds
               
              Started by chbruno, Yesterday, 04:10 PM
              1 response
              44 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by Max238, Today, 01:28 AM
              1 response
              24 views
              0 likes
              Last Post CactusMan  
              Started by giulyko00, Yesterday, 12:03 PM
              2 responses
              10 views
              0 likes
              Last Post giulyko00  
              Working...
              X