Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Draw a rectangle every 30 minutes which encompasses candles

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

    Draw a rectangle every 30 minutes which encompasses candles

    Hi,

    I'm just learning so bear with me and if I'm headed in the wrong direction let me know.

    I'm trying to draw a rectangle ever 30min that encompasses the candles in that 30min range. I would like to see a rectangle at 7:00 which goes till 7:30 at which a new rectangle is drawn and so on.

    I've got code that is using OnBarUpdate() and I can successfully draw a dot every 30 minutes using the following which seems to be working great.

    Code:
    if(Time[0].Minute % 30 == 0)
    {
    Draw.Dot(this, "YourDot"+CurrentBar, true, 0, Low[0] - TickSize, Brushes.Red);
    }
    I understand I'll need to use MIN and MAX to find the High and Low over the bars/candles inside the 30min range. How do I go from the above Draw.Dot code to getting my rectangles to draw every 30min?

    I've read that drawing rectangles can take a startTime and an endTime. I just don't understand how to get those values into the drawing code:
    Code:
    Draw.Rectangle(this, tagA, startTime, maxLow, endTime, maxHigh, Brushes.Cyan);
    Do I need to use some kind of looping inside my code above?

    Any hints, suggestions or guidance would be welcome and thank you in advance.

    #2
    Hello jkosnikowski,

    The rectangle will need a start bar and an end bar.

    The start bar would be 30 minutes previous to the current bar, which can be found with Bars.GetBar(Time[0].AddMinutes(-30)).

    Subtract this from CurrentBar and this would give you a barsAgo number (for how many bars ago 30 minutes was).

    This can then be used to get the MAX or MIN of that many bars.

    int barsAgoValue = CurrentBar - Bars.GetBar(Time[0].AddMinutes(-30));

    Print(MAX(High, barsAgoValue)[0]);
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Chelsea B,

      Thank you so much for the reply. I managed to get this working and it's perfect.
      For anyone doing something similar I ended up with the following.

      Code:
      if(Time[0].Minute % 30 == 0)
      {
      //Draw a red dot as a test
      Draw.Dot(this, "YourDot"+CurrentBar, true, 0, Low[0] - TickSize, Brushes.Red);
      
      //Rectangles
      int barsAgoValue = CurrentBar - Bars.GetBar(Time[0].AddMinutes(-30));
      double barMax = (MAX(High, barsAgoValue)[0]);
      double barMin = (MIN(Low, barsAgoValue)[0]);
      Draw.Rectangle(this, "tagAA"+CurrentBar, 0, barMin, barsAgoValue, barMax, Brushes.Cyan);
      }
      It does however have the issues of giving an error that the value of "Period" "MAX" is 0 and not in the valid range. I must be missing something else
      Last edited by jkosnikowski; 03-20-2022, 08:43 PM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      577 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      334 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      553 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      551 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X