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

Generic method to access DrawObject coordinates/price level

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

    Generic method to access DrawObject coordinates/price level

    Hi Team,

    I am working on an indicator that will alert the user the current price is near one of the HorizontalLine or lines that are drawn on the Chart.
    What I am looking for now, is a way to get the line's price (more important - HorizontalLine, and later I will try the same with other lines)


    From the documentation, I should use this (Start & End Anchor):
    Code:
    (draw as DrawingTools.HorizontalLine).StartAnchor.Price
    But using this code, I got a reference error:
    Code:
    Error on calling OnBarUpdate method on bar 0
    I know that I need to check if the object from the DrawingTool list is actually HorizontalLine and I do that using the following code:
    Code:
    if ( draw.GetType().Name == "HorizontalLine")
    Trying to do the same check with this code - didn't work
    Code:
    if (draw is DrawingTools.HorizontalLine)



    This is the full code I am using now to test:
    Code:
    foreach (DrawingTool draw in DrawObjects.ToList())
    {
    Print(draw.DisplayName + " --> " + draw.GetType().Name);
    
    if ( draw.GetType().Name == "HorizontalLine")
    {
    double myDbl = (draw as DrawingTools.HorizontalLine).StartAnchor.Price;
    Print("FOUND!!!! " + draw.DisplayName); //myDbl.ToString());
    }
    
    }

    Thank you for the help

    #2
    Hello liadmz,

    Thank you for the post.

    What was the actual error you had seen when running this? The error you provided is just the location and time when it happened or OnBarUpdate and Bar 0. There should be additional details with that for a C# error or other platform generated errors.

    You can find a full sample of working with drawing objects and identifying them in the following link. If you plan to export this as an assembly you would need to instead use the details in the second link:





    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      This is the error I got:
      System.NullReferenceException: Object reference not set to an instance of an object.at NinjaTrader.NinjaScript.Indicators.AlertNext2Line. OnBarUpdate()


      I was using the documentation:
      https://ninjatrader.com/support/help...ub=drawobjects

      But I cannot see any reference to the way I can get the price value of the line.

      Essentially, I want to be able to alert when the current price (Close[0]) is getting close the the lines drawn on te chart.


      I used this code to test the HorizontalLine and I get this strange behavior - seems like the code doesn't recognize the HorizontalLine Object as such
      Click image for larger version

Name:	Untitled.png
Views:	291
Size:	21.3 KB
ID:	1129251



      Thank you!
      Last edited by liadmz; 11-24-2020, 01:13 PM.

      Comment


        #4
        I have further looked into other questions here in the forum, this one is similar to my case:
        https://ninjatrader.com/support/foru...e-doesn-t-work


        If I try to use the code from this post, I get the same error


        Code:
        foreach (DrawingTool draw in DrawObjects.ToList())
        {
        string drawAsString = draw.ToString();
        if (drawAsString.Contains("HorizontalLine"))
        {
        Print("HorizontalLine Object: " + " GetType().Name: " + draw.GetType().Name + " draw.Tag: " + draw.Tag);
        DrawingTools.HorizontalLine hl = draw as DrawingTools.HorizontalLine;
        Print(hl.StartAnchor.Price);
        }
        }

        Result:
        HorizontalLine Object: GetType().Name: HorizontalLine draw.Tag: Horizontal Line 53
        Indicator 'AlertNext2Line': Error on calling 'OnBarUpdate' method on bar 3: Object reference not set to an instance of an object.



        Seems like the code is unable to convert the Draw Object into HorizontalLine object for some reason.

        That is when:
        GetType().Name --> Return "HorizontalLine"
        But
        if (draw is DrawingTools.HorizontalLine) will return false





        Last edited by liadmz; 11-24-2020, 01:42 PM.

        Comment


          #5
          Hello liadmz,

          The error you are getting is a C# error meaning that the object you used was null when you tried to use it. The following line would be one example of an area which can produce that error. If hl is null then accessing StartAnchor will cause that error.

          Code:
          Print(hl.StartAnchor.Price);
          In what you provided you are mixing two of the samples that we provide, one uses type casting and one does not. The second link I provided shows the way that does not use type casting, that uses the string types to locate the objects and then uses dynamic to avoid casts. If you are using a exported dll version of your code that would be important. If you are not you can stick to not using strings and just use the sample from the first link I provided.

          Based on the observation you made that when the types name is HorizontalLine but the cast fails that leads me to believe you are using this in a context where you need to use dynamic. That would be the sample from the second link I provided, that does not use type casts, you use strings to know if the type is equal.

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Hi Jesse,

            Thank you for the reply.
            I tried to use the dynamic method and it did work

            just for me to fully understand how it works,

            How do I know when should I use the regular way (first link) and when do i need to use the dynamic one?


            Thank you

            Comment


              #7
              Hello liadmz,

              Basically you would run into the situation you just did where the type cast fails. You should also see the object is null.

              Type casting will only work for types that are not held in the NinjaTrader.Custom assembly, this applies to any exports you make. Once your code is exported as a dll, referencing a type that's in the custom assembly would require using dynamic/strings. If you are working in the NinjaTrader.Custom context or just as source code you generally won't need to use dynamic because the context is the same, all the code is in the same place.

              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Hi Jesse,

                Thank you for the help!

                Now it works as expected and I can programmatically access all the horizontal lines on the chart.


                Is there a way to get also the Pivot lines (from the indicator "Pivots" PP, R1, R2, S1, S2) and the lines from the OrderFlow Volume Profile (POC and Value Area Lines)?


                Click image for larger version

Name:	Screenshot_8.png
Views:	267
Size:	29.2 KB
ID:	1129440

                Thank you!


                Attached Files

                Comment


                  #9
                  Hello liadmz,

                  You can call most indicators directly from the indicator you are making, that would be the correct approach in most cases. That makes sure the indicator being used has the data it needs and can be used in all contexts where the parent indicator is being used. This is also important for more complex indicators some have custom methods to access their data and not plotted objects.

                  The orderflow tools require a specific use which is shown in their help guide documentation. The volume profile is not currently exposed for NinjaScript.

                  The pivots you can access its plot data through code.



                  Its always best to check the indicator help guide page first to see what the example shows. For custom indicators you may need to explore the code if available to see how it plots, depending on how it draws would determine how you can access the data. some items simply don't expose data.

                  You can also use the chart controls indicator collection to find indicators if you are trying to do something custom in realtime. The indicators collection is dynamic like the drawing object collection, it will only be present in realtime and when the chart is present, the indicator load order is also not guaranteed. From this context you can access the plot data by using the indicators plots GetValueAt method and an index similar to how you would access data from OnRender.





                  Please let me know if I may be of further assistance.


                  JesseNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by algospoke, 04-17-2024, 06:40 PM
                  3 responses
                  26 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by bmartz, 03-12-2024, 06:12 AM
                  3 responses
                  28 views
                  0 likes
                  Last Post NinjaTrader_Zachary  
                  Started by Aviram Y, Today, 05:29 AM
                  2 responses
                  9 views
                  0 likes
                  Last Post Aviram Y  
                  Started by gentlebenthebear, Today, 01:30 AM
                  1 response
                  8 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by cls71, Today, 04:45 AM
                  1 response
                  7 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Working...
                  X