Current price

Modificado el Thu, 24 Aug 2023 a las 04:45 PM

Ejemplo de indicador que muestra el precio actual y la hora actual en un gráfico


[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);
            }
        }
    }

 


¿Le ha sido útil este artículo?

¡Qué bien!

Gracias por sus comentarios

¡Sentimos mucho no haber sido de ayuda!

Gracias por sus comentarios

¡Háganos saber cómo podemos mejorar este artículo!

Seleccione al menos una de las razones
Se requiere la verificación del CAPTCHA.

Sus comentarios se han enviado

Agradecemos su esfuerzo e intentaremos corregir el artículo