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

casting error?

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

    #16
    Hello td_910,

    Syntax errors do not cause run-time errors. Syntax errors cause compile errors.

    You are experiencing a run-time error, you are not experiencing a compile error.

    No, I do not see any errors with syntax.

    What is the exact line causing the run-time error?

    Are you able to reproduce using the test script I have provided you?

    If the error is not being produced when refreshing the chart, when is the error being produced?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #17
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello td_910,

      Syntax errors do not cause run-time errors. Syntax errors cause compile errors.

      You are experiencing a run-time error, you are not experiencing a compile error.

      No, I do not see any errors with syntax.

      What is the exact line causing the run-time error?

      Are you able to reproduce using the test script I have provided you?

      If the error is not being produced when refreshing the chart, when is the error being produced?
      yes, I know, I meant the syntax for the dynamic object handling and dynamic object properties
      the script runs without errors on the same chart

      Comment


        #18
        looking at that code, there should all errors been taken care of
        even checking twice, that the objects are not null before accessing them

        what exactly does the error msg mean?

        this stuff is delaying the launch of my website since two weeks
        all NT7 versions of my indicators run without any errors, but I want to start providing version 8 indicators as well

        Comment


          #19
          Code:
          2019-03-26 17:15:00:411|3|4|Indicator 'PASuppResBreakoutTest': Error on calling 'OnBarUpdate' method on bar 1167:
          
          [A]NinjaTrader.NinjaScript.DrawingTools.Line cannot be cast to
          [B]NinjaTrader.NinjaScript.DrawingTools.Line.
          
          Type A originates from '99e5424db07446b8aa696f44becabc5c, Version=8.0.17.2, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Users\Thomas\Documents\NinjaTrader 8\tmp\99e5424db07446b8aa696f44becabc5c.dll'.
          
          Type B originates from 'cce54770972f475cb2d53c957eb5d606, Version=8.0.17.2, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Users\Thomas\Documents\NinjaTrader 8\tmp\cce54770972f475cb2d53c957eb5d606.dll'.
          both DLLs and the corresponding XML files have the same size, the XML files are the same and the DLLs have a handful of differences only (using FC)

          Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.

          Comment


            #20
            Hello td_910,

            I notice you are still casting instead of using the dynamic object.

            In the example I have provided you, the dynamic object is used.


            The error is telling you that this object exists in two places and the object being used isn't the original.

            NinjaTrader takes the code you have written and then compiles it as part of the internal code. I can't get into much detail with this internal process but its the reason the dynamic keyword must be used when making a dll.

            The dynamic keyword basically means, hey its the same class please don't care where it came from.

            Below is a link to the microsoft documentation.
            Learn how to use the dynamic type. The dynamic type is a static type, but dynamic objects bypass static type checking.


            If the script I have provided for you doesn't reproduce, this could be a good place to start from.

            At this point you have not provided enough information for some to assist, unless they have a specific example of where they have run into this before and gotten around it.


            It would save considerable time if we didn't have to guess which line is the issue and you could tell us the exact problem and the exact line of code.
            You can use prints to find the exact line of code as I have previously recommended.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #21
              I think I misunderstood your 1st proposal
              I thought one has to use dynamic type, then check if it's the wanted type, then cast and then access object properties
              Sorry, I'm a trader and no programmer

              there is one step to much, the cast :-)

              this should do the trick?

              Code:
                          #region delete line two and three bars back
                          if (IsFirstTickOfBar)
                          {
                              if (DrawObjects["BT_Swing_Low"+unique+(CurrentBar-2)] != null && DrawObjects["BT_Swing_Low"+unique+(CurrentBar-1)] != null)
                              {
                                  dynamic BT_Swing_Low1 = DrawObjects["BT_Swing_Low"+unique+(CurrentBar-1)];
                                  dynamic BT_Swing_Low2 = DrawObjects["BT_Swing_Low"+unique+(CurrentBar-2)];
              
                                  if (BT_Swing_Low1.ToString().Equals("NinjaTrader.NinjaScript.DrawingTools.Line") &&
                                      BT_Swing_Low2.ToString().Equals("NinjaTrader.NinjaScript.DrawingTools.Line"))
                                  {
                                      //BT_Swing_Low1 = (NinjaTrader.NinjaScript.DrawingTools.Line) DrawObjects["BT_Swing_Low"+unique+(CurrentBar-1)];
                                      //BT_Swing_Low2 = (NinjaTrader.NinjaScript.DrawingTools.Line) DrawObjects["BT_Swing_Low"+unique+(CurrentBar-2)];
              
                                      if (BT_Swing_Low1 != null && BT_Swing_Low2 != null &&
                                          BT_Swing_Low1.StartAnchor.BarsAgo == BT_Swing_Low2.StartAnchor.BarsAgo+1 &&
                                          BT_Swing_Low1.StartAnchor.Price   == BT_Swing_Low2.StartAnchor.Price)
              
                                          RemoveDrawObject("BT_Swing_Low"+unique+(CurrentBar-2));
                                  }
                              }
                              if (DrawObjects["BT_Swing_Low"+unique+(CurrentBar-3)] != null && DrawObjects["BT_Swing_Low"+unique+(CurrentBar-1)] != null)
                              {
                                  dynamic BT_Swing_Low1 = DrawObjects["BT_Swing_Low"+unique+(CurrentBar-1)];
                                  dynamic BT_Swing_Low2 = DrawObjects["BT_Swing_Low"+unique+(CurrentBar-3)];
              
                                  if (BT_Swing_Low1.ToString().Equals("NinjaTrader.NinjaScript.DrawingTools.Line") &&
                                      BT_Swing_Low2.ToString().Equals("NinjaTrader.NinjaScript.DrawingTools.Line"))
                                  {
                                      //BT_Swing_Low1 = (NinjaTrader.NinjaScript.DrawingTools.Line) DrawObjects["BT_Swing_Low"+unique+(CurrentBar-1)];
                                      //BT_Swing_Low2 = (NinjaTrader.NinjaScript.DrawingTools.Line) DrawObjects["BT_Swing_Low"+unique+(CurrentBar-3)];
              
                                      if (BT_Swing_Low1 != null && BT_Swing_Low2 != null &&
                                          BT_Swing_Low1.StartAnchor.BarsAgo == BT_Swing_Low2.StartAnchor.BarsAgo+2 &&
                                          BT_Swing_Low1.StartAnchor.Price   == BT_Swing_Low2.StartAnchor.Price)
              
                                          RemoveDrawObject("BT_Swing_Low"+unique+(CurrentBar-3));
                                  }
                              }
                          }
                          #endregion

              Comment


                #22
                Hello td_910,

                Yes, with those lines commented out I would not be expecting casting errors.

                Was this posted because you are still getting an casting error?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #23
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hello td_910,

                  Yes, with those lines commented out I would not be expecting casting errors.

                  Was this posted because you are still getting an casting error?
                  Everything is fine now, thanks for your patience.

                  Comment


                    #24
                    I have Visual Studio running in the background all day and caught this

                    And I thought the null checks were safe

                    Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.

                    Comment


                      #25
                      Hello td_910,

                      Thank you for your note.

                      Null checks are safe if the index is valid or when using a variable that is not a collection.

                      The issue is that a tag is used as the index to find an element in a collection that was modified.
                      This likely means an object was removed from the collection which modified the collection.

                      What version of NinjaTrader 8 are you using?


                      Are there multiple instances of the script added to the chart?
                      https://ninjatrader.com/support/foru...417#post805417


                      Below is a link to a post that describes making a copy of the collection with ToList() as the copy will not be modified.
                      https://ninjatrader.com/support/foru...754#post811754

                      Using a for loop would also work to prevent the error.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #26
                        Hi Chelsea,

                        I'm using NT 8.0.17.2 64-bit.

                        Yes there are multiple instances of the indicator on that chart (2).

                        I'm already using a unique string to separate the drawing objects
                        Code:
                                        //unique identifier
                                        unique = BarsPeriod.Value.ToString("N") + Guid.NewGuid().ToString("N");
                        This is a snippet of an if statement of another indicator. How would I use a thread safe list copy with this many objects (OnPriceChange)?

                        Code:
                                        //prior opposite signal
                                        DrawObjects["Thumbs_DT"+(CurrentBar-2)] == null && DrawObjects["Thumbs_DT"+(CurrentBar-1)] == null && DrawObjects["Thumbs_DT"+(CurrentBar)] == null &&
                                        DrawObjects["Thumbs_TT"+(CurrentBar-2)] == null && DrawObjects["Thumbs_TT"+(CurrentBar-1)] == null &&  DrawObjects["Thumbs_TT"+(CurrentBar)] == null &&
                                        !(DrawObjects["Thumbs_mDT"+(CurrentBar-1)] != null && Bars.BarsSinceSession>8 && MAX(High,2)[4]<High[3]) &&
                        
                                        //prior bar and current are thumbs already
                                        (DrawObjects["Thumbs_mDB"+(CurrentBar-1)] == null || Low[2]!=Low[1]) &&
                                        DrawObjects["Thumbs_DB"+(CurrentBar)] == null && DrawObjects["Thumbs_TB"+(CurrentBar)] == null)

                        Comment


                          #27
                          Hello td_910,

                          Below is how a list copy of the collection can be made.
                          Code:
                          List<IDrawingTool> objs = DrawObjects.ToList();
                          for(int i = 0; i < objs.Count; i++)
                          {
                          IDrawingTool obj = objs[i];
                          Print(obj.GetType().Name);
                          }
                          Last edited by NinjaTrader_ChelseaB; 07-03-2019, 10:34 AM.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #28
                            Originally posted by NinjaTrader_ChelseaB View Post
                            Hello td_910,

                            Below is how a list copy of the collection can be made.\
                            Code:
                            List<IDrawingTool> objs = DrawObjects.ToList();
                            for(int i = 0; i < objs.Count; i++)
                            {
                            IDrawingTool obj = objs[i];
                            Print(obj.GetType().Name);
                            }
                            And how would I use that in that example if statement?

                            Code:
                             //prior opposite signal
                            DrawObjects["Thumbs_DT"+(CurrentBar-2)] == null && DrawObjects["Thumbs_DT"+(CurrentBar-1)] == null && DrawObjects["Thumbs_DT"+(CurrentBar)] == null &&
                            DrawObjects["Thumbs_TT"+(CurrentBar-2)] == null && DrawObjects["Thumbs_TT"+(CurrentBar-1)] == null && DrawObjects["Thumbs_TT"+(CurrentBar)] == null &&
                            !(DrawObjects["Thumbs_mDT"+(CurrentBar-1)] != null && Bars.BarsSinceSession>8 && MAX(High,2)[4]<High[3]) &&
                            
                            //prior bar and current are thumbs already
                            (DrawObjects["Thumbs_mDB"+(CurrentBar-1)] == null || Low[2]!=Low[1]) &&
                            DrawObjects["Thumbs_DB"+(CurrentBar)] == null && DrawObjects["Thumbs_TB"+(CurrentBar)] == null)

                            Comment


                              #29
                              Code:
                                      // Loops through the DrawObjects collection via a threadsafe list copy
                                      public string DrawObjectsCheck(string DrawObjectTag)
                                      {
                                          foreach(var tool in DrawObjects.ToList())
                                          {
                                              if (tool.Tag == DrawObjectTag)
                                              {
                                                  return DrawObjectTag;
                                              }
                                          }
                                          return null;
                                      }

                              using that method, this
                              Code:
                              [FONT=arial]DrawObjects["Thumbs_DT"+(CurrentBar-2)] == null && [/FONT]
                              becomes that
                              Code:
                              DrawObjectsCheck("Thumbs_DT"+(CurrentBar-2)) == null &&
                              ???

                              Comment


                                #30
                                Hello td_910,

                                Code:
                                private string unique = string.Empty;
                                private int barIndex;
                                
                                In OnStateChange():
                                else if (State == State.DataLoaded)
                                {
                                    unique    = Guid.NewGuid().ToString();
                                }
                                
                                In OnBarUpdate():
                                // draw a dot
                                if (State == State.Historical && CurrentBar == Count - 10)
                                {
                                    Draw.Dot(this, string.Format("dot{0}{1}", unique, CurrentBar), true, 0, High[0] + 3 * TickSize, Brushes.Blue);
                                    barIndex = CurrentBar;
                                }
                                
                                loop through a copy of the drawing objects collection (which won't be modified while we're looping)
                                List<IDrawingTool> objs = DrawObjects.ToList();
                                for (int i = 0; i < objs.Count; i++)
                                {
                                    IDrawingTool obj = objs[i];
                                    Print(obj.Tag);
                                
                                    Print(string.Format("dot{0}{1}\r\n", unique, barIndex));
                                // if its the drawing object i was looking for, remove it
                                    if (obj.Tag == string.Format("dot{0}{1}", unique,  barIndex))
                                        RemoveDrawObject(obj.Tag);
                                }
                                Chelsea B.NinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by martin70, 03-24-2023, 04:58 AM
                                15 responses
                                114 views
                                0 likes
                                Last Post NinjaTrader_Jesse  
                                Started by The_Sec, Today, 02:29 PM
                                1 response
                                7 views
                                0 likes
                                Last Post NinjaTrader_Jesse  
                                Started by jeronymite, 04-12-2024, 04:26 PM
                                2 responses
                                31 views
                                0 likes
                                Last Post NinjaTrader_BrandonH  
                                Started by Mindset, 05-06-2023, 09:03 PM
                                10 responses
                                265 views
                                0 likes
                                Last Post NinjaTrader_BrandonH  
                                Started by michi08, 10-05-2018, 09:31 AM
                                5 responses
                                743 views
                                0 likes
                                Last Post NinjaTrader_ChelseaB  
                                Working...
                                X