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

How to check, if an indicator's value is less than some number consecutively?

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

    How to check, if an indicator's value is less than some number consecutively?

    I am writing a strategy in ninja script and the main function of the script is to have the value of an indicator consecutively 15 bars in a range.
    For example, if i am using ADX, it is part of the strategy to have ADX below 20 for 15 bars in a row, and if after 15 consecutive times it breaks above 20, the strategy will buy or sell.
    I have used loops and some other functions but did not have a working code.
    I will appreciate any help in this regard.
    Thanks

    #2
    Hello sherazwaqar,

    You could custom code a loop that checks the prior 15 instances. There is also built in method CountIf() that can be used to check for occurrences of a condition. It returns the number of times the condition evaluates true over a lookback.

    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick reply, I was trying to use a loop. Code is below

      Code:
      int lvls =0;
      int i =0;
      while(Math.Abs(fibMC1[i]) < threshold*TickSize)
      {
              lvls++;
      					
      	cr1.cBar = CurrentBar;
      	cr1.upTV = DonchianChannel(fChannel).Upper[0];
      	cr1.dnTV = DonchianChannel(fChannel).Lower[0];
      	crList.Add(cr1);
      				
      	if(crList.Count > congestion)
      	{
      		crList.Clear();
      	}
      	i++;
      					
      	}
      	
      	if(lvls >= congestion)
      	{
      		int start	= crList.Count;
      				        DrawRectangle("Congestion",false,lvls,High[congestion]+TickSize,0,Low[congestion]-TickSize,Color.Blue,Color.Gold,4);
      			}
      It was working fine, when i was using it on ES CME Index Futures ETH, and when I try to use it on CME Index Futures ETH or 24/5, the indicator does not return any value.
      I have set

      if(CurrentBar < 200) return; a

      I was loading 100 days of data for a 3 min chart.

      Error in output windows is

      Error on calling 'OnBarUpdate' method for indicator 'FibSRV3' on bar 200: barsAgo needed to be between 0 and 255 but was 256

      I will try to use CountIf method.
      Last edited by sherazwaqar; 11-22-2011, 03:03 PM. Reason: missed something

      Comment


        #4
        Originally posted by sherazwaqar View Post
        I am writing a strategy in ninja script and the main function of the script is to have the value of an indicator consecutively 15 bars in a range.
        For example, if i am using ADX, it is part of the strategy to have ADX below 20 for 15 bars in a row, and if after 15 consecutive times it breaks above 20, the strategy will buy or sell.
        I have used loops and some other functions but did not have a working code.
        I will appreciate any help in this regard.
        Thanks
        You are checking for a range over a lookback period. Use the MAX() and MIN() methods.

        Comment


          #5
          CountIf() will likely do the trick. A quick example of a for loop you could use to check for consecutive conditions:

          Code:
          protected override void OnBarUpdate()
          {
          if (CurrentBar > 15)
          {
          	int myConditionCounter = 0;
          
          	for (int x = 0; x < 15; x++)
          	{
          		if(ADX(14)[x] < 20)
          		myConditionCounter++;		
          	}
          	
          	if (myConditionCounter >= 15)
                    Print("We have " + myConditionCounter + " occurrences of our condition");
          
                   myConditionCounter = 0; //Resets back to 0 after we access.
          
          }
          }
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            Thanks guys for your quick help

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by rtwave, 04-12-2024, 09:30 AM
            5 responses
            37 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by funk10101, Today, 12:02 AM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by GLFX005, Today, 03:23 AM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by nandhumca, Yesterday, 03:41 PM
            1 response
            13 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by The_Sec, Yesterday, 03:37 PM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Working...
            X