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

    casting error?

    I get two weird errors:

    019-03-21 15:50:00:670|3|4|Indicator '': Error on calling 'OnBarUpdate' method on bar 502: [A]NinjaTrader.NinjaScript.DrawingTools.Line cannot be cast to [B]NinjaTrader.NinjaScript.DrawingTools.Line. Type A originates from 'cdc306d6242949f6b5fd6be48c346d5e, Version=8.0.17.2, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Users\Thomas\Documents\NinjaTrader 8\tmp\cdc306d6242949f6b5fd6be48c346d5e.dll'. Type B originates from '388c0163315540e5bd34109cfaa14564, Version=8.0.17.2, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Users\Thomas\Documents\NinjaTrader 8\tmp\388c0163315540e5bd34109cfaa14564.dll'.

    2019-03-21 15:55:00:135|3|4|Indicator '': Error on calling 'OnBarUpdate' method on bar 502: [A]NinjaTrader.NinjaScript.DrawingTools.Text cannot be cast to [B]NinjaTrader.NinjaScript.DrawingTools.Text. Type A originates from 'cdc306d6242949f6b5fd6be48c346d5e, Version=8.0.17.2, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Users\Thomas\Documents\NinjaTrader 8\tmp\cdc306d6242949f6b5fd6be48c346d5e.dll'. Type B originates from '388c0163315540e5bd34109cfaa14564, Version=8.0.17.2, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Users\Thomas\Documents\NinjaTrader 8\tmp\388c0163315540e5bd34109cfaa14564.dll'.

    I do an unlock of all drawing objects, the indicator draws, there are lines and text

    Code:
                #region Unlock draw objects
                if (CurrentBar >= Count-2 && IsFirstTickOfBar)
                    foreach (DrawingTool draw in DrawObjects.ToList())
                        if (draw.Tag.StartsWith("BT_Swing_High"+unique)  ||
                            draw.Tag.StartsWith("BT_Swing_Low"+unique)   ||
                            draw.Tag.StartsWith("fBO_Swing_High"+unique) ||
                            draw.Tag.StartsWith("fBO_Swing_Low"+unique)
                            )
                            draw.IsLocked = false;
                #endregion
    from the time the error occurred it looks like its during "IsFirstTickOfBar"
    Last edited by td_910; 03-21-2019, 10:26 AM.

    #2
    Hello td_910,

    Is this happening with an open source script?

    (I just want to ensure its not an assembly which would need the dynamic keyword)
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello td_910,

      Is this happening with an open source script?

      (I just want to ensure its not an assembly which would need the dynamic keyword)
      https://ninjatrader.com/support/help...assemblies.htm
      Hi Chelsea,
      it's one of my proprietary indicators, not an assembly, yet. I plan to export it into an assembly though.

      Comment


        #4
        Hello td_910,

        I've made a test script with the code from the help guide that uses the dynamic keyword as you mentioned you will be exporting this as an assembly.

        Are you able to reproduce the error using this test script?
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello td_910,

          I've made a test script with the code from the help guide that uses the dynamic keyword as you mentioned you will be exporting this as an assembly.

          Are you able to reproduce the error using this test script?
          the script runs without any errors :-)
          how would I address text objects? just like that?

          Code:
                          foreach (dynamic text in DrawObjects.ToList())
                          {
                              if (text.ToString().Equals("NinjaTrader.NinjaScript.DrawingTools.Text"))
                                  // Indicates if this is a manually drawn or script generated line
                                  Print("Line Object: " + text.Tag + " Manually Drawn: " + taxt.IsUserDrawn);
                          }
          I have another piece of code, but that is dealing explicitly with lines only

          Code:
                      #region delete line two and three bars back
                      if (IsFirstTickOfBar)
                      {
                          if (DrawObjects["BT_Swing_High"+unique+(CurrentBar-2)] != null && DrawObjects["BT_Swing_High"+unique+(CurrentBar-1)] != null)
                          {
                              NinjaTrader.NinjaScript.DrawingTools.Line BT_Swing_High1 = (NinjaTrader.NinjaScript.DrawingTools.Line)DrawObjects["BT_Swing_High"+unique+(CurrentBar-1)];
                              NinjaTrader.NinjaScript.DrawingTools.Line BT_Swing_High2 = (NinjaTrader.NinjaScript.DrawingTools.Line)DrawObjects["BT_Swing_High"+unique+(CurrentBar-2)];
          
                              if (BT_Swing_High1 != null && BT_Swing_High2 != null &&
                                  BT_Swing_High1.StartAnchor.BarsAgo == BT_Swing_High2.StartAnchor.BarsAgo+1 &&
                                  BT_Swing_High1.StartAnchor.Price   == BT_Swing_High2.StartAnchor.Price)
                                  RemoveDrawObject("BT_Swing_High"+unique+(CurrentBar-2));
                          }
          
                          if (DrawObjects["BT_Swing_High"+unique+(CurrentBar-3)] != null && DrawObjects["BT_Swing_High"+unique+(CurrentBar-1)] != null)
                          {
                              NinjaTrader.NinjaScript.DrawingTools.Line BT_Swing_High1 = (NinjaTrader.NinjaScript.DrawingTools.Line)DrawObjects["BT_Swing_High"+unique+(CurrentBar-1)];
                              NinjaTrader.NinjaScript.DrawingTools.Line BT_Swing_High2 = (NinjaTrader.NinjaScript.DrawingTools.Line)DrawObjects["BT_Swing_High"+unique+(CurrentBar-3)];
          
                              if (BT_Swing_High1 != null && BT_Swing_High2 != null &&
          
                                  BT_Swing_High1.StartAnchor.BarsAgo == BT_Swing_High2.StartAnchor.BarsAgo+2 &&
                                  BT_Swing_High1.StartAnchor.Price == BT_Swing_High2.StartAnchor.Price)
          
                                  RemoveDrawObject("BT_Swing_High"+unique+(CurrentBar-3));
                          }
                      }
                      #endregion
          can this be the cause as well?

          Comment


            #6
            Hello td_910,

            Yes, all drawing objects would use this idea.
            You would cast as dynamic, then check the ToString() to ensure its the type you want.

            The new code you have provided is doing casting without checking the type first.
            There is a possibility this code can cause a casting error.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              like this:

              Code:
                              if (DrawObjects["BT_Swing_High"+unique+(CurrentBar-2)] != null && DrawObjects["BT_Swing_High"+unique+(CurrentBar-1)] != null)
                              {
                                  dynamic BT_Swing_High1 = DrawObjects["BT_Swing_High"+unique+(CurrentBar-1)];
                                  dynamic BT_Swing_High2 = DrawObjects["BT_Swing_High"+unique+(CurrentBar-2)];
              
                                  if (BT_Swing_High1.ToString().Equals("NinjaTrader.NinjaScript.DrawingTools.Line") && BT_Swing_High2.ToString().Equals("NinjaTrader.NinjaScript.DrawingTools.Line"))
                                  {
                                      BT_Swing_High1 = (NinjaTrader.NinjaScript.DrawingTools.Line)DrawObjects["BT_Swing_High"+unique+(CurrentBar-1)];
                                      BT_Swing_High2 = (NinjaTrader.NinjaScript.DrawingTools.Line)DrawObjects["BT_Swing_High"+unique+(CurrentBar-2)];
              
                                      if (BT_Swing_High1 != null && BT_Swing_High2 != null &&
                                          BT_Swing_High1.StartAnchor.BarsAgo == BT_Swing_High2.StartAnchor.BarsAgo+1 &&
                                          BT_Swing_High1.StartAnchor.Price   == BT_Swing_High2.StartAnchor.Price)
                                          RemoveDrawObject("BT_Swing_High"+unique+(CurrentBar-2));
                                  }
                              }

              Comment


                #8
                Hello td_910,

                With the checks for null, this appears right.

                Is this causing an error?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  No errors, this is just to verify my adaption of your proposal ;-)

                  up to now I always did something like that:
                  Code:
                                      Text Prior_xtF_Text = (Text)DrawObjects[xTF.ToString("N0")+"tF_Long" + (CurrentBar)];
                                      if (Prior_xtF_Text != null) Prior_xtF_Text.AreaBrush = bullColor;
                  Do I really have to go through all of my code and put in the dynamic stuff, even if I know the type of the object for sure?

                  Is that an issue with NT7 as well?
                  Last edited by td_910; 03-21-2019, 03:33 PM.

                  Comment


                    #10
                    Hello td_910,

                    If you plan to export as an assembly, yes, this is necessary.

                    https://ninjatrader.com/support/help...assemblies.htm


                    I don't believe this is necessary for NinjaTrader 7.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      another one :-(
                      running from the script and not the assembly

                      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'.


                      this is the only part of the code, where there is some casting be done:


                      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
                      Last edited by td_910; 03-26-2019, 11:36 AM.

                      Comment


                        #12
                        Hello td_910,

                        What is the exact line of code causing the error?
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_ChelseaB View Post
                          Hello td_910,

                          What is the exact line of code causing the error?
                          I have just the error msg. (see above)

                          the script compiles without any error

                          Comment


                            #14
                            Hello td_910,

                            More specifically, which specific line is causing the runtime error?

                            You will need to use prints and debug the code and find out which line is the line that needs to be addressed.

                            Add prints after each line of logic. Which print is the last print to appear before the error?
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_ChelseaB View Post
                              Hello td_910,

                              More specifically, which specific line is causing the runtime error?

                              You will need to use prints and debug the code and find out which line is the line that needs to be addressed.

                              Add prints after each line of logic. Which print is the last print to appear before the error?
                              hitting F5 doesn't reproduce the error, market replay?

                              the code worked flawless, before adding the dynamic stuff to the casting

                              I took out the entire section of code from the 1st post in the thread, so this is the ONLY part of the indicator code, that deals with casting objects
                              can't see any syntax errors in that code, do you?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by habeebft, Today, 07:27 AM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_ChristopherS  
                              Started by AveryFlynn, Today, 04:57 AM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by Max238, Today, 01:28 AM
                              4 responses
                              37 views
                              0 likes
                              Last Post Max238
                              by Max238
                               
                              Started by r68cervera, Today, 05:29 AM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by geddyisodin, Today, 05:20 AM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X