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

Need event when an IDrawObject is moved/resized/deleted

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

    Need event when an IDrawObject is moved/resized/deleted

    I have an advanced indicator that draws rectangles and rays on the chart using DrawRectangle() and DrawRay(). It needs to allow the user to move/resize/delete these drawn objects, but the indicator MUST be notified if the user does this so it can adjust its internal state and other display elements accordingly. However, I have not been able to find any way to register for such a notification.

    Ideally, there would be an event on the IDrawObject, perhaps called something like ChartObjectChanged, so that we could register an event handler function to fire when the drawn object is moved/resized/deleted on the chart. Less ideal but still workable would be an event on the ChartControl itself, just like the currently existing GlobalChartObjectChanged event but providing events for changes to *non-global*, indicator-drawn chart objects.

    Does any such event already exist? I looked through the members and googled and couldn't find anything. This seems like a really obvious feature given that users can move drawobjects around. Thanks!

    #2
    PolyTekPatrick, that would be unfortunately no dedicated event here, but you could check through the DrawObjectsCollection that would track changes for the drawing objects running on the primary series :

    BertrandNinjaTrader Customer Service

    Comment


      #3
      Originally posted by PolyTekPatrick View Post
      but the indicator MUST be notified if the user does this so it can adjust its internal state and other display elements accordingly
      PolyTekPatrick,

      Did you find a solution to this? I am running into the same issue!

      Any help is greatly appreciated.
      Daniel

      Comment


        #4
        NinjaTrader was no help as you can see earlier, but yes after quite a bit of experimentation I did find a hack that seems to work for ChartObject move/resize notifications. However, I didn't find anything that provides ChartObject deletion notifications. So if you are listening NinjaTrader, we still need a proper, officially-supported event notification for this!

        Here's my hackish code for change notifications though:
        Code:
        protected override void OnStartUp()
        {
            if (ChartControl != null && ChartControl.ChartObjects != null)
            {
                // Handle ChartObject collection insertion event (fires anytime a drawn object is modified).
                // Doesn't really make sense that InsertComplete works for this,
                // but it must be due to how NinjaTrader's internal implementation works.
                ChartControl.ChartObjects.InsertComplete += new CollectionEventHandler(ChartObjects_InsertComplete);
            }
        }
        
        private void ChartObjects_InsertComplete(object sender, CollectionEventArgs e)
        {
            ChartObject chartObject = e.Value as ChartObject;
            if (chartObject == null || chartObject.UserDrawn)
            {
                return;
            }
            if (chartObject.DrawType == DrawType.Ray)
            {
                ChartRay ray = chartObject as ChartRay;
                if (ray == null)
                {
                    return;
                }
                if (ray.Tag.StartsWith("YourPrefixHere"))
                {
                    // Now you can read or update the StartBar, Anchor1Y, etc... here
                    ray.StartBar = whatever;
                }
            }
        }

        Comment


          #5
          PolyTekPatrick,

          While I understand Ninjatrader's stance on supporting the ChartControl object, they are typically pretty good at giving you a nudge in the right direction, so I thank them for that. But even more so, thanks to members of the community like yourself that share the good stuff (and quick!)! I think this is really going to help! Thanks!

          While on the subject, what I am building is something that performs like the "measure" tool, but outputs custom analysis. The way I am approaching it is to listen for a click, note the location, then repeatedly draw a line from that start point to the high/low of the bar where the cursor currently is.

          The problem is, the code is only run once per tick, but I need it run every time the cursor moves. When I call SendKeys.Send("{F2}"); it will invoke the "line" tool (while I am also drawing MY line). This is great, since it now runs my code for every move of the mouse (for some reason), but in the end, I have two lines drawn. I've tried a few different ways to avoid this, but no real solution.

          Does what I'm saying make sense (it's hard to explain)? Have you created anything like a custom drawing tool? Perhaps just a way to invoke the "line" drawing tool while automatically giving it a custom Tag (so you can modify or monitor it later)?

          Thanks again!
          Daniel

          Comment


            #6
            No, I haven't made a custom drawing/measure tool. I'll bet someone has though... try looking around some more with google, or make a new thread to ask your question.

            If you need to do things based on mouse cursor movement though, you should be using one of the standard .NET mouse events for that. And using SendKeys for anything seems like a bad approach to me. Just draw/move your line using the relevant events to run your code.

            Comment


              #7
              Originally posted by PolyTekPatrick View Post
              No, I haven't made a custom drawing/measure tool. I'll bet someone has though... try looking around some more with google, or make a new thread to ask your question.

              If you need to do things based on mouse cursor movement though, you should be using one of the standard .NET mouse events for that. And using SendKeys for anything seems like a bad approach to me. Just draw/move your line using the relevant events to run your code.
              Yeah, I've been digging for quite awhile, but I haven't found any good examples out there yet.

              Yeah, the SendKeys thing was more of an experiment to see how Ninjatrader is doing things. So far, lots of dead ends.

              Thanks again,
              Daniel

              Comment


                #8
                Originally posted by PolyTekPatrick View Post
                However, I didn't find anything that provides ChartObject deletion notifications. So if you are listening NinjaTrader, we still need a proper, officially-supported event notification for this!
                Just noticed this. Can't you just use the RemoveComplete event?

                ChartControl.ChartObjects.RemoveComplete += new CollectionEventHandler(ChartObjects_RemoveComplete );

                etc...

                Daniel

                Comment


                  #9
                  No, RemoveComplete doesn't really do what you want. In my opinion, NinjaTrader's implementation for handling ChartObjects is poorly designed. Everytime any change needs to be made to a ChartObject, they remove it from the collection (firing the RemoveComplete event you just mentioned), make the change (or maybe it's a fresh new object, not sure on this), and then reinsert it back into the collection. Hence why I mentioned in my code comment above that InsertComplete shouldn't really work for this, but it does due to their specific implementation.

                  Comment


                    #10
                    Originally posted by PolyTekPatrick View Post
                    No, RemoveComplete doesn't really do what you want.
                    Right after I posted that, I started seeing the behavior that you were describing. If it's true that the line is removed, then re-added each time it's changed, then it's no wonder that RemoveComplete is fired so often (as well as InsertComplete).

                    I suppose you could maintain a collection of lines (tag names, objects, or whatever), then every time RemoveComplete is fired, check if all lines are still there. If not, then you know the one that was actually deleted. Yeah, not very efficient.

                    Daniel

                    Comment


                      #11
                      Rats, this technique doesn't seem to work in my strategy code.

                      In the strategy, InsertComplete is called once for each DrawHorizontalLine() that I draw (such as drawing 2 lines, at stop price and target price, drawn inside OnOrderUpdate) but then is never called again.

                      I have unlocked the lines in the code right after drawing, then I goto the chart window and drag the stop and target lines around manually, but my copious Print statements show that my ChartObject_InsertComplete() routine in the strategy is never called.

                      Are the InsertComplete/RemoveComplete events for indicators only?

                      Does anyone have a technique to detect the conclusion of the user's movement of DrawObjects drawn by the strategy?

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by suroot, 04-10-2017, 02:18 AM
                      4 responses
                      3,021 views
                      0 likes
                      Last Post truepenny  
                      Started by Stanfillirenfro, Today, 07:23 AM
                      1 response
                      6 views
                      0 likes
                      Last Post NinjaTrader_Gaby  
                      Started by cmtjoancolmenero, Yesterday, 03:58 PM
                      2 responses
                      22 views
                      0 likes
                      Last Post cmtjoancolmenero  
                      Started by olisav57, Yesterday, 07:39 PM
                      1 response
                      9 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Started by cocoescala, 10-12-2018, 11:02 PM
                      7 responses
                      944 views
                      0 likes
                      Last Post Jquiroz1975  
                      Working...
                      X