Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Draw.Rectangle for specific timeframe displaced

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

    Draw.Rectangle for specific timeframe displaced

    Hi, trying to build an indicator that will draw rectangles in a 5 min timeframe when the 15 min candles on 2 instruments are different (ie. up vs down).

    So far, I have achieved this but the rectangle drawn on the 5 min timeframe is anchored 2 candles in the future. How to reflect the same 15 min candle onto the 5 min candle?

    Also, would like to push the rectangle to the back and if possible make the line as thin as a wick width.

    Attached picture.

    Thanks



    Code:
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "DIVERGENCE";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DrawOnPricePanel = true;
    
    
    
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    
    }
    else if (State == State.Configure)
    {
    AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 15, Data.MarketDataType.Last);
    AddDataSeries("GBPUSD", Data.BarsPeriodType.Minute, 15, Data.MarketDataType.Last);
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[1] < 1
    || CurrentBars[2] < 0)
    return;
    // Set 1
    if ((Opens[1][0] < Closes[1][0])
    && (Opens[2][0] > Closes[2][0]))
    {
    Draw.Rectangle(this, Convert.ToString(CurrentBars[1]), false, 0, Lows[1][0], 3, Highs[1][0], Brushes.OliveDrab, Brushes.OliveDrab, 0);
    }

    #2
    Hello bowling,

    Drawing objects are always going to be in relation to the primary chart bars (and not to the added series). This means when choosing where the anchors will be placed, this will be in relation to the chart bars. The rectangle would not be two bars in the future for the primary series.

    Print all of the values in the condition to get an idea of when the condition is true, and print the time to see what the primary bar time is when the condition is true.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for posting the link, very useful.

      I printed the output when the condition is met, ie. the 11:00 am 15min. candle for EU is up and for GU is down and the output shows that.

      Now the issue is when this is calculated on the script. If I assume that the 15min. candle for 11:00 am goes from 10:45 am - 11:00 am, the script shows the wrong timestamp when those calculations are done? The High and Low are correct but I would expect to have the rectangle 2 candles swift to the left.

      Any suggestions on when the calculations should be done? It says the close of the 15min bar, why it draws the rectangle at the close of the 11:10 am 5min candle? What I am missing here?

      Thnks

      HTML Code:
      namespace NinjaTrader.NinjaScript.Indicators
      {
      public class Diverg : Indicator
      {
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "DIVERGENCE";
      Calculate = Calculate.OnBarClose;
      IsOverlay = true;
      DrawOnPricePanel = true;
      
      
      
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      
      }
      else if (State == State.Configure)
      {
      AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 15, Data.MarketDataType.Last);
      AddDataSeries("GBPUSD", Data.BarsPeriodType.Minute, 15, Data.MarketDataType.Last);
      }
      }
      
      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;
      
      if (CurrentBars[1] < 1
      || CurrentBars[2] < 0)
      return;
      // Set 1
      if ((Opens[1][0] < Closes[1][0])
      && (Opens[2][0] > Closes[2][0]))
      {
      Print(string.Format("{0} | EU[1]: {1} EU[0]: {2}> GB[1]: {3} GB[0]: {4} ", Time[0],Opens[1][0], Closes[1][0], Opens[2][0], Closes[2][0]));
      Draw.Rectangle(this, Convert.ToString(CurrentBars[1]), false, 0, Lows[1][0], 3, Highs[1][0], Brushes.OliveDrab, Brushes.OliveDrab, 0);
      }
      if ((Opens[1][0] > Closes[1][0])
      && (Opens[2][0] < Closes[2][0]))
      {
      Draw.Rectangle(this, Convert.ToString(CurrentBars[1]), false, 0, Lows[1][0], 3, Highs[1][0], Brushes.Red, Brushes.Red, 0);
      }
      
      }
      }
      }
      Attached Files

      Comment


        #4
        Hello bowling,

        If the confusion is about when bars are closing and what bars are being referenced, print the bar times to the output window along with the barsInProgress.

        Print(string.Format("{0} | BarsInProgress: {1} | Times[0][0]: {2}, Times[1][0]: {3}, Times[2][0]: {4}", Time[0], BarsInProgress, Times[0][0], Times[1][0], Times[2][0]));

        If you are wanting to understand why a condition is not evaluating as true, the print will need to be outside of that conditions action block so that print appears for every bar, and not only when the condition is true.

        Yes the bar time is the close time of the bar. For non time based bars, like range, renko, tick, etc, the close time of the bar updates to the current time until the bar is fully closed.

        Please also see the help guide on how bar data is referenced.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Ok, got it working now...

          Any ideas how ca I put it in the background and make it thinner?

          Thnks
          Attached Files

          Comment


            #6
            Hello bowling,

            The outline stroke has to be adjusted after the draw method is called.



            Rectangle myRec = Draw.Rectangle(this, "tag1", 10, Low[10] - TickSize, 5, High[5] + TickSize, Brushes.Blue);
            myRec.OutlineStroke.Width = 1;
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            581 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            338 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