namespace StockSharp.Charting; using Ecng.Configuration; using static StockSharp.Charting.IChartDrawData; /// /// extensions. /// public static class IChartExtensions { /// /// /// public static IIndicatorProvider IndicatorProvider => ConfigManager.GetService(); /// /// /// public static IIndicatorProvider TryIndicatorProvider => ConfigManager.TryGetService(); /// /// /// public static IChartIndicatorPainterProvider IndicatorPainterProvider => ConfigManager.GetService(); /// /// /// public static IChartIndicatorPainterProvider TryIndicatorPainterProvider => ConfigManager.TryGetService(); /// /// Fill using . /// /// Chart. public static void FillIndicators(this IChart chart) { if (chart == null) throw new ArgumentNullException(nameof(chart)); chart.IndicatorTypes.Clear(); chart.IndicatorTypes.AddRange(IndicatorProvider.All.ExcludeObsolete()); } /// /// The primary title for the X-axis. /// public const string DefaultXAxisId = "X"; /// /// The primary title for the Y-axis. /// public const string DefaultYAxisId = "Y"; /// /// Whether this axis can be removed from chart area. /// public static bool IsDefault(this IChartAxis axis) => axis.Id == DefaultXAxisId || axis.Id == DefaultYAxisId; /// /// Put the chart data. /// /// instance. /// The chart element. /// The chart value. /// instance. public static IChartDrawDataItem Add(this IChartDrawDataItem item, IChartElement element, object value) { switch (element) { case null: throw new ArgumentNullException(nameof(element)); case IChartCandleElement candleElem: return item.Add(candleElem, (ICandleMessage)value); case IChartIndicatorElement indElem: return item.Add(indElem, (IIndicatorValue)value); case IChartOrderElement orderElem: { var order = (Order)value; return item.Add(orderElem, order, order.State != OrderStates.Failed ? null : LocalizedStrings.Failed); } case IChartTradeElement tradeElem: return item.Add(tradeElem, (MyTrade)value); //case ChartActiveOrdersElement activeEleme: // return Add(activeEleme, (ChartActiveOrderInfo)value); case IChartLineElement lineElem: return value switch { null => throw new ArgumentNullException(nameof(value)), double d => item.Add(lineElem, d), decimal d => item.Add(lineElem, (double)d), Tuple t => item.Add(lineElem, t.Item1, t.Item2), _ => throw new ArgumentException(LocalizedStrings.UnsupportedType.Put(value.GetType().Name)), }; case IChartBandElement belem: return value switch { null => throw new ArgumentNullException(nameof(value)), double d => item.Add(belem, d, 0), decimal d => item.Add(belem, d), Tuple t => item.Add(belem, t.Item1, t.Item2), _ => throw new ArgumentException(LocalizedStrings.UnsupportedType.Put(value.GetType().Name)), }; default: throw new ArgumentException(LocalizedStrings.UnsupportedType.Put(element)); } } /// /// Put the candle data. /// /// instance. /// The chart element representing a candle. /// The candle data. /// instance. public static IChartDrawDataItem Add(this IChartDrawDataItem item, IChartCandleElement element, ICandleMessage candle) { if (candle == null) { //throw new ArgumentNullException(nameof(candle)); return item; } return item.Add(element, candle, candle.OpenPrice, candle.HighPrice, candle.LowPrice, candle.ClosePrice, candle.PriceLevels?.ToArray(), candle.State); } /// /// Put the candle data. /// /// instance. /// The chart element representing a candle. /// Candle. /// Opening price. /// Highest price. /// Lowest price. /// Closing price. /// Price levels. /// Candle state. /// instance. public static IChartDrawDataItem Add(this IChartDrawDataItem item, IChartCandleElement element, ICandleMessage candle, decimal openPrice, decimal highPrice, decimal lowPrice, decimal closePrice, CandlePriceLevel[] priceLevels, CandleStates state) { if (candle == null) { //throw new ArgumentNullException(nameof(candle)); return item; } return item.Add(element, candle.DataType, candle.SecurityId, openPrice, highPrice, lowPrice, closePrice, priceLevels, state); } /// /// Put the order data. /// /// instance. /// The chart element representing orders. /// The order value. /// Error registering/cancelling order. /// instance. public static IChartDrawDataItem Add(this IChartDrawDataItem item, IChartOrderElement element, Order order, string errorMessage = null) { if (order == null) return item; // throw new ArgumentNullException(nameof(order)); if (errorMessage.IsEmpty() && order.State == OrderStates.Failed) errorMessage = LocalizedStrings.Failed; return item.Add(element, order.TransactionId, null, order.Side, order.Price, order.Volume, errorMessage); } /// /// Put the order data. /// /// instance. /// The chart element representing trades. /// The trade value. /// instance. public static IChartDrawDataItem Add(this IChartDrawDataItem item, IChartTradeElement element, MyTrade trade) { if (trade == null) return item; // throw new ArgumentNullException(nameof(trade)); var tick = trade.Trade; return item.Add(element, tick.Id ?? default, tick.StringId, trade.Order.Side, tick.Price, tick.Volume); } /// /// To use automatic range for the X-axis. /// /// /// To use automatic range for the X-axis. public static bool GetAutoRange(this IChartArea area) => area.XAxises.All(a => a.AutoRange); /// /// To use automatic range for the X-axis. /// /// /// To use automatic range for the X-axis. public static void SetAutoRange(this IChartArea area, bool value) => area.XAxises.ForEach(a => a.AutoRange = value); /// /// The chart on which the element is drawn. /// /// /// The chart on which the element is drawn. public static IChart TryGetChart(this IChartElement elem) => elem.CheckOnNull(nameof(elem)).ChartArea?.Chart; /// /// X axis this element currently attached to. /// /// /// X axis this element currently attached to. public static IChartAxis TryGetXAxis(this IChartElement elem) => elem.CheckOnNull(nameof(elem)).ChartArea?.XAxises.FirstOrDefault(xa => xa.Id == elem.XAxisId); /// /// Y axis this element currently attached to. /// /// /// Y axis this element currently attached to. public static IChartAxis TryGetYAxis(this IChartElement elem) => elem.CheckOnNull(nameof(elem)).ChartArea?.YAxises.FirstOrDefault(xa => xa.Id == elem.YAxisId); /// /// Get all elements. /// /// /// Chart elements. public static IEnumerable GetElements(this IChart chart) => chart.CheckOnNull(nameof(chart)).Areas.SelectMany(a => a.Elements); /// /// Get all elements. /// /// type. /// /// Chart elements. public static IEnumerable GetElements(this IChart chart) where T : IChartElement => chart.GetElements().OfType(); /// /// To remove all areas from the chart. /// /// public static void ClearAreas(this IChart chart) { if (chart is null) throw new ArgumentNullException(nameof(chart)); chart.Reset(chart.GetElements()); foreach (var area in chart.Areas.ToArray()) chart.RemoveArea(area); } }