Custom Crosshair
Any indicator can draw on the chart anything you want, to provide this you need to overload base method OnPaintChart and get a link on System.Drawing.Graphics of the chart and coordinates of current drawing area. Let’s create an indicator which can add its own crosshair on the chart. First, we need to disable the built-in crosshair:
Then we need to compile and add an indicator to the chart: CustomCrosshair.cs. You can change a style and line color in the indicator’s settings. Custom crosshair looks as follows:
At first, we take current coordinates of mouse cursor and draw the crosshair lines in the accessible boundaries. Then using function GetTimePrice we request for time and price of the bar above which the cursor is located and display them on the screen.
The function code is written below:
public override void OnPaintChart(object sender, PaintChartEventArgs args) { Graphics gr = args.Graphics; Rectangle rect = args.Rectangle; //getting the cursor position in the client coordinates Point clientPoint = CurrentChart.PointToClient(Cursor.Position); int x = clientPoint.X; int y = clientPoint.Y; //draw anything when we out of the chart if (x < rect.X || x > rect.X + rect.Width || y < rect.Y || y > rect.Y + rect.Height) return; args.Graphics.DrawLine(pen, x, rect.Y, x, rect.Y + rect.Height); args.Graphics.DrawLine(pen, rect.X, y, rect.X + rect.Width, y); //getting price and time by client cooridnates TimePrice timePrice = CurrentChart.GetTimePrice(x, y); string text = Instruments.Current.FormatPrice(timePrice.Price) + "\n" + timePrice.Time.ToLocalTime().ToString("dd.MM.yyyy HH:mm"); args.Graphics.DrawString(text, font, brush, x + 15, y + 10); }