Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Enum TypeConverter Hide / Remove Properties

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Enum TypeConverter Hide / Remove Properties

    Hello,

    I am working on a strategy that has multiple options. Trying to hide/remove certain properties in order to prevent unnecessary clutter for the user.

    This is my first time working with converters. But thanks to this reference:
    https://forum.ninjatrader.com/forum/...-grid-behavior
    I was able to get it working when it comes to bools. (Case #1)

    Now I am trying to achieve the same thing from Case#1 but whenever I select an option from an enum.
    For example.

    When Selecting Option1 from the drop down. I want the Option2 properties to disappear.



    When Selecting Option 2 from the drop down. I want Option 1 properties to disappear.




    I wrote this simple strategy in an attempt to make a template for this specific request

    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    //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 EnumConverterTEST : Strategy
        {
            private bool Option1Bool;
            private bool Option2Bool;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "EnumConverterTEST";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 0;
                    StartBehavior                                = StartBehavior.WaitUntilFlat;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = false;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 20;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration    = true;
    
                    EnumType            = EnumSelector.Option1;
    
                    Option1Double        = 5.75;
                    Option1Int            = 5;
    
    
                    Option2Double        = 10.25;
                    Option2Int            = 3;
    
    
                    Option1Bool     = false;
                    Option2Bool        = false;
    
                }
                else if (State == State.Configure)
                {
                }
    
                else if (State == State.DataLoaded)
                {                
                    switch (EnumType)
                    {
                        case EnumSelector.Option1:
                        {
                            Option1Bool = true;
                            Print("Option1");
                            break;
                        }
                        case EnumSelector.Option2:
                        {
                            Option2Bool = true;
                            Print("Option2");
                            break;
                        }
                    }
                }
    
            }
    
            protected override void OnBarUpdate()
            {
                //Add your custom strategy logic here.
            }
    
            [RefreshProperties(RefreshProperties.All)]
            [Display(Name = "ENUM SELECTOR", Order = 0, GroupName = "00. PARAMETER")]
            public EnumSelector EnumType
            { get; set; }
    
            //Option 1...Options
            [NinjaScriptProperty]
            [Display(Name="Option1Double", Order=1, GroupName="00. PARAMETER")]
            public double Option1Double
            { get; set; }
    
            [NinjaScriptProperty]
            [Display(Name="Option1Int", Order=2, GroupName="00. PARAMETER")]
            public int Option1Int
            { get; set; }
    
    
            //Option 2...Options
            [NinjaScriptProperty]
            [Display(Name="Option2Double", Order=3, GroupName="00. PARAMETER")]
            public double Option2Double
            { get; set; }
    
            [NinjaScriptProperty]
            [Display(Name="Option2Int", Order=4, GroupName="00. PARAMETER")]
            public int Option2Int
            { get; set; }
    
    
    
        }
    
          public class myConverter : StrategyBaseConverter // or StrategyBaseConverter
        {
            public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object component, Attribute[] attrs)
            {
                // we need the indicator instance which actually exists on the grid
                EnumConverterTEST strategy = component as EnumConverterTEST;
    
                PropertyDescriptorCollection propertyDescriptorCollection = base.GetPropertiesSupported(context)
                                                                            ? base.GetProperties(context, component, attrs)
                                                                            : TypeDescriptor.GetProperties(component, attrs);
    
                if (strategy == null || propertyDescriptorCollection == null)
                    return propertyDescriptorCollection;
    
    
                //Option 1
                PropertyDescriptor Option1Double = propertyDescriptorCollection["Option1Double"];
                PropertyDescriptor Option1Int = propertyDescriptorCollection["Option1Int"];
    
                propertyDescriptorCollection.Remove(Option1Double);
                propertyDescriptorCollection.Remove(Option1Int);
    
                if (strategy.EnumType.Option1) //<<<<<<<<<<<Not sure what to put in these for the enum.
                {
                    propertyDescriptorCollection.Add(Option1Double);
                    propertyDescriptorCollection.Add(Option1Int);
                }
    
    
    
                //Option 2
                PropertyDescriptor Option2Double = propertyDescriptorCollection["Option2Double"];
                PropertyDescriptor Option2Int = propertyDescriptorCollection["Option2Int"];
    
                propertyDescriptorCollection.Remove(Option2Double);
                propertyDescriptorCollection.Remove(Option2Int);
    
                if (strategy.EnumSelector.Option2) //<<<<<<<<<<<Not sure what to put in these for the enum.
                {
                    propertyDescriptorCollection.Add(Option2Double);
                    propertyDescriptorCollection.Add(Option2Int);
                }
    
    
                return propertyDescriptorCollection;
            }
    
            // Important: This must return true otherwise the type convetor will not be called
            public override bool GetPropertiesSupported(ITypeDescriptorContext context)
            { return true; }
        }
    
    }
    
    public enum EnumSelector    
    {
        Option1,
        Option2
    }
    ​
    The code above won't compile because of these 2 lines:

    Click image for larger version

Name:	unknown.png
Views:	497
Size:	154.4 KB
ID:	1240107

    Not sure if I'm on the right track here and just missing something simple. Or if I'm totally off course with this.

    Any help with this would be greatly appreciated!

    Thank you in advance!
    TradeSaber Inc.NinjaTrader Ecosystem Vendor - TradeSaber Inc.

    #2
    Hello TradeSaber,

    Thank you for your post.

    I was able to find the following post containing a snippet that demonstrates how to toggle a bool based on the enum value selected. I modified the sample script attached to the post to include the enum in the snippet. Here is the link to that post:


    Using this idea, you should be able to get your enum to dynamically toggle whether the option 1 or option 2 properties are displayed.

    Please let me know if I may be of further assistance.
    Attached Files

    Comment


      #3
      This worked perfectly for what I needed!

      Thank you so much Emily, you are awesome!
      TradeSaber Inc.NinjaTrader Ecosystem Vendor - TradeSaber Inc.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      71 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      143 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      76 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      47 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      51 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X