Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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
    Josh G.NinjaTrader Customer Service

    Comment


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

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by algospoke, Yesterday, 06:40 PM
      2 responses
      23 views
      0 likes
      Last Post algospoke  
      Started by ghoul, Today, 06:02 PM
      3 responses
      14 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by jeronymite, 04-12-2024, 04:26 PM
      3 responses
      45 views
      0 likes
      Last Post jeronymite  
      Started by Barry Milan, Yesterday, 10:35 PM
      7 responses
      22 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by AttiM, 02-14-2024, 05:20 PM
      10 responses
      181 views
      0 likes
      Last Post jeronymite  
      Working...
      X