QuantitySelector

mail@pastecode.io avatar
unknown
csharp
3 years ago
10 kB
11
Indexable
#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.DrawingTools;

using System.Windows.Media.Imaging;
using System.Windows.Controls;
#endregion

//This namespace holds Indicators in this folder and is required. Do not change it. 
namespace NinjaTrader.NinjaScript.Indicators
{
	public class BuyMktSellMktHotkeysQS : Indicator
	{
		private Account myAccount;
		
		// QS Define a Chart object to refer to the chart on which the indicator resides
		private Chart chartWindow;

		// QS Define a Button
		NinjaTrader.Gui.Tools.QuantityUpDown Quantity_Selector = null;				  

		// QS
		private bool Is_ToolBar_Controls_Added;
		
		// QS
		private QuantityUpDown   myQuantityUpDown;
		
		
		protected override void OnStateChange()
		{
			if (State == State.SetDefaults)
			{
				Description									= @"Enter the description for your new custom Indicator here.";
				Name										= "BuyMktSellMktHotkeysQS";
				Calculate									= Calculate.OnEachTick;
				IsOverlay									= true;
				DisplayInDataBox							= true;
				DrawOnPricePanel							= true;
				DrawHorizontalGridLines						= true;
				DrawVerticalGridLines						= true;
				PaintPriceMarkers							= true;
				ScaleJustification							= NinjaTrader.Gui.Chart.ScaleJustification.Right;
				//Disable this property if your indicator requires custom values that cumulate with each new market data event. 
				//See Help Guide for additional information.
				IsSuspendedWhileInactive					= true;
				AccountName 								= "Sim101";
			}
			else if (State == State.Configure)
			{
			}
		    else if (State == State.Historical)
		    {
			    //Call the custom addButtonToToolbar method in State.Historical to ensure it is only done when applied to a chart
			    // -- not when loaded in the Indicators window
			    if (!Is_ToolBar_Controls_Added) Add_Controls_To_Toolbar();
		    }
			else if (State == State.DataLoaded)
			{
				if (ChartControl != null)
				{	
                   	ChartControl.PreviewKeyDown += ChartControl_PreviewKeyDown;
                   	ChartControl.PreviewKeyUp += ChartControl_PreviewKeyUp;					
				}
				
				// Find our account
				lock (Account.All)
					myAccount = Account.All.FirstOrDefault(a => a.Name == AccountName);
				
				// Subscribe to account item and order updates
		        if (myAccount != null)
				{
					myAccount.OrderUpdate 		+= OnOrderUpdate;
					myAccount.ExecutionUpdate 	+= OnExecutionUpdate;
					myAccount.PositionUpdate	+= OnPositionUpdate;
				}
			}
			else if (State == State.Terminated)
			{
				if (ChartControl != null)
				{	
                   	ChartControl.PreviewKeyDown -= ChartControl_PreviewKeyDown;
                   	ChartControl.PreviewKeyUp -= ChartControl_PreviewKeyUp;					
				}
				
				if (myAccount != null)
				{
					myAccount.OrderUpdate 		-= OnOrderUpdate;
					myAccount.ExecutionUpdate 	-= OnExecutionUpdate;
					myAccount.PositionUpdate	-= OnPositionUpdate;
				}
		    	//Call a custom method to dispose of any leftover objects in State.Terminated
		    	DisposeCleanUp();
			}
		}

		protected override void OnBarUpdate()
		{
			//Add your custom indicator logic here.
		}

		
		
		#region Add Controls To Tollbar
		private void Add_Controls_To_Toolbar()
		{
			// Use this.Dispatcher to ensure code is executed on the proper thread
			ChartControl.Dispatcher.InvokeAsync((Action)(() =>
			{
		 
				//Obtain the Chart on which the indicator is configured
				chartWindow = Window.GetWindow(this.ChartControl.Parent) as Chart;
				if (chartWindow == null)
				{
					Print("chartWindow == null");
					return;
				}

				Quantity_Selector = new NinjaTrader.Gui.Tools.QuantityUpDown();
				//Quantity_Selector.ValueChanged +=  On_Quantity_Selector_ValueChanged;
				Quantity_Selector.PreviewKeyDown      +=  On_Quantity_Selector_PreviewKeyDown;

				chartWindow.MainMenu.Add(Quantity_Selector);
 
				Is_ToolBar_Controls_Added = true;
			}));
		}
		#endregion   
		  
		  
	  
			  
		
		private void On_Quantity_Selector_ValueChanged(object sender, RoutedEventArgs e)
		{
			NinjaTrader.Gui.Tools.QuantityUpDown qs = sender as NinjaTrader.Gui.Tools.QuantityUpDown;
			if( sender != null )
			{
				Print( qs.Value );
			}
		}
		
//		private void On_Quantity_Selector_PreviewKeyDown(object sender, KeyEventArgs e)
//		{
//			e.Handled = true;
//			NinjaTrader.Gui.Tools.QuantityUpDown qs = sender as NinjaTrader.Gui.Tools.QuantityUpDown;

//			if( e.Key >= Key.D0 && e.Key <= Key.D9) qs.Content = (string)qs.Content + e.Key;
//		}		

		
		
		private void On_Quantity_Selector_PreviewKeyDown(object sender, KeyEventArgs e)
		{
			myQuantityUpDown.PreviewKeyDown += (o, p) =>
			{
				if (e.Key == Key.Delete || e.Key == Key.Back)
				{
					e.Handled = true;
					myQuantityUpDown.Value = 0;
					
				}
			
				if ((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))
				{
					e.Handled = true;
					
					string number = e.Key.ToString();
					string newnumber = myQuantityUpDown.Value.ToString();
					number = number.Replace("NumPad", "");
					number = number.Replace("D", "");
					int num = int.Parse(newnumber + number);
					myQuantityUpDown.Value = num;
					
				}
			};
		}
		
		
		
		#region DisposeCleanUp 
		private void DisposeCleanUp()
		{
			//ChartWindow Null Check
			if (chartWindow != null)
			{
				//Dispatcher used to Assure Executed on UI Thread
				ChartControl.Dispatcher.InvokeAsync((Action)(() =>
				{
					if( Quantity_Selector != null ) 	chartWindow.MainMenu.Remove(Quantity_Selector);
				}));
			}
		}
		#endregion
		
		
		
		private void OnOrderUpdate(object sender, OrderEventArgs e)
	    {
			NinjaTrader.Code.Output.Process("", PrintTo.OutputTab1);
			NinjaTrader.Code.Output.Process("OrderUpdate", PrintTo.OutputTab1);
	        // Output the order
	        NinjaTrader.Code.Output.Process(e.Order.ToString(), PrintTo.OutputTab1);
	    }
		
		private void OnExecutionUpdate(object sender, ExecutionEventArgs e)
	    {
			NinjaTrader.Code.Output.Process("", PrintTo.OutputTab1);
			NinjaTrader.Code.Output.Process("ExecutionUpdate", PrintTo.OutputTab1);
	        // Output the execution
	        NinjaTrader.Code.Output.Process(string.Format("Instrument: {0} Quantity: {1} Price: {2}",
	              e.Execution.Instrument.FullName, e.Quantity, e.Price), PrintTo.OutputTab1);
	    }
		
		private void OnPositionUpdate(object sender, PositionEventArgs e)
	    {
			NinjaTrader.Code.Output.Process("", PrintTo.OutputTab1);
			NinjaTrader.Code.Output.Process("PositionUpdate", PrintTo.OutputTab1);
	        // Output the order
	        NinjaTrader.Code.Output.Process(e.Position.ToString(), PrintTo.OutputTab1);
	    }
		
		protected void ChartControl_PreviewKeyDown(object sender, KeyEventArgs e)
        {
			TriggerCustomEvent(o =>
			{
				Order buyMktOrder = null;

				if (Keyboard.IsKeyDown(Key.NumPad7))
				{
					buyMktOrder  = myAccount.CreateOrder(Instrument, OrderAction.Buy, OrderType.Market, OrderEntry.Manual, TimeInForce.Day, 1, 0, 0, "", "buyMktOrder"+DateTime.Now.ToString(), DateTime.MaxValue, null);
				}
				
				myAccount.Submit(new[] { buyMktOrder });
			}, null);
			e.Handled = true;
			
			TriggerCustomEvent(p =>
			{
				Order sellMktOrder = null;
				
				if (Keyboard.IsKeyDown(Key.NumPad8))
				{
					sellMktOrder = myAccount.CreateOrder(Instrument, OrderAction.Sell, OrderType.Market, OrderEntry.Manual, TimeInForce.Day, 1, 0, 0, "", "sellMktOrder"+DateTime.Now.ToString(), DateTime.MaxValue, null);
				}
				
				myAccount.Submit(new[] { sellMktOrder });
			}, null);
			e.Handled = true;
        }		
		
      	private void ChartControl_PreviewKeyUp(object sender, KeyEventArgs e)
        {

        }
		
		[TypeConverter(typeof(NinjaTrader.NinjaScript.AccountNameConverter))]
		public string AccountName { get; set; }
	}
}

#region NinjaScript generated code. Neither change nor remove.

namespace NinjaTrader.NinjaScript.Indicators
{
	public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
	{
		private BuyMktSellMktHotkeysQS[] cacheBuyMktSellMktHotkeysQS;
		public BuyMktSellMktHotkeysQS BuyMktSellMktHotkeysQS()
		{
			return BuyMktSellMktHotkeysQS(Input);
		}

		public BuyMktSellMktHotkeysQS BuyMktSellMktHotkeysQS(ISeries<double> input)
		{
			if (cacheBuyMktSellMktHotkeysQS != null)
				for (int idx = 0; idx < cacheBuyMktSellMktHotkeysQS.Length; idx++)
					if (cacheBuyMktSellMktHotkeysQS[idx] != null &&  cacheBuyMktSellMktHotkeysQS[idx].EqualsInput(input))
						return cacheBuyMktSellMktHotkeysQS[idx];
			return CacheIndicator<BuyMktSellMktHotkeysQS>(new BuyMktSellMktHotkeysQS(), input, ref cacheBuyMktSellMktHotkeysQS);
		}
	}
}

namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
	public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
	{
		public Indicators.BuyMktSellMktHotkeysQS BuyMktSellMktHotkeysQS()
		{
			return indicator.BuyMktSellMktHotkeysQS(Input);
		}

		public Indicators.BuyMktSellMktHotkeysQS BuyMktSellMktHotkeysQS(ISeries<double> input )
		{
			return indicator.BuyMktSellMktHotkeysQS(input);
		}
	}
}

namespace NinjaTrader.NinjaScript.Strategies
{
	public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
	{
		public Indicators.BuyMktSellMktHotkeysQS BuyMktSellMktHotkeysQS()
		{
			return indicator.BuyMktSellMktHotkeysQS(Input);
		}

		public Indicators.BuyMktSellMktHotkeysQS BuyMktSellMktHotkeysQS(ISeries<double> input )
		{
			return indicator.BuyMktSellMktHotkeysQS(input);
		}
	}
}

#endregion