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

Code to identify bars in price range

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

    Code to identify bars in price range

    Hello,
    I am trying to develop an indicator that picks up when 4 or more bars move within a price range of say 2.5pts etc. I have used MAX and MIN and look back, and the code compiles, but I am getting too many results as the look back returns results for when any bar falls within the 2.5pts price range, rather than when a minimum of 4 bars and then any additional bars fall within the range. How do I get my code to pick up when a minimum of 4+ bars meets the criteria?
    I have pasted my code below:

    Thank you!

    public class MyFlagsRange3 : Indicator
    {
    #region Variables
    // Wizard generated variables
    private double flagPriceRangeMultiplier = 10; // Default setting for FlagRange 10 x TickSize = 2.5pts
    private double mediumPriceRangeMultiplier = 14; // Default setting for MediumRange 14 x TickSize = 3.5pts
    private double largePriceRangeMultiplier = 18; // Default setting for LargeRange 18 x TickSize = 4.5pts
    // User defined variables (add any user defined variables below)
    private int minLookBackPeriod = 4; //Default setting for minimum look back period
    private string TagBullVol = "bullVol";
    private string TagBearVol = "bearVol";

    bool maxMinWithinFlag; //Set to true below if all bars in minimum look back period (currently 4 for ES) are within the defined price range
    int maxMinWithinFlagBar; //below it defines the 1st bar of the minimum look back period (currently 4 for ES) within the defined price range

    #endregion

    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    Overlay = true;

    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    if (CurrentBar < 3)
    {
    return;
    }

    //Picks up if all bars in minimum look back period (currently 4 for ES) are within the defined price range
    if (MAX(High, minLookBackPeriod)[0] <= (flagPriceRangeMultiplier * TickSize) + MIN(Low, minLookBackPeriod)[0])
    {
    maxMinWithinFlag = true; //Set the bool variable to true
    maxMinWithinFlagBar = CurrentBar - (CurrentBar - 1); //Save the current bar number of the 1st of the 4 bars
    }
    Print(Time.ToString()+"Minimum 4 bars within flag");
    // plot below by replacing 'Close[0]' with your own formula.

    //Once all bars in minimum look back period (currently 4 for ES) are within the defined price range,
    //look back picks up other subsequent bars in that range before it is broken
    if (MAX(High, maxMinWithinFlagBar)[0] <= (flagPriceRangeMultiplier * TickSize) + MIN(Low, maxMinWithinFlagBar)[0])
    {
    DrawText (TagBullVol + CurrentBar, "Flag Region", 0, Low[0] - 5 * TickSize, Color.Red);
    }
    else
    {
    }
    Print(Time.ToString()+"More than 4 bars within flag");

    #2
    GeorgeW,

    After reviewing your inquiry and code, here's some sample code that should point you in the right direction:

    Code:
    protected override void OnBarUpdate()
    {
        if (CurrentBar < 3) return;
    
        if (FirstTickOfBar)
        {
            int counter = 0;
    
            for (int i = 1; i <= minLookBackPeriod; i++)
            {
                if (High[i] - Low[i] >= flagPriceRangeMultiplier * TickSize) counter++;
                /*
                In case you want open-close instead of high-low.
                if (Math.Abs(Open[i] - Close[i]) >= flagPriceRangeMultiplier * TickSize) counter++;
                */
            }
    
            if (counter >= minLookBackPeriod) DrawDot(TagBullVol + CurrentBar, AutoScale, 1, Low[1] - 5 * TickSize, Color.Red);
        }
    }
    Bobby Y.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the help on this BobbyY.

      When I commented out my code and inserted yours, the Output Screen showed:

      Error on calling 'OnBarUpdate' method for indicator 'MyFlagsRange3' on bar 3: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

      I had to change the 2nd argument in the 1st for loop i <= minLookBackPeriod; (which I think means it wouldn't do anything until 5 bars but was waiting for a minimum of 4, so I think it may have conflicted with the CurrentBar statement which was waiting for 4 bars) to i < minLookBackPeriod; for that error to stop.

      I am still not seeing any of the dots on the charts, although when I put my DrawText below the DrawDot, I do see the text. I have tried printing the counter to the Output Screen, but there is no count, just the time: Print(Time.ToString()+counter.ToString());

      Is there a way of restricting the text or the dots to once for each range, as opposed to once every time it sees a Bar[1] in that range?

      Also, in this line: if (High[i] - Low[i] >= flagPriceRangeMultiplier * TickSize) counter++;
      If I am only looking for bars within the range <= 2.5pts, shouldn't the sign be reversed?



      Thanks again.
      Last edited by GeorgeW; 12-29-2015, 03:46 AM.

      Comment


        #4
        The problem appears to be that the counter is never getting to equal or exceed the minLookBackPeriod, so there is a problem in the earlier logic for it. If I change the DrawDot line to display the dots if (counter <= minLookBackPeriod), then the dots are displayed, whereas what is wanted is for them to display when (counter >= minLookBackPeriod).

        Comment


          #5
          I have gotten it to work for 4 bars, but the counter does not go past that to pick up other bars that fall within the same range. Any ideas? Also, how do I get it to DrawDot once for each range of bars that falls within the same spread?

          Code:
          protected override void OnBarUpdate()
                  {
                      // Use this method for calculating your indicator values. Assign a value to each
          
              if (CurrentBar < 3) return;
          
              if (FirstTickOfBar)
              {
                  	int counter = 0;
          		
          		//Had to change 2nd argument from i <= minLookBackPeriod; (which means it wouldn't do anything until 5 bars but was
          		//waiting for a minimum of 4, to i < minLookBackPeriod; for it to stop: "Error on calling 'OnBarUpdate' method for indicator 
          		//'MyFlagsRange3' on bar 3: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a 
          		//series [barsAgo] with a value of 5 when there are only 4 bars on the chart."
          		//But still would not DrawDots until changed int i = 1, to 0, so that it matched the start of the counter.
                  for (int i = 0; i < minLookBackPeriod; i++)
                  {
                      	//if (High[i] - Low[i] <= flagPriceRangeMultiplier * TickSize) counter++;
          		//Changed because the above code was considering each bar independently, & I want all the bars counted to be within the same range.
          		if (MAX(High, minLookBackPeriod)[i] - MIN(Low, minLookBackPeriod)[i] <= flagPriceRangeMultiplier * TickSize) counter++;
                      
                  }
          
                  if (counter >= minLookBackPeriod) DrawDot(TagBullVol + CurrentBar, AutoScale, 1, Low[1] - 6 * TickSize, Color.Blue);

          Comment


            #6
            GeorgeW,

            Try setting a maxLookBackPeriod as well.

            Example: minLookBackPeriod = 4, maxLookBackPeriod = 10. It will find 4+ Bars in 10 bars period and if true will show the dot.
            Bobby Y.NinjaTrader Customer Service

            Comment


              #7
              Thanks for that, Bobby Y. That occurred to me when I went for a walk to clear my head, and it now works. Is there a way to define that maxLookBackPeriod as some infinite number?

              Also, let's say I have 12 bars which form a 2.5pt range, and they are marked with a dot, then immediately after, there is another bar that forms a range with the last four of the previous 12, so that gives me a dot, is there a way to code it to not put a dot for that one?

              Code:
              protected override void Initialize()
                      {
                          Overlay				= true;
              			
                      }
              
                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                          // Use this method for calculating your indicator values. Assign a value to each
              
                  if (CurrentBar < (minLookBackPeriod - 1)) return;
              
                  if (FirstTickOfBar)
                  {
                     		int counter = 0;
              		
              		//Had to change 2nd argument from i <= minLookBackPeriod; (which means it wouldn;t do anything until 5 bars but was
              		//waiting for a minimum of 4) to i < minLookBackPeriod; for it to stop: Error on calling 'OnBarUpdate' method for indicator 
              		//'MyFlagsRange3' on bar 3: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a 
              		//series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
              		//But still would not DrawDots until changed int i = 1, to 0, so that it matched the start of the counter.
                      for (int i = 0; i < minLookBackPeriod; i++)
                      {
                          //Changed the > sign here to <
              			//if (High[i] - Low[i] <= flagPriceRangeMultiplier * TickSize) counter++;
              			//Also MAX and MIN used, so that it is the range of all the bars considered together that is being tested, 
              			//and not just the range of individual bars
              			if (MAX(High, minLookBackPeriod)[i] - MIN(Low, minLookBackPeriod)[i] <= flagPriceRangeMultiplier * TickSize) counter++;
              
                          /*
                          In case you want open-close instead of high-low.
                          if (Math.Abs(Open[i] - Close[i]) >= flagPriceRangeMultiplier * TickSize) counter++;
                          */
                      }
              		
              		//My addition (4 lines) for where it exceeds minLookBackPeriod, need to keep including the bars
              		//Had to define a maxLookBackPeriod for it to work
              		for (int i = 0; i >= minLookBackPeriod; i++)
              		{
              		if (MAX(High, maxLookBackPeriod)[i] - MIN(Low, maxLookBackPeriod)[i] <= flagPriceRangeMultiplier * TickSize) counter++;
              		}
              
                      if (counter >= minLookBackPeriod) DrawDot(TagBullVol + CurrentBar, AutoScale, 1, Low[1] - 6 * TickSize, Color.Blue);
              Last edited by GeorgeW; 12-29-2015, 02:21 PM.

              Comment


                #8
                Essentially what I am saying is that a bar cannot be a part of 2 ranges.

                Comment


                  #9
                  Unfortunately this is still not working to identify between 4 to 15 bars in a 2.5pt range. I am getting the following error with the code below: Error on calling 'OnBarUpdate' method for indicator 'MyFlagsRange5' on bar 5: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

                  If I substitute minLookBackPeriod for all the maxLookBackPeriod, I do get the dots on the chart, but only for the minLookBackPeriod. I don't understand why it will go past the CurrentBar when I include minLookBackPeriod in the arguments below it, but it will not go past it when I include max below it, and the CurrentBar line is not changed. Does anyone have any ideas?

                  Also, is there a way of marking bars already included in an earlier range, so that they can be excluded from later ranges?

                  Thanks!




                  Code:
                  protected override void OnBarUpdate()
                          {
                              // Use this method for calculating your indicator values. Assign a value to each
                  
                      if (CurrentBar < (minLookBackPeriod - 1)) return;
                  
                      if (FirstTickOfBar)
                      {
                         
                  		int counter = 0;
                  		
                  		for (int i = 0; i < maxLookBackPeriod; i++)
                          {
                              if (MAX(High, maxLookBackPeriod)[i] - MIN(Low, maxLookBackPeriod)[i] <= flagPriceRangeMultiplier * TickSize) counter++;
                  
                          }
                  		
                          if (counter >= minLookBackPeriod) DrawDot(TagBullVol + CurrentBar, AutoScale, 0, Low[1] - 6 * TickSize, Color.Blue);

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by traderqz, Today, 12:06 AM
                  5 responses
                  8 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by Mongo, Today, 11:05 AM
                  2 responses
                  7 views
                  0 likes
                  Last Post Mongo
                  by Mongo
                   
                  Started by guillembm, Today, 11:25 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post guillembm  
                  Started by Tim-c, Today, 10:58 AM
                  1 response
                  3 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by traderqz, Yesterday, 09:06 AM
                  4 responses
                  30 views
                  0 likes
                  Last Post traderqz  
                  Working...
                  X