Bar Info
Menu
Debugging
Menu
Derivatives
Menu
Matrix Handling
Menu
Price Rounding
Menu
Regression Lines
Menu
Statistics
Menu
User Tracking
Menu
Example Strategies
Menu
Show / Hide Properties
Description:
Control which properties are viewable by the user for indicators or strategies.
Use Case:
When you only want certain relevent properties to be displayed when the user sets a certain value.
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
[TypeConverter("NinjaTrader.NinjaScript.Strategies.MyConverter")]
public class EnumShowHidePropertiesTest : Strategy
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"";
Name = "EnumShowHidePropertiesTest";
ToggleValue1 = 1;
ToggleValue2 = 2;
ToggleValue3 = 3;
ToggleValue4 = 4;
TrueFalse = true;
}
}
protected override void OnBarUpdate()
{
// logic goes here
}
[RefreshProperties(RefreshProperties.All)]
public bool TrueFalse
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Toggle value #1", Order = 1, GroupName = "Use Case #1")]
public int ToggleValue1
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Toggle value #2", Order = 2, GroupName = "Use Case #1")]
public int ToggleValue2
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Toggle value #3", Order = 3, GroupName = "Use Case #1")]
public int ToggleValue3
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Toggle value #4", Order = 4, GroupName = "Use Case #1")]
public int ToggleValue4
{ get; set; }
}
// This custom TypeConverter is applied ot the entire indicator object and handles two of our use cases
// IMPORTANT: Inherit from IndicatorBaseConverter so we get default NinjaTrader property handling logic
// IMPORTANT: Not doing this will completely break the property grids!
// If targeting a "Strategy", use the "StrategyBaseConverter" base type instead
public class MyConverter : StrategyBaseConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object component, Attribute[] attrs)
{
// we need the indicator instance which actually exists on the grid
EnumShowHidePropertiesTest strategy = component as EnumShowHidePropertiesTest;
// base.GetProperties ensures we have all the properties (and associated property grid editors)
// NinjaTrader internal logic determines for a given indicator
PropertyDescriptorCollection propertyDescriptorCollection = base.GetPropertiesSupported(context)
? base.GetProperties(context, component, attrs)
: TypeDescriptor.GetProperties(component, attrs);
if (strategy == null || propertyDescriptorCollection == null)
return propertyDescriptorCollection;
#region Use Case #1: Show/hide properties based on secondary input
// These two values are will be shown/hidden (toggled) based on "ShowHideToggle" bool value
PropertyDescriptor toggleValue1 = propertyDescriptorCollection["ToggleValue1"];
PropertyDescriptor toggleValue2 = propertyDescriptorCollection["ToggleValue2"];
PropertyDescriptor toggleValue3 = propertyDescriptorCollection["ToggleValue3"];
PropertyDescriptor toggleValue4 = propertyDescriptorCollection["ToggleValue4"];
// This removes the following properties from the grid to start off with
propertyDescriptorCollection.Remove(toggleValue1);
propertyDescriptorCollection.Remove(toggleValue2);
propertyDescriptorCollection.Remove(toggleValue3);
propertyDescriptorCollection.Remove(toggleValue4);
// Now that We've removed the default property descriptors, we can decide if they need to be re-added
// If "ShowHideToggle" is set to true, re-add these values to the property collection
if (strategy.TrueFalse == true)
{
propertyDescriptorCollection.Add(toggleValue1);
propertyDescriptorCollection.Add(toggleValue2);
}
else if (strategy.TrueFalse == false)
{
propertyDescriptorCollection.Add(toggleValue3);
propertyDescriptorCollection.Add(toggleValue4);
}
// otherwise, nothing else to do since they were already removed
#endregion
return propertyDescriptorCollection;
}
// Important: This must return true otherwise the type convetor will not be called
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{ return true; }
}
}