StrategyBaseConverter Class

View as Markdown

Definition

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

Notes:

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 StrategyBaseConverter : TypeConverter

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

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