Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

not all code paths return a value - StrategyBaseConverter

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

    not all code paths return a value - StrategyBaseConverter

    I'm getting this compile error:

    NinjaScript File Error Code Line Column
    MyXConverterStrat.cs 'NinjaTrader.NinjaScript.Strategies.MyXConverterSt rat.GetProperties(System.Component Model.ITypeDescriptorContext, object, System.Attribute[])': not all code paths return a value CS0161 9206 49
    ​From similar code as at the bottom of this post, at this line:

    Code:
    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object component, Attribute[] attrs)
    What's causing the error and what's the fix?

    I checked this prior answer
    ref. https://forum.ninjatrader.com/forum/ninjatrader-7/general-development/67251-error-cs0161-not-all-code-paths-return-a-value​
    which doesn't seem to pinpoint the issue since
    the public bool MyX() method does return false outside the if statement.​


    Ref. https://forum.ninjatrader.com/forum/...44#post1315244

    Code:
    namespace NinjaTrader.NinjaScript.[B]Strategies[/B]
    {
        [TypeConverter("NinjaTrader.NinjaScript.Strategies.MyXConverter")]​
    
        public class MyXConverterStrat : [B]Strategy[/B]
        {​
            
            public bool MyX()
            {
                if(Cond0())
                {
                     return true;
                 }
                    
                 return false;
              }
        }
    }

    Code:
    #region ConverterStuff
        
        public class MyXConverter: StrategyBaseConverter
        {
            public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object component, Attribute[] attrs)
            {
                MyXConverterStrat strategy = component as MyXConverterStrat; // need the same name as the Class name
    
                PropertyDescriptorCollection propertyDescriptorCollection = base.GetPropertiesSupported(context)
                                                                            ? base.GetProperties(context, component, attrs)
                                                                            : TypeDescriptor.GetProperties(component, attrs);
    
                if (strategy == null || propertyDescriptorCollection == null)
                    return propertyDescriptorCollection;
                    
    
                PropertyDescriptor UpSoundFile            = propertyDescriptorCollection["UpSoundFile"];
                
                    
                // remove removable properties first
                
                propertyDescriptorCollection.Remove(UpSoundFile);
    
                // Add back in if...
                            
                if (strategy.MyX())
                {
                    propertyDescriptorCollection.Add(UpSoundFile);                                
                }
            
                return propertyDescriptorCollection;
            }
            
            // Important:  This must return true otherwise the type convetor will not be called
            public override bool GetPropertiesSupported(ITypeDescriptorContext context)
            { return true; }
        }        
        #endregion
    }​


    extra source:
    I'm trying to write code that returns whether or not a given integer is divisible evenly by 1 to 20, but I keep receiving the following error: error CS0161: 'ProblemFive.isTwenty(int)': not all ...
    Last edited by PaulMohn; 08-23-2024, 12:21 AM.

    #2
    Fix ( was missing at the end of public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object component, Attribute[] attrs) method ):

    return propertyDescriptorCollection;

    }

    // Important: This must return true otherwise the type convetor will not be called
    public override bool GetPropertiesSupported(ITypeDescriptorContext context)
    { return true; }​

    Comment


      #3
      Hello PaulMohn,

      The method return type is PropertyDescriptorCollection so a PropertyDescriptorCollection must be returned.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        NinjaTrader_ChelseaB Hello,

        I'm getting this error:
        NinjaScript File Error Code Line Column
        MyEnum5.cs 'NinjaTrader.NinjaScript.Strategies.EnumtoLabelCon verter.ConvertTo(System.ComponentModel.ITypeDescri ptorContext, System.Globalization.CultureInfo, object, System.Type)': not all code paths return a value CS0161 271 33
        What's causing this error "not all code paths return a value CS0161 271 33" From this script?

        Code:
        #region Using declarations
        //using NinjaTrader.Cbi;
        //using NinjaTrader.Core.FloatingPoint;
        //using NinjaTrader.Data;
        //using NinjaTrader.Gui.Chart;
        //using NinjaTrader.Gui.SuperDom;
        //using NinjaTrader.Gui.Tools;
        //using NinjaTrader.Gui;
        //using NinjaTrader.NinjaScript.DrawingTools;
        //using NinjaTrader.NinjaScript.Indicators;
        //using NinjaTrader.NinjaScript;
        //using System.Collections.Generic;
        //using System.ComponentModel.DataAnnotations;
        //using System.ComponentModel;
        //using System.Linq;
        //using System.Text;
        //using System.Threading.Tasks;
        //using System.Windows.Input;
        //using System.Windows.Media;
        //using System.Windows;
        //using System.Xml.Serialization;
        //using System;
        
        
        
        //using System.Collections.ObjectModel;
        //using System.Collections;
        //using System.Windows.Controls;
        //using System.IO;
        //using System.Reflection;
        
        using NinjaTrader.Cbi;
        using NinjaTrader.Core.FloatingPoint;
        using NinjaTrader.Data;
        using NinjaTrader.Gui.Chart;
        using NinjaTrader.Gui.SuperDom;
        using NinjaTrader.Gui;
        using NinjaTrader.NinjaScript.DrawingTools;
        using NinjaTrader.NinjaScript.Indicators;
        using NinjaTrader.NinjaScript;
        using System.Collections.Generic;
        using System.Collections.ObjectModel;
        using System.Collections;
        using System.ComponentModel.DataAnnotations;
        using System.ComponentModel;
        using System.Globalization;
        using System.IO;
        using System.Linq;
        using System.Reflection;
        using System.Text;
        using System.Threading.Tasks;
        using System.Windows.Input;
        using System.Windows.Media;
        using System.Windows;
        using System.Xml.Serialization;
        using System;
        #endregion
        
        #region ENUMS
        
            public enum MyEnum5 // Strategies Buttons Selection
            {
                #region ENUMS STRATEGIES
                
                    A,
                    B,
                    C,
                
                    X,
                    Y,
                    Z,
                
                #endregion
            }
        
        #endregion
        
        //This namespace holds Strategies in this folder and is required. Do not change it.
        namespace NinjaTrader.NinjaScript.Strategies
        {
            
            public class _MyEnum5 : Strategy
            {
                
                #region #3 CLASS VARIABLES - State.SetDefaults : ENUM LOOP BUTTON LABEL == BUTTON ENUM STRING
                    
                    public static string btn1Lbl, btn2Lbl;
        
                    // Create a variable that stores the user's selection for a moving average
                    public static MyEnum5 st1EnumVar, nd2EnumVar;
        
                #endregion
                
                #region (OnStateChange)
                
                    protected override void OnStateChange()
                    {    
                        #region (State == State.SetDefaults)
                        
                            if (State == State.SetDefaults)
                            {
                                Description                                    = @"";
                                Name                                        = "_MyEnum5";
                                Calculate                                    = Calculate.OnBarClose;
                                EntriesPerDirection                            = 7;
                                EntryHandling                                = EntryHandling.AllEntries;
                                IsExitOnSessionCloseStrategy                = true;
                                ExitOnSessionCloseSeconds                    = 30;
                                IsFillLimitOnTouch                            = false;
                                
                                #region OnRender requires .Infinite
                                
                                    MaximumBarsLookBack                        = MaximumBarsLookBack.Infinite;
                                
                                #endregion
                                
                                OrderFillResolution                            = OrderFillResolution.Standard;
                                Slippage                                    = 0;
                                StartBehavior                                = StartBehavior.ImmediatelySubmitSynchronizeAccount;
                                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;
                        
                                #region BUTTONS PARAMETERS - BUTTONS METHOD ENUM SELECTORS
                        
                                    st1EnumVar                    = MyEnum5.X;
                                    nd2EnumVar                    = MyEnum5.B;
                            
                                #endregion
                            
                                #region BUTTONS PARAMETERS
                                    Button1Label                = Btn1Lbl;
                                    
                                    Button2Label                = Btn2Lbl;
                                
                                #endregion
        
                            }
                            
                        #endregion
                        
                        #region (State == State.Configure)
                        
                            else if (State == State.Configure)
                            {
                            }
                            
                        #endregion
                        
                        #region (State == State.Historical)
                        
                            else if (State == State.Historical)
                            {
                            }
                            
                        #endregion
                            
                        #region (State == State.Terminated)
        
                            else if (State == State.Terminated)
                            {
                            }
                            
                        #endregion
                    }
                
                #endregion
                
                #region REMOVE PARAMETERS LABELS FROM CHART
        
                    public override string DisplayName
                    {
                        get {return this.Name;}
                    }
        
                #endregion
                
                #region OnBarUpdate()
                
                    protected override void OnBarUpdate()
                    {
                    }
        
                #endregion
                
        
                #region #2 PROPERTIES
                
                    #region BUTTONS 1 TO 6
        
                        #region Button 1 Parameters
                            
                            //[TypeConverter(typeof(EnumtoLabelConverter))]
                            [Display(Name="Methods 1", Description="", Order=1, GroupName="Parameters")]
                            [RefreshProperties(RefreshProperties.All)]
                            public static MyEnum5 St1EnumVar
                            { get; set; }
                        
                            [NinjaScriptProperty]
                            [Display(Name="Label Button 1", Order=2, GroupName="Parameters")]
                            public string Button1Label
                            { get; set; }
                        
                            [NinjaScriptProperty]
                            [Display(Name="Label Btn1Lbl ", Order=3, GroupName="Parameters")]
                            public static string Btn1Lbl
                            { get; set; }
                            
                        #endregion
                        
                    
                        #region Button 2 Parameters
                            
                            //[TypeConverter(typeof(EnumtoLabelConverter))]
                            [Display(Name="Methods 2", Description="", Order=4, GroupName="Parameters")]
                            [RefreshProperties(RefreshProperties.All)]
                            public static MyEnum5 Nd2EnumVar
                            { get; set; }
                        
                            [NinjaScriptProperty]
                            [Display(Name="Label Button 2", Order=5, GroupName="Parameters")]
                            public string Button2Label
                            { get; set; }
                        
                            [NinjaScriptProperty]
                            [Display(Name="Label Btn2Lbl", Order=6, GroupName="Parameters")]
                            public static string Btn2Lbl
                            { get; set; }
                            
                        #endregion
                
                    #endregion
                    
                #endregion
        
                
                            
                
        //        #region Use Case #4: Display "Friendly" enum values
        
        //        [TypeConverter(typeof(EnumtoLabelConverter))] // Converts the enum to string values
        //        [PropertyEditor("NinjaTrader.Gui.Tools.StringStandardValuesEditorKey")] // Enums normally automatically get a combo box, but we need to apply this specific editor so default value is automatically selected
        //        [Display(Name = "Friendly Enum", Order = 10, GroupName = "Use Case #4")]
        //        public MyEnum5 Nd2EnumVar
        //        { get; set; }
        
        //        #endregion
            }
            
            #region
                
                // Since this is only being applied to a specific property rather than the whole class,
                // we don't need to inherit from IndicatorBaseConverter and we can just use a generic TypeConverter
                public class EnumtoLabelConverter : TypeConverter
                {
                    // Set the values to appear in the combo box
                    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
                    {
                        List<string> values = new List<string>()
                        { "A","B","C","X","Y","Z" };
        
                        return new StandardValuesCollection(values);
                    }
        
                    // map the MyEnum type to "Friendly" string
                    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
                    {
                        MyEnum5 stringVal = (MyEnum5) Enum.Parse(typeof(MyEnum5), value.ToString());
                    
                        if (_MyEnum5.st1EnumVar == stringVal)
                            _MyEnum5.btn1Lbl = value.ToString();
                        //else
                            //return string.Empty;
                    }
        
                    // required interface members needed to compile
                    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
                    { return true; }
        
                    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
                    { return true; }
        
                    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
                    { return true; }
        
                    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
                    { return true; }
                }
                
            #endregion
        }

        At this line:

        Code:
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)

        From the SampleIndicatorTypeConverter Use Case #4
        https://ninjatrader.com/support/helpguides/nt8/NT%20HelpGuide%20English.html?using_a_typeconverter_to_custo.htm

        or the SampleStrategyTypeConverter Use Case #4
        https://web.archive.org/web/20240827111419/https://paste.c-net.org/DiseaseConverts https://ninjatrader.com/support/helpguides/nt8/NT%20HelpGuide%20English.html?using_a_typeconverter_to_custo.htm



        I don't see extra return instructions:

        Code:
            #region Use Case #4: Display "friendly" enum values
            public enum MyEnum
            {
                MyCustom1,
                MyCustom2,
                MyCustom3
            }
        
            // Since this is only being applied to a specific property rather than the whole class,
            // we don't need to inherit from IndicatorBaseConverter and we can just use a generic TypeConverter
            public class FriendlyEnumConverter : TypeConverter
            {
                // Set the values to appear in the combo box
                public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
                {
                    List<string> values = new List<string>() { "My custom one", "My custom two", "My custom three" };
        
                    return new StandardValuesCollection(values);
                }
        
                // map the value from "Friendly" string to MyEnum type
                public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
                {
                    string stringVal = value.ToString();
                    
                    switch (stringVal)
                    {
                        case "My custom one":
                        return MyEnum.MyCustom1;
                        case "My custom two":
                        return MyEnum.MyCustom2;
                        case "My custom three":
                        return MyEnum.MyCustom3;
                    }
                    return MyEnum.MyCustom1;
                }
        
                // map the MyEnum type to "Friendly" string
                public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
                {
                    MyEnum stringVal = (MyEnum) Enum.Parse(typeof(MyEnum), value.ToString());
                    
                    switch (stringVal)
                    {
                        case MyEnum.MyCustom1:
                        return "My custom one";
                        case MyEnum.MyCustom2:
                        return "My custom two";
                        case MyEnum.MyCustom3:
                        return "My custom three";
                    }
                    return string.Empty;
                }
        
                // required interface members needed to compile
                public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
                { return true; }
        
                public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
                { return true; }
        
                public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
                { return true; }
        
                public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
                { return true; }
            }
            #endregion​

        Comment


          #5
          Hello PaulMohn,

          The method must return an object.

          The SampleIndicatorTypeConverter reference sample returns a string.

          // map the bool type to "Friendly" string
          public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
          {
          return (bool) value ? "Turn on" : "Turn off";
          }​
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            NinjaTrader_ChelseaB thanks,

            That is from Use Case #3,​ not what I used. I used Use Case #4.

            Here is use case #4 from the reduced SampleStrategyTypeConverter​ script:

            Code:
            //
            // Copyright (C) 2018, NinjaTrader LLC <www.ninjatrader.com>.
            // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
            //
            #region Using declarations
            using NinjaTrader.Cbi;
            using NinjaTrader.Core.FloatingPoint;
            using NinjaTrader.Data;
            using NinjaTrader.Gui.Chart;
            using NinjaTrader.Gui.SuperDom;
            using NinjaTrader.Gui;
            using NinjaTrader.NinjaScript.DrawingTools;
            using NinjaTrader.NinjaScript.Indicators;
            using NinjaTrader.NinjaScript;
            using System.Collections.Generic;
            using System.Collections.ObjectModel;
            using System.Collections;
            using System.ComponentModel.DataAnnotations;
            using System.ComponentModel;
            using System.Globalization;
            using System.IO;
            using System.Linq;
            using System.Reflection;
            using System.Text;
            using System.Threading.Tasks;
            using System.Windows.Input;
            using System.Windows.Media;
            using System.Windows;
            using System.Xml.Serialization;
            using System;
            #endregion
            
            //This namespace holds strategies in this folder and is required. Do not change it.
            namespace NinjaTrader.NinjaScript.Strategies
            {
                // Notes:
                // Helper classes and type converters are defined below the indicator, separated by use case
                // Property definitions are documented as necessary in the Properties region
                // Debugging issues will likely require compiling NS in debug mode and attaching VS to ninjatrader.exe
                // Setting breakpoints to examine values and seeing debug output that NT creates is helpful
                
                public class _SampleStrategyTypeConverter1 : Strategy
                {
                    
                    protected override void OnStateChange()
                    {
                        if (State == State.SetDefaults)
                        {
                            Description                                    = @"Demonstrating using type converters to customize the Indicator property grid";
                            Name                                        = "_SampleStrategyTypeConverter1";
                            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;
                            
                            EnumValue         = MyEnumOne.MyCustom2;
                        }
                    }
            
                    protected override void OnBarUpdate()
                    {
                    }
            
                    #region Use Case #4: Display "Friendly" enum values
            
                    [TypeConverter(typeof(FriendlyEnumConverter2))] // Converts the enum to string values
                    // Enums normally automatically get a combo box, but we need to apply this specific editor so default value is automatically selected
                    [PropertyEditor("NinjaTrader.Gui.Tools.StringStandardValuesEditorKey")]
                    [Display(Name = "Friendly Enum", Order = 8, GroupName = "Use Case #4")]
                    public MyEnumOne EnumValue
                    { get; set; }
            
                    #endregion
                }
            
            
                #region Use Case #4: Display "friendly" enum values
                public enum MyEnumOne
                {
                    MyCustom1,
                    MyCustom2,
                    MyCustom3
                }
            
                // Since this is only being applied to a specific property rather than the whole class,
                // we don't need to inherit from IndicatorBaseConverter and we can just use a generic TypeConverter
                public class FriendlyEnumConverter2 : TypeConverter
                {
                    // Set the values to appear in the combo box
                    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
                    {
                        List<string> values = new List<string>() { "My custom one", "My custom two", "My custom three" };
            
                        return new StandardValuesCollection(values);
                    }
            
                    // map the value from "Friendly" string to MyEnumOne type
                    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
                    {
                        string stringVal = value.ToString();
                        
                        switch (stringVal)
                        {
                            case "My custom one":
                            return MyEnumOne.MyCustom1;
                            case "My custom two":
                            return MyEnumOne.MyCustom2;
                            case "My custom three":
                            return MyEnumOne.MyCustom3;
                        }
                        return MyEnumOne.MyCustom1;
                    }
            
                    // map the MyEnumOne type to "Friendly" string
                    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
                    {
                        MyEnumOne stringVal = (MyEnumOne) Enum.Parse(typeof(MyEnumOne), value.ToString());
                        
                        switch (stringVal)
                        {
                            case MyEnumOne.MyCustom1:
                            return "My custom one";
                            case MyEnumOne.MyCustom2:
                            return "My custom two";
                            case MyEnumOne.MyCustom3:
                            return "My custom three";
                        }
                        return string.Empty;
                    }
            
                    // required interface members needed to compile
                    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
                    { return true; }
            
                    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
                    { return true; }
            
                    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
                    { return true; }
            
                    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
                    { return true; }
                }
                #endregion
            }​
            No return.
            But this one does compile.

            Comment


              #7
              I tested this but it still throws the error:

              Code:
              
              
                          // map the MyEnum type to "Friendly" string
                          public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
                          {
                              MyEnum5 stringVal = (MyEnum5) Enum.Parse(typeof(MyEnum5), value.ToString());
                          
                              if (_MyEnum5.st1EnumVar == stringVal)
                                  _MyEnum5.btn1Lbl = value.ToString();
                              else
                                  return string.Empty;
                          }​
              This compiles:

              Code:
                          // map the MyEnum type to "Friendly" string
                          public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
                          {
                              MyEnum5 stringVal = (MyEnum5) Enum.Parse(typeof(MyEnum5), value.ToString());
                              
                              if (_MyEnum5.st1EnumVar == stringVal)
                              {
                                  _MyEnum5.btn1Lbl = value.ToString();
                                  return _MyEnum5.btn1Lbl; // Return the label if the condition is met
                              }
                              
                              return string.Empty; // Return an empty string if the condition is not met
                          }​
              Last edited by PaulMohn; 08-27-2024, 07:02 AM.

              Comment


                #8
                Hello PaulMohn,

                if _MyEnum5.st1EnumVar is equal to stringVal what is returned?
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                51 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                127 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                69 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                42 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                46 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X