Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Can I position a button anywhere on the chart?

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

    Can I position a button anywhere on the chart?

    I've been looking through a bunch of button code examples trying to figure out how to place a button on the chart in the location I want it, not in a corner grid or on chart trader, etc.

    Here are a couple examples I found. One puts it in the toolbar. the other places it in the upper right corner in a grid.

    Is it possible to "render it" at coordinates on the chart that I specify?

    I was unable to figure it out from the examples I found

    thanks


    HTML Code:
            private void AddButtonToToolbar()
            {
                _chartWindow = Window.GetWindow(this.ChartControl.Parent) as Chart;
    
                if (_chartWindow == null)
                {
                    Print("chartWindow == null");
                    return;
                }
    
                _button = CreateButton("BUY", "buybutton", Brushes.White, Brushes.DarkRed);
                _button.Click += OnButtonBuyClick;
                _chartWindow.MainMenu.Add(_button);
                
    
                _isToolBarButtonAdded = true;
            }
    ​


    HTML Code:
                else if (State == State.Historical)
                {
                    myFont12 = new NinjaTrader.Gui.Tools.SimpleFont("Ariel", 12);
                    myFont24 = new NinjaTrader.Gui.Tools.SimpleFont("Ariel", 24);
    
                    
                    if (UserControlCollection.Contains(myGrid))
                        return;
                    
                    Dispatcher.InvokeAsync((() =>
                    {
                        myGrid = new System.Windows.Controls.Grid
                        {
                            Name = "MyCustomGrid", HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top
                        };
                        
                        System.Windows.Controls.ColumnDefinition column1 = new System.Windows.Controls.ColumnDefinition();
                        System.Windows.Controls.ColumnDefinition column2 = new System.Windows.Controls.ColumnDefinition();
                        
                        myGrid.ColumnDefinitions.Add(column1);
                        myGrid.ColumnDefinitions.Add(column2);
                        
                        longButton = new System.Windows.Controls.Button
                        {
                            Name = "LongButton", Content = "LONG", Foreground = Brushes.White, Background = Brushes.Green
                        };
                        
                        shortButton = new System.Windows.Controls.Button
                        {
                            Name = "ShortButton", Content = "SHORT", Foreground = Brushes.Black, Background = Brushes.Red
                        };
    
                        
    
    
                        longButton.Click += OnButtonClick;
                        shortButton.Click += OnButtonClick;
    
                        
                        System.Windows.Controls.Grid.SetColumn(longButton, 0);
                        System.Windows.Controls.Grid.SetColumn(shortButton, 1);
    
                        
                        myGrid.Children.Add(longButton);
                        myGrid.Children.Add(shortButton);
    
                        UserControlCollection.Add(myGrid);
                    }));
                }
                else if (State == State.Terminated)
                {
                    Dispatcher.InvokeAsync((() =>
                    {
                        if (myGrid != null)
                        {
                            if (longButton != null)
                            {
                                myGrid.Children.Remove(longButton);
                                longButton.Click -= OnButtonClick;
                                longButton = null;
                            }
                            if (shortButton != null)
                            {
                                myGrid.Children.Remove(shortButton);
                                shortButton.Click -= OnButtonClick;
                                shortButton = null;
                            }
                        }
                    }));
                }
            }
    ​

    #2
    I'd like to be able to use something like this to hold a button

    SharpDX.Rectangle timeBox = new SharpDX.Rectangle(xPriceCoordinate, yPriceCoordinate, 50, 20);

    Comment


      #3
      Hello cre8able,

      Thank you for your post.

      It is possible to render a rectangle, then when clicking the mouse check if the mouse coordinates are within the rectangle coordinates.

      Please see the attached sample script which demonstrates. This same concept can be used within an indicator.
      Attached Files

      Comment


        #4
        thanks Gaby. That sounds like a good plan

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Mindset, 04-21-2026, 06:46 AM
        0 responses
        57 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by M4ndoo, 04-20-2026, 05:21 PM
        0 responses
        78 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by M4ndoo, 04-19-2026, 05:54 PM
        0 responses
        39 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by cmoran13, 04-16-2026, 01:02 PM
        0 responses
        101 views
        0 likes
        Last Post cmoran13  
        Started by PaulMohn, 04-10-2026, 11:11 AM
        0 responses
        61 views
        0 likes
        Last Post PaulMohn  
        Working...
        X