Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding Start and Stop Time

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

    Adding Start and Stop Time

    Hello Support,

    I am adding Start and Stop time of my Strategy. I have the parameters done, but on the dialog box it is showing as Date and Time. I would like to have just the time.
    Please let me know how this can be done?
    If you have a sample code, it would be great.

    Thanks.

    Regards,
    EdwardK.

    #2
    Hi Edward Kunafi,

    Can you provide the code snippet so that I can see how you are creating the time filter?
    TimNinjaTrader Customer Service

    Comment


      #3
      Hello Tim,

      I have not gone into that portion. Just getting the StartTime and StopTime parameters ready. I can imagine it is not straightforward, as I need to take care of different time zone. Anyway, in short logic wise it would be something like:

      Add into OnBarUpdate:
      If (Time.now => Time.Start && Time.now <= Time.Stop) tradeEn = true;
      else tradeEn = false;

      Then each of my EnterLong, EnterShort, EnterLongLimit or EnterShortLimit will be qualified with tradeEn, but all other strategy logic is running as usual.

      Current positions will still be monitored till positions are nil. Then no more exits are taken.

      I have not figured out the Time Comparisons above to take care different timezone as StopTime can be earlier than StartTime. So if you have that code snippet it would be great.

      Thanks.

      Regards.
      EdwardK.


      Thanks.
      Regards.

      EdwardK.

      Comment


        #4
        Hi Edward Kunafi,

        Would you please first take a look at this sample to see if it covers all the things you are seeking to do:
        TimNinjaTrader Customer Service

        Comment


          #5
          No guarantees that this is the best way to do it but it seems to work for me. All times are local time zone in NT. I wanted not only start/stop times, but a duration limit (easier to optimize if one so chooses). I wanted the strategy analyzer and my real-time charts to include pre-market for the correct indicator calculations, but only trade within active sessions, and a duration within that.

          Variables:
          private bool useTimeStop = true;
          private int durMinutes = 120; // trade window in minutes
          private DateTime startTime, endTime;

          private bool ok2Trade = false;
          Initialize()
          startTime = new DateTime(0001, 1, 1, 5, 30, 00); // ignore the date portion. It is unused
          endTime = new DateTime(0001, 1, 1, 12, 59, 00);

          OnBarUpdate()
          Code:
          if  (Time[0].DayOfWeek != DayOfWeek.Saturday && Time[0].DayOfWeek != DayOfWeek.Sunday) 
          {
            if (!useTimeStop) ok2Trade=true;
                            // Checks to see if the time is during the following hours. NT always uses Local timezone.  
                            // The duration is also checked - whichever end time is reached first stops the trading.             
                            // else if (ToTime(Time[0]) >= startTime && ToTime(Time[0]) <= endTime) ok2Trade=true;
                            else if ( (Time[0]) >= startTime && (Time[0]) <= endTime && Time[0]<= startTime.AddMinutes(durMinutes) ) ok2Trade=true;
                            else ok2Trade=false;
          }
          if (!ok2Trade)    // Halt further processing of our strategy
          {
                            // This will ensure we do not have an unmanaged position left after we halt our strategy. 
                            StopStrategy();  
                            DrawTextFixed("Status", "NOT ok to trade", TextPosition.TopRight);
                            if (Position.MarketPosition==MarketPosition.Flat) return;
          }

          StopStrategy()
          private void StopStrategy()
          {
          // If our Limit order is still active we will need to cancel it.
          if (entryOrderL !=null) CancelOrder(entryOrderL); if (entryOrderS !=null) CancelOrder(entryOrderS);

          // If we want a hard stop to flatten all the positions, use this
          //if (Position.MarketPosition == MarketPosition.Long) ExitLong();
          //else if (Position.MarketPosition == MarketPosition.Short) ExitShort();
          }
          Input Properties
          Code:
          [Description("Set True to narrow trading to specific hours within a session")]
                  [Category("Parameters")]
                  [Gui.Design.DisplayName("03 a. Use Time Stop?")]
                  public bool UseTimeStop
                  {
                      get { return useTimeStop; }
                      set { useTimeStop = value; }
                  } 
                  [Description("Time to start trading (if you want to trade within a window or subset of the overall session). Use Military time (00-24)")]
                  [Category("Parameters")]
                  [Gui.Design.DisplayName("03 b. Start Time")]
                  public string StartTime
                  {
                      get { return startTime.ToString("HH:mm:ss"); } // "hh:mm:ss tt"
                      set { startTime = Convert.ToDateTime(value); }
                  }
                  [Description("Time to stop trading (if you want to trade within a window or subset of the overall session). Use Military time (00-24)")]
                  [Category("Parameters")]
                  [Gui.Design.DisplayName("03 c. End Time")]
                  public string EndTime
                  {
                      get { return endTime.ToString("HH:mm:ss"); } // "hh:mm:ss tt"  for AM/PM visual
                      set { endTime = Convert.ToDateTime(value); }
                  }
                  [Description("Length of Time Window in minutes. Can be used to shorten window")]
                  [Category("Parameters")]
                  [Gui.Design.DisplayName("03 d. Time Duration")]
                  public int TimeLength
                  {
                      get { return durMinutes; }
                      set { durMinutes = Math.Max(10, value); }
                  }

          Comment


            #6
            Lost Trader, thank you for the sample code here. I'm sure the community members will appreciate it.
            AustinNinjaTrader Customer Service

            Comment


              #7
              Hello Lost Trader,

              Thank you very much for posting your solution. I am sure I can adapt the codes for my purpose.

              Thanks.

              Regards,

              EdwardK

              PS: Thanks also to Tim for the SampleTimeFilter

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              656 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              371 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              109 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              574 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              579 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X