Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

create ellipse OnRender & BarsInProgress

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

    create ellipse OnRender & BarsInProgress

    Hello everyone, I'm trying to match the code for OnRender, but it doesn't work. Help what is the error?

    the task of drawing an ellipse in the form of a ball, at the closing price of the bar of the filtered volume

    Code:
    if (BarsInProgress == 1)
    {
    
    if (Volume[0] > 500)
    {
      VolBar = Close[0];
    }
    
    }
    
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
    
    
    for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex; idx++)
    {
    
    float x = chartControl.GetXByBarIndex(ChartBars, idx);
    float y = chartScale.GetYByValue(VolBar);
    
    //Line
    RenderTarget.DrawLine(new Vector2(x + 100, y), new Vector2(x, y), new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.Red), 4);
    
    // create two vectors to position the ellipse
    
    SharpDX.Vector2 startPoint = new SharpDX.Vector2(x, y);
    SharpDX.Vector2 endPoint = new SharpDX.Vector2(x, y);
    
    // calculate the center point of the ellipse from start/end points
    
    SharpDX.Vector2 centerPoint = (startPoint + endPoint) / 2;
    // set the radius of the ellipse
    float radiusX = 5;
    float radiusY = 5;
    
    // construct the rectangleF struct to describe the position and size the drawing
    
    SharpDX.Direct2D1.Ellipse ellipse = new SharpDX.Direct2D1.Ellipse(centerPoint, radiusX, radiusY);
    
    // define the brush used in the rectangle
    
    SharpDX.Direct2D1.SolidColorBrush customDXBrush = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.DodgerBlue);
    
    // execute the render target fill ellipse with desired values
    RenderTarget.FillEllipse(ellipse, customDXBrush);
    
    // always dispose of a brush when finished
    
    customDXBrush.Dispose();
    
    
    }
    }
    Last edited by memonolog; 03-19-2021, 02:11 AM.

    #2
    Hello memonolog,

    Thank you for the post.

    What is the specific error that you are seeing? The script provided is not complete so I cannot run it to see what the result was.

    I look forward to being of further assistance.

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello memonolog,

      Thank you for the post.

      What is the specific error that you are seeing? The script provided is not complete so I cannot run it to see what the result was.

      I look forward to being of further assistance.
      here is the complete script,

      I need a line and a circle at each close of the bar that is painted in Cyan color (seen on the chart)
      in place of this I get my line and circle at the last price - it works like the Price Line.

      Click image for larger version  Name:	NQ 06-21 (5 Minute) 2021_03_19 (16_43_38).png Views:	0 Size:	30.8 KB ID:	1147397
      Last edited by memonolog; 03-19-2021, 07:52 AM.

      Comment


        #4
        Hello memonolog,

        Thank you for the image that helps to explain what the problem is in contrast to the code.

        The problem is that you are setting a class level variable to a specific bars price but that holds no relation at all to OnRender. OnRender is independent of OnBarUpdate so accessing bar data from variables like that would not be correct if you wanted to do something for each bar. The way you have it now would be good to display a constant or always the same value that is not related to specific bars.

        One other item that comes up is that you are using a secondary series. The secondary series is not going to relate to the bars which OnRender is looping over, OnRender applies to the primary series.

        A possible way to approach this would be to use a Series<double> to contain your data and then reference that from OnRender.




        After adding a series<double> you would then set it a value like the following:

        Code:
        if (Volume[0] > 500) { myDoubleSeries[0] = Close[0]; }
        Then from OnRender you could check if there is a valid data point and if so get its value:



        Code:
        for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex; idx++)
        {
        
        if(myDoubleSeries.IsValidDataPointAt(idx))
        {
            float x = chartControl.GetXByBarIndex(ChartBars, idx);
            float y = chartScale.GetYByValue([B]myDoubleSeries.GetValueAt(idx)[/B]);
        
           //etc
        }
        }



        I look forward to being of further assistance.

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello memonolog,

          Thank you for the image that helps to explain what the problem is in contrast to the code.

          The problem is that you are setting a class level variable to a specific bars price but that holds no relation at all to OnRender. OnRender is independent of OnBarUpdate so accessing bar data from variables like that would not be correct if you wanted to do something for each bar. The way you have it now would be good to display a constant or always the same value that is not related to specific bars.

          One other item that comes up is that you are using a secondary series. The secondary series is not going to relate to the bars which OnRender is looping over, OnRender applies to the primary series.

          A possible way to approach this would be to use a Series<double> to contain your data and then reference that from OnRender.




          After adding a series<double> you would then set it a value like the following:

          Code:
          if (Volume[0] > 500) { myDoubleSeries[0] = Close[0]; }
          Then from OnRender you could check if there is a valid data point and if so get its value:



          Code:
          for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex; idx++)
          {
          
          if(myDoubleSeries.IsValidDataPointAt(idx))
          {
          float x = chartControl.GetXByBarIndex(ChartBars, idx);
          float y = chartScale.GetYByValue([B]myDoubleSeries.GetValueAt(idx)[/B]);
          
          //etc
          }
          }



          I look forward to being of further assistance.
          Thanks for the detailed answer, is this the only way to do this, or is it possible to draw a circle more easily?

          Comment


            #6
            Hello memonolog,

            The way you are drawing now is the more efficient way but does involve more complex code. The more simple way would be to just use drawing objects from your condition in OnBarUpdate.

            If the chart is going to contain thousands of objects it may degrade performance by using drawing objects. OnRender would be preferred if your logic generates a lot of drawings. If the condition is not frequently true or just makes a few hundred objects that would likely be ok to use with drawing objects from OnBarUpdate.

            I look forward to being of further assistance.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            659 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            374 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            109 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            574 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            579 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X