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

Looping through DrawObjects shown in Guide doesn't work?

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

    Looping through DrawObjects shown in Guide doesn't work?

    I'm following the code in the last example here: https://ninjatrader.com/support/help...rawobjects.htm

    I have about 650 horizontal lines on my chart and I'm trying to export their price to a text file but the loop is not registering that there are ANY horizontal lines in DrawObjects.

    As you can see in the screenshot on line 96, the pinned the variable 'draw' IS a HorizontalLine but the boolean is still FALSE. This screenshot was taken as I stepped through the loop and lines 98-100 were never touched.

    Am I going crazy?

    I tried with and without the full domain so there's no change if I remove NinjaTrader.NinjaScript

    Also, is line 100 correct for getting the Price of the horizontal line?

    Please help and thanks!
    Last edited by hillborne; 07-05-2020, 11:19 PM.

    #2
    Hello hillborne, thanks for your question.

    I suspect the problem is when the looping code is being called. Try moving the code so that it runs at or after State.RealTime, do you get the same issue?

    I look forward to hearing from you.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris,

      Thank you for responding.

      It's being called here:

      Code:
              protected override void OnBarUpdate()
              {
                  if(CurrentBar == 0)
                  {
                      ImportHSRYvalues();
                  }
      
                  if ((State == State.Realtime) && (CurrentBar % 100 == 0))
                  {//Every X number of bars write to file.
                      Print("Exporting HSR Y values on bar: " + CurrentBar);
                      ExportHSRYvalues();
                  }
              }
      I want to be able to import and see the horizontal lines even when not connected and export periodically when connected.

      I saw some other posts that mentioned having to use typeof or even dynamic like in the below link but is that still the case due to issue with ID #NTEIGHT-11857:
      https://ninjatrader.com/support/forum/forum/ninjatrader-8/indicator-development/99202-looping-through-drawobjects-odd-behaviour

      Can you draw some horizontal lines on your chart and follow the example in the guide and see if you have the same behavior and if not, why?


      Thanks again!

      Comment


        #4
        Hello hillborne, thanks for your reply.

        I'll try the same thing and let you know what I find.

        Thanks in advance for your patience.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hello hillborne, thanks for your patience.

          It looks like this post is the solution if you need the draw objects before realtime:


          That would be the only solution for pre-realtime loading. We have a feature request to add an official "OnDrawingToolAdded()" event method ID#SFT-1358. I'll add a vote to this for you.

          Kind regards.
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            Thanks for the reference, Chris.

            I'm still a newb when it comes to C# so I'm having trouble understanding how that post helps since events are still a mystery to me.

            Please confirm that the example in the guide is not workable unless the state is realtime? If yes, I'll work around that and see what I can do and suggest the guide be amended or at least a note left.

            Is the problem the fact that the objects are user drawn?

            Would script drawn objects be detected outside of realtime?

            The main reason I'm trying to do this is to synchronize drawn objects (at least horizontal lines) between the e-mini and micro e-mini since they are fundamentally the same instrument.

            Do you know of any way to do this more efficiently? I thought I could perhaps alter the global xml files for drawn objects before starting NT but that seems impractical and limited to once per day.

            Thanks, again.

            Comment


              #7
              Hello hillborne, thanks for your reply.

              This is only an issue for user drawn draw objects. Script drawn objects would be available before real-time. If I were making such a script, I would have a .txt file that held the price levels of the lines, then load in that text file and draw the lines using that data. One could even export a .txt file using an indicator that holds the price level information, to avoid editing the txt file manually. We have examples on file IO here:
              https://ninjatrader.com/support/helpGuides/nt8/?using_streamreader_to_read_fro.htm - Reading
              https://ninjatrader.com/support/help...o_write_to.htm - Writing

              Please let me know if I can assist any further.
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Hi Chris,

                I've struggled with this some more and am running into a problem with casting.
                I'm running this under State.Realtime now.

                The guide says to cast the iterator as a DrawingTools.HorizontalLine but when I do that, it remains null when I go to access its StartAnchor.Price.
                Can you explain why the below code doesn't work?

                Btw, I had to us the ToString() method to access the drawn objects because the guide doesn't work...don't know why.

                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;
                                        outputList.Add(hl.StartAnchor.Price);
                                    }
                                }
                Can you please try out what is written in the guide and see if there's a problem there or on my side? I feel like the guide is out of date based on some code updates but not sure.
                Thanks!
                Last edited by hillborne; 07-08-2020, 09:32 PM.

                Comment


                  #9
                  Hello hillborne, thanks for your patience. I have an example here that works for me:

                  Code:
                    public class TestDrawObjectArray : Indicator
                      {
                          protected override void OnStateChange()
                          {
                              if (State == State.SetDefaults)
                              {
                                  //...
                              }
                          }
                  
                          bool drewline = false;
                          private HorizontalLine HZ;
                          protected override void OnBarUpdate()
                          {
                              if(drewline)
                              {
                                  foreach (DrawingTool draw in DrawObjects.ToList())
                                  {
                                      if (draw is DrawingTools.HorizontalLine)
                                      {              
                                          if(HZ != null)
                                          {
                                              Print("HLine Object: " + draw.Tag);
                                              Print(HZ.StartAnchor.Price);
                                          }
                  
                                      }
                                  }
                              }
                  
                              if(!drewline)
                              {
                                  HZ = Draw.HorizontalLine(this, "HLINE", Close[0], Brushes.Red);
                                  drewline = true;
                              }
                  
                          }
                      }
                  It looks like you just need a null check for your line object.

                  Please let me know if I can assist any further.
                  Chris L.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi Chris,

                    Your code doesn't address the main conflict I have with the NinjaTrader guide on iterating existing drawn objects.

                    I created a new chart on the future ES, added a dozen horizontal lines and then created a new indicator with your code and it did not print the existing horizontal lines' price. It just printed the closing price a dozen times.

                    The 2 problems I have are:
                    1. When you iterate existing user-drawn objects as per the guide with "if (draw is DrawingTools.HorizontalLine)" it doesn't recognize the user drawn lines as being horizontal lines.

                    2. If you instead search for "if (draw.ToString.Contains("HorizontalLine"), it works and so you can then try to access the properties of 'draw.' But first you have to cast it to a temporary variable, right? When you write "DrawingTools.HorizontalLine HZ = draw as DrawingTools.HorizontalLine" it fails for some reason. HZ becomes null which means the cast failed, right? Keep in mind I'm an amateur programmer so I could be missing something.

                    I'm sorry to be a pain about this so could you please create a new chart on ES, add a bunch of horizontal lines and then try to print their prices and see how to resolve the above 2 problems.

                    My sincere thanks for your help so far!

                    Comment


                      #11
                      Hello hillborne, thanks for your reply.

                      I wanted to let you know I am working on an example and will reply with my findings ASAP.

                      Thanks in advance for your patience.
                      Chris L.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello, thanks for your patience.

                        I was able to get user drawn lines from the OnStateChanged method, so we do not need to rely on OnBarUpdate being called:

                        Code:
                            
                        public class TestDrawObjectArray : Indicator
                            {
                                private HorizontalLine HZ;
                        
                                protected override void OnStateChange()
                                {
                                    if (State == State.SetDefaults)
                                    {
                                        //....
                                    }
                                    else if (State == State.Configure)
                                    {
                                    }
                                    else if (State == State.Realtime)
                                    {
                                        foreach (DrawingTool draw in DrawObjects.ToList())
                                        {
                                            if (draw is DrawingTools.HorizontalLine)
                                            {              
                                                    HZ = draw as DrawingTools.HorizontalLine;
                                                    Print("HLine Object: " + HZ.Tag);
                                                    Print(HZ.StartAnchor.Price);
                        
                                            }
                                        }
                                    }
                                }
                        Please let me know if this does not resolve your inquiry.
                        Chris L.NinjaTrader Customer Service

                        Comment


                          #13
                          Hi Chris,

                          Sadly, the two problems persist.

                          I opened up a new chart on CL, drew 5 lines. I then made a new indicator and used your code with one additional print statement to show count of drawn objects.
                          (private HorizontalLine HZ; is declared just like in your code but not visible in my screenshots)

                          The first time I ran the script, it didn't print anything because it doesn't recognize "draw is DrawingTools.HorizontalLine."
                          I bypassed that by searching the ToString method to see if it contains "HorizontalLine" and then I ran into the problem of the casting fails to treat draw AS DrawingTools.HorizontalLine and leaves draw as null.

                          Can you show me your output window? I have no idea what to do or why there is a problem.

                          My only guess is that draw needs to be cast to something else? A base object/interface? Or maybe DrawObjects have been changed since the guide was last updated?

                          Please see the 2 screenshots from both problems.

                          I'm sorry if there's something basic I'm missing! Thanks.
                          Last edited by hillborne; 07-14-2020, 11:15 PM.

                          Comment


                            #14
                            Hello hillborne, thanks for your reply.

                            I attached my test script thats working for manually drawn lines. My output window gives:

                            Code:
                            HLine Object: Horizontal Line
                            3206.02732984293
                            HLine Object: Horizontal Line 2
                            3204.83109947644
                            HLine Object: Horizontal Line 3
                            3201.50041884817
                            Could you try a testing the source code I posted with manually drawn objects?

                            ​​​​​​​
                            Attached Files
                            Chris L.NinjaTrader Customer Service

                            Comment


                              #15
                              Hi Chris,

                              I think I've found a reproducible problem that has been affecting our efforts here. This is what I did to identify and recreate the problem.

                              Started up NT and connected to Continuum.
                              Created a new CL chart and added a few Horizontal Lines.
                              I then created a new indicator with your code, complied it, added it to the chart and it worked! WTF?

                              Since I wanted to make some changes to figure out what was going on, I removed the indicator from the chart.
                              I then commented out your first Print statement: "Print("HLine Object: " + HZ.Tag);" and saved and compiled the indicator.
                              I added the indicator back to the chart and....nothing was printed. No StartAnchor.Prices were shown in the output.

                              Hmmm, okay. I removed the indicator from the chart.
                              I uncommented the first Print statement (to put it back to exactly the way you wrote it), saved and recompiled.
                              I added it back to the chart and again, nothing printed!

                              I restarted NT and reconnected.
                              I added your indicator back to the CL chart and now it Printed both lines even though nothing changed.

                              I repeated the steps above removing the indicator, commenting out the first print statement, adding it back and saw nothing printed again.

                              This time I noticed that something else is changing in the indicator while saving: the Ninjascript auto-generated code at the bottom.
                              Since all I did was comment out one print statement, I think the error might be coming from that auto-generated code.

                              I see that there are references to caching of the indicator in the auto-gen code so maybe that's the problem when working on State.Realtime...I don't know, just guessing.

                              Could you go through the above steps and see if the same thing happens to you? The only work around I could find is to restart NT and reconnect every time I make any change to the indicator.

                              Thanks!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by FAQtrader, Today, 03:35 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post FAQtrader  
                              Started by rocketman7, Today, 09:41 AM
                              5 responses
                              16 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by frslvr, 04-11-2024, 07:26 AM
                              9 responses
                              124 views
                              1 like
                              Last Post caryc123  
                              Started by selu72, Today, 02:01 PM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Zachary  
                              Started by WHICKED, Today, 02:02 PM
                              2 responses
                              19 views
                              0 likes
                              Last Post WHICKED
                              by WHICKED
                               
                              Working...
                              X