Announcement

Collapse
No announcement yet.

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 - Unsuitable

    #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.
    https://ninjatrader.com/support/help...attachedto.htm

    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)
    https://ninjatrader.com/support/help.../draw_text.htm
    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:	42
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:	38
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 - Unsuitable

      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 - Unsuitable

          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.

              https://i.imgur.com/7kbT7cG.png
              https://i.imgur.com/j5Fzo2n.png

              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; Yesterday, 10:29 AM.
              Unsuitable
              NinjaTrader Ecosystem Vendor - Unsuitable

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by localappleseed, Today, 09:51 AM
              0 responses
              3 views
              0 likes
              Last Post localappleseed  
              Started by wehewwehd, Today, 09:13 AM
              0 responses
              4 views
              0 likes
              Last Post wehewwehd  
              Started by aaajv, Yesterday, 06:45 AM
              3 responses
              21 views
              0 likes
              Last Post NinjaTrader_ThomasC  
              Started by asmmbillah, Today, 08:13 AM
              0 responses
              5 views
              0 likes
              Last Post asmmbillah  
              Started by Bionian, Today, 07:07 AM
              2 responses
              7 views
              0 likes
              Last Post Bionian
              by Bionian
               
              Working...
              X