Hi I was wondering if someone can advice what is missing from my code. Long and Short buttons work, however when in the position "Close All" doesnt close anything. Maybe there is a working example i can look at?
private Chart chartWindow;
private new System.Windows.Controls.Button btnLongsOn;
private new System.Windows.Controls.Button btnShortsOn;
private new System.Windows.Controls.Button btnCloseAll;
private bool IsToolBarButtonAdded;
private bool longsOn = true;
private bool shortsOn = true;
#region Buttons
private void AddButtonToToolbar()
{
chartWindow = Window.GetWindow(this.ChartControl.Parent) as Chart;
if (chartWindow == null)
{
Print("chartWindow == null");
return;
}
Style btnStyle = new Style();
btnStyle.TargetType = typeof(System.Windows.Controls.Button);
btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.FontSizeProperty, 11.0));
btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.FontFamilyProperty, new FontFamily("Arial")));
btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.FontWeightProperty, FontWeights.Bold));
btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.MarginProperty, new Thickness(2, 0, 2, 0)));
btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.PaddingProperty, new Thickness(4, 2, 4, 2)));
btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.ForegroundProperty, Brushes.WhiteSmoke));
btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.BackgroundProperty, Brushes.DimGray));
btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.IsEnabledProperty, true));
btnStyle.Setters.Add(new Setter(System.Windows.Controls.Button.HorizontalAlignmentProperty, HorizontalAlignment.Center));
btnLongsOn = new System.Windows.Controls.Button();
btnShortsOn = new System.Windows.Controls.Button();
btnCloseAll = new System.Windows.Controls.Button();
btnLongsOn.Content = "Longs On";
btnShortsOn.Content = "Shorts On";
btnCloseAll.Content = "Close All";
btnLongsOn.Style = btnStyle;
btnShortsOn.Style = btnStyle;
btnCloseAll.Style = btnStyle;
chartWindow.MainMenu.Add(btnLongsOn);
chartWindow.MainMenu.Add(btnShortsOn);
chartWindow.MainMenu.Add(btnCloseAll);
btnLongsOn.Visibility = Visibility.Visible;
btnShortsOn.Visibility = Visibility.Visible;
btnCloseAll.Visibility = Visibility.Visible;
btnLongsOn.Click += btnLongsOnClick;
btnShortsOn.Click += btnShortsOnClick;
btnCloseAll.Click += btnCloseAllClick;
IsToolBarButtonAdded = true;
}
private void btnLongsOnClick(object sender, RoutedEventArgs e)
{
System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
if (button != null)
{
if (!longsOn)
{
longsOn = true;
btnLongsOn.Content = "Longs On";
}
else
{
longsOn = false;
btnLongsOn.Content = "Longs Off";
}
}
}
private void btnShortsOnClick(object sender, RoutedEventArgs e)
{
System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
if (button != null)
{
if (!shortsOn)
{
shortsOn = true;
btnShortsOn.Content = "Shorts On";
}
else
{
shortsOn = false;
btnShortsOn.Content = "Shorts Off";
}
}
}
private void btnCloseAllClick(object sender, RoutedEventArgs e)
{
System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
if (button != null)
{
Dispatcher.InvokeAsync((() =>
{
if (Position.MarketPosition == MarketPosition.Long)
ExitLong();
else if (Position.MarketPosition == MarketPosition.Short)
ExitShort();
}));
}
}
private void DisposeCleanUp()
{
if (btnLongsOn != null) chartWindow.MainMenu.Remove(btnLongsOn);
btnLongsOn.Click -= btnLongsOnClick;
if (btnShortsOn != null) chartWindow.MainMenu.Remove(btnShortsOn);
btnShortsOn.Click -= btnShortsOnClick;
if (btnCloseAll != null) chartWindow.MainMenu.Remove(btnCloseAll);
btnCloseAll.Click -= btnCloseAllClick;
}
#endregion

Comment