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 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();
    			}
    			
    			[B]//Time filter
    			else
    			if[/B] (Time[0].DayOfWeek == DayOfWeek.Sunday)
    			{
    				tradeok = 0;
    			}
    			
    			[B]else
    			if[/B] (Time[0].DayOfWeek == DayOfWeek.Monday
    				&& ToTime(Time[0]) < ToTime(6, 0, 0))
    			{
    				tradeok = 0;
    			}
    			
    			[B]else
    			if[/B] (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.

    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))
          			[B][U]return; //This will return the OnBarUpdate without processing anything further in the code if the above condition is true.[/U][/B]
          
          			//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?

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by tkaboris, Today, 08:01 AM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by Lumbeezl, 01-11-2022, 06:50 PM
          31 responses
          817 views
          1 like
          Last Post NinjaTrader_Adrian  
          Started by xiinteractive, 04-09-2024, 08:08 AM
          5 responses
          15 views
          0 likes
          Last Post NinjaTrader_Erick  
          Started by swestendorf, Today, 11:14 AM
          2 responses
          6 views
          0 likes
          Last Post NinjaTrader_Kimberly  
          Started by Mupulen, Today, 11:26 AM
          0 responses
          7 views
          0 likes
          Last Post Mupulen
          by Mupulen
           
          Working...
          X