Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

A boolean in OnBarUpdate() can not be called in OnRender()

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

    #31
    Originally posted by Stanfillirenfro View Post
    Hello NinjaTrader_Emily!

    I am still failing to complete this topic.
    Let consider the bar with the arrows.

    (1) Represents the Close. float val1 = chartScale.GetYByValue(Close.GetValueAt(index));
    (2) Represents the Open. Float val2 = chartScale.GetYByValue(Open.GetValueAt(index));
    (3) Represents the plotted indicator, beneath is the rectangle drawn in OnRender().
    float yRec;
    This rectangle should overlay the bar.
    To draw a rectangle, we need the following function:
    HTML Code:
    SharpDX.RectangleF rect = new SharpDX.RectangleF(xStart, yStart, rectWidth, rectHeight);
    My goal here is to find yStart.
    This is what I have done with the unexpexted result (See picture).
    HTML Code:
    float space1 = val2 - yRec;
    float space2 = val1 - val2;
    yStart = val1- space1-space2;
    ​
    I am failing to have the rectangle overlayed on the bar.
    I would appreciate it if you could point out where I am mistaking.

    Best regards,


    The HeikenAshi8 indicator that I previously shared calculates the bar's width as well as coordinates for the open, high, low, and close values of the bar. It uses these values when rendering the rectangle objects onto the bars and you should be able to use these same ideas in your script:
    Code:
            protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
            {
                
                if (Bars == null || ChartControl == null)
                    return;            
    
                int barPaintWidth = Math.Max(3, 1 + 2 * ((int)ChartControl.BarWidth - 1) + 2 * shadowWidth);
    
                for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex; idx++)
                {
                    if (idx - Displacement < 0 || idx - Displacement >= BarsArray[0].Count || ( idx - Displacement < BarsRequiredToPlot))
                        continue;
            
                    double valH = HAHigh.GetValueAt(idx);
                    double valL = HALow.GetValueAt(idx);
                    double valC = HAClose.GetValueAt(idx);
                    double valO = HAOpen.GetValueAt(idx);
                    int x  = chartControl.GetXByBarIndex(ChartBars, idx);  //was chartControl.BarsArray[0]
                    int y1 = chartScale.GetYByValue(valO);
                    int y2 = chartScale.GetYByValue(valH);
                    int y3 = chartScale.GetYByValue(valL);
                    int y4 = chartScale.GetYByValue(valC);
    
                    SharpDX.Direct2D1.Brush    shadowColordx     = shadowColor.ToDxBrush(RenderTarget);  // prepare for the color to use
                    var xy2 = new Vector2(x, y2);
                    var xy3 = new Vector2(x, y3);
                    RenderTarget.DrawLine(xy2, xy3, shadowColordx, shadowWidth);    
    
                    if (y4 == y1)
                        RenderTarget.DrawLine( new Vector2( x - barPaintWidth / 2, y1),  new Vector2( x + barPaintWidth / 2, y1), shadowColordx, shadowWidth);
                    else
                    {
                        if (y4 > y1)
                        {
                            SharpDX.Direct2D1.Brush    barColorDowndx     = barColorDown.ToDxBrush(RenderTarget);  // prepare for the color to use                        
                            RenderTarget.FillRectangle( new RectangleF(x - barPaintWidth / 2, y1, barPaintWidth, y4 - y1), barColorDowndx);
                            barColorDowndx.Dispose();
                        }
                        else
                        {
                            SharpDX.Direct2D1.Brush    barColorUpdx     = barColorUp.ToDxBrush(RenderTarget);  // prepare for the color to use
                            RenderTarget.FillRectangle( new RectangleF(x - barPaintWidth / 2, y4, barPaintWidth, y1 - y4),barColorUpdx);
                            barColorUpdx.Dispose();
                        }
                         RenderTarget.DrawRectangle( new RectangleF( x - barPaintWidth / 2 + (float)shadowWidth / 2,
                           Math.Min(y4, y1), barPaintWidth - (float)shadowWidth, Math.Abs(y4 - y1)), shadowColordx, shadowWidth);
                    }    
                    shadowColordx.Dispose();    
                }
            }        
    ​
    For more info on vectors and chart coordinates, please refer back to the following resources I have shared with you previously:Thank you for your time and patience.

    Comment


      #32
      Hello NinjaTrader_Emily !

      Many thanks for your very valuable help.

      I have finally fixed the problem.

      Many thanks again.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      597 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      343 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
      556 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      555 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X