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

PathTool gives Object reference not set to an instance of an object

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

    PathTool gives Object reference not set to an instance of an object

    I'm drawing 4 anchors with

    "Draw.PathTool(
    NinjaScriptBase owner,
    string tag,
    bool isAutoScale,
    int anchor1BarsAgo,
    double anchor1Y,
    int anchor2BarsAgo,
    double anchor2Y,
    int anchor3BarsAgo,
    double anchor3Y,
    int anchor4BarsAgo,
    double anchor4Y)"

    and everything works fine if I draw them normally, but then I tried drawing them using OnRender and a "ChartBars.ToIndex; ChartBars.FromIndex"-loop to increase performance.
    All of a sudden I'm getting "Object reference not set to an instance of an object".
    When debugging, the value that is null is PathTool ChartAnchors, but all the other values are there barsAgo, price etc.

    I then tried with inserting a list of anchors with
    "Draw.PathTool(
    NinjaScriptBase owner,
    string tag,
    bool isAutoScale,
    List<ChartAnchor> chartAnchors,
    Brush brush,
    DashStyleHelper dashStyle)" and everything draws, but unfortunately ChartAnchor only accepts DateTime and not BarsAgo.

    Since I'm using Renko bars, the Time value is not always correct for the bars, meaning sometimes when price has moved very fast, the bars are drawn with the same Time, but different barIndex. This causes the drawn lines to be off by several bars as seen in the picture.

    Click image for larger version

Name:	swing points wrong.png
Views:	132
Size:	99.7 KB
ID:	1229199
    From experience I've learned that Time is generally not a good value too use when drawing on charts that have bar types such as Renkos and other time independent charts. I've tried using Tick Replay but there isn't any difference.

    Any suggestions on how I can solve this? A new feature to be able to create a PathTool list of chartAnchors with BarsAgo values or even barIndex would make things lots easier.

    #2
    Hello Borgen,

    Thank you for your post.

    In many situations where an "Object reference not set to an instance of an object" error is thrown, this may be resolved by adding in a null reference check. This is explained with some examples on the following page:


    You mentioned you are trying to draw the PathTool in Onrender; do you mean that you are still using the Draw.PathTool() method within OnRender? If so, you would actually want to modify your script to use SharpDX for your custom chart rendering if you are looking to reap the benefits of enhanced performance. Please see the "Using DrawObjects vs custom graphics in OnRender()" section of the performance best practices here for an example:


    We also have a resource page about using SharpDX for custom rendering here:


    NinjaTrader also comes with the indicator SampleCustomRender and the source code may be viewed within the NinjaScript Editor for an example of how to use OnRender() for custom rendering and help you get started with your own drawings via custom rendering.

    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the tip, but I don't see how checking for a null reference would be beneficial here. I'm creating the PathTool from values that are already verified. The method wouldn't be called if there weren't values to put in it.

      The method I'm using with 4 anchors is not supposed to have a reference for ChartAnchors is it? If I use the version with a list of ChartAnchors it gets referenced though. I still don't see why it works with the same object outside of OnRender, but when I move it in there it gives the error.

      OnRender with "ChartBars.ToIndex"-loops gives performance enhancements for Draw methods as well. I wasn't aware it's only intended for SharpDX, because I've used it for drawing lines, dots, rectangles etc in addition to the base.OnRender and it gives a boost to performance.

      I rather not use SharpDX since the amount of excessive code I have to write feels like I'm coding a game and not an indicator. I can get most of what I want done with the regular Draw methods. Plus SharpDX has been deprecated for years and is being retired. If Ninjatrader is ever updated to .NET core it won't even work anymore from what I read.

      I actually ended up just switching to drawing regular lines instead of a PathTool and now it works.​

      Comment


        #4
        Hello Borgen,

        Thank you for your reply.

        Draw methods should not be called in OnRender(). OnRender() is recommended for custom rendering or modifying the properties of a default NinjaScript draw object (such as an object drawn in OnBarUpdate), but not to call the draw methods directly. When a draw method is called, such as Draw.PathTool(), that object will call OnRender() within its own script (you may see this use of OnRender inside of the tool in the source code found at NinjaScript Editor > DrawingTools > PathTool). I advise calling the NinjaScript draw methods from OnBarUpdate().

        This is the explanation offered in the NinjaScript Best Practices page I previously linked:
        "When using Draw methods, a new instance of the Draw object is created including its custom rendering and calculation logic. These methods are convenient in many situations, but can quickly introduce performance issues if used too liberally. In some situations, you may see better performance for rendering via SharpDX in OnRender().

        Why: Each draw object instance will see its own OnRender() called to render values. If you instead implement custom rendering in the your object, you would only see a single OnRender() call for your custom created graphics.

        With just a little extra code (much less than what is in the Draw methods) custom SharpDX rendering greatly reduces CPU and Memory consumption"
        Even when using the draw methods, they are utilizing SharpDX that has been scripted as part of the drawing tool being used. Please refrain from using draw methods inside of OnRender(), as this is not their intended use and may result in errors. I am glad to hear that drawing lines works rather than using the PathTool, however, please be sure those lines are being drawn outside of OnRender() such as within OnBarUpdate().

        Thank you for your time and understanding.
        Emily C.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by trilliantrader, 04-18-2024, 08:16 AM
        4 responses
        18 views
        0 likes
        Last Post trilliantrader  
        Started by mgco4you, Today, 09:46 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by wzgy0920, Today, 09:53 PM
        0 responses
        9 views
        0 likes
        Last Post wzgy0920  
        Started by Rapine Heihei, Today, 08:19 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by Rapine Heihei, Today, 08:25 PM
        0 responses
        10 views
        0 likes
        Last Post Rapine Heihei  
        Working...
        X