///second part of the code
public class PnLMonitorWindowvROI : Window
{
private PnLMonitorROI _addOn;
private ComboBox _accountComboBox;
private TextBlock _pnlTextBlock;
private TextBlock _accountValueTextBlock;
private TextBlock _accountLabel;
private TextBox _profitTextBox;
private TextBox _lossTextBox;
private ItemsControl _positionsControl;
private RotateTransform _needleRotation;
public PnLMonitorWindowvROI(PnLMonitorROI addOn)
{
_addOn = addOn;
Title = "PnL Monitor in Real-Time";
Width = 360;
Height = 620;
MinWidth = 340;
MinHeight = 500;
Background = new SolidColorBrush(Color.FromRgb(30, 30, 30));
WindowStartupLocation = WindowStartupLocation.CenterScreen;
Topmost = true;
Grid mainGrid = new Grid();
mainGrid.Margin = new Thickness(12, 10, 12, 10);
mainGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(30) }); // 0
mainGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(20) }); // 1
mainGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); // 2
mainGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); // 3
mainGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(140) }); // 4
mainGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); // 5
mainGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); // 6
mainGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) }); // 7 (bottom)
StackPanel accountPanel = new StackPanel() { Orientation = Orientation.Horizontal };
TextBlock selectText = new TextBlock()
{
Text = "Account: ",
Foreground = Brushes.White,
VerticalAlignment = VerticalAlignment.Center,
Margin = new Thickness(0, 0, 10, 0)
};
_accountComboBox = new ComboBox() { Width = 180, Height = 24, VerticalAlignment = VerticalAlignment.Center };
_accountComboBox.SelectionChanged += OnAccountSelectionChanged;
lock (Account.All)
{
foreach (Account acc in Account.All)
_accountComboBox.Items.Add(acc.Name);
}
accountPanel.Children.Add(selectText);
accountPanel.Children.Add(_accountComboBox);
Grid.SetRow(accountPanel, 0);
mainGrid.Children.Add(accountPanel);
_accountLabel = new TextBlock()
{
Text = "Monitoring: No Account Selected",
Foreground = Brushes.LightGray,
FontSize = 11,
VerticalAlignment = VerticalAlignment.Center
};
Grid.SetRow(_accountLabel, 1);
mainGrid.Children.Add(_accountLabel);
Grid limitsGrid = new Grid();
limitsGrid.Margin = new Thickness(0, 2, 0, 2);
limitsGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
limitsGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
StackPanel profitPanel = new StackPanel() { Margin = new Thickness(0, 0, 4, 0) };
TextBlock profitLbl = new TextBlock() { Text = "Profit Target ($):", Foreground = Brushes.White, FontSize = 11, Margin = new Thickness(0, 0, 0, 2) };
_profitTextBox = new TextBox()
{
Text = _addOn.ProfitTarget.ToString(CultureInfo.InvariantCulture),
Height = 22,
Background = new SolidColorBrush(Color.FromRgb(45, 45, 45)),
Foreground = Brushes.White,
BorderBrush = Brushes.Gray
};
profitPanel.Children.Add(profitLbl);
profitPanel.Children.Add(_profitTextBox);
Grid.SetColumn(profitPanel, 0);
limitsGrid.Children.Add(profitPanel);
StackPanel lossPanel = new StackPanel() { Margin = new Thickness(4, 0, 0, 0) };
TextBlock lossLbl = new TextBlock() { Text = "Loss Limit ($):", Foreground = Brushes.White, FontSize = 11, Margin = new Thickness(0, 0, 0, 2) };
_lossTextBox = new TextBox()
{
Text = _addOn.LossLimit.ToString(CultureInfo.InvariantCulture),
Height = 22,
Background = new SolidColorBrush(Color.FromRgb(45, 45, 45)),
Foreground = Brushes.White,
BorderBrush = Brushes.Gray
};
lossPanel.Children.Add(lossLbl);
lossPanel.Children.Add(_lossTextBox);
Grid.SetColumn(lossPanel, 1);
limitsGrid.Children.Add(lossPanel);
Grid.SetRow(limitsGrid, 2);
mainGrid.Children.Add(limitsGrid);
Button applyLimitsButton = new Button()
{
Content = "Apply Limits",
Width = 120,
Height = 28,
HorizontalAlignment = HorizontalAlignment.Center,
Margin = new Thickness(0, 6, 0, 8),
Background = new SolidColorBrush(Color.FromRgb(50, 100, 180)),
Foreground = Brushes.White,
BorderBrush = new SolidColorBrush(Color.FromRgb(210, 210, 210)),
FontSize = 11,
FontWeight = FontWeights.SemiBold,
Cursor = System.Windows.Input.Cursors.Hand
};
applyLimitsButton.Click += OnApplyLimitsClicked;
Grid.SetRow(applyLimitsButton, 3);
mainGrid.Children.Add(applyLimitsButton);
Grid gaugeGrid = new Grid();
gaugeGrid.Width = 320;
gaugeGrid.Height = 140;
gaugeGrid.HorizontalAlignment = HorizontalAlignment.Center;
gaugeGrid.VerticalAlignment = VerticalAlignment.Top;
System.Windows.Shapes.Path backgroundArc = CreateSemicircularArc(Color.FromRgb(55, 58, 63), 14);
gaugeGrid.Children.Add(backgroundArc);
LinearGradientBrush arcGradient = new LinearGradientBrush();
arcGradient.StartPoint = new Point(0, 0);
arcGradient.EndPoint = new Point(1, 0);
arcGradient.GradientStops.Add(new GradientStop(Color.FromRgb(239, 83, 80), 0.0));
arcGradient.GradientStops.Add(new GradientStop(Color.FromRgb(100, 110, 120), 0.5));
arcGradient.GradientStops.Add(new GradientStop(Color.FromRgb(42, 184, 115), 1.0));
System.Windows.Shapes.Path coloredArc = CreateSemicircularArc(Colors.Transparent, 2);
coloredArc.Stroke = arcGradient;
coloredArc.StrokeThickness = 3;
gaugeGrid.Children.Add(coloredArc);
Grid needleContainer = new Grid();
needleContainer.Width = 220;
needleContainer.Height = 220;
needleContainer.VerticalAlignment = VerticalAlignment.Top;
needleContainer.Margin = new Thickness(0, 20, 0, 0);
System.Windows.Shapes.Polygon premiumNeedle = new System.Windows.Shapes.Polygon();
Point pointLeft = new Point(107, 110);
Point pointTip = new Point(110, 15);
Point pointRight = new Point(113, 110);
premiumNeedle.Points.Add(pointLeft);
premiumNeedle.Points.Add(pointTip);
premiumNeedle.Points.Add(pointRight);
premiumNeedle.Fill = new SolidColorBrush(Color.FromRgb(255, 110, 0));
premiumNeedle.Stroke = new SolidColorBrush(Color.FromRgb(242, 244, 247));
premiumNeedle.StrokeThickness = 0.5;
System.Windows.Shapes.Ellipse needleCap = new System.Windows.Shapes.Ellipse();
needleCap.Width = 18;
needleCap.Height = 18;
needleCap.Fill = new SolidColorBrush(Color.FromRgb(30, 30, 30));
needleCap.Stroke = new SolidColorBrush(Color.FromRgb(242, 244, 247));
needleCap.StrokeThickness = 3;
needleCap.HorizontalAlignment = HorizontalAlignment.Center;
needleCap.VerticalAlignment = VerticalAlignment.Center;
_needleRotation = new RotateTransform(0, 110, 110);
premiumNeedle.RenderTransform = _needleRotation;
needleContainer.Children.Add(premiumNeedle);
needleContainer.Children.Add(needleCap);
gaugeGrid.Children.Add(needleContainer);
Grid.SetRow(gaugeGrid, 4);
mainGrid.Children.Add(gaugeGrid);
_pnlTextBlock = new TextBlock()
{
Text = "$0.00",
Foreground = Brushes.White,
FontSize = 34,
FontWeight = FontWeights.Bold,
FontFamily = new FontFamily("Segoe UI"),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Margin = new Thickness(0, 15, 0, 4)
};
Grid.SetRow(_pnlTextBlock, 5);
mainGrid.Children.Add(_pnlTextBlock);
_accountValueTextBlock = new TextBlock()
{
Text = "Account Value: $0.00",
Foreground = Brushes.White,
FontSize = 18,
FontWeight = FontWeights.SemiBold,
FontFamily = new FontFamily("Segoe UI"),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Margin = new Thickness(0, 0, 0, 12)
};
Grid.SetRow(_accountValueTextBlock, 6);
mainGrid.Children.Add(_accountValueTextBlock);
Grid bottomSplitGrid = new Grid();
bottomSplitGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
bottomSplitGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
bottomSplitGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
bottomSplitGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
Grid.SetRow(bottomSplitGrid, 7);
mainGrid.Children.Add(bottomSplitGrid);
TextBlock authorLabel = new TextBlock()
{
Text = "@ProyectoROI25",
Foreground = Brushes.DarkGray,
FontSize = 11,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(0, 0, 0, 18)
};
Grid.SetRow(authorLabel, 0);
Grid.SetColumn(authorLabel, 0);
Grid.SetColumnSpan(authorLabel, 2);
bottomSplitGrid.Children.Add(authorLabel);
StackPanel logoButtonPanel = new StackPanel
{
Orientation = Orientation.Vertical,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
try
{
string imagePath = System.IO.Path.Combine(NinjaTrader.Core.Globals.UserDataDir, "templates", "ROIPro.png");
if (System.IO.File.Exists(imagePath))
{
System.Windows.Media.Imaging.BitmapImage bitmap = new System.Windows.Media.Imaging.BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(imagePath);
bitmap.EndInit();
System.Windows.Controls.Image logoImage = new System.Windows.Controls.Image
{
Source = bitmap,
Height = 65,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Margin = new Thickness(0, 0, 0, 8),
Stretch = System.Windows.Media.Stretch.Uniform
};
logoButtonPanel.Children.Add(logoImage);
}
}
catch (Exception)
{
}
Button closeAllButton = new Button
{
Content = "Close All Trades",
Width = 120,
Height = 28,
HorizontalAlignment = HorizontalAlignment.Center,
Background = new SolidColorBrush(Color.FromRgb(180, 45, 45)),
Foreground = Brushes.White,
BorderBrush = new SolidColorBrush(Color.FromRgb(210, 210, 210)),
FontSize = 11,
FontWeight = FontWeights.SemiBold,
Cursor = System.Windows.Input.Cursors.Hand
};
closeAllButton.Click += delegate(object s, RoutedEventArgs e)
{
if (System.Windows.MessageBox.Show(
"Close all open trades for the selected account?",
"Confirm Close All",
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
_addOn.CloseAllOpenTrades();
}
};
logoButtonPanel.Children.Add(closeAllButton);
Grid.SetRow(logoButtonPanel, 1);
Grid.SetColumn(logoButtonPanel, 0);
bottomSplitGrid.Children.Add(logoButtonPanel);
// --- "Open Positions:" + lista: fila 1, columna 1, alineado arriba ---
StackPanel posContainerPanel = new StackPanel()
{
Margin = new Thickness(8, 0, 0, 0),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top
};
TextBlock posHeader = new TextBlock()
{
Text = "Open Positions:",
Foreground = Brushes.Gray,
FontSize = 10,
FontWeight = FontWeights.Bold,
Margin = new Thickness(0, 0, 0, 4)
};
posContainerPanel.Children.Add(posHeader);
_positionsControl = new ItemsControl();
_positionsControl.ItemTemplate = new DataTemplate();
ScrollViewer positionsScrollViewer = new ScrollViewer
{
VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled,
VerticalAlignment = VerticalAlignment.Top
};
positionsScrollViewer.Content = _positionsControl;
posContainerPanel.Children.Add(positionsScrollViewer);
Grid.SetRow(posContainerPanel, 1);
Grid.SetColumn(posContainerPanel, 1);
bottomSplitGrid.Children.Add(posContainerPanel);
// ================== /BOTTOM AREA ==================
Content = mainGrid;
if (_addOn.AccountName != "No Account Selected")
{
_accountComboBox.SelectedItem = _addOn.AccountName;
UpdateAccountLabel(_addOn.AccountName);
}
this.Closed += delegate(object sender, EventArgs e)
{
_addOn.ClearWindowReference();
};
}
private void OnApplyLimitsClicked(object sender, RoutedEventArgs e)
{
double newProfitTarget;
double newLossLimit;
bool profitValid = double.TryParse(_profitTextBox.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out newProfitTarget);
bool lossValid = double.TryParse(_lossTextBox.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out newLossLimit);
if (!profitValid || !lossValid)
{
System.Windows.MessageBox.Show(
"Please enter valid numeric values for both Profit Target and Loss Limit.",
"Invalid Limits",
MessageBoxButton.OK);
return;
}
bool valuesChanged = (_addOn.ProfitTarget != newProfitTarget) || (_addOn.LossLimit != newLossLimit);
_addOn.ProfitTarget = newProfitTarget;
_addOn.LossLimit = newLossLimit;
_addOn.ResetThresholdAlertState();
string msg = valuesChanged
? "Profit Target and Loss Limit updated.\nThreshold alert state has been reset."
: "Limits reapplied.\nThreshold alert state has been reset.";
System.Windows.MessageBox.Show(
msg,
"Limits Applied",
MessageBoxButton.OK);
}
private void OnAccountSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_accountComboBox.SelectedItem == null)
return;
string selectedAccountName = _accountComboBox.SelectedItem.ToString();
lock (Account.All)
{
Account selectedAccount = Account.All.FirstOrDefault(a => a.Name == selectedAccountName);
if (selectedAccount != null)
_addOn.SetAccount(selectedAccount);
}
}
public void UpdateAccountLabel(string accountName)
{
if (!Dispatcher.CheckAccess())
{
Dispatcher.InvokeAsync(new Action(delegate()
{
UpdateAccountLabel(accountName);
}));
return;
}
_accountLabel.Text = "Monitoring: " + accountName;
}
public void UpdatePnL(double pnlValue, double accountValue, double maxProfit, double minLoss)
{
if (!Dispatcher.CheckAccess())
{
Dispatcher.InvokeAsync(new Action(delegate()
{
UpdatePnL(pnlValue, accountValue, maxProfit, minLoss);
}));
return;
}
if (pnlValue >= 0)
{
_pnlTextBlock.Text = pnlValue.ToString("C2", new CultureInfo("en-US"));
_pnlTextBlock.Foreground = new SolidColorBrush(Color.FromRgb(42, 184, 115));
}
else
{
_pnlTextBlock.Text = pnlValue.ToString("$#,0.00;-$#,0.00", new CultureInfo("en-US"));
_pnlTextBlock.Foreground = new SolidColorBrush(Color.FromRgb(239, 83, 80));
}
if (pnlValue >= 0)
{
_accountValueTextBlock.Text = "Account Value: " + accountValue.ToString("C2", new CultureInfo("en-US"));
_accountValueTextBlock.Foreground = new SolidColorBrush(Color.FromRgb(42, 184, 115));
}
else
{
_accountValueTextBlock.Text = "Account Value: " + accountValue.ToString("$#,0.00;-$#,0.00", new CultureInfo("en-US"));
_accountValueTextBlock.Foreground = new SolidColorBrush(Color.FromRgb(239, 83, 80));
}
if (maxProfit <= minLoss)
maxProfit = minLoss + 1;
double targetScale = pnlValue >= 0 ? maxProfit : Math.Abs(minLoss);
if (targetScale <= 0)
targetScale = 1;
double percentage = pnlValue / targetScale;
double angle = percentage * 90.0;
angle = Math.Max(-90.0, Math.Min(90.0, angle));
_needleRotation.Angle = angle;
if (_addOn != null)
{
string currentAccName = _accountLabel.Text.Replace("Monitoring: ", "");
Account targetAccount = null;
lock (Account.All)
{
targetAccount = Account.All.FirstOrDefault(a => a.Name == currentAccName);
}
_positionsControl.Items.Clear();
if (targetAccount != null && targetAccount.Positions != null)
{
var openPositions = targetAccount.Positions
.Where(p => p.MarketPosition != MarketPosition.Flat)
.Select(p =>
{
double posPnL = p.GetUnrealizedProfitLoss(PerformanceUnit.Currency);
int signedQuantity = p.MarketPosition == MarketPosition.Short
? -Math.Abs(p.Quantity)
: Math.Abs(p.Quantity);
string directionSign = posPnL >= 0 ? "+" : "";
string displayStr = p.Instrument.MasterInstrument.Name
+ " (" + signedQuantity + ") "
+ directionSign
+ posPnL.ToString("C0", new CultureInfo("en-US"));
Brush textBrush = posPnL >= 0 ? Brushes.LimeGreen : Brushes.Red;
return new
{
DisplayText = displayStr,
ColorBrush = textBrush,
RawPnL = posPnL
};
})
.OrderByDescending(p => p.RawPnL)
.ToList();
if (openPositions.Count > 0)
{
foreach (var pos in openPositions)
{
_positionsControl.Items.Add(new TextBlock
{
Text = pos.DisplayText,
Foreground = pos.ColorBrush,
FontSize = 10,
FontWeight = FontWeights.SemiBold,
Margin = new Thickness(0, 1, 0, 1)
});
}
}
else
{
_positionsControl.Items.Add(new TextBlock
{
Text = "No Active Trades",
Foreground = Brushes.DarkGray,
FontSize = 10,
FontStyle = FontStyles.Italic
});
}
}
else
{
_positionsControl.Items.Add(new TextBlock
{
Text = "No Active Trades",
Foreground = Brushes.DarkGray,
FontSize = 10,
FontStyle = FontStyles.Italic
});
}
}
}
private System.Windows.Shapes.Path CreateSemicircularArc(Color strokeColor, double thickness)
{
System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
path.Stroke = new SolidColorBrush(strokeColor);
path.StrokeThickness = thickness;
path.StrokeEndLineCap = PenLineCap.Round;
path.StrokeStartLineCap = PenLineCap.Round;
path.VerticalAlignment = VerticalAlignment.Top;
path.HorizontalAlignment = HorizontalAlignment.Center;
path.Margin = new Thickness(0, 20, 0, 0);
PathGeometry pathGeometry = new PathGeometry();
PathFigure pathFigure = new PathFigure();
pathFigure.StartPoint = new Point(10, 110);
ArcSegment arcSegment = new ArcSegment();
arcSegment.Point = new Point(210, 110);
arcSegment.Size = new Size(100, 100);
arcSegment.SweepDirection = SweepDirection.Clockwise;
pathFigure.Segments.Add(arcSegment);
pathGeometry.Figures.Add(pathFigure);
path.Data = pathGeometry;
return path;
}
}
}