_SubEnumFilter foreach Ninjatrader

https://forum.ninjatrader.com/forum/ninjatrader-8/strategy-development/1315482-error-cs0117-does-not-contain-a-definition-for-value?p=1316855#post1316855
 avatar
unknown
csharp
2 years ago
14 kB
22
No Index
//https://forum.ninjatrader.com/forum/ninjatrader-8/strategy-development/1315482-error-cs0117-does-not-contain-a-definition-for-value?p=1316855#post1316855

#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
{
	public enum MyMainEnum
	{Item1,Item2,Item3,ItemA,ItemB,ItemC};

	public class _SubEnumFilter : Strategy
	{
		#region CLASS VARIABLES
		
			#region CONTROLS VARIABLES
		
				private System.Windows.Controls.RowDefinition	addedRow1, addedRow2, addedRow3;
				private Gui.Chart.ChartTab						chartTab;
				private Gui.Chart.Chart							chartWindow;
				private System.Windows.Controls.Grid			chartTraderGrid, chartTraderButtonsGrid, upperButtonsGrid, lowerButtonsGrid, lowerButtonsGrid1;
				private System.Windows.Controls.Button[]		buttonsArray;
				private bool									panelActive;
				private System.Windows.Controls.TabItem			tabItem;
			
			#endregion
	
			#region #4 CLASS VARIABLES - BUTTONS : BUTTONS TICKS VARIABLES
			
	    			private int _button0Ticks, _button1Ticks;

			#endregion
	
			#region #4 CLASS VARIABLES - CreateWPFControls() - BUTTONS : SHOW TICKS # ONLY FOR STP MOVE METHODS 
	        	
				private string subTicksCheck0,subTicksCheck1;

			#endregion

		#endregion
		
		#region region OnstateChange
		
			protected override void OnStateChange()
			{
				#region region State.SetDefaults
				
					if (State == State.SetDefaults)
					{
						Description									= @"Enter the description for your new custom Strategy here.";
						Name										= "_SubEnumFilter";
						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;

						
						#region BUTTONS PARAMETERS - BUTTONS METHOD ENUM SELECTORS
				
							EnumValA					= MyMainEnum.ItemA;
							EnumValB					= MyMainEnum.Item3;
					
						#endregion
						
						
						#region BUTTONS PARAMETERS
	
							Button0Label				= "Btn0Lbl";
							Button0Color				= Brushes.ForestGreen;
							_button0Ticks				= 4;
	
							Button1Label				= "Btn1Lbl";
							Button1Color				= Brushes.Crimson;
							_button1Ticks				= 2;
						
						#endregion
					}
			
				#endregion
				
				#region region State.Configure
				
					else if (State == State.Configure)
					{	
						#region
						
						#endregion
					}
					
				#endregion
				
				#region region State.DataLoaded

					else if (State == State.DataLoaded)
					{	
						#region
						
						#endregion
					}
					
				#endregion
				
				#region region State.Historical
				
					else if (State == State.Historical)
					{
						#region Chart Trader Buttons Load
						
							if (ChartControl != null)
							{
								ChartControl.Dispatcher.InvokeAsync(() =>
								{
									CreateWPFControls();
								});
							}
						
						#endregion

					}
				
				#endregion
				
				#region (State == State.Terminated)

					else if (State == State.Terminated)
					{
						if (ChartControl != null)
						{
							ChartControl.Dispatcher.InvokeAsync(() =>
							{
								DisposeWPFControls();
							});
						}
					}
					
				#endregion
			}
			
		#endregion

		protected void CreateWPFControls()
		{
			#region region GRID & ROWS & BUTTONS
			
				#region REGION GRID & ROWS
				
					chartWindow		= Window.GetWindow(ChartControl.Parent) as Gui.Chart.Chart;

					// if not added to a chart, do nothing
					if (chartWindow == null)
						return;

					// this is the entire chart trader area grid
					chartTraderGrid			= (chartWindow.FindFirst("ChartWindowChartTraderControl") as Gui.Chart.ChartTrader).Content as System.Windows.Controls.Grid;

					// this grid contains the existing chart trader buttons
					chartTraderButtonsGrid	= chartTraderGrid.Children[0] as System.Windows.Controls.Grid;

					// 1st Grid of 2 buttons below bid and ask prices and order controls (chartTraderButtonsGrid)
					upperButtonsGrid = new System.Windows.Controls.Grid();
					System.Windows.Controls.Grid.SetColumnSpan(upperButtonsGrid, 3);

					upperButtonsGrid.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());
					upperButtonsGrid.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition() { Width = new GridLength((double)Application.Current.FindResource("MarginBase")) }); // separator column
					upperButtonsGrid.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());

					// these rows will be added later, but we can create them now so they only get created once
					addedRow1	= new System.Windows.Controls.RowDefinition() { Height = new GridLength(31) };

					// this style (provided by NinjaTrader_MichaelM) gives the correct default minwidth (and colors) to make buttons appear like chart trader buttons
					Style basicButtonStyle	= Application.Current.FindResource("BasicEntryButton") as Style;
				
				#endregion
			
				#region BUTTONS
					
					#region BUTTONS ARRAY AND LOOP
					
					    #region ADD BUTTONS TICKS # ONLY TO STP MOVE ENUM METHODS
					                    
							MyMainEnum[] MySubEnumFilterArray = new MyMainEnum[]
		                    {
		                        MyMainEnum.ItemB,
		                        MyMainEnum.Item2,
		                    };


							foreach (var value in MySubEnumFilterArray)
							{
								if (EnumValA == value)
								{
									subTicksCheck0 = ( " + " + _button0Ticks.ToString() );
    								break;
								}
								else
								{
									subTicksCheck0 = string.Empty;
								}
								
								if (EnumValB == value)
								{
									subTicksCheck1 = ( " + " + _button1Ticks.ToString() );
    								break;
								}
								else
								{
									subTicksCheck1 = string.Empty;
								}
							}

					    #endregion
						
						#region BUTTONS CREATING LOOP
							
							buttonsArray = new System.Windows.Controls.Button[2];

							for (int i = 0; i < 2; ++i)
							{
								System.Windows.Media.Brush ButtonBackground;
								string ButtonLabels, ButtonTicks;
						
								switch(i) 
								{
								  case 0:
									ButtonLabels        = Button0Label;
									ButtonBackground	= Button0Color;
									ButtonTicks         = subTicksCheck0; 
									break;
								  case 1:
									ButtonLabels        = Button1Label;
									ButtonBackground	= Button1Color;
									ButtonTicks         = subTicksCheck1;
									break;	
								  default:
									ButtonLabels	    = "";
									ButtonBackground	= null;
									ButtonTicks         = "1";						
									break;
								}
								
								buttonsArray[i]	= new System.Windows.Controls.Button()	
								{
									Content			= string.Format("{0}{1}",ButtonLabels,ButtonTicks),
									Height			= 25,
									Margin			= new Thickness(0,0,0,0),
									Padding			= new Thickness(0,0,0,0),
									Style			= basicButtonStyle,
									FontFamily 		= new FontFamily("Agency Fb"),
									FontSize 		= 11,
									FontWeight		= FontWeights.Bold
								};

								buttonsArray[i].BorderBrush	= Brushes.DimGray;
								buttonsArray[i].Background = ButtonBackground;
								
							}
						
						#endregion
						
					#endregion

					#region BUTTONS CLICKS EVENTS SUBSCRIPTIONS
						
						buttonsArray[0].Click += Button1Click;
						buttonsArray[1].Click += Button2Click;

					#endregion

					#region BUTTONS GRID COLUMNS

						System.Windows.Controls.Grid.SetColumn(buttonsArray[0], 0);
						System.Windows.Controls.Grid.SetColumn(buttonsArray[1], 2);

					#endregion
					
					#region BUTTONS CONTROLS BOOL SWITCHES ON/OFF
						
						upperButtonsGrid.Children.Add(buttonsArray[0]);
						upperButtonsGrid.Children.Add(buttonsArray[1]);

					#endregion

					#region TABS SELECTION SHIFT
						
						if (TabSelected())
							InsertWPFControls();

						chartWindow.MainTabControl.SelectionChanged += TabChangedHandler;
					
					#endregion
				
				#endregion

			#endregion
		}
		
		
		#region BUTTONS CLICK EVENTS METHODS

			protected void Button1Click(object sender, RoutedEventArgs e)
			{
			}

			protected void Button2Click(object sender, RoutedEventArgs e)
			{
			}
			
		#endregion
		
		
		public void InsertWPFControls()
		{
			if (panelActive)
				return;

			// add a new row (addedRow1) for upperButtonsGrid below the ask and bid prices and pnl display
			chartTraderGrid.RowDefinitions.Add(addedRow1);
			// set our upper grid to that new panel
			System.Windows.Controls.Grid.SetRow(upperButtonsGrid, (chartTraderGrid.RowDefinitions.Count - 1));
			// and add it to the buttons grid
			chartTraderGrid.Children.Add(upperButtonsGrid);

			panelActive = true;
		}

		
		protected override void OnBarUpdate() 
		{
		}

		
		public void DisposeWPFControls()
		{
			if (chartWindow != null)
				chartWindow.MainTabControl.SelectionChanged -= TabChangedHandler;

			if (buttonsArray[0] != null)
				buttonsArray[0].Click -= Button1Click;
			if (buttonsArray[0] != null)
				buttonsArray[1].Click -= Button2Click;

			RemoveWPFControls();
		}
		

		protected void RemoveWPFControls()
		{
			if (!panelActive)
				return;

			if (chartTraderButtonsGrid != null || upperButtonsGrid != null)
			{
				chartTraderGrid.Children.Remove(upperButtonsGrid);
				chartTraderGrid.RowDefinitions.Remove(addedRow1);
			}

			panelActive = false;
		}

		
		private bool TabSelected()
		{
			bool tabSelected = false;

			// loop through each tab and see if the tab this indicator is added to is the selected item
			foreach (System.Windows.Controls.TabItem tab in chartWindow.MainTabControl.Items)
				if ((tab.Content as Gui.Chart.ChartTab).ChartControl == ChartControl && tab == chartWindow.MainTabControl.SelectedItem)
					tabSelected = true;

			return tabSelected;
		}

		
		private void TabChangedHandler(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
		{
			if (e.AddedItems.Count <= 0)
				return;

			tabItem = e.AddedItems[0] as System.Windows.Controls.TabItem;
			if (tabItem == null)
				return;

			chartTab = tabItem.Content as Gui.Chart.ChartTab;
			if (chartTab == null)
				return;

			if (TabSelected())
				InsertWPFControls();
			else
				RemoveWPFControls();
		}
		

		#region Properties
		
		    #region Button 1 Parameters
		
				[Display(Name="Enum Selector Button 0", Description="", Order=0, GroupName="Button #0 Parameters")]
				[RefreshProperties(RefreshProperties.All)]
				public MyMainEnum EnumValA
				{ get; set; }
		    
        		[NinjaScriptProperty]
                [Display(Name="Label Button 0 On", Order=1, GroupName="Button #0 Parameters")]
                public string Button0Label
                { get; set; }
	        
        		[NinjaScriptProperty]
        		[XmlIgnore]
        		[Display(Name="Button0Color", Order=2, GroupName="Button #0 Parameters")]
        		public System.Windows.Media.Brush Button0Color
        		{ get; set; }
        
        		[Browsable(false)]
        		public string Button0ColorSerializable
        		{
        			get { return Serialize.BrushToString(Button0Color); }
        			set { Button0Color = Serialize.StringToBrush(value); }
        		}
					
        		[NinjaScriptProperty]
        		[Range(0, int.MaxValue)]
        		[Display(Name="Button0Ticks", Order=3, GroupName="Button #0 Parameters")]
        		public int _Button0Ticks
				{
				    get { return _button0Ticks; }

				    set { _button0Ticks = value; }
				}
        		
        	#endregion
        	
		
		    #region Button 2 Parameters
		
				[Display(Name="Enum Selector Button 1", Description="", Order=0, GroupName="Button #1 Parameters")]
				[RefreshProperties(RefreshProperties.All)]
				public MyMainEnum EnumValB
				{ get; set; }
		    
        		[NinjaScriptProperty]
                [Display(Name="Label Button 1 On", Order=1, GroupName="Button #1 Parameters")]
                public string Button1Label
                { get; set; }
	        
        		[NinjaScriptProperty]
        		[XmlIgnore]
        		[Display(Name="Button1Color", Order=2, GroupName="Button #1 Parameters")]
        		public System.Windows.Media.Brush Button1Color
        		{ get; set; }
        
        		[Browsable(false)]
        		public string Button1ColorSerializable
        		{
        			get { return Serialize.BrushToString(Button1Color); }
        			set { Button1Color = Serialize.StringToBrush(value); }
        		}
					
        		[NinjaScriptProperty]
        		[Range(0, int.MaxValue)]
        		[Display(Name="Button1Ticks", Order=3, GroupName="Button #1 Parameters")]
        		public int _Button1Ticks
				{
				    get { return _button1Ticks; }

				    set { _button1Ticks = value; }
				}
        		
        	#endregion
        	
		#endregion
	}
}
Editor is loading...
Leave a Comment