Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

casting error?

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

    #31
    Originally posted by NinjaTrader_ChelseaB View Post
    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);
    }
    Thank You!

    But this approach wouldn't be useful in the code snippet with the if statement I posted above.

    What do you think of the idea to replace the "normal" null check with that method, that uses threadsafe list copy, I posted above?
    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

    Comment


      #32
      Hello td_910,

      This would remove a drawing object if the tag name matches a specific name.

      If this is not what you are wanting to achieve, could you provide further detail about what you would like to achieve?
      Chelsea B.NinjaTrader Customer Service

      Comment


        #33
        Hi Chelsea,

        I'd like to replace e.g. a statement like that:
        Code:
        if (DrawObjects["Thumbs_DT"+(CurrentBar-2)] == null) do something[B][B][B][B][B][B][B][B][B][B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B]
        with
        Code:
        if (DrawObjectsCheck["Thumbs_DT"+(CurrentBar-2)] == null) do something[B][B][B][B][B][B][B][B][B][B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B]
        then I'm able to replace all DrawObjects checks with search&replace in my code, which would be hundreds I'd like to us this method
        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;         }  [B][B][B][B][B][B][B][B][B][B][/B][/B][/B][/B][/B][/B][/B][/B][/B][/B]

        Comment


          #34
          Hello td_910,

          Is DrawObjectsCheck a list copy of DrawObjects.ToList()?

          A List<IDrawingTool> object is not going to have index keys like a dictionary..

          With multiple instances of the indicator on the chart, using key indexes in the actual DrawObjects() collection and removing objects will cause a collection modified error.

          The code of the DrawObjectsCheck method appears to return a tag name. I'm not certain what returning this tag is meant to be used for.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #35
            Hi Chelsea,

            The tag name is just a string, that is returned, to be different than null, so I can just search and replace in the existing code

            Since there is a list copy created with each method call and the tags have a unique string, why would there be a "a collection modified error"

            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;
                    }

            Comment


              #36
              Hello td_910,

              Looping through a list copy of the DrawObjects is fine to do.

              Specifically it's using a key index with the actual DrawObjects collection that would cause the error.

              The code you have posted above I would not expect to cause an error.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #37
                Hi Chelsea,

                I didn't get this (I'm not enough of a progammer, yet ;-)) :

                Specifically it's using a key index with the actual DrawObjects collection that would cause the error.

                Good to hear, so I can go ahaed and modify the code for all my indicators.
                Thanks for your help.

                Originally posted by NinjaTrader_ChelseaB View Post
                Hello td_910,

                Looping through a list copy of the DrawObjects is fine to do.

                Specifically it's using a key index with the actual DrawObjects collection that would cause the error.

                The code you have posted above I would not expect to cause an error.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                608 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                355 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                105 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                560 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                561 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X