Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cannot add/remove button in ChartTrader window successfully

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

    Cannot add/remove button in ChartTrader window successfully

    Hello,
    I am new to AddOn Development. I am trying to add a button on the chartTrader panel. I followed the instructions on the documentation page for AddOns to do this but it doesn't work perfectly. When I recompile Ninjascript, there is some undefined behaviour (I see two buttons sometimes, sometimes Ninja throws an Error "The calling thread cannot access this object because a different thread owns it"). What is the right way to create and destroy a button / or any resource for that matter when developing addons. I have attached my code.

    Code:
    public class DataSenderAddon : NinjaTrader.NinjaScript.AddOnBase
        {
    
    
          private Gui.Chart.Chart currentChart;
          private Gui.Chart.ChartTrader currentChartTrader;
          private Grid currentChartTraderGrid, myGrid;
    private RowDefinition rowDefinitionsMyGrid;
    
          private Button sampleButton;
    
    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            Description                                    = @"Enter the description for your new custom Add on here.";
            Name                                        = "DataSenderAddon";
        }
        else if (State == State.Configure)
        {
        }
    }
    
    protected override void OnWindowCreated(Window window)
    {
        try
        {
                  currentChart = window as Chart;
                  if (currentChart == null)
                      return;
    
            // currentChartTrader = currentChart.ChartTrader;
            currentChartTrader = Window.GetWindow(currentChart.ActiveChartControl.Parent).FindFirst("ChartWindowChartTraderControl") as ChartTrader;
    
            if (currentChartTrader == null)
                return;
    
            // Get ChartTrader Grid
            currentChartTraderGrid = currentChartTrader.FindName("grdMain") as Grid;
    
            if (currentChartTraderGrid == null)
                return;
    
                  Output.Process($"{currentChartTraderGrid.RowDefinitions.Count}", PrintTo.OutputTab1);
                  // Add row to Chart Trader Grid
                  rowDefinitionsMyGrid = new RowDefinition();
            currentChartTraderGrid.RowDefinitions.Add(rowDefinitionsMyGrid);
                  Output.Process($"{currentChartTraderGrid.RowDefinitions.Count}", PrintTo.OutputTab1);
    
            
                // Make Grid
                myGrid = new Grid
                {
                    Background = Brushes.Transparent,
                    Name = "ButtonsGrid",
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center,
                };
                myGrid.RowDefinitions.Add(new RowDefinition());
                myGrid.ColumnDefinitions.Add(new ColumnDefinition());
    
                sampleButton = new Button
                {
                    Name = "enableBotButton",
                    Content = "Click Me",
                    ToolTip = "Click Me Button",
                    Foreground = Brushes.White,
                    Background = Brushes.Green,
                    Margin = new System.Windows.Thickness(5, 10, 5, 10),
                    FontSize = 12,
                    Opacity = 0.7,
                    Cursor = System.Windows.Input.Cursors.Hand
                };
                sampleButton.Click += OnClick;
                myGrid.Children.Add(sampleButton);
                Grid.SetRow(sampleButton, 1);
                Grid.SetRow(sampleButton, 1);
    
                currentChartTraderGrid.Children.Add(myGrid);
                Grid.SetRow(myGrid, currentChartTraderGrid.RowDefinitions.Count);
            
              }
    
        catch(Exception e)
        {
            Output.Process($"{e.Message}", PrintTo.OutputTab1);
        }
        
    
    }
    
    protected void OnClick(object sender, RoutedEventArgs rea)
    {
        if (sender == sampleButton)
            Output.Process($"Clicked", PrintTo.OutputTab1);
    }
    
          protected override void OnWindowDestroyed(Window window)
          {
              if(!(window is Chart))
        {
            return;
        }
    
              
              myGrid.Children.Remove(sampleButton);
              sampleButton.Click -= OnClick;
              sampleButton = null;
          
              currentChartTraderGrid.Children.Remove(myGrid);
              myGrid = null;
              
    
        Output.Process($"Here", PrintTo.OutputTab1);
        currentChartTraderGrid.RowDefinitions.Remove(rowDefinitionsMyGrid);
        
        Output.Process($"{currentChartTraderGrid.RowDefinitions.Count}", PrintTo.OutputTab1);
        
    
          }
      }​

    #2
    Hello jkm64,

    Below is a link to the reference sample that shows how to add buttons to chart trader. Model your code after this.
    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


    Use ChartControl.Dispatcher.InvokeAsync() to interact with the UI thread.
    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    48 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    126 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    66 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X