Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Assistance with Programmatic UI Interaction in NinjaTrader

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

    Assistance with Programmatic UI Interaction in NinjaTrader

    Dear NinjaTrader Support and Community,

    I am currently working on a project involving NinjaTrader, where I need to programmatically interact with the UI, specifically to automate certain actions in the Control Center. My goal is to programmatically select and open the "Tools" menu and then the "Options" item within it.

    Despite various attempts using NinjaScript, I've encountered challenges in simulating click events on the menu items. Methods such as raising click events programmatically and setting IsPressed on NTMenuItem have not yielded the desired result. It appears that these menu items do not respond to artificial click events in the same way as user-initiated ones.

    I am reaching out to seek advice or alternative solutions for this task. Are there specific methods or approaches within NinjaTrader's framework that would facilitate this type of UI interaction? Any guidance or suggestions from the community or NinjaTrader's support team would be greatly appreciated.

    Current Code is an AddOnBase class:
    Code:
            protected override void OnWindowCreated(Window window)
            {
                Print($"window name: {window}");
                controlCenter = window as Gui.ControlCenter;
                
                if (controlCenter != null)
                {
                    Print($"control center content: {controlCenter.Content}");
                    FindToolsMenueItem(controlCenter);
                }​
             }
    
            private async void FindToolsMenueItem(DependencyObject parent, int depth = 0)
            {
                
                int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < childrenCount; i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    // Check if the child is a ListBoxItem
                    if (child is NTMenuItem toolsMenuItem)
                    {
    //                    Print($"Control Center Child: {toolsMenuItem}, Name: {toolsMenuItem.Name}");
                        if (toolsMenuItem.Name == "toolsMenuItem")
                        {    
                            Print("Found toolsMenuItem");
    //                        toolsMenuItem.RaiseEvent(new RoutedEventArgs(MenuItem.ClickEvent));
                            toolsMenuItem.IsSubmenuOpen = true;
                            
                            await Task.Delay(100);
    //                        toolsMenuItem.IsPressed = true;
                            FindOptionsMenuItem(toolsMenuItem);
                            break;
                        }
                    }
            
                    // Recurse into the child
                    FindToolsMenueItem(child, depth + 1);
                }
            }
            
            private async void FindOptionsMenuItem(DependencyObject toolsMenuItem)
            {
                int childrenCount = VisualTreeHelper.GetChildrenCount(toolsMenuItem);
                for (int i = 0; i < childrenCount; i++)
                {
                    var child = VisualTreeHelper.GetChild(toolsMenuItem, i);
    //                Print($"Options child: {child}");
    
                    if (child is System.Windows.Controls.Primitives.Popup popUp )
                    {
                        FindOptionsMenuItem(popUp.Child);
                        break;
                    }
                    if (child is NTMenuItem optionsMenuItem && optionsMenuItem.Header == "Options")
                    {
                        Print("Found the option menu item");
    //                    optionsMenuItem.RaiseEvent(new RoutedEventArgs(NTMenuItem.ClickEvent)); // Doesnt Work
                        optionsMenuItem.IsPressed = true; // Doesnt Compile because the set accessor is inaccessible
                        
                    }
            
                    // Recurse if needed
                     FindOptionsMenuItem(child);
                }
            }​


    Also, I'm doing all of this in order to completely disable "Use order entry hot keys", if there's any way programmatically to disable this option without needing to snoop through all the UI elements that would be much better than what i did.

    Thank you for your assistance and for providing a platform that offers such extensive customization capabilities.

    Best regards, Aviram Y
    Last edited by Aviram Y; 01-21-2024, 11:19 AM.

    #2
    Hello Aviram Y,

    Thanks for your post.

    This could likely be accomplished using unsupported advanced C# code which would go beyond the scope of support we would be able to provide you with in the Support department at NinjaTrader.

    There are no supported NinjaScript methods or properties available for programmatically disabling the 'Use order entry hot keys' option in the Tools > Options window.

    The forum thread will be open for other community members to share their insights on possible unsupported advanced C# code you could use to accomplish this.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hey Aviram Y,
      If you go ahead and are successful, could you share your code?
      I'm trying to programmatically click the "Start/Pause" button in the playback window.​
      It would be great​.

      Comment


        #4
        Originally posted by rafaelcoisa View Post
        Hey Aviram Y,
        If you go ahead and are successful, could you share your code?
        I'm trying to programmatically click the "Start/Pause" button in the playback window.​
        It would be great​.
        Hello, the primary challenge encountered was in attempting to programmatically click buttons within NinjaTrader's UI, explored through the use of Snoop. The 'IsPressed' attribute of these buttons is read-only, which seems to limit direct interaction without source code access. While it's possible to enable or disable buttons and modify them in other ways, triggering a click event programmatically proved elusive.

        I did achieve partial success in my endeavors, though the direct simulation of button presses remained out of reach. My goal was to automatically deactivate the "Use order entry hot keys" option found under Options->Trading->Properties. This was feasible when the user navigates to the options menu, as the Trading buttons, being ListBoxItems, could be selected programmatically (e.g., tradingListBox.IsSelected = true). However, this approach necessitates locating the specific ListBoxItem.

        The approach I took involved traversing the visual tree to locate the required UI elements, a task that demands precision and caution, as per my experience. I've attached an image to illustrate the outcome. It's worth noting that while buttons can be programmatically enabled or disabled within the context of a particular window instance, these changes revert to their default state upon closing the window. Nonetheless, this isn't a significant concern since the window's reopening triggers the desired disablement and deactivation.


        There might be another way to achieve your goal, but I failed in doing so, Good luck!
        Click image for larger version

Name:	image.png
Views:	57
Size:	17.6 KB
ID:	1291172

        Comment


          #5
          This is another example of what you can achieve programmatically for charts:
          In this instance, depending on the account name, the code disables all chart buttons and the context menu buttons so the user won't be able to trade.
          Click image for larger version

Name:	image.png
Views:	45
Size:	43.8 KB
ID:	1291174

          Comment


            #6
            Originally posted by rafaelcoisa View Post
            Hey Aviram Y,
            If you go ahead and are successful, could you share your code?
            I'm trying to programmatically click the "Start/Pause" button in the playback window.​
            It would be great​.
            Just for good measure, i snooped the Playback button:
            Note how the IsPressed value is grayed out, this means that even if you find it programatically you wont be able to assign it a true value
            Click image for larger version  Name:	image.png Views:	0 Size:	74.9 KB ID:	1291178
            I've opened a new thread if you want to subscribe to it, maybe we'll get an answer there:
            Hello NinjaTrader Community, I'm currently working on developing a custom add-on for NinjaTrader and have encountered a situation where I need more control over certain UI elements than the platform seems to natively allow. Specifically, I'm looking to programmatically interact with button controls in a way that goes beyond
            Last edited by Aviram Y; 02-13-2024, 11:24 AM.

            Comment


              #7
              Hey Aviram Y, thanks for you time.
              After your answer, now I'm thinking about using reflection or a program like autohotkey.​​
              Last edited by rafaelcoisa; 02-17-2024, 04:44 PM.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by jxs_xrj, 01-12-2020, 09:49 AM
              6 responses
              3,290 views
              1 like
              Last Post jgualdronc  
              Started by Touch-Ups, Today, 10:36 AM
              0 responses
              8 views
              0 likes
              Last Post Touch-Ups  
              Started by geddyisodin, 04-25-2024, 05:20 AM
              8 responses
              61 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by Option Whisperer, Today, 09:55 AM
              0 responses
              8 views
              0 likes
              Last Post Option Whisperer  
              Started by halgo_boulder, 04-20-2024, 08:44 AM
              2 responses
              24 views
              0 likes
              Last Post halgo_boulder  
              Working...
              X