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

Clarification on redrawing objects in NinjaTrader using SharpDX

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

    Clarification on redrawing objects in NinjaTrader using SharpDX

    Hello support and all,

    I'm currently working on a strategy in NinjaTrader that involves drawing objects using SharpDX for rendering. I have a question regarding the best approach to redraw objects in each OnRender event.

    Based on my understanding, NinjaTrader follows a retained mode rendering system where the recommended practice is to redraw all the objects in each OnRender event, regardless of whether they have changed or not. This approach ensures that the chart is consistently rendered with the latest state of all elements.

    In light of this, I wanted to confirm if my understanding is correct and if it is indeed the recommended approach in NinjaTrader to redraw all objects in each OnRender event when using SharpDX for rendering. Additionally, I would appreciate any insights or best practices related to optimizing the rendering process or any other considerations I should be aware of.

    Thank you in advance for your guidance and assistance!

    #2
    Hello trendisyourfriend,

    Yes that is correct, each time OnRender is called whatever code is being used to render in that render pass ends up visualized. If you don't call the rendering code it won't be displayed.

    If you were to only call the rendering code sometimes when something has changed it would just briefly flash and then disappear.

    As a best practice your data calculation and conditions should usually go in OnBarUpdate and you would store anything you need to render to variable. In OnRender you just have your rendering logic which references those variables. That ensures values are only being calculated at the slower OnBarUpdate rate and OnRender doesn't have to do any specific heavy work and just renders.
    JesseNinjaTrader Customer Service

    Comment


      #3
      What about the drawing objects which are outside the visible chart area ? should my script try to filter those objects and skip them or should i render them just as well even though they may be located outside the visible area ?

      Comment


        #4
        Hello trendisyourfriend,

        If by Drawing objects you mean the built in tools like Draw.Text those should generally be used from OnBarUpdate to create or modify them to avoid high resource usage. if you are using drawing objects from OnRender that can cause high utilization because of how frequently OnRender is called. Any objects outside of the viewable area that do not have autoscale enabled will be hidden.

        If you otherwise mean items you are rendering using the RenderTarget , if the X/Y values for that rendering is outside of the chart area it just won't be rendered. It would be more efficient to avoid trying to render a lot of items that are outside of the chart X/Y/W/H.

        Most scripts that use OnRender utilize series or variables that are set from OnBarUpdate. Inside OnRender you would use a loop over the FromIndex and ToIndex to only render data that is on the currently visible bars, anything on other bars is not used. In your X/Y calculation logic you can check for anything that is outside of the chart panels X/Y/W/H to ignore those data points.

        Here is an example of the loop: https://ninjatrader.com/support/help...htsub=frominde




        JesseNinjaTrader Customer Service

        Comment


          #5
          You can check to see whether the objects are off the visible screen and if they're not going to show, don't draw them. You CAN still draw them, but it's just wasting cycles.
          Bruce DeVault
          QuantKey Trading Vendor Services
          NinjaTrader Ecosystem Vendor - QuantKey

          Comment


            #6
            Originally posted by QuantKey_Bruce View Post
            You can check to see whether the objects are off the visible screen and if they're not going to show, don't draw them. You CAN still draw them, but it's just wasting cycles.
            QuantKey_Bruce
            Thank you for your valuable advice; I genuinely appreciate it. If I may extend my request for your assistance, I would like to inquire further about obtaining the X and Y coordinates of the painted area on a chart. My objective is to determine whether my drawing object, such as a sharpDX rectangle or a line, intersects or overlaps with the visible area. Any guidance on how to proceed with this would be greatly appreciated.

            I was thinking about using these commands:

            // find the first visible price
            double topPrice = chartScale.GetValueByY(1);
            This would return the first price at the top of the chart

            // find the last visible price
            double bottomPrice = chartScale.GetValueByY( chartScale.Height - 1 );

            then to filter my lines i might use a for loop to check if it falls between the topPrice and bottomPrice.

            What do you think ?


            Last edited by trendisyourfriend; 05-26-2023, 09:21 AM.

            Comment


              #7
              Be sure to check for nulls and values to be in range so you don't get exceptions.

              You can tell the first and last bar index on the screen by ChartBars.FromIndex and ChartBars.ToIndex.

              Using those, you can tell the first and last datetimes using ChartBars.Bars.GetTime(ChartBars.FromIndex) and ChartBars.Bars.GetTime(ChartBars.ToIndex).

              Given these, you can also get the first and last pixel coordinates using ChartControl.GetXByBarIndex or ChartControl.GetXByTime.

              The top and bottom of the screen are available several ways, but one approach is the top price is ChartScale.MaxValue and the bottom price is ChartScale.MinValue.

              Those in pixels are ChartScale.GetYByValue(ChartScale.MaxValue) and ChartScale.GetYByValue(ChartScale.MinValue).

              You can also access ChartScale.GetValueByY and use ChartPanel.Y and ChartPanel.H.

              You can also access ChartPanel.X and ChartPanel.W to get the pixel dimensions.

              Between all of these, you should have no trouble deducing what of your things you would be drawing are visible.

              The cases are sort of like (for each axis): (1) the object is completely off the screen to the left, (2) the object is completely off the screen to the right, or (3) some other thing, such as it's spanning the screen or completely on the screen or part of it's on the screen - these are the ones you need to draw. Same for the vertical axis.
              Bruce DeVault
              QuantKey Trading Vendor Services
              NinjaTrader Ecosystem Vendor - QuantKey

              Comment


                #8
                Thanks very much that's exactly the commands i was looking for.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by naanku, Today, 07:25 PM
                0 responses
                6 views
                0 likes
                Last Post naanku
                by naanku
                 
                Started by milfocs, Today, 07:23 PM
                0 responses
                4 views
                0 likes
                Last Post milfocs
                by milfocs
                 
                Started by PaulMohn, Today, 06:59 PM
                0 responses
                7 views
                0 likes
                Last Post PaulMohn  
                Started by bortz, 11-06-2023, 08:04 AM
                48 responses
                1,753 views
                0 likes
                Last Post carnitron  
                Started by Jonker, 04-27-2024, 01:19 PM
                3 responses
                24 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Working...
                X