I am working on a strategy that has multiple options. Trying to hide/remove certain properties in order to prevent unnecessary clutter for the user.
This is my first time working with converters. But thanks to this reference:
https://forum.ninjatrader.com/forum/...-grid-behavior
I was able to get it working when it comes to bools. (Case #1)
Now I am trying to achieve the same thing from Case#1 but whenever I select an option from an enum.
For example.
When Selecting Option1 from the drop down. I want the Option2 properties to disappear.
When Selecting Option 2 from the drop down. I want Option 1 properties to disappear.
I wrote this simple strategy in an attempt to make a template for this specific request
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.Indicators;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
[TypeConverter("NinjaTrader.NinjaScript.Strategies.myConverter")]
public class EnumConverterTEST : Strategy
{
private bool Option1Bool;
private bool Option2Bool;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "EnumConverterTEST";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
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;
EnumType = EnumSelector.Option1;
Option1Double = 5.75;
Option1Int = 5;
Option2Double = 10.25;
Option2Int = 3;
Option1Bool = false;
Option2Bool = false;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
switch (EnumType)
{
case EnumSelector.Option1:
{
Option1Bool = true;
Print("Option1");
break;
}
case EnumSelector.Option2:
{
Option2Bool = true;
Print("Option2");
break;
}
}
}
}
protected override void OnBarUpdate()
{
//Add your custom strategy logic here.
}
[RefreshProperties(RefreshProperties.All)]
[Display(Name = "ENUM SELECTOR", Order = 0, GroupName = "00. PARAMETER")]
public EnumSelector EnumType
{ get; set; }
//Option 1...Options
[NinjaScriptProperty]
[Display(Name="Option1Double", Order=1, GroupName="00. PARAMETER")]
public double Option1Double
{ get; set; }
[NinjaScriptProperty]
[Display(Name="Option1Int", Order=2, GroupName="00. PARAMETER")]
public int Option1Int
{ get; set; }
//Option 2...Options
[NinjaScriptProperty]
[Display(Name="Option2Double", Order=3, GroupName="00. PARAMETER")]
public double Option2Double
{ get; set; }
[NinjaScriptProperty]
[Display(Name="Option2Int", Order=4, GroupName="00. PARAMETER")]
public int Option2Int
{ get; set; }
}
public class myConverter : StrategyBaseConverter // or StrategyBaseConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object component, Attribute[] attrs)
{
// we need the indicator instance which actually exists on the grid
EnumConverterTEST strategy = component as EnumConverterTEST;
PropertyDescriptorCollection propertyDescriptorCollection = base.GetPropertiesSupported(context)
? base.GetProperties(context, component, attrs)
: TypeDescriptor.GetProperties(component, attrs);
if (strategy == null || propertyDescriptorCollection == null)
return propertyDescriptorCollection;
//Option 1
PropertyDescriptor Option1Double = propertyDescriptorCollection["Option1Double"];
PropertyDescriptor Option1Int = propertyDescriptorCollection["Option1Int"];
propertyDescriptorCollection.Remove(Option1Double);
propertyDescriptorCollection.Remove(Option1Int);
if (strategy.EnumType.Option1) //<<<<<<<<<<<Not sure what to put in these for the enum.
{
propertyDescriptorCollection.Add(Option1Double);
propertyDescriptorCollection.Add(Option1Int);
}
//Option 2
PropertyDescriptor Option2Double = propertyDescriptorCollection["Option2Double"];
PropertyDescriptor Option2Int = propertyDescriptorCollection["Option2Int"];
propertyDescriptorCollection.Remove(Option2Double);
propertyDescriptorCollection.Remove(Option2Int);
if (strategy.EnumSelector.Option2) //<<<<<<<<<<<Not sure what to put in these for the enum.
{
propertyDescriptorCollection.Add(Option2Double);
propertyDescriptorCollection.Add(Option2Int);
}
return propertyDescriptorCollection;
}
// Important: This must return true otherwise the type convetor will not be called
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{ return true; }
}
}
public enum EnumSelector
{
Option1,
Option2
}
Not sure if I'm on the right track here and just missing something simple. Or if I'm totally off course with this.
Any help with this would be greatly appreciated!
Thank you in advance!

Comment