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

Setting the 'Attached To' Property of a Draw Object to the Chart Programmatically

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

    Setting the 'Attached To' Property of a Draw Object to the Chart Programmatically

    Hello,

    The indicator draws an object. The code down below sets the 'Attached To' property to null and when saving, closing, and reopening the NT, the draw object remains. Opposed to disappearing when refreshing the indicator, this is good. However, when the object is first drawn, the 'Attached To' is set to null not the chart. BUT when reopening NT, the draw object is now set to the chart. Functionally, it doesn't matter that it's set to null when initially drawing it, but how does one grab the chart and attached the object to it? *The text is a DrawingTools.Text object

    Code:
    text.AttachedTo.ChartObject = chartScale.GetFirstChartBars();
    text.DrawnBy = null;
    Thanks
    Unsuitable
    NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

    #2
    Hello Unsuitable,

    The drawing object AttachedTo property is not meant to be set after the object is drawn, and is instead meant to provide information about what the object is attached to.


    The parent should be set when first calling the draw method as the NinjaScriptBase owner parameter.
    Draw.Text(NinjaScriptBase owner, string tag, string text, int barsAgo, double y)

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi ChelseaB,

      So if I understand correctly, the owner is equal to the AttachedTo property? For the NinjaScriptBase owner field, it's set 'this' (the indicator). How do I set to the chart that the indicator is attached to? this.Owner doesn't work... that would be too easy.

      I want this 'attached to' property

      Click image for larger version

Name:	XgjMo1X.png
Views:	672
Size:	61.8 KB
ID:	1123309

      to be set to the thing that user drawn objects are set to.

      Click image for larger version

Name:	9ClVWHp.png
Views:	631
Size:	51.0 KB
ID:	1123310

      A bit digging and it seems that the user drawn objects are attached to the ChartPanel (could be completely wrong). This where I'm at so far with the method. It gives this error
      https://i.imgur.com/XAhbAU9.png.

      Code:
      ChartPanel chartPanel = (*how to get chart panel*) as ChartPanel;
      text.AttachedTo.ChartObject = chartPanel.ChartObjects;
      text.DrawnBy = null;
      text.IsLocked = false;

      Thanks
      Attached Files
      Unsuitable
      NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

      Comment


        #4
        Hello Unsuitable,

        It only accepts NinjaScriptBase. A chart is not that type of object.

        What are you trying to achieve?

        Are you wanting to leave drawing objects on the chart after removing the indicator? (this is not supported)

        Are you trying to get global objects?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi ChelseaB,

          Originally posted by NinjaTrader_ChelseaB View Post
          Hello Unsuitable,
          Are you wanting to leave drawing objects on the chart after removing the indicator? (this is not supported)
          Yeah I know it's not officially supported, however, using the code from post #1 it's able to draw an object that is independent of the indicator. It can be saved and reloaded. The problem with it now is that when the indicator is deleted or changed, it errors out, "Unable to cast object of type 'NinjaTrader.Gui.ChartBars' to type 'NinjaTrader.NinjaScriptBase". This makes sense as I'm setting completely wrong values.

          Now getting the 'AttachedTo' property from an object that has been user drawn, it's able to set the 'AttachedTo' to the chart. This works, but when initially removing the indicator and trying to move text, it jumps to the beginning of the chart. This only happens once afterward it's ok. **I noticed that if I move the draw object before deleting the indicator, the draw object remains in place** I imagine by moving it, its "updating and committing to memory" its properties (position in the chart and 'AttachedTo' property, which was set after the object was instantiated and is not recommended) Plus this solution requires a user drawn object on the chart. This may be remedied by setting the owner properly.

          Code:
          text.AttachedTo = DrawObjects["Text 175"].AttachedTo;
          This issue probably has to do with the owner of the draw object when it's first created, which is set to 'this' (the indicator). One could set the owner field to DrawObjects, but that won't work because it "cannot convert 'NinjaTrader.Gui.NinjaScript.IDrawObjects' to 'NinjaTrader.Gui.NinjaScript'". So the question would be how to get the NinjaTrader.Gui.NinjaScript' of the chart that the indicator is attached to (perhaps from the chartPanel).

          To summarize, the indicator is able to draw text on the chart and remain on the chart once the indicator is removed. The problem: if the draw object is not moved around by the user before the indicator removed, the draw object jumps to the beginning of the chart.
          Last edited by Unsuitable; 10-19-2020, 06:47 AM.
          Unsuitable
          NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

          Comment


            #6
            Hello Unsuitable,

            This would not be supported by NinjaTrader Support.

            You may be able to find an unsupported workaround. This thread will remain open for any community members that would like to assist.

            I am happy to submit a feature request for development to consider officially supporting allowing drawing objects from indicators to remain after the indicator is terminated.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello ChelseaB,

              I kept trying to find an unsupported way to set the owner of a draw object to the chart. Realistically that's all that would need to be done. If the owner of a draw object was set to the chart than everything else should be handled properly. I tried setting the owner to "Bars", it looked promising but that doesn't work.




              I've also tried doing it the sketchy way of setting the properties after the object is made, but even this as a workaround is too sketchy.

              Code:
              //text.AttachedTo.AttachedToType = AttachedToType.Bars; // this does not work as its read-only. Also tried creating the draw object with this as the owner, did not work
              
              // I dug into some of the saved chart XML files and found this https://i.imgur.com/hiI1Vbx.png so the 'AttachedTo' property consists of these two elements
              // I double-checked to see if I was passing the correct information with a manually drawn object and yes those are the proper things to set
              // https://i.imgur.com/RANS6mU.png the D chartObject and instrument is the 'AttachedTo' properties of a user drawn object.
              text.AttachedTo.ChartObject = ChartBars;
              text.AttachedTo.Instrument = Instrument;
              
              // for the heck of it I tried setting the serialze properties of 'AttachedTo', this did nothing.
              text.AttachedTo.ChartObjectSerialize = "ChartBars";
              text.AttachedTo.InstrumentSerialize = Instrument.FullName;
              By the end of this code, changing the properties or removing the indicator results in this error https://i.imgur.com/eAadl0b.png. This confirms that the 'AttachedTo' property is the 'Owner' field when instantiating a new draw object. In order for an object to remain drawn on the chart when the indicator is refreshed, the 'DrawnBy' property must be set to false. There's a note that setting it to null makes the draw object user drawn. In order for the object to remain drawn on the chart when the indicator is removed, the AttachedTo must not be set to the indicator. The code above sets to null (the 'DisplayName' cannot be manually set).

              So, all this to say. Would it be possible on your end to dig into the user's IDrawingTool script and check how it sets it's 'AttachedTo' property or 'Owner' field?

              Thanks
              Last edited by Unsuitable; 10-24-2020, 10:29 AM.
              Unsuitable
              NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

              Comment


                #8
                Hello Unsuitable,

                Unfortunately, I would not be able to provide support for this unsupported code.

                This thread will remain open for any community members that may know of a way or would like to assist.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hello,

                  Have there been any developments of attaching objects drawn by an indicator to the chart?
                  Unsuitable
                  NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

                  Comment


                    #10
                    I would be very interested in this as well.
                    I'm developing an indicator that allows the user to saves & load user all DrawObjects to a file.
                    That way users can quickly save and load up an annotated chart and switch between different annotations.

                    Saving works just fine, However, after loading it, I re-draw all the (stored) draw objects using the Draw.xxxx(this,....) calls
                    But this causes all the draw objects to be attached to the indicator, meaning the user cannot delete them anymore

                    Any (unsupported) help would be greatly appreciated

                    Erwin
                    EB Worx
                    NinjaTrader Ecosystem Vendor - EB Worx

                    Comment


                      #11
                      Hello EB Worx,

                      Please check out the following post for an unsupported solution.

                      Hello folks, I have been hard at work trying to come up with a workaround that attaches indicator drawn objects to the chart. The solution is not 100% and there are two major flaws. 1. If an object is not moved around before the indicator is removed, moving the object once the indicator is removed makes the object jumped to


                      Thanks
                      Unsuitable
                      NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

                      Comment


                        #12
                        Originally posted by Unsuitable View Post
                        Hello EB Worx,

                        Please check out the following post for an unsupported solution.

                        https://ninjatrader.com/support/foru...ically-sort-of

                        Thanks
                        Thanks, I'm already using the following piece of code which seems to perform beautifully as well
                        Basically, I just use reflection to set the AttachToType to AttachedToType.Bars


                        Code:
                        private void SetPrivatePropertyValue(object obj, string propName, AttachedToType val)
                        {
                            var t = obj.GetType();
                            if (t.GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) == null)
                                 throw new ArgumentOutOfRangeException("propName", string.Format("Property {0} was not found in Type {1}", propName, obj.GetType().FullName));
                           t.InvokeMember(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.Instance, null, obj, new object[] { val });
                        }
                        
                        ....
                        drawObj.IsLocked = false;
                        drawObj.AttachedTo.ChartObject = _chartScale.GetFirstChartBars();
                        drawObj.AttachedTo.Instrument = Instrument;
                        drawObj.DrawnBy = null;
                        SetPrivatePropertyValue(drawObj.AttachedTo, "AttachedToType", AttachedToType.Bars);
                        ...
                        Last edited by EB Worx; 06-24-2021, 03:14 AM.
                        EB Worx
                        NinjaTrader Ecosystem Vendor - EB Worx

                        Comment


                          #13
                          Hello EB Worx,

                          That looks like a better-unsupported method than what I did. Do you know how to set the attached type to global directly? I tried using NinjaTrader.NinjaScript.DrawingTools.AttachedToTyp e.GlobalInstrument but that doesn't work.

                          Thanks
                          Unsuitable
                          NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by The_Sec, Yesterday, 03:37 PM
                          1 response
                          11 views
                          0 likes
                          Last Post NinjaTrader_Gaby  
                          Started by vecnopus, Today, 06:15 AM
                          0 responses
                          1 view
                          0 likes
                          Last Post vecnopus  
                          Started by Aviram Y, Today, 05:29 AM
                          0 responses
                          5 views
                          0 likes
                          Last Post Aviram Y  
                          Started by quantismo, 04-17-2024, 05:13 PM
                          3 responses
                          27 views
                          0 likes
                          Last Post NinjaTrader_Gaby  
                          Started by ScottWalsh, 04-16-2024, 04:29 PM
                          7 responses
                          36 views
                          0 likes
                          Last Post NinjaTrader_Gaby  
                          Working...
                          X