Vertical alignment of OBJ_TEXT.
Hi,
Is there a way to align OBJ_TEXT vertically if it's sitting on top of a candle and the text contains newline characters? I want the whole string to sit on top of the candle such that the bottom of this string aligns with the top of the candle.
Regards.
Hi, AminderG! You can accomplish this using overriding OnPaintChart method in NETIndicator and standard C# functions for drawing text. Wrote small example for you:
using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Drawing; using PTLRuntime.NETScript; using PTLRuntime.NETScript.Charts; using System.Drawing.Drawing2D; namespace VerticalText { public class VerticalText : NETIndicator { public override void OnPaintChart(object sender, PaintChartEventArgs args) { base.OnPaintChart(sender, args); // Calc current bar scale int curChartScale = (int)(CurrentChart.MainWindow.WindowRectangle.Width / (CurrentChart.VisibleBarsCount + CurrentChart.RightOffset)); // Skip drawing on small bars if (curChartScale < 10) return; // Go through all displayed bars on chart for (int i = 0; i < CurrentChart.VisibleBarsCount; i++) { // Get data index int dataIndex = CurrentChart.GetBarOffset(i); // Get time and price for bar (using High for example) DateTime dt = CurrentData.Time(dataIndex); double price = CurrentData.GetPrice(PriceType.High, dataIndex); // Get display coordinates for time/price PointF pt = CurrentChart.GetChartPoint(dt, price); // Via functions from internet drawing a rotated text on chart DrawRotatedTextAt(args.Graphics, -90, "Bar info:\r\n" + dt.ToShortTimeString(), pt.X + curChartScale / 2, pt.Y, new Font("Arial", 8), Brushes.Green); } } /// <summary> /// Draw a rotated string at a particular position. (http://csharphelper.com/blog/2014/07/draw-rotated-text-in-c/) /// </summary> private void DrawRotatedTextAt(Graphics gr, float angle, string txt, float x, float y, Font the_font, Brush the_brush) { // Save the graphics state. GraphicsState state = gr.Save(); gr.ResetTransform(); // Rotate. gr.RotateTransform(angle); // Translate to desired position. Be sure to append // the rotation so it occurs after the rotation. gr.TranslateTransform(x, y, MatrixOrder.Append); StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center; // Draw the text at the origin. gr.DrawString(txt, the_font, the_brush, 0, 0, sf); // Restore the graphics state. gr.Restore(state); } } }
Result:
Hi Bogdan Alexey,
Thanks for your taking the time and effort in producing the above code.
My issue is slightly different. I would like the text to be straight and not rotated. At the time of writing, the functions ChartTimePriceToXY() and ChartXYToTimePrice() are not available in AlgoStudio which is what I would like to have.
For my display to work correctly, I'd have to take into account the font size (in pixels) of the text object on the chart. The number of newline characters in my text and the font size would then be used to calculate the number of pixels above the candle where the text should start such that the text does not bleed into the candle from above. The ChartXYToTimePrice() would then be used to place the text at an appropriate location at the point of creation. NOTE: This is not a problem if you have to place the text at the bottom of the candle.
Regards.
Hi Bogdan Alexey,
Thanks for getting back to me.
Basically, I'm trying to add text to the top (high) and bottom (low) of the candles. The bottom part is fine. The top part is where the problem is. When you create the text object , you have to specify the price point where the text will have its origin. If the origin is where the top of the candle is (high), this text will smother the candle and the candle itself will be very difficult to see. As it is currently implemented in AlgoStudio, does not provide functions such that I can convert the price point, font sizes, etc. to pixels. These functions are required to be able to calculate more accurately the placement of the text object at the top such that no part of the text touches the candle.
Also, I have a loads and loads of code in other languages and currently I have no plans to re-code it all into C#.
Hope that helps.
Regards.