New problem:
Given what follows, how to get the enum's inputs displayed on the Strategy Properties Windows other than with the static keyword?
( in this short clip, only "Method 2" enum variable is displayed because it is both non static and not used in the EnumtoLabelConverter : TypeConverter class,
whereas "Method 1" enum variable, which is used in the EnumtoLabelConverter : TypeConverter class), can't be displayed because it would need to be non-static but if it is the error below is thrown)
If I remove the static keyword from this line,
public static MyEnum5 St1EnumVar
then it throws this error:
NinjaScript File Error Code Line Column
The script needed this editing to show the enums inputs on the Strategy Properties Windows from the chart :
#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
{
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))] // Converts the enum to string values
[PropertyEditor("NinjaTrader.Gui.Tools.StringStandardValuesEditorKey")]
[Display(Name="Methods 1", Description="", Order=1, GroupName="Methods 1")]
//[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))] // Converts the enum to string values
[PropertyEditor("NinjaTrader.Gui.Tools.StringStandardValuesEditorKey")]
[Display(Name="Methods 2", Description="", Order=4, GroupName="Methods 2")]
//[RefreshProperties(RefreshProperties.All)]
public 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 string Btn2Lbl
{ get; set; }
#endregion
#endregion
#endregion
}
#region ENUMS
public enum MyEnum5
{ A, B, C, X, Y, Z }
#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();
return _MyEnum5.btn1Lbl; // Return the label if the condition is met
}
return string.Empty; // Return an empty string if the condition is not met
}
// 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
}
Reduced pseudocode:
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class _MyEnum5 : Strategy
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
St1EnumVar = MyEnum5.X;
Nd2EnumVar = MyEnum5.B;
}
}
[TypeConverter(typeof(EnumtoLabelConverter))] // Converts the enum to string values
[PropertyEditor("NinjaTrader.Gui.Tools.StringStandardValuesEditorKey")]
[Display(Name="Methods 1", Description="", Order=1, GroupName="Methods 1")]
//[RefreshProperties(RefreshProperties.All)]
public MyEnum5 St1EnumVar
{ get; set; }
[TypeConverter(typeof(EnumtoLabelConverter))] // Converts the enum to string values
[PropertyEditor("NinjaTrader.Gui.Tools.StringStandardValuesEditorKey")]
[Display(Name="Methods 2", Description="", Order=4, GroupName="Methods 2")]
//[RefreshProperties(RefreshProperties.All)]
public MyEnum5 Nd2EnumVar
{ get; set; }
}
public enum MyEnum5
{ A, B, C, X, Y, Z }
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();
return _MyEnum5.btn1Lbl; // Return the label if the condition is met
}
return string.Empty; // Return an empty string if the condition is not met
}
// 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; }
}
}

Comment