Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unable to fix workspace change script

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

    Unable to fix workspace change script

    region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using System.Windows.Controls;
    #endregion



    // This namespace holds Add ons in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.AddOns
    {
    public class RemoveChartTraderButtons : NinjaTrader.NinjaScript.AddOnBase
    {
    region Constants and Instance Variables
    private const string CHART_TRADER_AUTOMATION_ID = "ChartWindowChartTraderControl";
    private const string CHART_TRADER_MAIN_GRID_NAME = "grdMain";
    private const string TARGET_WORKSPACE_NAME = "space1";
    #endregion



    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"RemoveChartTraderButtons";
    Name = "RemoveChartTraderButtons";
    }
    else if (State == State.Configure)
    {
    }
    }



    region OnWindowCreated
    protected override void OnWindowCreated(Window window)
    {
    // Check if the active workspace is the target workspace
    if (NinjaTrader.Core.Globals.ActiveWorkspace != TARGET_WORKSPACE_NAME)
    return;



    // Only apply to Chart windows
    if (window as Chart == null) return;



    // Find Chart Trader from the calling chart window by its Automation ID
    ChartTrader chartTrader = window.FindFirst(CHART_TRADER_AUTOMATION_ID) as ChartTrader;
    if (chartTrader == null) return; // something is seriously wrong if chartTrader is null



    // Find the main Chart Trader grid where the default buttons and controls reside
    Grid mainGrid = chartTrader.FindName(CHART_TRADER_MAIN_GRID_NAME) as Grid;
    if (mainGrid != null) // something is seriously wrong if mainGrid is null
    {
    var grid = mainGrid.Children[0] as System.Windows.Controls.Grid;



    foreach (var child in grid.Children)
    {
    System.Windows.Controls.Button button = child as System.Windows.Controls.Button;
    if (button != null)
    {
    // Only collapse buttons that are not the Close button
    if (button.Name != "btnClose")
    {
    button.Visibility = Visibility.Collapsed;
    }
    }
    }



    // Disable right-click Buy/Sell options (to deactivate manual trades)
    chartTrader.ContextMenuOpening += (sender, args) =>
    {
    var contextMenu = chartTrader.ContextMenu;
    if (contextMenu != null)
    {
    // Loop through menu items and disable trade-related options
    foreach (var menuItem in contextMenu.Items)
    {
    if (menuItem is MenuItem item)
    {
    // Check if the item header includes trade-related keywords
    if (item.Header != null &&
    (item.Header.ToString().Contains("Buy") ||
    item.Header.ToString().Contains("Sell") ||
    item.Header.ToString().Contains("Order") ||
    item.Header.ToString().Contains("Limit") ||
    item.Header.ToString().Contains("Market")))
    {
    item.IsEnabled = false; // Disable the item
    item.Visibility = Visibility.Collapsed; // Hide the item
    }
    }
    }
    }
    };
    }
    }
    #endregion
    }
    }​



    i have this NT addon that I made to remove all buttons except close on chart trader to prevent me from manual trades, it should only work on a specfic workspace called space1.It works but only for newly made chart windows after i load this script. When i restart NT , it either shows all buttons on all spaces, or just the close button on all spaces. How to fix this/ where is the flaw in this code

    #2
    region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using System.Windows.Controls;
    #endregion



    // This namespace holds Add ons in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.AddOns
    {
    public class RemoveChartTraderButtons : NinjaTrader.NinjaScript.AddOnBase
    {
    region Constants and Instance Variables
    private const string CHART_TRADER_AUTOMATION_ID = "ChartWindowChartTraderControl";
    private const string CHART_TRADER_MAIN_GRID_NAME = "grdMain";
    private const string TARGET_WORKSPACE_NAME = "space1";
    #endregion



    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"RemoveChartTraderButtons";
    Name = "RemoveChartTraderButtons";
    }
    else if (State == State.Configure)
    {
    }
    }



    region OnWindowCreated
    protected override void OnWindowCreated(Window window)
    {
    // Check if the active workspace is the target workspace
    if (NinjaTrader.Core.Globals.ActiveWorkspace != TARGET_WORKSPACE_NAME)
    return;



    // Only apply to Chart windows
    if (window as Chart == null) return;



    // Find Chart Trader from the calling chart window by its Automation ID
    ChartTrader chartTrader = window.FindFirst(CHART_TRADER_AUTOMATION_ID) as ChartTrader;
    if (chartTrader == null) return; // something is seriously wrong if chartTrader is null



    // Find the main Chart Trader grid where the default buttons and controls reside
    Grid mainGrid = chartTrader.FindName(CHART_TRADER_MAIN_GRID_NAME) as Grid;
    if (mainGrid != null) // something is seriously wrong if mainGrid is null
    {
    var grid = mainGrid.Children[0] as System.Windows.Controls.Grid;



    foreach (var child in grid.Children)
    {
    System.Windows.Controls.Button button = child as System.Windows.Controls.Button;
    if (button != null)
    {
    // Only collapse buttons that are not the Close button
    if (button.Name != "btnClose")
    {
    button.Visibility = Visibility.Collapsed;
    }
    }
    }



    // Disable right-click Buy/Sell options (to deactivate manual trades)
    chartTrader.ContextMenuOpening += (sender, args) =>
    {
    var contextMenu = chartTrader.ContextMenu;
    if (contextMenu != null)
    {
    // Loop through menu items and disable trade-related options
    foreach (var menuItem in contextMenu.Items)
    {
    if (menuItem is MenuItem item)
    {
    // Check if the item header includes trade-related keywords
    if (item.Header != null &&
    (item.Header.ToString().Contains("Buy") ||
    item.Header.ToString().Contains("Sell") ||
    item.Header.ToString().Contains("Order") ||
    item.Header.ToString().Contains("Limit") ||
    item.Header.ToString().Contains("Market")))
    {
    item.IsEnabled = false; // Disable the item
    item.Visibility = Visibility.Collapsed; // Hide the item
    }
    }
    }
    }
    };
    }
    }
    #endregion
    }
    }​



    i have this NT addon that I made to remove all buttons except close on chart trader to prevent me from manual trades, it should only work on a specfic workspace called space1.It works but only for newly made chart windows after i load this script. When i restart NT , it either shows all buttons on all spaces, or just the close button on all spaces. How to fix this/ where is the flaw in this code

    Comment


      #3
      Hello Adam Klaus,

      Please note injecting WPF into open windows is an advanced C# concept and is not fully supported by the Scripting Support team.

      There can be limitations using unsupported code and advanced C# concepts in a NinjaScript, so your mileage may vary. As of now, we do not have any examples that can be used to navigate that path.​


      That said, here are some things to consider.

      Are you adding debugging prints to understand the issue with Code.Output.Process()?

      What is printing when you print the workspace name?

      Are you detecting when the workspace changes?


      Are you adding objects to windows when the workspace name matches, and then removing the objects from the windows when the workspace name changes and does not match?
      (There is similar concept for adding removing objects when a chart tab changes in the SampleWPFModifications help guide reference sample)


      This thread will also remain open for any community members that would like to provide unsupported code for achieving this goal.
      Chelsea B.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by lucyb, Today, 03:38 AM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_Eduardo  
      Started by several, Today, 03:53 AM
      1 response
      23 views
      1 like
      Last Post NinjaTrader_Clayton  
      Started by FaaastEddy, 07-16-2021, 01:58 PM
      7 responses
      1,744 views
      0 likes
      Last Post AgriTrdr  
      Started by sastrades, 03-14-2025, 08:02 AM
      4 responses
      32 views
      0 likes
      Last Post sastrades  
      Started by HappyTrader76, Yesterday, 04:14 PM
      4 responses
      28 views
      0 likes
      Last Post HappyTrader76  
      Working...
      X