IndicatorBaseConverter Class

View as Markdown

Definition

A custom TypeConverter class handling the designed behavior of an indicator’s property descriptor collection. Use this as a base class for any custom TypeConverter you are applying to an indicator class.

  • A working NinjaScript demo can be found through the reference sample on “Using a TypeConverter to Customize Property Grid Behavior
  • When applying the custom converter, you must fully qualify the name (e.g., “NinjaTrader.NinjaScript.Indicators.MyCustomConveter”)
  • Additional TypeConverter information can be found from the MSDN documentation
  • See also TypeConverterAttribute
  • For Strategies, see the StrategyBaseConverter class

Relevant base methods

MethodDescription
TypeConverter.GetProperties()When overriding GetProperties(), calling base.GetProperties() ensures that all default property grid behavior works as designed
TypeConverter.GetPropertiesSupported()In your custom converter class, you must override GetPropertiesSupported() and return a value of true in order for your custom type converter to work

Syntax

public class IndicatorBaseConverter : TypeConverter

Failure to apply a type of IndicatorBaseConverter on an indicator class can result in unpredictable behavior of the standard NinjaTrader WPF property grid.

Tip: Common indicator functions like Print() are not available to a type converter instance. To debug a type converter class, you can use the AddOn Debug Concepts or attach to a debugger (recommended).

Examples

1// This namespace holds Indicators in this folder and is required. Do not change it.
2namespace NinjaTrader.NinjaScript.Indicators
3{
4 // When applying the type converter, you must fully qualify the name
5 [TypeConverter("NinjaTrader.NinjaScript.Indicators.MyCustomConveter")]
6 public class MyCustomIndicator : Indicator
7 {
8 protected override void OnStateChange()
9 {
10 if (State == State.SetDefaults)
11 {
12 Name = "MyCustomIndicator";
13 }
14 }
15
16 protected override void OnBarUpdate()
17 {
18 // Add your custom indicator logic here.
19 }
20 }
21
22 public class MyCustomConveter : IndicatorBaseConverter
23 {
24 // A general TypeConveter method used for converting types
25 public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object component, Attribute[] attrs)
26 {
27 // sometimes you may need the indicator instance which actually exists on the grid
28 MyCustomIndicator indicator = component as MyCustomIndicator;
29
30 // base.GetProperties ensures we have all the properties (and associated property grid editors)
31 // NinjaTrader internal logic handles for a given indicator
32 PropertyDescriptorCollection propertyDescriptorCollection = base.GetPropertiesSupported(context)
33 ? base.GetProperties(context, component, attrs) : TypeDescriptor.GetProperties(component, attrs);
34
35 if (indicator == null || propertyDescriptorCollection == null)
36 return propertyDescriptorCollection;
37
38 // example of why you may need the instance that exists on the grid....
39 if (indicator.EntryHandling == EntryHandling.UniqueEntries)
40 {
41 // do something in the event a property contains some value...
42 }
43
44 // Loop all of the properties of the indicator
45 foreach (PropertyDescriptor property in propertyDescriptorCollection)
46 {
47 // do something with a specific property
48
49 // cannot call **Print()** here
50 // but you can call the static Output window "Process()"
51 NinjaTrader.Code.Output.Process(property.Name, PrintTo.OutputTab1);
52 }
53
54 // must return the collection after making changes
55 return propertyDescriptorCollection;
56 }
57
58 // Important: This must return true otherwise the type converter will not be called
59 public override bool GetPropertiesSupported(ITypeDescriptorContext context)
60 { return true; }
61 }
62}