Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Set QuantityUpDown value from keyboard

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PaulMohn
    replied
    Originally posted by NinjaTrader_Jesse View Post
    Hello,

    Thank you for the post.

    What you have now would mostly work if you were using the PreviewKeyDown event rather than just the KeyDown event.

    Here is a simple example, this would only work for values 0-9, anything over this would require that you form some logic to combine input into a correct number value. This control does not take a string so you will need to use logic to form an int:


    Code:
    myQuantityUpDown.PreviewKeyDown += (o, e) =>
    {
    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();
    number = number.Replace("NumPad", "");
    number = number.Replace("D", "");
    int num = int.Parse(number);
    NinjaTrader.Gui.Tools.QuantityUpDown qs = o as NinjaTrader.Gui.Tools.QuantityUpDown;
    if (qs != null) qs.Value = num;
    }
    };


    I look forward to being of further assistance.

    Hello Jesse, I've completed your hardcoded quantity solution successfully to related thread ( https://ninjatrader.com/support/foru...26#post1189426 ) with shared app
    https://ninjatraderecosystem.com/use...ellmkthotkeys/

    I've got some questions about the advanced method you showed above.
    Can you please explain how you went from

    PHP Code:
    private void On_Quantity_Selector_KeyDown(object sender, KeyEventArgs e)
    {
        e.Handled = true;
        NinjaTrader.Gui.Tools.QuantityUpDown qs = send er as NinjaTrader.Gui.Tools.QuantityUpDown;
    
        if( e.Key >= Key.D0 && e.Key <= Key.D9) qs.Con tent = (string)qs.Content + e.Key;
    } 
    

    to (Taking the expanded version from fx.practice below (post #5)
    PHP Code:
         myQuantityUpDown.PreviewKeyDown += (o, e) =>
         {
              if (e.Key == Key.Delete || e.Key == Key.Back)
              {
                   e.Handled = true;
                   spQtySelector1.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;
                   NinjaTrader.Gui.Tools.QuantityUpDown qs = o as NinjaTrader.Gui.Tools.QuantityUpDown;
                   if (qs != null) qs.Value = num;
    
              }
         }; 
    

    It is the first time I see that structure and I'm not sure what to make of it.

    I understand the functional part of the code:
    Delete or Back key pressed sets the Quantity to zero.
    0-9 (main keys or numpad keys) single key or double keys sets the quantity to the typed number.

    But I don't understand the use of myQuantityUpDown/spQtySelector1. What does it come from? A custom class or something else?


    From comparisons to snippets #1 and #2 below, here's what I've tried to reconstruct from it (with no compile errors).
    From snippet #2 example, I added a variable (private QuantityUpDown myQuantityUpDown; ) to assign the "myQuantityUpDown" "method" to.
    And i embedded it in the private void On_Quantity_Selector_PreviewKeyDown(object sender, KeyEventArgs e){} method (to test if that was what was needed).
    But it seem to be an unusual method. And the Quantity Selector Field is not populated when I type numbers (the Dataseries picker is prompted instead).
    the indicator scipt avaiable at

    PHP Code:
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
         public class BuyMktSellMktHotkeysQS : Indicator
         {
         ...
    
         // QS
         private QuantityUpDown myQuantityUpDown;
         ...
    
         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;
                   }
              };
         } 
    


    Snippet #1 (from https://ninjatraderecosystem.com/use...ellmkthotkeys/ )
    PHP Code:
    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;
    } 
    


    Snipper #2 ( from this doc: https://ninjatrader.com/support/help...tityupdown.htm )
    PHP Code:
    private AtmStrategy.AtmStrategySelector atmStrategySelector;
    
    ...
    
    private DependencyObject LoadXAML()
    
    {
        ...
    
        // When our ATM selector's selection changes
    
        atmStrategySelector.SelectionChanged += (o, args) =>
    
        {
    
            if (atmStrategySelector.SelectedItem == null)
    
                  return;
    
            if (args.AddedItems.Count > 0)
    
             {
    
                  // Change the selected TIF in our TIF selector too
    
                  AtmStrategy selectedAtmStrategy = args.AddedItems[0] as AtmStrategy;
    
                  if (selectedAtmStrategy != null)
    
                       tifSelector.SelectedTif = selectedAtmStrategy.TimeInForce;
    
             }
    
     };
    
    
    
    } 
    

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello rare312,

    Unfortunately I am not aware of a way to control that with the given control. That would very likely need to be implemented into the control its self, to make the control work in that way in general. I am not certain the preview key events would be able to be used in that use case. You could try research controlling the cursor in a standard WPF textbox as a starting point, you could then try applying that to the NinjaTrader control to see if a similar situation can be accomplished.

    Please let me know if I may be of further assistance.

    Leave a comment:


  • rare312
    replied
    Originally posted by NinjaTrader_Jesse View Post
    Hello sidlercom80,

    That would be difficult to do with this control. You could potentially append the strings together to parse a new value but this control is a int value so you could not enter 01 as an example it would just parse as 1. You could in theory enter other values like 10 which would parse as 10. This would likely be fairly buggy and would require adding more conditions to handle the specific entries you needed.

    I don't have a script offhand to test this with but the idea would be if the box started with a value and you typed a 1 it would take the existing string and 1 string and combine them to form a new value.

    Code:
    string number = "1";
    string newVal = "2;
    int num = int.Parse(number + newVal); // comes out to 12
    or

    Code:
    string number= qs.Value.ToString();
    string newVal= e.Key.ToString();
    newVal = number.Replace("NumPad", "");
    newVal = number.Replace("D", "");
    
    int num = int.Parse(number + newVal);
    You would have to handle other keys like delete or backspace and I am unsure if the cursor would be able to work with this use case. If you need a entry which supports more than one number a text entry may be a better choice however that would not have the numeric up/down.

    I look forward to being of further assistance.
    Hello Jesse.
    What will be the best way to type digits anywhere inside a string?
    Not at the end only.
    I believe we need some cursor position index to create substrings.
    But I couldn't find any related parameter for QuantityUpDown control.

    Leave a comment:


  • sidlercom80
    replied
    thank you _Jesse for your answer. I've solved it this way:

    Code:
     
     spQtySelector1.PreviewKeyDown += (o, e) =>         {             if (e.Key == Key.Delete || e.Key == Key.Back)             {                 e.Handled = true;                 spQtySelector1.Value = 0;                   if (PrintDetails)                     Print(DateTime.Now + " PositionSize1 changed to " + spQtySelector1.Value);             }             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 = spQtySelector1.Value.ToString();                 number = number.Replace("NumPad", "");                 number = number.Replace("D", "");                 int num = int.Parse(newnumber + number);                 spQtySelector1.Value = num;                   if (PrintDetails)                     Print(DateTime.Now + " PositionSize1 changed to " + spQtySelector1.Value);             }         };
    Attached Files
    Last edited by sidlercom80; 04-02-2020, 05:09 AM.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello sidlercom80,

    That would be difficult to do with this control. You could potentially append the strings together to parse a new value but this control is a int value so you could not enter 01 as an example it would just parse as 1. You could in theory enter other values like 10 which would parse as 10. This would likely be fairly buggy and would require adding more conditions to handle the specific entries you needed.

    I don't have a script offhand to test this with but the idea would be if the box started with a value and you typed a 1 it would take the existing string and 1 string and combine them to form a new value.

    Code:
    string number = "1";
    string newVal = "2;
    int num = int.Parse(number + newVal);  // comes out to 12
    or

    Code:
    string number= qs.Value.ToString();
    string newVal= e.Key.ToString();
    newVal =  number.Replace("NumPad", "");
    newVal = number.Replace("D", "");
    
    int num = int.Parse(number + newVal);
    You would have to handle other keys like delete or backspace and I am unsure if the cursor would be able to work with this use case. If you need a entry which supports more than one number a text entry may be a better choice however that would not have the numeric up/down.

    I look forward to being of further assistance.

    Leave a comment:


  • sidlercom80
    replied
    Hello _Jesse thank you for the code. i just added it to my script and it works fine. however, i wonder what the code should look like if instead of just one number, you enter several numbers in a row? unfortunately only single-digit numbers are possible.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello,

    Thank you for the post.

    What you have now would mostly work if you were using the PreviewKeyDown event rather than just the KeyDown event.

    Here is a simple example, this would only work for values 0-9, anything over this would require that you form some logic to combine input into a correct number value. This control does not take a string so you will need to use logic to form an int:


    Code:
    myQuantityUpDown.PreviewKeyDown += (o, e) =>
    {
    	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();
    		number =  number.Replace("NumPad", "");
    		number = number.Replace("D", "");
    		int num = int.Parse(number);
    		NinjaTrader.Gui.Tools.QuantityUpDown qs = o as NinjaTrader.Gui.Tools.QuantityUpDown;
    		if (qs != null) qs.Value = num;
    	}
    };


    I look forward to being of further assistance.

    Leave a comment:


  • fx.practic
    started a topic Set QuantityUpDown value from keyboard

    Set QuantityUpDown value from keyboard

    Hello.
    I add QuantityUpDown on ChartWindow.MainMenu.
    Up \ down arrows input work great.
    But when I select the text element and try to edit it using keyboard, if I type any key NT takes over and thinks I'm trying to load a new data series or Instrument.

    I found this thread, but can't imagine nothing working: http://ninjatrader.com/support/forum...QuantityUpDown

    For example, this way not work properly:
    PHP Code:
    private void On_Quantity_Selector_KeyDown(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;
    } 
    


    What is the right way?
    May be, just need to invoke base event handler? But how to do it?
    Last edited by fx.practic; 09-04-2017, 02:28 AM.

Latest Posts

Collapse

Topics Statistics Last Post
Started by DannyP96, 05-18-2026, 02:38 PM
1 response
90 views
0 likes
Last Post NinjaTrader_ChelseaB  
Started by CarlTrading, 05-11-2026, 05:56 AM
0 responses
144 views
0 likes
Last Post CarlTrading  
Started by CarlTrading, 05-10-2026, 08:12 PM
0 responses
83 views
0 likes
Last Post CarlTrading  
Started by Hwop38, 05-04-2026, 07:02 PM
0 responses
257 views
0 likes
Last Post Hwop38
by Hwop38
 
Started by Mindset, 04-21-2026, 06:46 AM
0 responses
335 views
0 likes
Last Post Mindset
by Mindset
 
Working...
X