Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to get the template name of an indicator

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

    How to get the template name of an indicator

    Hello,

    how can I get the template name of an indicator per script?
    The DrawObjects have a property Template for that purpose, e.g.:

    string templateName = DrawObjects["MyLine"].Template;

    Is there something similar for indicators?
    I was not able to find anything like this.

    Thank you !!!
    Armin

    #2
    Hello Armin,

    Thank you for your post.

    What is your purpose for getting a template name for an indicator? Are you looking to get the template name for indicators that were added manually to a chart? Otherwise, the system indicator methods don't use a template and the default template is applied. If you call AddChartIndicator() from a strategy, only the plot properties are accessible from the Indicators window and any other properties must be set programmatically. Please clarify what you are looking for so I may better assist you.

    I look forward to your reply.

    Comment


      #3
      Hello Emily,

      the purpose is to remove and add indicators from a script.
      This could be done from within an Indicator or Addon, that does not matter.
      Example for adding:
      // Load indicator
      MyIndicator indi = new MyIndicator()
      {
      // Do the settings
      ​IsOwnedByChart = true,
      NumberOfPanles = 1,
      Panel = -1,
      // how can I set the template ???
      //templateName = "IndiTemplate 1-Min",
      };
      ChartControl.Indicators.Add(indi);
      // Do the things after loading like refresh etc.

      When loading, it would be fine if I could set the template for the indicator, so that it is loaded with the correct settings and it must not be done manually.
      When unloading, it would be fine to get the tempalte name before doing that.

      Thanks for your support !!!
      Armin

      Comment


        #4
        Good reading here, esp towards the bottom.

        Comment


          #5
          This post may also be relevant.

          Comment


            #6
            bltdavid Thank you for the interesting threads.

            Unfortunately there is not mentioned if and how an template can be applied to the indicator that is added.

            Comment


              #7
              Originally posted by Armin View Post
              Unfortunately there is not mentioned if and how an template can be applied to the indicator that is added.
              It's probably done via the state change protocol.

              That is, I presume, just before sending State.SetDefaults to the
              indicator instance, the saved parameter values in the Xml template
              file are loaded into the indicator instance via an XmlSerializer.

              I wrote my own template manager for NT7, and I could save and
              restore indicator (or strategy) parameters to/from any number of
              Xml template files.

              I probably won't port this to NT8, since NT8 now has its own.

              Maybe this will help.
              Below are the most relevant & juiciest pieces of code that I wrote for
              my NT7 indicator template manager.

              Note that the 'this' argument to XmlTemplate_CreateObject referred
              to the indicator (or strategy) instance.

              If you study the code carefully, you'll see that I used most of the
              same code in my template manager for NT7 strategies.

              Code:
              // load class properties as parameters from Xml template file
              protected virtual void XmlTemplate_LoadParameters(string filnam)
              {
                  object XmlObject = XmlTemplate_CreateObject(this, filnam);
              
                  PropertyInfo[] XmlProperties = XmlObject.GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance);
                  PropertyInfo[] CurProperties = this.GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance);
              
                  foreach (PropertyInfo prop in CurProperties)
                  {
                      if (Attribute.IsDefined(prop, typeof(XmlIgnoreAttribute)))
                          /* PrintString("Property='{0}' XmlIgnore attribute is defined", prop.Name); */
                          continue;
              
                      if (!prop.CanRead || !prop.CanWrite)
                          /* PrintString("Property='{0}' CanRead={1} CanWrite={2}", prop.Name, prop.CanRead, prop.CanWrite); */
                          continue;
              
                      if (prop.GetIndexParameters().Length != 0)
                          /* PrintString("Property='{0}' is an indexed property", prop.Name); */
                          continue;
              
                      int indx = Array.FindIndex(XmlProperties, p => p.Name == prop.Name);
                      if (indx == -1)
                          /* PrintString("Property='{0}' not found in XmlTemplate", prop.Name); */
                          continue;
              
                      /* ------- */
              
                      /* PrintString("Property='{0}' Type='{1}' OldValue='{2}' NewValue='{3}'", prop.Name, prop.PropertyType.Name, */
                                   /* prop.GetValue(this, null), XmlProperties[indx].GetValue(XmlObject, null)); */
              
                      try {
                          #if DEFINED_INDICATOR
                          if (prop.Name == "Plots")
                          {
                              for (int n = 0; n < Plots.Length; ++n)
                              {
                                  Plot[] XmlPlots = (Plot[])XmlProperties[indx].GetValue(XmlObject, null);
                                  int x = Array.FindIndex(XmlPlots, p => p.Name.Trim() == Plots[n].Name.Trim());
                                  /* PrintString("Plots[{0}].Name='{1}' FindIndex={2}", n, Plots[n].Name.Trim(), x); */
                                  if (x != -1)
                                  {
                                      Plots[n].Pen.Color = XmlPlots[x].Pen.Color;
                                      Plots[n].Pen.DashStyle = XmlPlots[x].Pen.DashStyle;
                                      Plots[n].Pen.Width = XmlPlots[x].Pen.Width;
                                      Plots[n].PlotStyle = XmlPlots[x].PlotStyle;
                                      Plots[n].Min = XmlPlots[x].Min;
                                      Plots[n].Max = XmlPlots[x].Max;
                                  }
                              }
                          }
                          else if (prop.Name == "Lines")
                          {
                              for (int n = 0; n < Lines.Length; ++n)
                              {
                                  Line[] XmlLines = (Line[])XmlProperties[indx].GetValue(XmlObject, null);
                                  int x = Array.FindIndex(XmlLines, p => p.Name.Trim() == Lines[n].Name.Trim());
                                  /* PrintString("Lines[{0}].Name='{1}' FindIndex={2}", n, Lines[n].Name.Trim(), x); */
                                  if (x != -1)
                                  {
                                      Lines[n].Pen.Color = XmlLines[x].Pen.Color;
                                      Lines[n].Pen.DashStyle = XmlLines[x].Pen.DashStyle;
                                      Lines[n].Pen.Width = XmlLines[x].Pen.Width;
                                      Lines[n].Value = XmlLines[x].Value;
                                  }
                              }
                          }
                          else
                          #endif
                              prop.SetValue(this, XmlProperties[indx].GetValue(XmlObject, null), null);
                      }
                      catch (Exception ex) {
                          PrintString("Property='{0}' SetValue failed: {1}", prop.Name, ex.Message.Trim());
                      }
                  }
              }
              
              /* --------------------------------------------------------------------------------------------------- */
              
              // return class object deserialized from Xml template file
              protected static object XmlTemplate_CreateObject(object instance, string filnam)
              {
                  XmlSerializer xmlSerializer = new XmlSerializer(instance.GetType());
              
                  using (StreamReader xmlStream = new StreamReader(filnam))
                  {
                      // reuse variable for return value
                      instance = xmlSerializer.Deserialize(xmlStream);
                  }
              
                  return instance;
              }
              Good luck!
              I hope this helps you in your quest!

              Last edited by bltdavid; 04-13-2023, 10:14 AM.

              Comment


                #8
                The actual name of the Xml template file is stored at,

                <UserFolder>\Documents\NinjaTrader 8\templates\Indicator\<IndicatorName>\Default.xml

                which may or may not exist.

                The initial portion of the directory is known as the User Data Directory,
                and is available in Core.Globals.UserDataDir.

                Something like this,
                Code:
                string filnam = Core.Globals.UserDataDir + "templates\\Indicator\\MyIndicator\\Default.xml";
                then make sure the file exists,

                Code:
                if (File.Exists(filnam))
                    XmlTemplate_LoadParameters(filnam);
                Of course, you'll still need to follow through with porting and testing
                the code (in my prior post) to work with NT8.

                Let me know how it goes.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                639 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                366 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                107 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                569 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                572 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X