Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnRender RenderTarget Line

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

    OnRender RenderTarget Line

    My OnRender Lines show up for a second then disappear with no errors being logged when my custom indicator is added to chart.

    I'm having a hard time figuring out why they are disappearing.

    Is it because of my x2?
    if I change endPoint to this SharpDX.Vector2 endPoint = new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, y);
    it's the same problem.

    Code:
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
    base.OnRender(chartControl, chartScale);
    
    for(int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
    {
    
    DateTime timeValue = Bars.GetTime(barIndex);
    int barsAgo = BarsArray[1].GetBar(timeValue);
    {
    
    if(mid.IsValidDataPointAt(barsAgo))
    
    {
    
    float x = chartControl.GetXByBarIndex(ChartBars, barsAgo);
    float x2 = Convert.ToSingle(x+dAvgRatio.GetValueAt(barsAgo)*300f);
    float y = chartScale.GetYByValue(mid.GetValueAt(barsAgo));
    
    
    SharpDX.Vector2 startPoint = new SharpDX.Vector2(x, y);
    SharpDX.Vector2 endPoint = new SharpDX.Vector2(x2, y);
    
    if(dAvgRatio.GetValueAt(barsAgo) > 2)
    {
    SharpDX.Direct2D1.SolidColorBrush customDXBrush2 = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, new SharpDX.Color4(new SharpDX.Color3(0f, 0f, 255f), 0.490f));
    RenderTarget.DrawLine(startPoint, endPoint, customDXBrush2, 2);
    customDXBrush2.Dispose();
    }
    Last edited by mlprice12; 03-02-2022, 03:00 PM.

    #2
    Hello mlprice12,

    Thank you for your post.

    This looks like you are looping through the visible bars of the primary series, looking up an associated bar index from a secondary series, then trying to draw a line, but it looks like you're basing that on the secondary series instead of the primary series.

    You could get information from the secondary series, find associated bars on the primary series, and that is where you would want to draw (since we can only draw on primary and not secondary).

    You should just be using barIndex within OnRender with things like GetXByBarIndex and GetValueAt.

    Am I understanding correctly what you're attempting?

    Thanks in advance; I look forward to assisting you further.

    Comment


      #3
      NinjaTrader_Kate

      Yes this is correct. I would like to render the line based on information from a secondary series and start the line at the corresponding bar of the primary series.

      Comment


        #4
        Hello mlprice12,

        Thank you for your reply.

        You're using the value you found for the secondary series index to get the start value for the line:

        float x = chartControl.GetXByBarIndex(ChartBars, barsAgo);

        Try using:

        float x = chartControl.GetXByBarIndex(ChartBars,barIndex);

        Thanks in advance; I look forward to assisting you further.

        Comment


          #5
          NinjaTrader_Kate

          that change does create the correct start x but the lines still disappear.
          x2 end point is also the correct location.
          Last edited by mlprice12; 03-02-2022, 03:01 PM.

          Comment


            #6
            I am guessing there is an error that is not showing in the log because RenderTarget.DrawLine() doesn't give an error if fails
            Last edited by mlprice12; 03-02-2022, 03:18 PM.

            Comment


              #7
              Hello mlprice12,

              Thank you for your replies.

              Can you provide a compilable, reduced version of the script that illustrates the issue? Any code unnecessary to reproduce should be removed (such as other plots, drawing objects, etc). Please export the script from Tools > Export > NinjaScript Addon. Do not check the box to export as a compiled assembly or I will not be able to review your code. Please attach the resulting .Zip file to your reply.

              Thanks in advance; I look forward to assisting you further.

              Comment


                #8
                Added prints to see the last print before the lines disappear.

                Last print was #1

                Code:
                Print("1");
                if(mid.IsValidDataPointAt(barsAgo) && dAvgRatio.IsValidDataPointAt(barsAgo))
                
                {
                
                float x = chartControl.GetXByBarIndex(ChartBars, barIndex);
                float x2 = Convert.ToSingle(x+dAvgRatio.GetValueAt(barsAgo)*3 00f);
                float y = chartScale.GetYByValue(mid.GetValueAt(barsAgo));
                Print("2");
                
                // create two vectors to position the ellipse
                SharpDX.Vector2 startPoint = new SharpDX.Vector2(x, y);
                SharpDX.Vector2 endPoint = new SharpDX.Vector2(x2, y);
                
                
                
                // define the brush used in the rectangle
                Print("3");
                if(dAvgRatio.GetValueAt(barsAgo) > 2)
                {
                SharpDX.Direct2D1.SolidColorBrush customDXBrush2 = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, new SharpDX.Color4(new SharpDX.Color3(0f, 0f, 255f), 0.490f));
                RenderTarget.DrawLine(startPoint, endPoint, customDXBrush2, 2);
                customDXBrush2.Dispose();
                }

                Comment


                  #9
                  NinjaTrader_Kate

                  Thanks a lot
                  Attached Files

                  Comment


                    #10
                    By the way I am running this on an ES chart 10 tick

                    Comment


                      #11
                      Now that market is closed the error doesn't occur

                      Comment


                        #12
                        Hello mlprice12,

                        Thank you for your reply.

                        I'm seeing this draw lines on the primary series. I believe the issue you're seeing with lines disappearing is that if the bar that the line is drawn from is no longer on screen, the line associated with that bar will vanish, as you're only cycling through the bars visible on screen when drawing the lines. So if the bar that you get for the x value moves off screen, that line would no longer be drawn.

                        Click image for larger version

Name:	2022-03-02_15-13-56.png
Views:	245
Size:	64.7 KB
ID:	1192194

                        Is this what you're seeing as well?

                        Thanks in advance; I look forward to assisting you further.

                        Comment


                          #13
                          Does it still render now that data is live?

                          Comment


                            #14
                            Hello mlprice12,

                            Thank you for your reply.

                            I'd just like to note that as I've been reviewing, I've seen several formatting issues with the script that affect it's legibility. Many programming mistakes can be quickly spotted when using cleanly formatted code. Please make sure to give your code a once over with formatting before exporting and asking for other input, as it can help us (and others!) better assist.

                            That being said, try explicitly telling it which bars series to look at for the initial x value (note we've also renamed your barsAgo variable to barIndex2 as that's more accurately descriptive):

                            Code:
                            protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
                            {
                            base.OnRender(chartControl, chartScale);
                            
                            ClearOutputWindow();
                            
                            // loop through only the rendered bars on the chart
                            for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
                            {
                            // get the time stamp at the selected bar index value
                            DateTime timeValue = [B]BarsArray[0].GetTime(barIndex);[/B]
                            int barIndex2 = BarsArray[1].GetBar(timeValue);
                            
                            Print("1");
                            
                            if (mid.IsValidDataPointAt(barIndex2) && dAvgRatio.IsValidDataPointAt(barIndex2))
                            {
                            float x = chartControl.GetXByBarIndex(ChartBars, barIndex);
                            float x2 = Convert.ToSingle(x + dAvgRatio.GetValueAt(barIndex2) * 300f);
                            float y = chartScale.GetYByValue(mid.GetValueAt(barIndex2));
                            
                            Print("2");
                            
                            // create two vectors to position the ellipse
                            SharpDX.Vector2 startPoint = new SharpDX.Vector2(x, y);
                            SharpDX.Vector2 endPoint = new SharpDX.Vector2(x2, y);
                            
                            // define the brush used in the rectangle
                            Print("3");
                            if (dAvgRatio.GetValueAt(barIndex2) > 2)
                            {
                            SharpDX.Direct2D1.SolidColorBrush customDXBrush2 = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, new SharpDX.Color4(new SharpDX.Color3(0f, 0f, 255f), 0.490f));
                            RenderTarget.DrawLine(startPoint, endPoint, customDXBrush2, 2);
                            customDXBrush2.Dispose();
                            }
                            }
                            }
                            }
                            You should always be explicit in OnRender which data series you want to access, since it runs for every change in chart data and isn't related directly to what bar series is currently processing in OnBarUpdate.

                            Please let us know if we may be of further assistance to you.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            596 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
                            554 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X