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

How do I implement Decorated Enums

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

    How do I implement Decorated Enums

    Hi,

    I believe decorated enums were supported in Ninja7, however, I am not able to figure out how to get it to work in Ninja8.

    I would like to have visually friendly enums that are populated in the properties.. Like words with spaces or other characters and the like.

    Thanks in advance.

    #2
    Hello KhaosTrader,

    A custom type converter would be necessary to have custom display attributes in the properties menu. However, custom type converters are not supported by NinjaTrader Support.

    I have submitted a feature request to our development on your behalf to allow for friendly enum display names. It is up to the NinjaTrader Development to decide if and when a feature request will be implemented.

    That said, NinjaTrader_MichaelM and I were able to find a way to do this and I want to share our work with you.
    This example is not supported by the NinjaTrader Support as this uses unsupported methods to achieve this goal (custom type converters). In the process of determining the issue, we were able to workout a non supported solution I wish to share with you.

    Code:
    public enum AnEnumList
    {
    	[Description("Enum 1 friendly name")]
    	Enum1,
    	[Description("Enum 2 friendly name")]
    	Enum2,
    	[Description("Enum 3 friendly name")]
    	Enum3,
    }
    Code:
    public class EnumDescriptionConverter : EnumConverter
    {
    	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);
    	}
    }
    Code:
    [NinjaScriptProperty]
    [TypeConverter(typeof(EnumDisplayAttributeExampleTools.EnumDescriptionConverter))]
    [PropertyEditor("NinjaTrader.Gui.Tools.StringStandardValuesEditorKey")]
    [Display(Name = "My Enum List", Order = 0, GroupName = "Parameters", Description = "A list of enums")]
    public EnumDisplayAttributeExampleTools.AnEnumList MyEnumList
    { get; set; }
    Attached Files
    Last edited by NinjaTrader_ChelseaB; 01-08-2017, 01:37 PM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello KhaosTrader,

      Our development has provided me with a tracking ID for your feature request.
      For future reference your request for friendly enum display names in a property grid is being tracked with ID #SFT-1771.

      We appreciate your feedback on this subject.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        You are awesome Chelsea (and Michael)

        You always go beyond what's 'required' and you truly are an asset to this forum.

        Comment


          #5
          Thank you, how do I check the status of the request?

          Comment


            #6
            Hello KhaosTrader,

            Feature request SFT-1771 has not been implemented.

            To check on the status of a request, wait until a new build of NinjaTrader has been released and check for the feature.
            If the feature is not in the release notes or you have determined the feature request is not available, you can send an email to platformsupport [at] ninjatrader [dot] com and provide the tracking ID and inquire if the request has been implemented.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Possible issues

              All in all this works very well.

              However I have had some strange behaviour in some circumstances.

              I noticed if I set a few enums in one indicator, with one option in each enum being 'None', if I use:

              PHP Code:
               
              public enum mySillyEnum
              {
                     [
              Description(">=")]
                     
              GreaterOrEqualTo,
                     [
              Description("<=")]
                     
              LessThanOrEqualTo,
                     [
              Description("None")]
                     
              None

              I was getting a 'conflict' if a few of my other enums had also "None' within them. My indicators just did not seem to want to load up.

              If I used:

              PHP Code:
               
              public enum mySillyEnum
              {
                     [
              Description(">=")]
                     
              GreaterOrEqualTo,
                     [
              Description("<=")]
                     
              LessThanOrEqualTo,
                     [
              Description("No ?????")] //substitute a random name for '?????'
                     
              None

              ....this seemed to remove unexplained loading issues. I can't say this is 100% the cause of these errors but since it is one of the few things I did differently then I can certainly swing my keyboard into that direction. I'll suss it out more, just curious if anyone else had the same issues........

              Comment


                #8
                Hello Sim22,

                I wanted to check, what was the specific conflict or error you had seen? I am not certain I follow the explanation you provided, I could only see a conflict if you had declared two of the same enum potentially. Could you provide either the error you had seen or an expanded example that shows the syntax causing the conflict?

                I noted you said if you set a few enums in the indicator, is this a combination of multiple different enums causing the conflict?


                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Crash in 32 bit version

                  This solution is causing NT 8 32 bit to crash with message "could not load file or assembly. Unhandled exception: The parameter is incorrect(Exception from HRESULT:0x80070057"

                  Comment


                    #10
                    I see that sim22 code says php, is that c# or php? do we have working decorators for enums now? in ninja 8?

                    Comment


                      #11
                      Hello,

                      Thank you for the replies.

                      @AnkaS
                      I tried importing the indicator posted in this thread and it seems to work for me in the 32 bit version. Can you provide more detail on what you had imported and what specifically is failing?

                      @KhaosTrader
                      The forum offers a few code boxes or ways to format what you post, it looks like Sim22 chose the php box instead of a code box. This is still C# code, it's just a header over the code box.

                      I look forward to being of further assistance.
                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        I tried doing decorated enums using the example, and I am not getting the substitutions for ninja8, what am I doing wrong? I am using the 64 bit version of NT8
                        Last edited by KhaosTrader; 09-25-2017, 07:27 AM.

                        Comment


                          #13
                          Hello,

                          I would not be sure without seeing what you have tried. I would suggest to either compose a simple example that is separate from any larger scripts you are working on, mainly just an indicator showing how you are using the enum by its self. Or to import the sample provided in post #2 and then review what is different between your file and the sample.

                          I tried the sample this morning in 8.0.9.0 and see that it is working as expected from my end.


                          I look forward to being of further assistance.
                          JesseNinjaTrader Customer Service

                          Comment


                            #14
                            The easiest way to do this, if I understand correctly what it is you wish to achieve, is to write your own TypeConverter.

                            Comment


                              #15
                              Hi, I got it to work, thanks, BUT, what is the font of the property grid itself?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bsbisme, Yesterday, 02:08 PM
                              1 response
                              15 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by prdecast, Today, 06:07 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post prdecast  
                              Started by i019945nj, 12-14-2023, 06:41 AM
                              3 responses
                              60 views
                              0 likes
                              Last Post i019945nj  
                              Started by TraderBCL, Today, 04:38 AM
                              2 responses
                              18 views
                              0 likes
                              Last Post TraderBCL  
                              Started by martin70, 03-24-2023, 04:58 AM
                              14 responses
                              106 views
                              0 likes
                              Last Post martin70  
                              Working...
                              X