21 using System.Collections.Generic;
31 private static string PortfolioMarginTooltip =
"{SERIES_NAME}: {VALUE}%";
32 private static string PortfolioMarginIndexName =
"Margin Used (%)";
33 private static readonly
int _portfolioMarginSeriesCount = Configuration.Config.GetInt(
"portfolio-margin-series-count", 5);
40 if (portfolioState ==
null || portfolioState.
PositionGroups ==
null)
45 var topSeries =
new HashSet<string>(_portfolioMarginSeriesCount);
46 foreach (var positionGroup
in portfolioState.PositionGroups
47 .OrderByDescending(x => x.PortfolioValuePercentage)
48 .DistinctBy(x => GetPositionGroupName(x, mapFileProvider, currentTime))
49 .Take(_portfolioMarginSeriesCount))
51 topSeries.Add(positionGroup.Name);
56 foreach (var positionGroup
in portfolioState.PositionGroups)
58 var name = GetPositionGroupName(positionGroup, mapFileProvider, currentTime);
59 if (topSeries.Contains(name))
61 var series = GetOrAddSeries(portfolioChart, name, Color.Empty);
62 series.AddPoint(
new ChartPoint(portfolioState.Time, positionGroup.PortfolioValuePercentage * 100));
66 others ??= GetOrAddSeries(portfolioChart,
"OTHERS", Color.Gray);
67 var value = positionGroup.PortfolioValuePercentage * 100;
68 if (currentOthers !=
null && currentOthers.Time == portfolioState.Time)
71 currentOthers.y += value;
75 currentOthers =
new ChartPoint(portfolioState.Time, value);
76 others.AddPoint(currentOthers);
80 foreach (var series
in portfolioChart.Series.Values)
83 var lastPoint = series.Values.LastOrDefault() as
ChartPoint;
84 if (lastPoint ==
null || lastPoint.Time != portfolioState.Time && lastPoint.Y.HasValue)
86 series.AddPoint(
new ChartPoint(portfolioState.Time,
null));
98 if (
string.IsNullOrEmpty(positionGroup.
Name))
100 positionGroup.
Name =
string.Join(
", ", positionGroup.
Positions.Select(x =>
102 if (mapFileProvider == null)
104 return x.Symbol.Value;
106 return GetMappedSymbol(mapFileProvider, x.Symbol, currentTime).
Value;
110 return positionGroup.
Name;
113 private static Series GetOrAddSeries(Chart portfolioChart,
string seriesName, Color color)
115 if (!portfolioChart.Series.TryGetValue(seriesName, out var series))
117 series = portfolioChart.Series[seriesName] =
new Series(seriesName,
SeriesType.StackedArea, 0,
"%")
120 Tooltip = PortfolioMarginTooltip,
121 IndexName = PortfolioMarginIndexName,
125 return (Series)series;
128 private static Symbol GetMappedSymbol(
IMapFileProvider mapFileProvider, Symbol symbol, DateTime referenceTime)
130 if (symbol.RequiresMapping())
133 if (mapFileResolver.Any())
138 symbol = symbol.UpdateMappedSymbol(mapFile.GetMappedSymbol(referenceTime.Date, symbol.Value));
154 var notNullPointsCount = 0;
155 foreach (var point
in x.Values.OfType<
ChartPoint>())
157 if (point != null && point.Y.HasValue)
159 notNullPointsCount++;
160 if (notNullPointsCount > 1)
166 return notNullPointsCount > 1;
168 .ToDictionary(x => x.Name, x => x);