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; }
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Casting to BarsPeriodType
Collapse
X
-
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:Last edited by SystemTrading; 02-11-2019, 11:08 AM.Tags: None
-
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.htmJosh G.NinjaTrader Customer Service
-
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by CarlTrading, 03-31-2026, 09:41 PM
|
1 response
156 views
1 like
|
Last Post
|
||
|
Started by CarlTrading, 04-01-2026, 02:41 AM
|
0 responses
90 views
1 like
|
Last Post
by CarlTrading
04-01-2026, 02:41 AM
|
||
|
Started by CaptainJack, 03-31-2026, 11:44 PM
|
0 responses
140 views
2 likes
|
Last Post
by CaptainJack
03-31-2026, 11:44 PM
|
||
|
Started by CarlTrading, 03-30-2026, 11:51 AM
|
0 responses
130 views
1 like
|
Last Post
by CarlTrading
03-30-2026, 11:51 AM
|
||
|
Started by CarlTrading, 03-30-2026, 11:48 AM
|
0 responses
107 views
0 likes
|
Last Post
by CarlTrading
03-30-2026, 11:48 AM
|

Comment