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

Time Filter is not working if going into the next day

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

    Time Filter is not working if going into the next day

    HI when i use filter intradday, that is on the same day it works but if I select dates, such as 1800 to 0900, in backtesting it doesnt open trades. It probably wont open trades either. Do you have any sugggestions? or example?


    Start = DateTime.Parse("09:30", System.Globalization.CultureInfo.InvariantCulture) ;
    End = DateTime.Parse("16:00", System.Globalization.CultureInfo.InvariantCulture) ;​

    Onbarupdate
    if ( ((Time[1].TimeOfDay >= Start.TimeOfDay) && (Time[1].TimeOfDay <= End.TimeOfDay))





    #2
    Hello, thanks for writing in. I attached an example of a multi day time filter that I have made in the past.

    Kind regards,
    -ChrisL
    Attached Files
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      You have this logic

      ((Times[0][0].TimeOfDay > new TimeSpan(23, 0, 0))
      || (Times[0][0].TimeOfDay < new TimeSpan(13, 0, 0))))

      Do i keep my default settings ?
      Start = DateTime.Parse("09:30", System.Globalization.CultureInfo.InvariantCulture) ;
      End = DateTime.Parse("16:00", System.Globalization.CultureInfo.InvariantCulture) ;​​

      Comment


        #4
        Hi, you can use the TimeOfDay property to access the time portion of the DateTime object Start and End:

        DateTime Start = DateTime.Parse("09:30", System.Globalization.CultureInfo.InvariantCulture) ;
        Print(Start.TimeOfDay);​

        In the If statement it would be:

        ((Times[0][0].TimeOfDay > Start.TimeOfDay)
        || (Times[0][0].TimeOfDay < End.TimeOfDay)))​
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          thank you but it seems not working as I am getting trades still open after specified time in settings. I set End to 11:50 but it still opened trades. Is there another tweak?

          Start = DateTime.Parse("09:30", System.Globalization.CultureInfo.InvariantCulture) ;
          End = DateTime.Parse("16:00", System.Globalization.CultureInfo.InvariantCulture) ;​

          if

          ((Times[0][0].TimeOfDay > Start.TimeOfDay)
          || (Times[0][0].TimeOfDay < End.TimeOfDay))​

          Comment


            #6
            Hi, I am not certain why it will not work in your script. My example is working, please import this into your installation to see it working. To debug your script you can add Print() methods in side of the condition to see why its triggering e.g.

            if((Times[0][0].TimeOfDay > Start.TimeOfDay)
            || (Times[0][0].TimeOfDay < End.TimeOfDay))
            {
            Print("Current Time" + Times[0][0].TimeOfDay);
            Print("End Time" + End.TimeOfDay);
            Print("Start Time" + Start.TimeOfDay);
            }​​
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Thats ok. When I do this it messes up my other code and have errors such as enum class and statement expected. However if i remove {Print} there is no compilation. Maybe to fix it is beyond me but I will try to figure it out

              if
              ((Times[0][0].TimeOfDay > Start.TimeOfDay)
              || (Times[0][0].TimeOfDay < End.TimeOfDay))
              {
              Print("Current Time" + Times[0][0].TimeOfDay);
              Print("End Time" + End.TimeOfDay);
              Print("Start Time" + Start.TimeOfDay);
              }
              && Position.MarketPosition == MarketPosition.Flat && CurrentBar > BarsRequiredToTrade
              && (dailyPnL > -DailyLossLimit)
              && (dailyPnL < DailyProfitLimit)
              )​

              Comment


                #8
                I have these two Date Timem in red like enum and something else expected

                [NinjaScriptProperty]
                [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKe y")]
                [Display(Name="New Start Time", Order=0, GroupName="Time Management")]
                public DateTime Start
                { get; set; }

                [NinjaScriptProperty]
                [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKe y")]
                [Display(Name="New End Time", Order=1, GroupName="Time Management")]
                public DateTime End
                { get; set; }​

                Comment


                  #9
                  Hello, thanks for the follow up. I will need to see a screen shot of the code and the error message you are getting. I will be happy to point out the issue if I am able to.
                  Chris L.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by tkaboris View Post
                    HI when i use filter intradday, that is on the same day it works but if I select dates, such as 1800 to 0900, in backtesting it doesnt open trades. It probably wont open trades either. Do you have any sugggestions? or example?
                    Try looking at this example.

                    It correctly handles times that span midnight.

                    Comment


                      #11
                      I have this time filter buy its not doing any filtering. The main issue i have is to have time filter for multiple days, like start time 1800 - end time 300AM, and visa versa. I uploaded strategy if you can helpt that woudl be great.

                      Start = DateTime.Parse("09:30", System.Globalization.CultureInfo.InvariantCulture) ;
                      End = DateTime.Parse("16:00", System.Globalization.CultureInfo.InvariantCulture) ;​

                      protected override void OnBarUpdate()
                      {
                      if(Bars.IsFirstBarOfSession)
                      currentPnl=0;

                      if (CurrentBar < Math.Max(BarsRequiredToTrade, BarsBefore))

                      return;

                      if((Times[0][0].TimeOfDay > Start.TimeOfDay)
                      || (Times[0][0].TimeOfDay < End.TimeOfDay)

                      Comment


                        #12
                        Hi, thanks for the follow up. The first thing you should do to debug your script is to add prints to see why the strategy is not filtering by your desired time range.

                        Chris L.NinjaTrader Customer Service

                        Comment


                          #13
                          I wanted to debug by putting that Print statement but its giving me compile errors.
                          I provide screenshot

                          HTML Code:
                          protected override void OnBarUpdate()
                                  {
                                      if(Bars.IsFirstBarOfSession)
                                          currentPnl=0;
                          
                                      if (CurrentBar < Math.Max(BarsRequiredToTrade, BarsBefore))            
                                          return;
                          
                          //            if ((Time[1].TimeOfDay >= Start.TimeOfDay) && (Time[1].TimeOfDay <= End.TimeOfDay) //Trade is taking place within specified time
                          //            if((Times[0][0].TimeOfDay > new TimeSpan(23, 0, 0))
                          //                 || (Times[0][0].TimeOfDay < new TimeSpan(13, 0, 0))    
                                          if((Times[0][0].TimeOfDay > Start.TimeOfDay)
                                          || (Times[0][0].TimeOfDay < End.TimeOfDay))
                                          {
                                          Print("Current Time" + Times[0][0].TimeOfDay);
                                          Print("End Time" + End.TimeOfDay);
                                          Print("Start Time" + Start.TimeOfDay);
                                          }​​
                          
                                          //ToTime(Time[0]) >= StartTradeTime * 100
                                          //&& ToTime(Time[0]) < StopTradeTime * 100
                                          && Position.MarketPosition == MarketPosition.Flat && CurrentBar  > BarsRequiredToTrade
                          
                                          && (dailyPnL > -DailyLossLimit) //Loss remains 'above' limit
                                          && (dailyPnL < DailyProfitLimit) //Profit remains 'below' limit
                                          )
                                      {
                                          double emaAngulation = fastEMA[0] - slowEMA[0];
                                          double emaAngulation34R = fastEMA[0] - slowEMA[0];
                                          double emaAngulation8B = Math.Abs(fastEMA[0] - slowEMA[0]);
                                          double angulationSwing = Math.Abs(fastEMA[0] - slowEMA[0]); //5
                                          double dist200ToPrice = Math.Abs(EMA(200)[0] - Close[0]);
                          
                          
                                          double emaAngulationXBarsBefore = fastEMA[BarsBefore] - slowEMA[BarsBefore];    
                                          double MarginToEMAL = Math.Abs(Low[0] - slowEMA[0]);
                                          Print(string.Format("{0} | DistTo200EMA: {1} >= dist200ToPrice * TickSize: {2}", Time[0], DistTo200EMA, dist200ToPrice * TickSize));
                                          ​

                          Comment


                            #14
                            Hi, thanks for the follow up. You are placing the running code (Print) in between a condition set. Your syntax is not correct. Code can not be placed randomly in between your If conditions in a C# script. If statements in C# need to have their boolean conditions first, then the running code e.g.

                            Code:
                            if(<collection of boolean statements>)
                            {
                                //running code here. 
                            }
                            ​
                            Chris L.NinjaTrader Customer Service

                            Comment


                              #15
                              I am looking at the example of TestTimeFilters.cs and i see in that example times are hardcoded and
                              ((Times[0][0].TimeOfDay > new TimeSpan(23, 0, 0))
                              || (Times[0][0].TimeOfDay < new TimeSpan(13, 0, 0))))​

                              Again my problem is that i need multi day filter, so i can enter 2100pm start time and 0400am endtime.
                              Even if I put these two lines filter still not working
                              Startime = DateTime.Parse("19:53", System.Globalization.CultureInfo.InvariantCulture) ;
                              Endtime = DateTime.Parse("19:53", System.Globalization.CultureInfo.InvariantCulture) ;​

                              Can someoneprovide a working multi day filter?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by CortexZenUSA, Today, 12:53 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post CortexZenUSA  
                              Started by CortexZenUSA, Today, 12:46 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post CortexZenUSA  
                              Started by usazencortex, Today, 12:43 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post usazencortex  
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              168 responses
                              2,266 views
                              0 likes
                              Last Post sidlercom80  
                              Started by Barry Milan, Yesterday, 10:35 PM
                              3 responses
                              13 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Working...
                              X