• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time filter query

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

    Time filter query

    Hi guys,

    I have been running this strategy for some time now and i noticed something strange I can't explain, your advise is very welcome.

    Basically I have a time filter, I want the strategy only to take long/short entries between 6am Monday and 6pm Friday and clear all positions at 6pm on Friday, in order to not carry risk over the week-end. Below is the code i am using:
    Code:
            protected override void OnBarUpdate()
            {
    			//Enter long
    			if (CrossAbove(FisherTransform(period).Value,0,1)
    				&& tradeok == 1)
    			{
    				EnterLong();
    			}
    			
    			//Enter short
                if (CrossBelow(FisherTransform(period).Value,0,1)
    				&& tradeok == 1)
    			{
    				EnterShort();
    			}
    			
    			//Time filter
    			else
    			if (Time[0].DayOfWeek == DayOfWeek.Sunday)
    			{
    				tradeok = 0;
    			}
    			
    			else
    			if (Time[0].DayOfWeek == DayOfWeek.Monday
    				&& ToTime(Time[0]) < ToTime(6, 0, 0))
    			{
    				tradeok = 0;
    			}
    			
    			else
    			if (Time[0].DayOfWeek == DayOfWeek.Friday
    				&& ToTime(Time[0]) >= ToTime(16, 0, 0))
    			{
    				tradeok = 0;
    			}	
    			
    			else
    			{
    			tradeok = 1;
    			}
    			
            }
    What is strange, is that if I remove the else statement in-front of if, basically if I use "if" rather than "else if" the strategy doesn't work properly. Could somebody explain me why? and moreover is there a better way to code what a time filter?
    Last edited by sburtt; 05-11-2013, 01:37 PM.

    #2
    Hello sburtt,

    Thank you for your post.

    I would consolidate your time filter into one if statement and then just else for the tradeok = 1, also make the Time Filter the first condition to check:
    Code:
    //Time filter
    			
    			if (Time[0].DayOfWeek == DayOfWeek.Sunday || Time[0].DayOfWeek == DayOfWeek.Monday
    				&& ToTime(Time[0]) < ToTime(6, 0, 0) || Time[0].DayOfWeek == DayOfWeek.Friday
    				&& ToTime(Time[0]) >= ToTime(16, 0, 0))
    			{
    				tradeok = 0;
    			}			
    			else
    			{
    			tradeok = 1;
    			}
    
    //Enter long
    			if (CrossAbove(FisherTransform(period).Value,0,1)
    				&& tradeok == 1)
    			{
    				EnterLong();
    			}
    			
    			//Enter short
                if (CrossBelow(FisherTransform(period).Value,0,1)
    				&& tradeok == 1)
    			{
    				EnterShort();
    			}
    Please let me know if you have any questions.
    Patrick H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_PatrickH View Post
      Hello sburtt,

      Thank you for your post.

      I would consolidate your time filter into one if statement and then just else for the tradeok = 1, also make the Time Filter the first condition to check:
      Code:
      //Time filter
      			
      			if (Time[0].DayOfWeek == DayOfWeek.Sunday || Time[0].DayOfWeek == DayOfWeek.Monday
      				&& ToTime(Time[0]) < ToTime(6, 0, 0) || Time[0].DayOfWeek == DayOfWeek.Friday
      				&& ToTime(Time[0]) >= ToTime(16, 0, 0))
      			{
      				tradeok = 0;
      			}			
      			else
      			{
      			tradeok = 1;
      			}
      
      //Enter long
      			if (CrossAbove(FisherTransform(period).Value,0,1)
      				&& tradeok == 1)
      			{
      				EnterLong();
      			}
      			
      			//Enter short
                  if (CrossBelow(FisherTransform(period).Value,0,1)
      				&& tradeok == 1)
      			{
      				EnterShort();
      			}
      Please let me know if you have any questions.
      Patrick, i've been working on it over the week end and arrived at the same conclusion. Also I noticed that if i use else if or if on the code you presented results don't change. thanks

      Comment


        #4
        Originally posted by NinjaTrader_PatrickH View Post
        Hello sburtt,

        Thank you for your post.

        I would consolidate your time filter into one if statement and then just else for the tradeok = 1, also make the Time Filter the first condition to check:
        Code:
        //Time filter
        			
        			if (Time[0].DayOfWeek == DayOfWeek.Sunday || Time[0].DayOfWeek == DayOfWeek.Monday
        				&& ToTime(Time[0]) < ToTime(6, 0, 0) || Time[0].DayOfWeek == DayOfWeek.Friday
        				&& ToTime(Time[0]) >= ToTime(16, 0, 0))
        			{
        				tradeok = 0;
        			}			
        			else
        			{
        			tradeok = 1;
        			}
        
        //Enter long
        			if (CrossAbove(FisherTransform(period).Value,0,1)
        				&& tradeok == 1)
        			{
        				EnterLong();
        			}
        			
        			//Enter short
                    if (CrossBelow(FisherTransform(period).Value,0,1)
        				&& tradeok == 1)
        			{
        				EnterShort();
        			}
        Please let me know if you have any questions.
        Patrick, does it matter if i leave the time filter at the bottom of my strategy, or should it be on top? is there any difference? if yes, pls could you explain me why?

        Thanks,

        Comment


          #5
          Hello sburtt,

          Thank you for your response.

          The code is processed logically. Why check for entry conditions when you have not first checked the condition that is key to entering or not entering a position?
          It may work either way, but logically it makes sense to check your time filter first.

          You can also try checking is the time filter condition is true and returning the OnBarUpdate() method if it is so that nothing further processes. Then process your entries afterwards, and they should only process if the time filter is not true:
          Code:
          //Time filter
          			
          			if (Time[0].DayOfWeek == DayOfWeek.Sunday || Time[0].DayOfWeek == DayOfWeek.Monday
          				&& ToTime(Time[0]) < ToTime(6, 0, 0) || Time[0].DayOfWeek == DayOfWeek.Friday
          				&& ToTime(Time[0]) >= ToTime(16, 0, 0))
          			return; //This will return the OnBarUpdate without processing anything further in the code if the above condition is true.
          
          			//Enter long
          			if (CrossAbove(FisherTransform(period).Value,0,1))
          			{
          				EnterLong();
          			}
          			
          			//Enter short
                                  if (CrossBelow(FisherTransform(period).Value,0,1))
          			{
          				EnterShort();
          			}
          Has adjusting your strategy's code resolved the code not processing as expected?
          Patrick H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by StealthM93, Today, 07:58 PM
          0 responses
          2 views
          0 likes
          Last Post StealthM93  
          Started by Mykro, Today, 11:01 AM
          1 response
          5 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by bauerjj10, Today, 12:24 PM
          1 response
          14 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by Newtrader101, Today, 06:01 PM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_EricB  
          Started by Steve L, Today, 03:50 PM
          1 response
          9 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X