Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Converting a List to an Enum

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

    Converting a List to an Enum

    Hello,

    How do you create an enum using values from a list? So far the code retrieves a list of templates available for the Text draw object. I know it's not best practice to create an enum dynamically, however, it would make the user experience of the indicator more straightforward.

    I've done it in the past by selecting an account for a parameter using AccountNameConverter.

    Code:
    [TypeConverter(typeof(NinjaTrader.NinjaScript.AccountNameConverter))]
    [Display(ResourceType = typeof(Custom.Resource), Name="Account", Order=5, GroupName="Parameters")]
    public string AccountName { get; set; }
    So far I have this, but how would I set it for a ninja property?

    Code:
    public enum TextTemplates
    {
      // this part I don't know how to set
      templateNameList.StringsToEnums<TemplateListEnum>( );
    }
    
    public static class StringEnumerableExtensions {
      public static IEnumerable<T> StringsToEnums<T>( this IEnumerable<string> strs) where T : struct, IConvertible {
        Type t = typeof( T );
    
        var ret = new List<T>();
    
        if( t.IsEnum ) {
          T outStr;
          foreach( var str in strs ) {
            if( Enum.TryParse( str, out outStr ) ) {
              ret.Add( outStr );
            }
          }
        }
    
      return ret;
    }
    Big thanks
    Unsuitable
    NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

    #2
    Hello Unsuitable,

    Enums must be specified at compile time for NinjaScript use.

    The account selection you provided is not an enum but is a string and a type converter which pulls accounts into a custom dropdown box and the selection is then passed back as a string.

    For this type of use you would really need to create a custom type converter and return a string. You can find a sample of that concept in the following link which pulls ATM strategy templates from the filesystem and makes a selection : https://ninjatrader.com/support/foru...027#post838027

    Comment


      #3
      Hello Jesse,

      This is exactly what I was looking for. Thank you for being super helpful!

      I have a type converter for using special characters that I will leave here if anyone finds it useful.

      Code:
      [TypeConverter(typeof(EnumDescriptionConverter))]
      [PropertyEditor("NinjaTrader.Gui.Tools.StringStandardValuesEditorKey")]
      [Display(Name = "Modifier Style", Order = 1, GroupName = "Modifier Configuration")]
      public ModifierStyle modifierStyle
      { get; set; }
      Code:
      public enum ModifierStyle
      {
      [Description("⮚")]
      U2B9A,
      [Description("⮞")]
      U2B9E,
      [Description("⯈")]
      U2BC8,
      [Description("▷")]
      U21E5,
      [Description("⯮")]
      U2BEE,
      [Description("⭢")]
      U2192
      }
      Code:
      public class EnumDescriptionConverter : EnumConverter
      {
        #region Code for using chars in enums
        private Type enumType;
      
      public EnumDescriptionConverter(Type type) : base(type)
      {
        enumType = type;
      }
      
      public override bool CanConvertFrom(ITypeDescriptorContext context, Type srcType)
      {
        return srcType == typeof(string);
      }
      
      public override bool CanConvertTo(ITypeDescriptorContext context, Type destType)
      {
        return destType == typeof(string);
      }
      
      public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
      {
        foreach (System.Reflection.FieldInfo fi in enumType.GetFields())
        {
          DescriptionAttribute dna = (DescriptionAttribute)Attribute.GetCustomAttribute (fi, typeof(DescriptionAttribute));
          if (dna != null && (string)value == dna.Description)
            return Enum.Parse(enumType, fi.Name);
        } 
        return Enum.Parse(enumType, (string)value);
      }
      
      public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
      {
        System.Reflection.FieldInfo fi = enumType.GetField(value.ToString());
        DescriptionAttribute dna = (DescriptionAttribute) Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
        if(dna != null)
          return dna.Description;
        else
          return value.ToString();
      }
      
      public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
      {
        List<string> values = new List<string>();
        foreach (var v in Enum.GetValues(enumType))
        {
          System.Reflection.FieldInfo fi = enumType.GetField(v.ToString());
          DescriptionAttribute dna = (DescriptionAttribute)Attribute.GetCustomAttribute (fi, typeof(DescriptionAttribute));
          if (dna != null)
            values.Add(dna.Description);
          else
            values.Add(v.ToString());
        }
        return new StandardValuesCollection(values);
      }
      Unsuitable
      NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      558 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      324 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      545 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      547 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X