Creating Custom Enum Types in NinjaScript
Problem Definition
CREATING CUSTOM ENUM TYPES IN NINJASCRIPT
Project:
Document Owner:
Creation Date:
Revision Number:
Modified Date:
File:
Sensitivity:
NinjaScript Programming
Scott Daggett (Author of NinjaScript Programmer's Launch Pad)
June 1, 2012
1.0.0
June 4, 2012
Creating Custom Enum Types In Ninjascript.Doc
Public
Problem Definition
This document results from a question by a NinjaScript Support Forum user, ds1111. The question title was "User
defined parameter type gives error when using save as." (Read from the bottom up.) The problem ds1111 was
running into was related to the way custom data types are defined in NinjaScript. Ds1111 was using enums and
following the standard approach of placing the enum definition outside the namespace in the indicator code as
follows:
} // bracket marking the end of the indicator's NinjaTrader.Indicator namespace
public enum MyCustomEnumType
{
Selection 1,
Selection 2,
Selection 3
}
The problem occurred when ds1111 would save a new version of the indicator as progress was made on it using
the save as feature. Since the custom type was already defined in the C# default namespace, saving the indicator
under a new name also created a new enum definition with the same name in the same namespace. This, of
course, will not compile.
Note: An important objective here is to enable the user of the indicator to select related property values from
the Indicators properties dialog box.
Four Solutions
Without testing anything, I responded to ds1111 that it might work to put the enum definition in the
UserDefinedMethods.cs file, which can be edited with the NinjaScript indicator editor. I also mentioned that it
Modified: 6/4/2012 9:23:00 AM
1
Creating Custom Enum Types in NinjaScript2.doc
Public
Creating Custom Enum Types in NinjaScript
Four Solutions
could be put in the NinjaTrader.Indicator namespace (without thinking things through). I also pointed out that, if
the value selections in the enum changed from indicator version to version, then this might not be the best choice.
A couple of days later, the thought about this just wouldn't go away. I had to test things out to see what was
possible. So, I tested four scenarios:
1. Placing the enum definition in the UserDefinedMethods.cs file in the default C# namespace.
2. Placing the enum definition in a special indicator that serves solely as a placeholder for custom type
definitions, in the default C# namespace. (This file could be distributed with a related indicator if so desired.)
3. Placing the enum definition in that special placeholder indicator, but in the NinjaTrader.Indicator namespace.
4. Placing the enum definition in the indicator class itself.
Note:
There is another proposal from ETFVoyageur to create a new namespace in the default, or global,
namespace that has the same name as the indicator class itself. I believe this works as well as #4 above,
or better. I have not personally tested it.
My testing went only as far as creating and developing the indicator, and exporting it to a .cs file. I did not
perform any tests in regard to export as an assembly.
The Results
All four approaches seem to work fine, although with solutions #3 and #4, you must fully qualify the property
type with the full namespace. The same is true of ETFVoyager's approach.) I tested all four approaches, and the
user is able to select the desired value from the drop down list that appears in the properties dialog box, as hoped.
I have included the code from my tests. There are two indicators, plus the UserDefinedMethod.cs file (not
included in the zip file). Here are the descriptions of the files:
EnumHolderTest.cs
Holds two custom enum types that are used by the EnumClientTest indicator. One is
defined in the default C# namespace and one in the NinjaTrader.Indicator
namespace.
EnumClientTest.cs
Contains properties defined by the custom types in EnumHolderTest and
UserDefinedMethods.cs. It also contains a custom enum type defined in the indicator
class itself, and has a property based on it. Add this class to a chart to see the userselectable properties.
UserDefinedMethods.cs
Contains a custom enum type defined in the default C# namespace.
Below is a screen shot of the user-selectable properties of each type described above.
Modified: 6/4/2012 9:23:00 AM
2
Creating Custom Enum Types in NinjaScript2.doc
Public
Creating Custom Enum Types in NinjaScript
Four Solutions
Custom Type Properties in Indicators Dialog Box
Recommended Solution
This section has been updated after helpful forum users and NinjaTrader Support personnel directed me to prior
forum discussions on this rather hot topic. As it turns out, NinjaTrader supports only one approach, and that is to
put the enum definition in the global namespace. (As of NinjaTrader 7.0.1000.9 anyway.) You can read the
following threads in the forum to learn the details of why this is, but it basically has to do with the exporting of
your indicators to assemblies as opposed to .cs files. Maybe a future version of NinjaTrader will provide a more
workable solution. Thanks to koganam for the links:
http://www.ninjatrader.com/support/forum/showthread.php?t=37521&highlight=pollute+global+namespace
http://www.ninjatrader.com/support/forum/showthread.php?t=37662&highlight=pollute+global+namespace
http://www.ninjatrader.com/support/forum/showthread.php?t=43938&highlight=pollute+global+namespace
So, the official guideline is to put the enum in the global namespace, however, you can temporarily use approach
#4 while developing your indicator, so as to eliminate the save as problem, and then move the enum to the global
namespace when you are ready to export to an assembly and perform final testing before shipping to customers or
friends. The only challenge, I think, is to remember to move the enum definition and remove the qualifying
portion of the property type prior to testing and shipping. If you forget and leave it defined in the indicator class,
you may be introducing some problems for the users of your indicator, such as not being able to install it.
Approach #4: put the enum definition in the class, and then fully qualify the property type when writing the
property.
Important!
I recommend against using approach #1 (using the UserDefinedMethods.cd file), because it
makes it difficult to export your indicator in the normal manner. Since you can't select
UserDefnedMethods in the export window, your indicators fail to compile for the export
process. Unless you know something I don't, avoid UserDefinedMethods altogether. If you
need an external file to support your indicators, just create a "holder" indicator and then you
Modified: 6/4/2012 9:23:00 AM
3
Creating Custom Enum Types in NinjaScript2.doc
Public
Creating Custom Enum Types in NinjaScript
Four Solutions
can export that with your client indicators. THIS IDEA NEEDS TO BE TESTED WITH A
PROTECTED COMPILED ASSEMBLY. IT MAY NOT WORK IN THAT CASE.
Here is an example of approach #4:
namespace NinjaTrader.Indicator
{
/// <summary>
/// Uses enum property defined in the indicator.
/// </summary>
[Description("Uses enum property defined in the indicator.")]
public class EnumClientTest : Indicator
{
#region Variables
TestEnumInIndicator enumInIndicator = TestEnumInIndicator.Selection1;
#endregion
protected override void Initialize()
{
Overlay = false;
}
protected override void OnBarUpdate()
{
}
public enum TestEnumInIndicator
{
Selection1,
Selection2,
Selection3
}
#region Properties
[Description("Property defined in Indicator")]
[GridCategory("Parameters")]
public NinjaTrader.Indicator.EnumClientTest.TestEnumInIndicator
EnumInIndicator
{
get { return enumInIndicator; }
set { enumInIndicator = value; }
}
#endregion
}
}
The benefit of this approach is that, well, it does what ds1111 wanted in the first place. Ds1111 can have the
indicator contain its own enum definition, allow the user to select values defined in it, save the indicator as a new
version, and be able to have different enum values from version to version.
Again, please keep in mind that this approach is not supported by NinjaTrader Support. I only recommend it for
the development process to eliminate the save as problem when saving new versions of your indicator. It's up to
you to remember to move the enum when ready to finish up.
Modified: 6/4/2012 9:23:00 AM
4
Creating Custom Enum Types in NinjaScript2.doc
Public
Creating Custom Enum Types in NinjaScript
Important!
Important Installation Instructions
Because placing a new enum type in the default, or global, namespace "pollutes" that
namespace, increasing the likelihood of naming conflicts with other indicator and strategy
providers, choose a name that is likely not going to be selected by another provider. You might
prefix the indicator and enum type with an acronym for your company name, for example.
This is in harmony with NinjaTrader's recommended practices.
Important Installation Instructions
In order to successfully install the EnumTest.zip indicators, you must first modify and compile
UserDefinedMethods.cs in the Indicators directory. Place the following code at the very end of the file, then
compile.
public enum TestEnumUserDefined
{
Selection1,
Selection2,
Selection3
}
Once you do this, you should be able to import EnumTest.zip. It worked for me anyway.
Modified: 6/4/2012 9:23:00 AM
5
Creating Custom Enum Types in NinjaScript2.doc
Public
© Copyright 2026 Paperzz