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 Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        656 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        371 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        109 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        574 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        579 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X