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

Events for objects drawn on the chart

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

    Events for objects drawn on the chart

    Hello,

    I have looked on the forums and references for this feature, it seems it's not available by default.

    I wonder if there's any third-party library I could use, or maybe there has been an update for it that I'm not aware of.

    I would like events for:

    DrawingTool Created
    DrawingTool Deleted
    DrawingTool Properties Changed

    If this isn't available, can it be made a feature request?

    Regards,

    #2
    Hello Waxavi,

    Thank you for your post.

    The ChartScales has a ChartObjects object which is an observable collection and therefore has a CollectionChanged NotifyCollectionChangedEvent. One example of utilizing this may be found on this forum post:



    I'm also including another example that uses this below. It can detect if a drawing tool is created, removed, or changed.

    Please let us know if we may be of further assistance to you.
    Attached Files
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hello Kate,

      Edit
      Thanks for replying, and excuse my ignorance, what should I replace "PropertyChangedDrawingToolExample" with? there's a missing reference.
      If I replace it with Line, there's no "PropertyChanged" inside this object (Line 63), implementing this INotifyPropertyChanged to all tools is quite something

      Would appreciate it if you could give me the complete sample.

      Edit2: So it seems this adds "Spy" properties which I suppose wraps the properties I need inside a DrawingTool?

      Regards,
      Last edited by Waxavi; 02-05-2022, 06:12 PM.

      Comment


        #4
        Hello Waxavi,

        Thank you for your reply.

        The example script works for me without error - what is the error you are receiving?

        Thanks in advance; I look forward to assisting you further.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hello Kate,

          I don't have "PropertyChangedDrawingToolExample", it's missing from the files, I guess it's a modified drawing tool.

          Comment


            #6
            Hello Waxavi,

            Thank you for your reply.

            Ah, I forgot there's an associated drawing tool script it needs. Here's an importable version with both the above example and that drawing tool script.

            Please let us know if we may be of further assistance to you.
            Attached Files
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Thank you, Kate,
              I think I can work with this

              But I would like that these events would be available by default inside the API in the future.

              Regards,

              Comment


                #8
                Code:
                // I opted to use ChartObjects.CollectionChanged here to detect new drawing objects
                // + subscribe to them in the HandleChartObjectsChanged method
                // rather than do this in the performance killer i.e., OnRender()
                if (ChartControl != null)
                {
                  foreach (ChartScale cs in ChartControl.ChartPanels.Where(x => x != null).SelectMany(cp => cp.Scales))
                  {
                     Print("Subscribing to HandleChartsObjectAdded");
                     cs.ChartObjects.CollectionChanged += HandleChartObjectsChanged;
                  }
                  needsUnsubscribed = true;
                }
                Inside State.Configure, I would like to ask why does it need to subscribe to all ChartScales? I think it triggers the same event 3 times or more
                Last edited by Waxavi; 02-08-2022, 01:23 PM.

                Comment


                  #9
                  Hello Waxavi,

                  Thank you for your reply.

                  That line is checking to see if an object was added to the ChartObjects for each price scale. It is necessary as a chart can have objects attached to the left, right or overlay scales.

                  Please let us know if we may be of further assistance to you.
                  Kate W.NinjaTrader Customer Service

                  Comment


                    #10
                    Okay, I think all is good now, thanks for your help Kate

                    Comment


                      #11
                      Hello Waxavi/Kate,

                      Is there a way to know code an event so it knows when an object is done drawing?

                      The code on post #2 triggers the event when an object is initially created. The calculations require all anchors to be drawn. From my understanding the DrawObject.PropertyChanged can be used to make another event listener.

                      Below is the part where I get confused. Is the e.PropertyName actually IsSelectedSpy or DrawingStateSpy?

                      Code:
                      // this should be called anytime one of our "spy" properties from the drawing tool changes
                              private void PropertyChangedInIndicator(object sender, PropertyChangedEventArgs e)
                              {
                                  // we can filter through the property name to call various methods
                                  // Note: we set this property name during the DrawingTools "DoPropertyChanged()" method
                                  // if you want to add/change properties, please make sure to update its string
                                  switch (e.PropertyName)
                                  {
                                      case "IsSelectedSpy":
                                      {
                                          DoWorkOnSelectionChanged(myLine.IsSelectedSpy);
                                          break;
                                      }
                                      case "DrawingStateSpy":
                                      {
                                          DoWorkOnDrawingStateChanged(myLine.DrawingStateSpy);
                                          break;
                                      }
                                      
                                  }
                              }​
                      Then the next part (down below) makes sense. Do stuff based on the state. This is the part where the calculations would be made

                      Code:
                      // do something when drawing state changes
                              private void DoWorkOnDrawingStateChanged(DrawingState drawingState)
                              {
                                  if (drawingState == DrawingState.Building)
                                  {
                                      Print("Do Building State work");
                                  }
                                  if (drawingState == DrawingState.Editing)
                                  {
                                      Print("Do Editing State work");
                                      myLine.Stroke.Brush = Brushes.Red;
                                  }
                                  if (drawingState == DrawingState.Moving)
                                  {
                                      Print("Do Moving State work");
                                      myLine.Stroke.Brush = Brushes.Purple;
                                  }
                                  if (drawingState == DrawingState.Normal)
                                  {
                                      Print("Do Normal State work");
                                      myLine.Stroke.Brush = Brushes.Yellow;
                                  }
                              }​

                      So does e.PropertyName actually return IsSelectedSpy or DrawingStateSpy or are these custom drawing tool properties? My goal is to call a method once the new Risk Reward tool is done drawing (or in DrawingState.Normal)

                      Best regards,
                      Unsuitable
                      Unsuitable
                      NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

                      Comment


                        #12
                        Hello Unsuitable,

                        DrawingStateSpy is an added public variable that when set from OnMouseDown on line 90 calls DoPropertyChanged() on line 44 which triggers the PropertyChangedEvent on line 69.
                        This event is what the indicator is subscribed to on line 63.

                        The e.PropertyName would be referring to the DrawingStateSpy property which is updating with the PropertyChangedEvent.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Hello ChelseaB,

                          I see where the confusion is. The example above uses a custom drawing tool. In my case I would like to capture the properties of the a Risk Reward tool once it's done drawing.

                          Best regards,

                          Unsuitable
                          Unsuitable
                          NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

                          Comment


                            #14
                            Hello Unsuitable,

                            You would need to make a copy of the RiskReward drawing tool and modify this to add the property changed handling demonstrated in this example.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Hello Chelsea_B,

                              Right, that's not ideal.

                              Here's a sudo solution. Have the code use the event listener for changes in DrawObjects. Filter for states. If its building then store the object. Then in OnBarUpdate, check to see if the stored object is null. If it it's not, keep checking the drawing state until its normal. At which point run the appropriate code and set the stored object pointer back to null.

                              I figured if OnBarUpdate gets called regardless if the there's code in there or not (assumption), do one more check and see if the code should compute the relevant data.

                              The only other solution I could think pf would be to launch an event listener for the next mouse click on the chart. However, that's not as explicit as it assumes the next click will complete the Risk Reward drawing. Plus it may be over engineering the problem.

                              Best regards,

                              Unsuitable
                              Unsuitable
                              NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by futtrader, 04-21-2024, 01:50 AM
                              4 responses
                              41 views
                              0 likes
                              Last Post futtrader  
                              Started by Option Whisperer, Today, 09:55 AM
                              1 response
                              12 views
                              0 likes
                              Last Post bltdavid  
                              Started by port119, Today, 02:43 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post port119
                              by port119
                               
                              Started by Philippe56140, Today, 02:35 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post Philippe56140  
                              Started by 00nevest, Today, 02:27 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post 00nevest  
                              Working...
                              X