Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Read from indicator code the order quantity in the chart trader
Collapse
X
-
Read from indicator code the order quantity in the chart trader
I am developing an indicator and I need to read the order quantity in in the chart trader textbox. I found code at the link below that shows how to change that value, but when I try that code to read it instead it does not work. Is this possible?
Historical archive for NinjaTrader 8 beta.Tags: None
-
Hello seblife,
Are you assigning a value of 5 like the in the code sample, or are you printing the value with Print()?when I try that code to read it instead it does not work
What are you getting in the print output?
I'm testing and it's working without issue for me.
Chelsea B.NinjaTrader Customer Service
-
I am using this code:
Code:double nbContractsFromUI = 0; ChartControl.Dispatcher.InvokeAsync((Action)(() => { NinjaTrader.Gui.Tools.QuantityUpDown quantitySelector = (Window.GetWindow(ChartControl.Parent).FindFirst(" ChartTraderControlQuantitySelector") as NinjaTrader.Gui.Tools.QuantityUpDown); nbContractsFromUI = quantitySelector.Value; Print("Inside: "+nbContractsFromUI); })); Print("Outside: "+nbContractsFromUI);
The inside part prints the correct value, but when it reaches the outside the value reverts to zero.
Comment
-
What would be the correct syntax to do what I want to achieve? When I remove the InvokeAsync and just access the quantitySelector directly it does not work, actually my entire indicator stops displaying on the Chart.
Just like this does not work, only inside the InvokeAsync I can access the value, but how do I pass it to the code outside?Code:NinjaTrader.Gui.Tools.QuantityUpDown quantitySelector = (Window.GetWindow(ChartControl.Parent).FindFirst(" ChartTraderControlQuantitySelector") as NinjaTrader.Gui.Tools.QuantityUpDown); nbContractsFromUI = quantitySelector.Value;Last edited by seblife; 10-20-2022, 09:01 AM.
Comment
-
Hello seblife,
If you want to retrieve a value from a UI control, you have to do that in the UI thread. Meaning any actions you want to take using that value, should be done in the Action callback.
The syntax of NinjaScript is C#.
The syntax of an Action block appears as:
Code:ChartControl.Dispatcher.InvokeAsync((Action)(() => { // this code will run on the UI thread where UI controls can be accessed. Run any code that uses these values here. }));Chelsea B.NinjaTrader Customer Service
Comment
-
I think there is way to have InvokeAsync return a value, using InvokeAsync<TResult>(Func<TResult>) instead as mentioned in the link below, but I just cannot figure out the syntax for that version of the function to have it return a int value.
https://learn.microsoft.com/en-us/do...em-func((-0)))
Comment
-
Hello seblife,
Its a matter of timing. The code below the invoke action continues to run asynchronously. Meaning that code will have run already by the time the value is retrieved from the UI control.
This cannot be synchronous as this would cause a threading deadlock.
Just put the code that uses the value in the action and this will resolve the issue.
Or you could set the variable, and then wait for the next update of OnBarUpdate to use it. The value will have been retrieved by that time.Chelsea B.NinjaTrader Customer Service
Comment
-
seblife You could always try this:
This has the same end result in the code stream after the Dispatcher as a synchronous Invoke, but still using InvokeAsync. In theory, it should make what you want to do easy.Code:await Dispatcher.InvokeAsync(new Action(() => { // Whatever you want to do })); // Follow-on actions dependent on the Dispatcher completing the above
Perhaps NinjaTrader can comment on whether this stills holds any risk of deadlocks.
Thanks.
Comment
-
Hello jeronymite,
This is thread blocking, is not supported by NinjaTrader to do, and may cause unexpected behavior (as it will block data and order methods from updating).
No risk of deadlocks, but its definitely going to block the entire NinjaScript thread.
However, there is no reason for this. Putting the code that uses the UI values in the action block will resolve the problem.Last edited by NinjaTrader_ChelseaB; 10-21-2022, 06:37 AM.Chelsea B.NinjaTrader Customer Service
- Likes 1
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
647 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
369 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
108 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
572 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
573 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment