Go Back   NinjaTrader Support Forum > NinjaTrader 8 > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

NinjaTrader
Reply
 
Thread Tools Display Modes
Old 01-07-2017, 10:01 AM   #1
KhaosTrader
Senior Member
 
Join Date: Jan 2012
Posts: 203
Thanks: 41
Thanked 9 times in 9 posts
Default 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.
KhaosTrader is offline  
Reply With Quote
The following user says thank you to KhaosTrader for this post:
Old 01-08-2017, 01:35 PM   #2
NinjaTrader_ChelseaB
NinjaTrader Customer Service
 
NinjaTrader_ChelseaB's Avatar
 
Join Date: Dec 2012
Location: Denver, CO
Posts: 7,524
Thanks: 119
Thanked 1,670 times in 1,573 posts
Default

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
File Type: zip EnumDisplayAttributeExample_NT8.zip (2.1 KB, 28 views)
Last edited by NinjaTrader_ChelseaB; 01-08-2017 at 01:37 PM.
NinjaTrader_ChelseaB is offline  
Reply With Quote
The following 2 users say thank you to NinjaTrader_ChelseaB for this post:
Old 01-09-2017, 09:28 AM   #3
NinjaTrader_ChelseaB
NinjaTrader Customer Service
 
NinjaTrader_ChelseaB's Avatar
 
Join Date: Dec 2012
Location: Denver, CO
Posts: 7,524
Thanks: 119
Thanked 1,670 times in 1,573 posts
Default

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.
NinjaTrader_ChelseaB is offline  
Reply With Quote
Old 01-21-2017, 02:04 AM   #4
Sim22
Senior Member
 
Join Date: Jun 2010
Location: Australia
Posts: 192
Thanks: 127
Thanked 48 times in 38 posts
Default

You are awesome Chelsea (and Michael)

You always go beyond what's 'required' and you truly are an asset to this forum.
Sim22 is offline  
Reply With Quote
Old 01-22-2017, 02:33 AM   #5
KhaosTrader
Senior Member
 
Join Date: Jan 2012
Posts: 203
Thanks: 41
Thanked 9 times in 9 posts
Default

Thank you, how do I check the status of the request?
KhaosTrader is offline  
Reply With Quote
Old 01-23-2017, 11:43 AM   #6
NinjaTrader_ChelseaB
NinjaTrader Customer Service
 
NinjaTrader_ChelseaB's Avatar
 
Join Date: Dec 2012
Location: Denver, CO
Posts: 7,524
Thanks: 119
Thanked 1,670 times in 1,573 posts
Default

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.
NinjaTrader_ChelseaB is offline  
Reply With Quote
Old 02-03-2017, 02:10 AM   #7
Sim22
Senior Member
 
Join Date: Jun 2010
Location: Australia
Posts: 192
Thanks: 127
Thanked 48 times in 38 posts
Default 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........
Sim22 is offline  
Reply With Quote
Old 02-03-2017, 10:15 AM   #8
NinjaTrader_Jesse
NinjaTrader Customer Service
 
NinjaTrader_Jesse's Avatar
 
Join Date: Mar 2014
Location: Denver, CO
Posts: 4,068
Thanks: 28
Thanked 1,053 times in 985 posts
Default

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.
NinjaTrader_Jesse is offline  
Reply With Quote
Old 09-23-2017, 09:28 AM   #9
AnkaS
Junior Member
 
Join Date: Jul 2012
Posts: 18
Thanks: 0
Thanked 1 time in 1 post
Default 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"
AnkaS is offline  
Reply With Quote
Old 09-23-2017, 04:39 PM   #10
KhaosTrader
Senior Member
 
Join Date: Jan 2012
Posts: 203
Thanks: 41
Thanked 9 times in 9 posts
Default

I see that sim22 code says php, is that c# or php? do we have working decorators for enums now? in ninja 8?
KhaosTrader is offline  
Reply With Quote
Old 09-25-2017, 07:14 AM   #11
NinjaTrader_Jesse
NinjaTrader Customer Service
 
NinjaTrader_Jesse's Avatar
 
Join Date: Mar 2014
Location: Denver, CO
Posts: 4,068
Thanks: 28
Thanked 1,053 times in 985 posts
Default

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.
NinjaTrader_Jesse is offline  
Reply With Quote
Old 09-25-2017, 07:23 AM   #12
KhaosTrader
Senior Member
 
Join Date: Jan 2012
Posts: 203
Thanks: 41
Thanked 9 times in 9 posts
Default

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 at 07:27 AM.
KhaosTrader is offline  
Reply With Quote
Old 09-25-2017, 07:37 AM   #13
NinjaTrader_Jesse
NinjaTrader Customer Service
 
NinjaTrader_Jesse's Avatar
 
Join Date: Mar 2014
Location: Denver, CO
Posts: 4,068
Thanks: 28
Thanked 1,053 times in 985 posts
Default

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.
NinjaTrader_Jesse is offline  
Reply With Quote
Old 09-25-2017, 11:32 AM   #14
Zeos6
Senior Member
 
Join Date: Jan 2011
Posts: 547
Thanks: 15
Thanked 40 times in 35 posts
Default

The easiest way to do this, if I understand correctly what it is you wish to achieve, is to write your own TypeConverter.
Zeos6 is offline  
Reply With Quote
Old 09-25-2017, 09:38 PM   #15
KhaosTrader
Senior Member
 
Join Date: Jan 2012
Posts: 203
Thanks: 41
Thanked 9 times in 9 posts
Default

Hi, I got it to work, thanks, BUT, what is the font of the property grid itself?
KhaosTrader is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PointLineLocation Enums devdas Version 8 Beta 5 11-01-2016 12:58 AM
Enums with [Flags] zweistein Version 8 Beta 4 11-09-2015 03:52 PM
Enums in Indicators GrumpyTrader Version 8 Beta 2 10-26-2015 08:47 AM
Enums in optimization GrumpyTrader Strategy Development 2 04-03-2015 09:35 AM
NinjaTrader enums MikeInMA General Development 4 09-03-2010 07:41 AM


All times are GMT -6. The time now is 03:14 AM.

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates. 

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content.