• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Casting to BarsPeriodType

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

    Casting to BarsPeriodType

    I've created a custom BarsType and assigned the enumeration value of 1891861. The custom BarsType does not appear as an option in the BarsPeriodType enum, so I created a type converter to show the option in a drop down of my indicator parameters. Everything works as expected with the exception that it throws an error when serializing. The error message I get says "Instance validation error: '1891861' is not a valid value for NinjaTrader.Data.BarsPeriodType". How would I go about casting the enumeration value to a BarsPeriodType?

    Code:
    public class MyBarsPeriodTypeConverter : TypeConverter
    {
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            List<string> values = new List<string>();
            foreach (BarsPeriodType barsPeriodType in (BarsPeriodType[]) Enum.GetValues(typeof(BarsPeriodType)))
                values.Add(barsPeriodType.ToString());
            values.Add("My Custom Bars");
    
            return new StandardValuesCollection(values);
        }
    
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string stringVal = value.ToString();
            switch (stringVal)
            {
                case "Day":
                    return BarsPeriodType.Day;
                case "HeikenAshi":
                    return BarsPeriodType.HeikenAshi;
                case "Kagi":
                    return BarsPeriodType.Kagi;
                case "LineBreak":
                    return BarsPeriodType.LineBreak;
                case "Minute":
                    return BarsPeriodType.Minute;
                case "Month":
                    return BarsPeriodType.Month;
                case "PointAndFigure":
                    return BarsPeriodType.PointAndFigure;
                case "Range":
                    return BarsPeriodType.Range;
                case "Renko":
                    return BarsPeriodType.Renko;
                case "Second":
                    return BarsPeriodType.Second;
                case "Tick":
                    return BarsPeriodType.Tick;
                case "Volume":
                    return BarsPeriodType.Volume;
                case "Volumetric":
                    return BarsPeriodType.Volumetric;
                case "Week":
                    return BarsPeriodType.Week;
                case "Year":
                    return BarsPeriodType.Year;
                case "My Custom Bars":
                    return (BarsPeriodType)1891861;
            }
            return BarsPeriodType.Minute;
        }
    
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {    
            BarsPeriodType stringVal = (BarsPeriodType) Enum.Parse(typeof(BarsPeriodType), value.ToString());        
            switch (stringVal)
            {
                case BarsPeriodType.Day:
                    return BarsPeriodType.Day.ToString();
                case BarsPeriodType.HeikenAshi:
                    return BarsPeriodType.HeikenAshi.ToString();
                case BarsPeriodType.Kagi:
                    return BarsPeriodType.Kagi.ToString();
                case BarsPeriodType.LineBreak:
                    return BarsPeriodType.LineBreak.ToString();
                case BarsPeriodType.Minute:
                    return BarsPeriodType.Minute.ToString();
                case BarsPeriodType.Month:
                    return BarsPeriodType.Month.ToString();
                case BarsPeriodType.PointAndFigure:
                    return BarsPeriodType.PointAndFigure.ToString();
                case BarsPeriodType.Range:
                    return BarsPeriodType.Range.ToString();
                case BarsPeriodType.Renko:
                    return BarsPeriodType.Renko.ToString();
                case BarsPeriodType.Second:
                    return BarsPeriodType.Second.ToString();
                case BarsPeriodType.Tick:
                    return BarsPeriodType.Tick.ToString();
                case BarsPeriodType.Volume:
                    return BarsPeriodType.Volume.ToString();
                case BarsPeriodType.Volumetric:
                    return BarsPeriodType.Volumetric.ToString();
                case BarsPeriodType.Week:
                    return BarsPeriodType.Week.ToString();
                case BarsPeriodType.Year:
                    return BarsPeriodType.Year.ToString();
                case (BarsPeriodType)1891861:
                    return "My Custom Bars";
            }
            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; }
    }
    Last edited by SystemTrading; 02-11-2019, 11:08 AM.

    #2
    Hello SystemTrading,
    Thanks for your post.

    The custom BarsType is never going to appear in the BarsPeriodType enum. That is for the built in NinjaTrader bar types only.

    In order to cast the BarsPeriodType simply use an integer of the registered enum value and and then cast it. There is an example of how to do this in the "Tips" section of the AddDataSeries() Help Guide page: https://ninjatrader.com/support/help...dataseries.htm
    JoshNinjaTrader Customer Service

    Comment


      #3
      That did the trick. Thank you for your assistance.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by sohailashraf, Yesterday, 10:18 PM
      0 responses
      4 views
      0 likes
      Last Post sohailashraf  
      Started by jjnc2006, Yesterday, 07:36 PM
      0 responses
      9 views
      0 likes
      Last Post jjnc2006  
      Started by futurenick, Yesterday, 06:26 AM
      1 response
      12 views
      0 likes
      Last Post nkhoi
      by nkhoi
       
      Started by NinjaCustomer, Yesterday, 12:29 PM
      2 responses
      10 views
      0 likes
      Last Post NinjaCustomer  
      Started by Steve L, Yesterday, 11:32 AM
      0 responses
      8 views
      0 likes
      Last Post Steve L
      by Steve L
       
      Working...
      X