I've worked REALLY HARD on this indicator. I mean, HOURS. I still has 3 issues.
1. My rectangles aren't extending to the right until there's a close above swing high/below swing low.
2. My rectangles sometimes continue to draw new rectangles after it just drew one.
3. Sometimes the rectangle isn't drawn from the high to the close; low to the close...some rectangles extend higher or lower than they should.
Here's my code:
private bool swingStart = true;
private bool keepDrawing = true;
private int mystartBar;
private double rect_high, rect_low, rect_close1, rect_close2;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Shows potential support and resistance levels via rectangles.";
Name = "DSRLevels";
Calculate = Calculate.OnBarClose;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = false;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = false;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
BullishOut = Brushes.ForestGreen;
BullishBod = Brushes.LightGreen;
BullOpacity = 5;
BearishOut = Brushes.DarkRed;
BearishBod = Brushes.Red;
BearOpacity = 5;
}
else if (State == State.Configure)
{
}
}
protected override void OnBarUpdate()
{
if(CurrentBar < 7)
return;
//if 3rd bar in 7 has 3 lower highs to the right and left, draw a rectangle from swing highest high to swing highest close
if(High[6] <= High[3] && High[5] <= High[3] && High[4] < High[3] && High[2] <= High[3] && High[1] <= High[3] && High[0] <= High[3])
{
mystartBar = CurrentBar - 8; // pick a startpoint
rect_high = MAX(High,8)[0]; // pick a high level
rect_low = MIN(Low,8)[0]; // pick a low level
rect_close1 = MAX(Close,8)[0]; // pick a close for highest bar level
rect_close2 = MIN(Close,8)[0]; // pick a close for lowest bar level
Draw.Rectangle(this, CurrentBar.ToString() + "myRect", false, 4, rect_close1, 0, rect_high, BearishOut, BearishBod, BearOpacity, false);
swingStart = false;
}
else if (keepDrawing)
{
Draw.Rectangle(this, CurrentBar.ToString() + "myRect", false, 4, rect_close1, 0, rect_high, BearishOut, BearishBod, BearOpacity, false); // redraw (extend) rectangle to current bar
}
if(HighestBar(Close, 1) > HighestBar(High, mystartBar))
{
keepDrawing = false;
}
//if 3rd bar in 7 has 3 higher lows to the right and left, draw a rectangle from swing lowest low to swing lowest close
if(Low[6] >= Low[3] && Low[5] >= Low[3] && Low[4] > Low[3] && Low[2] >= Low[3] && Low[1] >= Low[3] && Low[0] >= Low[3])
{
mystartBar = CurrentBar - 8; // pick a startpoint
rect_high = MAX(High,8)[0]; // pick a high level
rect_low = MIN(Low,8)[0]; // pick a low level
rect_close1 = MAX(Close,8)[0]; // pick a close for highest bar level
rect_close2 = MIN(Close,8)[0]; // pick a close for lowest bar level
Draw.Rectangle(this, CurrentBar.ToString() + "myRect", false, 4, rect_close2, 0, rect_low, BullishOut, BullishBod, BullOpacity, false);
swingStart = false;
}
else if (keepDrawing)
{
Draw.Rectangle(this, CurrentBar.ToString() + "myRect", false, 4, rect_close2, 0, rect_low, BullishOut, BullishBod, BullOpacity, false); // redraw (extend) rectangle to current bar
}
if(LowestBar(Close, 1) < LowestBar(Low, mystartBar))
{
keepDrawing = false;
}
}

Comment