Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    Emily C.NinjaTrader Customer Service

    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 ageeholdings, Today, 07:43 AM
      0 responses
      3 views
      0 likes
      Last Post ageeholdings  
      Started by pibrew, Today, 06:37 AM
      0 responses
      4 views
      0 likes
      Last Post pibrew
      by pibrew
       
      Started by rbeckmann05, Yesterday, 06:48 PM
      1 response
      14 views
      0 likes
      Last Post bltdavid  
      Started by llanqui, Today, 03:53 AM
      0 responses
      6 views
      0 likes
      Last Post llanqui
      by llanqui
       
      Started by burtoninlondon, Today, 12:38 AM
      0 responses
      12 views
      0 likes
      Last Post burtoninlondon  
      Working...
      X