Use Case #1

Sample Indicator TypeConverter Ninjatrader https://pastecode.io/s/gmoe33p2 https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?using_a_typeconverter_to_custo.htm
 avatar
unknown
csharp
9 months ago
3.4 kB
17
No Index

        // Custom Grid properties which will implement custom behavior
        #region Use Case #1: Show/hide properties based on secondary input

        [RefreshProperties(RefreshProperties.All)] // Needed to refresh the property grid when the value changes
        [Display(Name = "Toggle show/hide", Order = 1, GroupName = "Use Case #1")]
        public bool ShowHideToggle
        { get; set; }

        [Range(1, int.MaxValue)]
        [Display(Name = "Toggle value #1", Order = 2, GroupName = "Use Case #1")]
        public int ToggleValue1
        { get; set; }

        [Range(1, int.MaxValue)]
        [Display(Name = "Toggle value #2", Order = 3, GroupName = "Use Case #1")]
        public int ToggleValue2
        { get; set; }

        #endregion


    #region Use Case #1/: Show/hide properties based on secondary input & Disable/enable properties based on secondary input

    // 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 : IndicatorBaseConverter // or StrategyBaseConverter
    {
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object component, Attribute[] attrs)
        {
            // we need the indicator instance which actually exists on the grid
            SampleIndicatorTypeConverter indicator = component as SampleIndicatorTypeConverter;

            // 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 (indicator == 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"];

            // This removes the following properties from the grid to start off with
            propertyDescriptorCollection.Remove(toggleValue1);
            propertyDescriptorCollection.Remove(toggleValue2);

            // 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 (indicator.ShowHideToggle)
            {
                propertyDescriptorCollection.Add(toggleValue1);
                propertyDescriptorCollection.Add(toggleValue2);
            }

            // otherwise, nothing else to do since they were already removed

            #endregion
    }
Editor is loading...
Leave a Comment