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");
Comment