Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Read from indicator code the order quantity in the chart trader

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

    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?


    #2
    Hello seblife,

    when I try that code to read it instead it does not work
    Are you assigning a value of 5 like the in the code sample, or are you printing the value with Print()?

    What are you getting in the print output?

    I'm testing and it's working without issue for me.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      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


        #4
        Hello seblife,

        That is expected. An InvokeAsync means the code is being processed in another thread. The code outside of the invoke is not on that thread and will be run asynchrony.

        Below is a link to the help guide.


        It is working as expected.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          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.

          Code:
          NinjaTrader.Gui.Tools.QuantityUpDown quantitySelector = (Window.GetWindow(ChartControl.Parent).FindFirst(" ChartTraderControlQuantitySelector") as NinjaTrader.Gui.Tools.QuantityUpDown);
          nbContractsFromUI = quantitySelector.Value;
          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?​
          Last edited by seblife; 10-20-2022, 09:01 AM.

          Comment


            #6
            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


              #7
              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


                #8
                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


                  #9
                  seblife You could always try this:

                  Code:
                  await Dispatcher.InvokeAsync(new Action(() =>
                  {
                          // Whatever you want to do
                  }));
                  
                  // Follow-on actions dependent on the Dispatcher completing the above
                  ​
                  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.

                  Perhaps NinjaTrader can comment on whether this stills holds any risk of deadlocks.

                  Thanks.
                  Multi-Dimensional Managed Trading
                  jeronymite
                  NinjaTrader Ecosystem Vendor - Mizpah Software

                  Comment


                    #10
                    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.
                    Attached Files
                    Last edited by NinjaTrader_ChelseaB; 10-21-2022, 06:37 AM.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks, NinjaTrader_ChelseaB . That's very helpful.

                      Thanks.
                      Multi-Dimensional Managed Trading
                      jeronymite
                      NinjaTrader Ecosystem Vendor - Mizpah Software

                      Comment


                        #12
                        This is exactly what I was looking for. Thank you

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        648 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        369 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        108 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        572 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        573 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X