Skip to content

Instantly share code, notes, and snippets.

@samuelcaldas
Last active March 28, 2023 02:22
Show Gist options
  • Save samuelcaldas/1d9061343b38c1a388465ebfd08a9729 to your computer and use it in GitHub Desktop.
Save samuelcaldas/1d9061343b38c1a388465ebfd08a9729 to your computer and use it in GitHub Desktop.
#region Using declarations
using System;
using System.Windows;
using System.Windows.Controls;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Tools;
using NinjaTrader.NinjaScript;
#endregion
namespace NinjaTrader.NinjaScript.AddOns
{
public class MyAddOn : AddOnBase
{
private NTMenuItem myAddOnMenuItem;
private NTMenuItem existingMenuItemInControlCenter;
private MyAddOnWindow window;
protected override void OnWindowCreated(Window window)
{
ControlCenter cc = window as ControlCenter;
if (cc == null)
return;
existingMenuItemInControlCenter = cc.FindFirst("ControlCenterMenuItemTools") as NTMenuItem;
if (existingMenuItemInControlCenter == null)
return;
myAddOnMenuItem = new NTMenuItem { Header = "My AddOn" };
existingMenuItemInControlCenter.Items.Add(myAddOnMenuItem);
myAddOnMenuItem.Click += OnMenuItemClick;
}
protected override void OnWindowDestroyed(Window window)
{
if (myAddOnMenuItem != null && window is ControlCenter)
{
if (existingMenuItemInControlCenter != null && existingMenuItemInControlCenter.Items.Contains(myAddOnMenuItem))
existingMenuItemInControlCenter.Items.Remove(myAddOnMenuItem);
myAddOnMenuItem.Click -= OnMenuItemClick;
myAddOnMenuItem = null;
}
}
private void OnMenuItemClick(object sender, RoutedEventArgs e)
{
Core.Globals.RandomDispatcher.BeginInvoke(new Action(() => new MyAddOnWindow().Show()));
}
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"My AddOn";
Name = "MyAddOn";
}
}
}
public class MyAddOnWindow : NTWindow
{
public MyAddOnWindow()
{
Caption = "My AddOn Window";
Width = 300;
Height = 200;
StackPanel panel = new StackPanel();
Content = panel;
Button button1 = new Button { Content = "Button 1" };
button1.Click += (sender, e) => {
// Instead of Print()
NinjaTrader.Code.Output.Process("bt1", PrintTo.OutputTab1);
};
panel.Children.Add(button1);
Button button2 = new Button { Content = "Button 2" };
button2.Click += (sender, e) => {
// Instead of Print()
NinjaTrader.Code.Output.Process("bt2", PrintTo.OutputTab1);
};
panel.Children.Add(button2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment