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
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!
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
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 futtrader, 04-21-2024, 01:50 AM
|
4 responses
41 views
0 likes
|
Last Post
by futtrader
Today, 03:01 PM
|
||
Can someone turn this thinkscript into a ninjascript. 5PM CST OHL and Previous Day
by Option Whisperer
Started by Option Whisperer, Today, 09:55 AM
|
1 response
11 views
0 likes
|
Last Post
by bltdavid
Today, 02:44 PM
|
||
Started by port119, Today, 02:43 PM
|
0 responses
3 views
0 likes
|
Last Post
by port119
Today, 02:43 PM
|
||
Started by Philippe56140, Today, 02:35 PM
|
0 responses
4 views
0 likes
|
Last Post Today, 02:35 PM | ||
Started by 00nevest, Today, 02:27 PM
|
0 responses
2 views
0 likes
|
Last Post
by 00nevest
Today, 02:27 PM
|
Comment