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

Iterating drawn objects

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

    Iterating drawn objects

    I've managed to successfully iterate through the list of DrawObjects using in OnBarUpdate() :

    foreach (DrawingTool draw in DrawObjects.ToList()){
    Print("A");
    if(b.Equals(draw.GetType())){
    Print("B");
    This works fine in recognizing that I have drawn a line. I am then able to programmatically change it's tag to a custom value and read it's various values, anchors etc.

    However, if the line is drawn prior to enabling the strategy the "B" will not print. I noticed this while debugging another part of my code, when compiling, disabling the strategy and re-enabling it failed to recognize the same line I had drawn prior to the recompile. Is there a way to iterate the draw objects that are on the chart prior to enabling the strategy?


    #2
    Hello Chippy,

    The DrawObjects collection would only be available once the script is running in realtime, there is not a way to access it before that because its loading time is not guaranteed to be in sync with the script. It should recognize a line that was already on the chart however it may take at least one call to OnBarUpdate before it populates the collection.


    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks Jesse... there's something weird going on with it that I can't quite put my finger on.... It's only a problem sometimes after deleting and redrawing lines. Something appears to be altering the DrawObject type but I'm not familiar enough with the Class to troubleshoot it further.

      I believe I have a work around though... I can simply iterate through the draw objects and search for the required tags since your developers saw fit to use tags with keywords that identify the type object.

      Comment


        #4
        Well... it mostly works. For some reason, if I run the strategy live, then add my lines, then disable the strategy I can't re-enable the strategy! When I tick the box in the control panel it just disappears instantly. For a split second I can see the connection column flash "My NT Continuum" before it disappears. Any idea why that might happen?

        Comment


          #5
          Hello Chippy,

          That sounds like an error, I would suggest looking in the control center log or the output winnow to see if an error is happening when you enable it.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Thanks Jesse... I found the error but don't know what to do about it.

            Strategy 'ConsecutiveRangeBars': Error on calling 'OnBarUpdate' method on bar 25: [A]NinjaTrader.NinjaScript.DrawingTools.Line cannot be cast to [B]NinjaTrader.NinjaScript.DrawingTools.Line. Type A originates from '42eef5aac7f94ff9a4cf4080dc38be5b, Version=8.0.26.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Users\me\Documents\NinjaTrader 8\tmp\42eef5aac7f94ff9a4cf4080dc38be5b.dll'. Type B originates from '0c1a9d2cab30460aa79154f0e08fac03, Version=8.0.26.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Users\me\Documents\NinjaTrader 8\tmp\0c1a9d2cab30460aa79154f0e08fac03.dll'.


            Here is the line where I'm casting:

            foreach (DrawingTool draw in DrawObjects.ToList()){

            myLine=(NinjaTrader.NinjaScript.DrawingTools.Line) draw;

            of course, myLine is declared outside the class.

            I appreciate your help

            Comment


              #7
              Hello Chippy,

              If you are using an assembly then you nee to avoid casting to specific types and use dynamic instead, there is a sample of that concept in the following link:



              You would find the type by using the string name of the type and then use dynamic to assume properties on the object instead of casting directly to a type.

              JesseNinjaTrader Customer Service

              Comment


                #8
                Hi Jesse.... I am not creating DLLs!

                Comment


                  #9
                  Jesse, I could still use some guidance on this. The error I posted is still happening. To reiterate. I am not creating dlls... this is just a simple bit of code. I've boiled it down to this (and still get the error posted above):

                  private int drOb = 0;
                  NinjaTrader.NinjaScript.DrawingTools.Line myLine = new NinjaTrader.NinjaScript.DrawingTools.Line();

                  protected override void OnBarUpdate()
                  {
                  if(CurrentBar<2){ClearOutputWindow(); return;}
                  drOb = DrawObjects.Count;
                  if(drOb >0){
                  foreach (DrawingTool draw in DrawObjects.ToList()){
                  myLine=(NinjaTrader.NinjaScript.DrawingTools.Line) draw;
                  }
                  }
                  }
                  }

                  Comment


                    #10
                    Hello Chippy,

                    This snippet of code does not produce an error by itself in a clean environment.

                    Do you see the same if you test the snippet in a clean environment? (Close NinjaTrader, Rename the "Documents\NinjaTrader 8" folder, Reinstall)

                    The matter looks specific to the DLL's noted in Documents\NinjaTrader 8\tmp\.

                    If you remove those DLL's does the issue persist?

                    It is possible those DLL's may not be accessing the DrawObjects as we would suggest when using DLL's which would create the ambiguity.
                    JimNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by AaronKoRn, Today, 09:49 PM
                    0 responses
                    11 views
                    0 likes
                    Last Post AaronKoRn  
                    Started by carnitron, Today, 08:42 PM
                    0 responses
                    10 views
                    0 likes
                    Last Post carnitron  
                    Started by strategist007, Today, 07:51 PM
                    0 responses
                    11 views
                    0 likes
                    Last Post strategist007  
                    Started by StockTrader88, 03-06-2021, 08:58 AM
                    44 responses
                    3,980 views
                    3 likes
                    Last Post jhudas88  
                    Started by rbeckmann05, Today, 06:48 PM
                    0 responses
                    9 views
                    0 likes
                    Last Post rbeckmann05  
                    Working...
                    X