Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Custom & dynamic WPF controls or chart trader elements

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

    Custom & dynamic WPF controls or chart trader elements

    Hello fellow humans,

    I'm trying to add custom elements either to the chart trader window or an additional grid like the chart trader.
    So far I'm able to create them, manage the layout and utilize buttons which trigger functions.
    What I'm not able to do are dynamic TextBlocks. This is probably something simple, but I can't figure out how to update TextBlocks. The variables used for the text content are only read on creation and when I keep a reference to the TextBlock elements, i.e. as a variable, any time I try to change the textcontent or any other attribute the strategy terminates without any error.

    A second question I have: How can I implement numeric input fields? I've seen people use the quantity selector. Is this the only solution for it? If so, how can I use them? Ideally I'd like to have a numeric input field similar to the ones created for properties.

    #2
    Hello Human#102,

    Thank you for your post.

    Please see the SampleWPFModifications script linked below:
    https://ninjatrader.com/support/help...ui)-modifi.htm

    This example uses Draw.TextFixed to draw a text box on the chart which is dynamically changed when the user clicks on the Buy Market or Sell Market button.


    Please see the Help Guide page below for the QuantityUpDownSelector, which has a code snippet demonstrating how to implement it:



    Using AddOn, you can also implement a NumericTextBox instead of a quantity selector.

    NumericTextBox - https://ninjatrader.com/support/help...rictextbox.htm

    Please let me know if you have any other questions.

    Comment


      #3
      Hi Gaby,

      Thank you for the reply. Drawing Textboxes on the chart is pretty simple, I'm having difficulties updating textblocks I'm adding to the chart trader. This is not a huge issue, if its not possible or not recommended to do so I can of course draw them onto the chart instead.

      Comment


        #4
        Hello,

        It is possible to add / update TextBlock objects and other UIElement types to the ChartTrader grid area, however modifying the WPF in any way is not documented or explicitly supported.

        If you were to do this in a chart, you can use the same object added to UserControlCollection.

        https://ninjatrader.com/support/help...collection.htm

        Make sure you are calling InvokeAsync() to access objects on the UI thread from your script's calling event thread.

        https://ninjatrader.com/support/help...-threading.htm

        Comment


          #5
          Hi Gaby,

          Thank you for the response.
          I've actually ran into different issues due to different reasons. Eventually managed to figure it all out. So I'm leaving this here in case anyone else stumbles upon this thread. My main issues were regarding how to interact with and update custom UI elements in custom grids & areas properly. There're basically two ways:

          First, if it's a button or in general an element with a trigger/event function that's added to it like `MyButton += MyClickFuntion` it's possible to change MyButton from within this function straight forward like:
          protected void MyClickFunction(object sender, RoutedEventArgs e)
          {
          System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
          button.Content = "New Text";
          }​

          Second, if you want to change the element from within the code the simplest solution ist to create a variable for that custom element to store the reference to it like:
          private System.Windows.Controls.TextBlock DailyPnLTextBlock = new System.Windows.Controls.TextBlock()
          {
          FontFamily = ChartControl.Properties.LabelFont.Family,
          FontSize = 13,
          HorizontalAlignment = HorizontalAlignment.Center,
          Margin = new Thickness(10, 5, 10, 5),
          Text = "Daily PnL: " + DailyPnLRealized
          };
          And whenever you want to update that element you can do so through the DailyPnLTextBlock variable but only within the `InvokeAsync` function, with null checking and using ForceRefresh(). e.g.:

          protected void UpdateDailyPnLTextBox()
          {
          ChartControl.Dispatcher.InvokeAsync((Action)(() =>
          {
          if (DailyPnLTextBlock != null)
          {
          DailyPnLTextBlock.Text = "Daily PnL: " + DailyPnLRunning;
          ForceRefresh();
          }
          }));
          }

          Whilst not in all cases parts like ForeRefresh or null checks are necessary, I've been always running in some issues in every case​ I've had without all these parts.

          Comment


            #6
            Hi,
            unfortunately I keep running into another issue. Perhaps I'm simply overlooking something, but cannot find it.
            I've managed to implement a numeric input box like so:
            Click image for larger version

Name:	image.png
Views:	650
Size:	4.8 KB
ID:	1285833
            ​With the code being:
            OffsetInput = new NumericTextBox()
            {
            Minimum = 0,
            Maximum = int.MaxValue,
            ValueType = typeof(Int32),
            HorizontalAlignment = HorizontalAlignment.Stretch,
            ToolTip = "Adds an offset (ticks) for the entry orders."
            };​

            But what happens when I begin to type inside it is this:
            Click image for larger version

Name:	image.png
Views:	622
Size:	8.0 KB
ID:	1285834

            A new window opens up. Did I misunderstand what the numeric box is for? Why am I still able to type in text? And how can I implement a simple number input? unfortunately I was also unable to find any examples on how to implement an additional quantity up down selector. (I can read the value from the chart trader inputs, but that is not the goal). For context I'm working inside an indicator for this.

            Comment


              #7
              Hello,

              That is the Instrument Overlay. Please see this example script that prevents the instrument overlay from appearing with your numeric text box.



              Take a look at the AddOnFramework sample script from the link below, this demonstrates implementing both a numeric text box and a quantity up down selector.


              Comment


                #8
                It's quite interesting how I never typed, not even by accident, on a chart. Didn't know this was a default function.
                After figuring that out I eventually was able to find threads which discussed exactly where I was stuck. Leaving the links here for any future visitor.


                https://forum.ninjatrader.com/forum/...-from-keyboard

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                627 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                359 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                105 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                562 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                568 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X