Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time Filter Template

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

    Time Filter Template

    I found a SampleTimeFilter template in the Ninjatrader help guide which I've attached for reference. I would like to use the 24hr format that it's currently in, but would like to be able to modify the time when I select the strategy. Below is a code of what I've modified so far. Would this be the correct way?


    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    StartTradingTime = 083000;
    EndTradingTime = 150000;
    }
    }

    protected override void OnBarUpdate()
    {
    if (ToTime(Time[0]) >= StartTradingTime && ToTime(Time[0]) < EndTradingTime)
    {
    Logic
    }
    }

    #region Properties
    [NinjaScriptProperty]
    [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKe y")]
    [Display(Name="StartTradingTime", Order=1, GroupName="TradingTime")]
    public DateTime StartTradingTime
    { get; set; }

    [NinjaScriptProperty]
    [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKe y")]
    [Display(Name="EndTradingTime", Order=2, GroupName="TradingTime")]
    public DateTime EndTradingTime
    { get; set; }


    Attached Files

    #2
    Hello AgriTrdr,

    If you wanted to maintain the same format of using 083000 then the public properties should be integers.

    Code:
    [NinjaScriptProperty]
    [Display(Name="StartTradingTime", Order=1, GroupName="TradingTime")]
    public int StartTradingTime
    { get; set; }
    
    [NinjaScriptProperty]
    [Display(Name="EndTradingTime", Order=1, GroupName="TradingTime")]
    public int EndTradingTime
    { get; set; }

    Comment


      #3
      Thanks for your reply. Does there need to be anything for the Range?

      #region Properties
      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(Name="StartTradingTime", Order=1, GroupName="TradingTime")]
      public int StartTradingTime
      { get; set; }

      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(Name="EndTradingTime", Order=2, GroupName="TradingTime")]
      public int EndTradingTime
      { get; set; }

      Comment


        #4
        Hello AgriTrdr,

        No you would not need the range attribute for this.

        Comment


          #5
          If there's a secondary data series in the strategy, would the below be the correct way for the if condition or do I leave it as I had initially?

          if (ToTime(Times[0][0]) >= StartTradingTime && ToTime(Times[0][0]) < EndTradingTime)

          Comment


            #6
            Hello AgriTrdr,

            This code is using Times[0][0] meaning the primary series times. If you wanted to check the time for both series that depends if this code is within a BarsInProgress condition.

            If there are no BarsInProgress conditions you can use Time[0] to reference whichever series has called OnBarUpdate.

            Code:
            protected override void OnBarUpdate()
            {
            
            
            if (ToTime(Time[0]) >= StartTradingTime && ToTime(Time[0]) < EndTradingTime)

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by CarlTrading, 03-31-2026, 09:41 PM
            1 response
            43 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            21 views
            0 likes
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            30 views
            1 like
            Last Post CaptainJack  
            Started by CarlTrading, 03-30-2026, 11:51 AM
            0 responses
            50 views
            0 likes
            Last Post CarlTrading  
            Started by CarlTrading, 03-30-2026, 11:48 AM
            0 responses
            41 views
            0 likes
            Last Post CarlTrading  
            Working...
            X