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

> x but less than y for a z seconds

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

    > x but less than y for a z seconds

    Hi folks:

    This is just a "sample indicator" example code condition, but I would like to do the following. I am wanting it to execute only if the sample indicator is greater than > 1 BUT less < 2 IF the condition lasts for more than X seconds (say X is like 30 seconds as an example). How would I condition for the X seconds? I think the first two for greater than BUT less than is ok...just need to include the seconds component (or some equivalent that would work well) and have it reset each time the condition goes back below >1 or above <2 each time.

    ---
    && sample indicator[0] > 1
    && sample indicator[0] < 2
    && the two above sample indicator conditions last for greater than x seconds (say 30 seconds for this example).

    I would want to be sure it resets the seconds counter each time the condition goes outside or below 1 and /or outside or above 2 so if it comes back down it the range the seconds counter would start over.
    ---

    The idea is that I want to exit if the condition is sustain for some time, BUT if there is a move past or beyond these conditions in a fast moving market, I don't want it to execute a exit while in between these conditions (rather I want it to keep me in longer to benefit from the faster moving market). Then, I might have a different condition to get me out at a better level (separate condition from the above).

    Do you have some thoughts on how I can achieve in the code and where to put it. I am sure some kind of seconds counter variable I would have to add too etc...

    Last edited by birdog; 11-17-2013, 10:28 AM.

    #2
    Hello birdog,

    Thank you for your post.

    You could use Add() to add in a 1 second period type, and then calculate the X based on this bar series, resetting when appropriate.

    If you want to create a custom timer you can view an example by going to Tools > Edit NinjaScript > Indicator > BarTimer.

    Please let me know if I may be of further assistance.
    Last edited by NinjaTrader_PatrickH; 11-17-2013, 11:11 PM.

    Comment


      #3
      Originally posted by NinjaTrader_PatrickH View Post
      Hello birdog,

      Thank you for your post.

      You could use Add() to add in a 1 second period type, and then calculate the X based on this bar series, resetting when appropriate.

      if you want to create a custom timer you can view an example by going to Tools > Edit NinjaScript > Indicator > BarTimer.

      Please let me know if I may be of further assistance.
      Thanks...smile...

      @patrickH could you post the code I would use to achieve it? I am a little confused on how to approach it. I looked at the bar timer...does not help...smile...sorry...
      Last edited by birdog; 11-17-2013, 08:14 PM.

      Comment


        #4
        Or, anyone can reply if they know...

        Comment


          #5
          Hello birdog,

          Thank you for your patience.

          Below is an example I have created with my own custom indicator:
          Code:
                  #region Variables		
                  private int myCount = 0;
                  #endregion
          
                  
                  protected override void Initialize()
                  {
                     	Add(PeriodType.Second, 1);
          			CalculateOnBarClose = false;
                  }
          		
          		protected override void OnBarUpdate()
          		{
          			if(CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired)
          				return;
          			
          			if(BarsInProgress == 1)
          			{
          				if(MyTestIndicator(BarsArray[0]).MyInt[0] == 2)
          					myCount++;
          				else
          					myCount = 0;
          			}
          			
          			if(myCount >= 30)
          				Print("Condition held longer than 30 seconds");
          		}
          Adding in the 1 Second Period Type allows me to check the condition is true every second through BarsInProgress == 1. I then add a count to add one to myCount for each second the condition is true, the moment the condition is false I set myCount to 0. The use of BarsArray[0] makes sure the indicator is checking the primary bar series rather than the 1 Second Period Type.

          Please review the information at the following link for using multiple time frames in your code: http://www.ninjatrader.com/support/h...nstruments.htm

          Please let me know if you have any questions.

          Comment


            #6
            Originally posted by NinjaTrader_PatrickH View Post
            Hello birdog,

            Thank you for your patience.

            Below is an example I have created with my own custom indicator:
            Code:
                    #region Variables		
                    private int myCount = 0;
                    #endregion
            
                    
                    protected override void Initialize()
                    {
                       	Add(PeriodType.Second, 1);
            			CalculateOnBarClose = false;
                    }
            		
            		protected override void OnBarUpdate()
            		{
            			if(CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired)
            				return;
            			
            			if(BarsInProgress == 1)
            			{
            				if(MyTestIndicator(BarsArray[0]).MyInt[0] == 2)
            					myCount++;
            				else
            					myCount = 0;
            			}
            			
            			if(myCount >= 30)
            				Print("Condition held longer than 30 seconds");
            		}
            Adding in the 1 Second Period Type allows me to check the condition is true every second through BarsInProgress == 1. I then add a count to add one to myCount for each second the condition is true, the moment the condition is false I set myCount to 0. The use of BarsArray[0] makes sure the indicator is checking the primary bar series rather than the 1 Second Period Type.

            Please review the information at the following link for using multiple time frames in your code: http://www.ninjatrader.com/support/h...nstruments.htm

            Please let me know if you have any questions.
            Thank you @PatrickH (I am checking it out)!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by llanqui, Today, 03:53 AM
            0 responses
            6 views
            0 likes
            Last Post llanqui
            by llanqui
             
            Started by burtoninlondon, Today, 12:38 AM
            0 responses
            10 views
            0 likes
            Last Post burtoninlondon  
            Started by AaronKoRn, Yesterday, 09:49 PM
            0 responses
            15 views
            0 likes
            Last Post AaronKoRn  
            Started by carnitron, Yesterday, 08:42 PM
            0 responses
            11 views
            0 likes
            Last Post carnitron  
            Started by strategist007, Yesterday, 07:51 PM
            0 responses
            14 views
            0 likes
            Last Post strategist007  
            Working...
            X