Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Add on issue

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

    Add on issue

    HI

    I am attempting to get the code below to place a row at the top of the chart window
    I can't see where I am going wrong. it compiles just doesn;t produce anything.
    It looks relatively simple but any help much appreciated, as I just can't see where i am going wrong.

    Code:
    #region Using declarations
    using System;
    using System.Linq;
    using System.Windows;
    using System.Windows.Media;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.Tools;
    using System.Windows.Controls;
    #endregion
    
    
    #region -- the code --
    namespace NinjaTrader.NinjaScript.AddOns.NinjaAddons
    {
    public class NinjaAddonsToolBar : AddOnBase
    {
    //---------------- Fields / Const -------------------
    #region -- Constantes Values based on NT UI (can be modified in other NT8 release) --
    private const int DRAWINGTOOLS_INDEX = 3;
    private const double ITEM_WIDTH = 20;
    private const string STAYINDRAWMODE_HEADER = "Stay In Draw Mode";
    private const string DRAWINGOBJECTS_HEADER = "Drawing Objects...";
    private const string SNAPMODE_HEADER = "Snap Mode";
    private string[] MISC_HEADERS = new string[1] { "Risk-Reward",};//"Region Highlight X", "Region Highlight Y" };
    private string[] LINE_HEADERS = new string[4] { "Line", "Ray", "Arrow Line", "Horizontal Line"};// "Vertical Line" };
    // private string[] LINE_HEADERS = new string[5] { "Line", "Ray", "Extended Line", "Arrow Line", "Horizontal Line"};// "Vertical Line" };
    
    // private string[] FIBS_HEADERS = new string[4] { "Fibonacci Retracements", "Fibonacci Extensions", "Fibonacci Time Extensions", "Fibonacci Circle" };
    // private string[] CHANNELS_HEADERS = new string[5] { "Andrew's Pitchfork", "Gann Fan", "Regression Channel", "Trend Channel", "Time Cycles" };
    private string[] SHAPES_HEADERS = new string[3] { "Ellipse", "Rectangle", "Text" };
    // private string[] SHAPES_HEADERS = new string[7] { "Ellipse", "Rectangle", "Triangle", "Polygon", "Order Flow Volume Profile", "Arc", "Text" };
    
    private const string CHARTMARKER_HEADER = "Chart Marker";
    private const string ARROWUP_HEADER = "Arrow Up";
    private const string ARROWDOWN_HEADER = "Arrow Down";
    private const string TRIANGLEUP_HEADER = "Triangle Up";
    private const string TRIANGLEDOWN_HEADER = "Triangle Down";
    private const string REMOVEOBJECTS_HEADER = "Remove All Drawing Objects";
    #endregion
    
    private const string TOOLBARSHORTCUT_NAME = "ShortCutToolbar";
    
    //---------------- virtuals -------------------
    #region -- protected override void OnStateChange() --
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults) Name = "Toolbar Shortcut by NinjaAddons.com";
    }
    #endregion
    
    #region -- protected override void OnWindowCreated(Window window) --
    protected override void OnWindowCreated(Window window)
    {
    if (window != null)
    {
    window.Dispatcher.InvokeAsync((Action)(() =>
    {
    var chart = window as Gui.Chart.Chart;
    if (chart == null) return;
    
    var chartGrid = (chart.FindFirst("ChartWindowChartTraderControl") as ChartTrader).Parent as Grid;
    if (chartGrid == null) return;
    
    //if toolbar already exists, do not recreate it
    foreach (var v in chartGrid.Children)
    if (v is VirtualizingStackPanel && (v as VirtualizingStackPanel).Uid == TOOLBARSHORTCUT_NAME) return;
    
    #region -- Create Toolbar Container --
    VirtualizingStackPanel verticalStackPanel = new VirtualizingStackPanel()
    {
    Name = TOOLBARSHORTCUT_NAME,
    Uid = TOOLBARSHORTCUT_NAME,
    Orientation = System.Windows.Controls.Orientation.Horizontal,
    VerticalAlignment = VerticalAlignment.Stretch,
    HorizontalAlignment = HorizontalAlignment.Stretch,
    Margin = new Thickness(0, 0, 7, 0)//Margin is used for separators
    };
    #endregion
    
    #region -- Get all Drawing Items and Copy there Style and Events to new Drawing Items --
    NTMenuItem ntDrawingTools = ((Menu)chart.MainMenu[DRAWINGTOOLS_INDEX]).Items[0] as NTMenuItem;
    foreach (var item in ntDrawingTools.Items)
    {
    if (item is NTMenuItem && ((NTMenuItem)item).Header != null)
    {
    NTMenuItem mnItem = item as NTMenuItem;
    Print(mnItem.Header.ToString());
    
    if (mnItem.Header.ToString() == STAYINDRAWMODE_HEADER || mnItem.Header.ToString() == DRAWINGOBJECTS_HEADER || mnItem.Header.ToString() == SNAPMODE_HEADER) continue;
    
    #region -- Add Misc --
    else if (MISC_HEADERS.Contains(mnItem.Header.ToString()))
    {
    verticalStackPanel.Children.Add(new NTMenuItem()
    {
    ToolTip = mnItem.Header,
    Style = mnItem.Style,
    Icon = mnItem.Icon,
    Command = mnItem.Command,
    Margin = new Thickness(1),
    Width = ITEM_WIDTH,
    HorizontalAlignment =
    HorizontalAlignment.Left
    });
    if (mnItem.Header.ToString() == MISC_HEADERS.Last()) verticalStackPanel.Children.Add(new Separator());
    }
    #endregion
    
    #region -- Add Lines --
    else if (LINE_HEADERS.Contains(mnItem.Header.ToString()))
    {
    verticalStackPanel.Children.Add(new NTMenuItem()
    {
    ToolTip = mnItem.Header,
    Style = mnItem.Style,
    Icon = mnItem.Icon,
    Command = mnItem.Command,
    Margin = new Thickness(1),
    Width = ITEM_WIDTH,
    HorizontalAlignment =
    HorizontalAlignment.Left
    });
    if (mnItem.Header.ToString() == LINE_HEADERS.Last()) verticalStackPanel.Children.Add(new Separator());
    }
    #endregion
    
    #region -- Add Shapes --
    else if (SHAPES_HEADERS.Contains(mnItem.Header.ToString()) )
    {
    verticalStackPanel.Children.Add(new NTMenuItem()
    {
    ToolTip = mnItem.Header,
    Style = mnItem.Style,
    Icon = mnItem.Icon,
    Command = mnItem.Command,
    Margin = new Thickness(1),
    Width = ITEM_WIDTH,
    HorizontalAlignment =
    HorizontalAlignment.Left
    });
    if (mnItem.Header.ToString() == SHAPES_HEADERS.Last()) verticalStackPanel.Children.Add(new Separator());
    }
    #endregion
    
    #region -- Add Markers --
    else if (mnItem.Header.ToString() == CHARTMARKER_HEADER)
    {
    foreach (var subitem in mnItem.Items)
    {
    if (subitem is NTMenuItem)
    {
    NTMenuItem subitemMnItem = subitem as NTMenuItem;
    NTMenuItem newSubItem = new NTMenuItem()
    {
    ToolTip = subitemMnItem.Header,
    Style = subitemMnItem.Style,
    Icon = subitemMnItem.Icon,
    Command = subitemMnItem.Command,
    Margin = new Thickness(1),
    Width = ITEM_WIDTH,
    HorizontalAlignment = HorizontalAlignment.Left
    };
    
    if (subitemMnItem.Header.ToString() == ARROWUP_HEADER || subitemMnItem.Header.ToString() == TRIANGLEUP_HEADER)
    newSubItem.Foreground = Brushes.Green;
    else if (subitemMnItem.Header.ToString() == ARROWDOWN_HEADER || subitemMnItem.Header.ToString() == TRIANGLEDOWN_HEADER)
    newSubItem.Foreground = Brushes.Red;
    
    verticalStackPanel.Children.Add(newSubItem);
    }
    }
    verticalStackPanel.Children.Add(new Separator());
    }
    #endregion
    
    #region -- Add Remove Objects --
    else if (mnItem.Header.ToString() == REMOVEOBJECTS_HEADER)
    {
    verticalStackPanel.Children.Add(new NTMenuItem()
    {
    ToolTip = mnItem.Header,
    Style = mnItem.Style,
    Icon = mnItem.Icon,
    Command = mnItem.Command,
    Margin = new Thickness(1),
    Width = ITEM_WIDTH,
    HorizontalAlignment =
    HorizontalAlignment.Left
    });
    }
    #endregion
    }
    }
    #endregion
    
    #region -- Add Toolbar in first column of chart grid --
    
    chartGrid.RowDefinitions.Insert(0,new RowDefinition());
    
    chartGrid.RowDefinitions[0].Height = new GridLength(ITEM_WIDTH + 12);
    chartGrid.Margin = new Thickness(0);
    
    // loop over existing items and move those to the newly created columns and rows
    for (int i = 0; i < chartGrid.Children.Count; i++)
    
    Grid.SetRow(chartGrid.Children[i], Grid.GetRow(chartGrid.Children[i]) + 1);
    
    
    chartGrid.Children.Add(verticalStackPanel);
    
    //Move Existing Elements down one row, since our new content will take the top row
    foreach (UIElement element in chartGrid.Children)
    {
    element.SetValue(Grid.RowProperty, (int)element.GetValue(Grid.RowProperty) + 1);
    }
    Grid.SetRow(verticalStackPanel,0);
    #endregion
    }));
    }
    }
    
    private void Tmp_Click(object sender, RoutedEventArgs e)
    {
    throw new NotImplementedException();
    }
    #endregion
    
    #region -- protected override void OnWindowDestroyed(Window window) --
    protected override void OnWindowDestroyed(Window window)
    {
    var chart = window as Chart;
    if (chart == null) return;
    
    var chartGrid = (chart.FindFirst("ChartWindowChartTraderControl") as ChartTrader).Parent as Grid;
    if (chartGrid == null) return;
    
    VirtualizingStackPanel verticalStackPanel = null;
    foreach (var v in chartGrid.Children)
    if (v is VirtualizingStackPanel && (v as VirtualizingStackPanel).Uid == "ShortCutToolbar") { verticalStackPanel = v as VirtualizingStackPanel; break; }
    if (verticalStackPanel != null)
    chartGrid.Children.Remove(verticalStackPanel);
    
    for (int i = 0; i < chartGrid.ColumnDefinitions.Count - 1; i++)
    
    chartGrid.RowDefinitions[i].Height = chartGrid.RowDefinitions[i + 1].Height;
    
    for (int i = 0; i < chartGrid.Children.Count; i++)
    // if ((Grid.GetColumn(chartGrid.Children[i]) - 1) >= 0)
    // Grid.SetColumn(chartGrid.Children[i], Grid.GetColumn(chartGrid.Children[i]) - 1);
    if ((Grid.GetRow(chartGrid.Children[i]) - 1) >= 0)
    Grid.SetRow(chartGrid.Children[i], Grid.GetRow(chartGrid.Children[i]) - 1);
    
    
    chartGrid.RowDefinitions.RemoveRange(chartGrid.Row Definitions.Count - 1, 1);
    
    }
    #endregion
    }
    }
    #endregion
    Last edited by Mindset; 03-11-2022, 10:16 AM. Reason: updated code

    #2
    Hello Mindset,

    I would not really be able to say what the problem is without debugging the code. There is an existing sample ChartCustomToolbarExample in the following linked post. That shows adding a new toolbar (row) to the chart with a custom menu, you could add buttons or other WPF controls into that area if you wanted.

    Hello All, Moving forward this will be maintained in the help guide reference samples and no longer maintained on the forum. Creating Chart WPF (UI) Modifications from an Indicator - https://ninjatrader.com/support/help...ui)-modifi.htm (https://ninjatrader.com/support/helpGuides/nt8/creating-chart-wpf-(ui)-modifi.htm) I've


    Comment


      #3
      Jesse
      yes i see that and i got everything to work in the chart trader but it takes up a huge foot print.
      I would like to know how i can get a horizontal row at the top - i can get the row there but i can't successfully push all the other elements down

      the help section shows this

      Code:
       //Move Existing Elements down one row, since our new content will take the top row
        foreach (UIElement element in mainWindowGrid.Chi ldren)
        {
            element.SetValue(Grid.RowProperty, (int)element. GetValue(Grid.RowProperty) + 1);
        }
      but I can't get it to work

      Comment


        #4
        Hello Mindset,

        For the chart trader I wouldn't be able to make a suggestion on moving the items down, you could see the other examples in the link I provided which adds to the bottom of the chart trader. There are many grid row spaces between buttons and other items that you would need to re create in order to move things down. You can certainly explore doing that but I would not be able to make a suggestion of something that exists already.

        Comment


          #5
          Jesse
          Let's go back to basics .The core code for this is written in NT help. It's supposed to help.



          To quote the help
          Below is another example of adding elements into chart windows. In this example, we add a new panel to the top of all chart windows, then take all existing chart content and move it into a row beneath the panel we've just added:

          But it doesn't - it throws up error messages.
          I would expect the help file to be complete and that is what I am asking for. To me the help files show you what you can go so that you can then expand and explore.

          I am not a coding expert but neither am I a newbie - just asking for a little help to get the help code to work lol.
          Just show me how to do what's in your help section because I have tried and I cannot get it to work without error messages.



          Comment


            #6
            Hello Mindset,

            You can find a working example of a toolbar that is kept current in the forum post that I had linked to "ChartCustomToolbarExample". The help guide sample also works, without knowing what errors you are seeing I could not make a recommendation. That is a WPF modification so you would need to add the using statements for any C# namespaces that may be missing, for example:

            using System.Windows;
            using System.Windows.Controls;


            Comment


              #7
              Jesse

              We really aren't on the same wavelength! Maybe it's me getting older.
              A nyway I spent a few dozen hours sorting out things and arrived at the following - which I post here stripped of it's functionality.
              I started off with a strategy but then Ninjatrader doesn't show execution prices for manual trades ( go figure??).
              If anyone is looking to have a horizontal grid with buttons - please note this is NOT an Add on but an indicator- here it is.
              Not elegant and probably not that efficient but it works and I use it now every day and so far have no issues or errors,yay!
              Attached Files

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              587 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              341 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              103 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              555 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              552 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X