Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SR Indicator still has 3 issues

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

    SR Indicator still has 3 issues

    First off, I'd like to say thank you to PaulH. You've been a great help!

    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;
    }
    }

    #2
    Hello jamestrader21x,

    Thank you for your note.

    A few things I see here - first, I don't see anywhere in your logic that sets keepDrawing back to true after the first time it gets switched to false. Next, since you are using the same keepDrawing bool to control whether bullish or bearish rectangles extend themselves you could be drawing rectangles where you're not intending one to be.

    Also, when you want to redraw a drawing object, the tag names need to be the same. If you're using CurrentBar.ToString() + "myRect" for the tag name, you'll need to save that and reuse that same tag to redraw the rectangle since the next bar will be a different current bar number.

    I'd look at the logic you're using for your bools and maybe use separate bools to control whether bullish or bearish rectangles are extended. I'd also definitely make sure you're using the same tags for the extended rectangles as you were for the original.

    Please let us know if we may be of further assistance to you.

    Comment


      #3
      First off, I am an amateur coder. I've looked at this for hours, looked at an example(not exact example) PaulH was nice enough to code for me. I've done as much as I can on my own. I've tried everything I could think of.

      Re-arranged the keepDrawing = false; code so that it checks for that condition first before instructions are given to keep re-drawing the rectangle.

      I've tried different ints combinations in the "int startBarago" section of Draw.Rectangle. It keeps drawing multiple rectangles.

      I have no clue how to make it stop drawing the rectangle when price closes above the swing high of the rectangle/below the low...

      I'm asking someone(ANYONE) who knows more about coding than me.
      Please just give me the answer. I've tried my hardest to figure it out.
      And if you're feeling extra generous, please tell me what I was doing wrong. So I can learn something.
      I don't want to give up. However, I'm really sick of thinking about it.

      Below is the last of what I worked on. Some of it is commented out so could I locate the code that's not working correctly.

      What the code is supposed to do is whenever there's a high with 3 lower highs to left and 3 lower highs to the right, it suppose to draw a rectangle from the high of the swing high candle to the close of the swing high candle.(vice versa for bullish rectangles) It is supposed to keep drawing the same rectangle until there's a close above that particular swing high or that particular swing low.


      private bool swingStart = true;
      private bool keepDrawingBullRect = true;
      private bool keepDrawingBearRect = true;
      private int mystartBar;
      private double rect_high, rect_low, rect_close1, rect_close2,lastHigh,lastLow;

      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 < 31)
      return;

      string myRect = "myRect";
      string myRect2= "myRect2";

      //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
      bool swingHigh = High[6] <= High[3] && High[5] <= High[3] && High[4] < High[3] && High[2] <= High[3] && High[1] <= High[3] && High[0] <= High[3];


      if(swingHigh)
      {
      mystartBar = CurrentBar - 4; // pick a startpoint
      rect_high = MAX(High,4)[0]; // pick a high level
      rect_low = MIN(Low,4)[0]; // pick a low level
      rect_close1 = MAX(Close,4)[0]; // pick a close for highest bar level
      rect_close2 = MIN(Close,4)[0]; // pick a close for lowest bar level
      lastHigh = HighestBar(High,20); // look back 20 bars for last highest high
      //lastLow = MIN(Low,20)[0]; // look back 20 bars for last lowest low

      Draw.Rectangle(this, CurrentBar.ToString() + myRect, false, (CurrentBar - mystartBar), rect_close1, 0, rect_high, BearishOut, BearishBod, BearOpacity, false);
      swingHigh = false;
      }

      else if(CrossAbove(High, lastHigh, 1))
      {
      keepDrawingBullRect = false;
      }

      else if (keepDrawingBullRect)
      {
      Draw.Rectangle(this, CurrentBar.ToString() + myRect, false, (CurrentBar - mystartBar), rect_close1, 0, rect_high, BearishOut, BearishBod, BearOpacity, false); // redraw (extend) rectangle to current bar
      }




      //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
      /*else 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 (keepDrawingBearRect)
      {
      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;
      }*/
      keepDrawingBullRect = true;
      }

      Comment


        #4
        When I layed down to get some sleep(I haven't been sleeping well the past 72hrs) I started brainstorming about how I can re-draw the rectangles from one specific point per rectangle. I thought I could run a for loop and scan for my specific swingHigh/Low criteria. If true, return the index(es) that/those swingHigh/Lows in an array(list). I'm gonna try that.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        585 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        340 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        103 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        554 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        552 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X