Example of an indicator which displays the current price and current time in the chart
[DisplayName("Current price")]
public class CurrentPrice : Indicator
{
RenderFont _font = new RenderFont("Roboto", 14);
private Color _background = Color.Blue;
private Color _textColor = Color.AliceBlue;
private RenderStringFormat _stringFormat=new RenderStringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Far };
public System.Windows.Media.Color Background
{
get => _background.Convert();
set => _background = value.Convert();
}
public System.Windows.Media.Color TextColor
{
get => _textColor.Convert();
set => _textColor = value.Convert();
}
public float FontSize
{
get => _font.Size;
set
{
if (value < 5)
return;
_font = new RenderFont("Roboto", value);
}
}
public bool ShowTime { get; set; } = true;
public string TimeFormat { get; set; } = "HH:mm:ss";
public CurrentPrice():base(true)
{
SubscribeToDrawingEvents(DrawingLayouts.Final);
EnableCustomDrawing = true;
DataSeries[0].IsHidden = true;
}
#region Overrides of BaseIndicator
protected override void OnCalculate(int bar, decimal value)
{
}
#endregion
protected override void OnRender(RenderContext context, DrawingLayouts layout)
{
if (LastVisibleBarNumber != CurrentBar - 1)
return;
var candle = GetCandle(LastVisibleBarNumber);
var priceString = candle.Close.ToString();
var size = context.MeasureString(priceString, _font);
var x = (int)(ChartInfo.GetXByBar(LastVisibleBarNumber) + ChartInfo.PriceChartContainer.BarsWidth);
var y = ChartInfo.GetYByPrice(candle.Close, false);
var rectangle = new Rectangle(x + 10, y - (int)size.Height / 2, (int)size.Width + 10, (int)size.Height);
var points = new List<point>
{
new Point(x, y),
new Point(rectangle.X, rectangle.Y),
new Point(rectangle.X + rectangle.Width, rectangle.Y),
new Point(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height),
new Point(rectangle.X, rectangle.Y + rectangle.Height),
};
context.FillPolygon(_background, points.ToArray());
rectangle.Y++;
context.DrawString(priceString, _font, _textColor, rectangle, _stringFormat);
if (ShowTime)
{
var time = DateTime.Now.ToString(TimeFormat);
size = context.MeasureString(time, _font);
context.DrawString(time, _font, _textColor, rectangle.X + rectangle.Width - size.Width, rectangle.Y - size.Height);
}
}
}
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article