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

Draw Custom OHLC Plots using OnRender

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

    Draw Custom OHLC Plots using OnRender

    Hello,
    I am trying to draw Custom OHLC Plots using OnRender:
    https://gist.github.com/Zz9uk3/4c3f6...7a3ba0a178e395

    For some unknown reason the low and the high point are a bit off from the default drawn OHLC plots by the configured data series. Moreover the left and right tick of open and close are sometimes to small:


    Does anyone know what I am doing wrong here or how I can account for the difference between the default OHLC plots and my plots?


    #2
    Here is a video of the default plot getting bigger as soon as I stretch the horizontal axis:
    Discover the magic of the internet at Imgur, a community powered entertainment destination. Lift your spirits with funny jokes, trending memes, entertaining gifs, inspiring stories, viral videos, and so much more from users.

    Comment


      #3
      Hello mrzz93,
      Thanks for your post.

      I do not see anything right off the bat, and I am unable to test the GitHub snippet.

      Can you add some prints and see what values are getting passed in versus what is appearing on the chart?

      Josh G.NinjaTrader Customer Service

      Comment


        #4
        Hey Josh! Thanks for your reply
        The solution was to first of all acknowledge that if the width is under 1 OnRender wont draw line. So I came up with the following to always keep my plots visible:

        Code:
        float barPixelWidth = (float)chartControl.BarWidth;
        if (barPixelWidth < 1) barPixelWidth = 1;
        Lates on I added the barPixelWidth to the top and low of the points I choose for my lines:

        Code:
        var yHigh = chartScale.GetYByValue(high) - barPixelWidth / 2;
        var yLow = chartScale.GetYByValue(low) + barPixelWidth / 2;
        Now the low and top end of my OHLC candle always aligns perfectly.

        I also monitored the values closely and they were always equal which makes me believe NT also adds a few numbers to the top and bottom of the OHLC bar drawn for the DataSeries.

        Comment


          #5
          mrzz93,

          Happy to help. Thanks for sharing!
          Josh G.NinjaTrader Customer Service

          Comment


            #6
            Code:
            var yHigh = chartScale.GetYByValue(high) - barPixelWidth / 2;
            var yLow = chartScale.GetYByValue(low) + barPixelWidth / 2;
            Thank you for sharing.

            JoshG, I don't understand. Why do we have to add/subtract "barPixelWidth / 2" for the correct alignment? Doesn't chartScale.GetYByValue(high) give the exact y position of high?
            Last edited by stupiddove; 10-17-2021, 07:13 PM.

            Comment


              #7
              Hi, thanks for posting.

              barPixelWidth / 2 places the point in the center of the bar no matter what the width setting for the bar is.

              Best regards,
              -ChrisL
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                ChrisL, thank you for your reply.

                I don't quite understand. The function "chartScale.GetYByValue()" returns the chart's y-pixel coordinate of the price value. If I input the close px, I will get the y-pixel coordinate of that close px, I override onRender() that a white line is placed on each primary price bar.


                Code:
                private void CreatePlot(ChartControl chartControl, ChartScale chartScale, int barIndex)
                {
                    // get x-coordinate
                    int x = chartControl.GetXByBarIndex(ChartBars, barIndex);
                
                   float barPixelWidth = (float)chartControl.BarWidth;
                
                   double high_px = High.GetValueAt(barIndex);
                   double low_px = Low.GetValueAt(barIndex);
                
                   float y_high = chartScale.GetYByValue(high_px);
                   float y_low = chartScale.GetYByValue(low_px);
                
                   SharpDX.Direct2D1.SolidColorBrush barBrushDx = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.White);
                   barBrushDx.Opacity = 0.8f;
                
                   SharpDX.Vector2 v_high = new SharpDX.Vector2(x, y_high);
                   SharpDX.Vector2 v_low = new SharpDX.Vector2(x, y_low);
                
                   RenderTarget.DrawLine(v_low, v_high, barBrushDx, (float)ChartControl.BarWidth);
                
                   barBrushDx.Dispose();
                
                }
                The result is:

                Click image for larger version  Name:	chart03.png Views:	0 Size:	33.4 KB ID:	1175433


                Why are the white bars not covering the price bar from high to low?







                Attached Files

                Comment


                  #9
                  Hi, thanks for your reply.

                  The OHLC chart style is available to study through NinjaScript Editor>ChartStyles>OhlcStyle.cs file. In the OnRender method for this chart style there is some slight modifications to the high/low values, if you mimic the renering techniques used here it should match up e.g.:
                  point0.X = point1.X = x;
                  point0.Y = high - lineWidth * 0.5f;
                  point1.Y = low + lineWidth * 0.5f;

                  Best regards,
                  -ChrisL
                  Chris L.NinjaTrader Customer Service

                  Comment


                    #10
                    I see, thank you for your help.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by andrewtrades, Today, 04:57 PM
                    1 response
                    7 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by chbruno, Today, 04:10 PM
                    0 responses
                    5 views
                    0 likes
                    Last Post chbruno
                    by chbruno
                     
                    Started by josh18955, 03-25-2023, 11:16 AM
                    6 responses
                    436 views
                    0 likes
                    Last Post Delerium  
                    Started by FAQtrader, Today, 03:35 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post FAQtrader  
                    Started by rocketman7, Today, 09:41 AM
                    5 responses
                    19 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Working...
                    X