19 using System.Collections.Generic;
38 if (equityCurve.IsEmpty || orders.Count == 0)
56 var leverage = portfolios.GroupBy(portfolio => portfolio.Time)
57 .Select(group =>
new KeyValuePair<DateTime, double>(group.Key, (
double)group.Last().Leverage))
73 if (equityCurve.IsEmpty || orders.Count == 0)
89 var portfolioHoldings = portfolios.GroupBy(x => x.Time)
90 .Select(kvp => kvp.Last())
93 var totalPortfolioValueOverTime = (double)portfolioHoldings.Sum(x => x.Holdings.Sum(y => y.AbsoluteHoldingsValue));
94 var holdingsBySymbolOverTime =
new Dictionary<Symbol, double>();
96 foreach (var portfolio
in portfolioHoldings)
98 foreach (var holding
in portfolio.Holdings)
100 if (!holdingsBySymbolOverTime.ContainsKey(holding.Symbol))
102 holdingsBySymbolOverTime[holding.Symbol] = (double)holding.AbsoluteHoldingsValue;
106 holdingsBySymbolOverTime[holding.Symbol] = holdingsBySymbolOverTime[holding.Symbol] + (double)holding.AbsoluteHoldingsValue;
111 holdingsBySymbolOverTime.Keys,
112 holdingsBySymbolOverTime.Values.Select(x => x / totalPortfolioValueOverTime).ToList()
128 if (equityCurve.IsEmpty || orders.Count == 0)
130 return Frame.CreateEmpty<DateTime, Tuple<SecurityType, OrderDirection>>();
145 public static Frame<DateTime, Tuple<SecurityType, OrderDirection>>
Exposure(List<PointInTimePortfolio> portfolios,
OrderDirection direction)
149 var holdingsByAssetClass =
new Dictionary<SecurityType, List<KeyValuePair<DateTime, double>>>();
152 foreach (var portfolio
in portfolios)
154 List<KeyValuePair<DateTime, double>> holdings;
155 if (!holdingsByAssetClass.TryGetValue(portfolio.Order.SecurityType, out holdings))
157 holdings =
new List<KeyValuePair<DateTime, double>>();
158 holdingsByAssetClass[portfolio.Order.SecurityType] = holdings;
161 var assets = portfolio.Holdings
162 .Where(pointInTimeHoldings => pointInTimeHoldings.Symbol.SecurityType == portfolio.Order.SecurityType)
165 if (assets.Count > 0)
168 var sum = (double)assets.Where(pointInTimeHoldings => multiplier * pointInTimeHoldings.HoldingsValue > 0)
169 .Select(pointInTimeHoldings => pointInTimeHoldings.AbsoluteHoldingsValue)
172 holdings.Add(
new KeyValuePair<DateTime, double>(portfolio.Time, sum / (
double)portfolio.TotalPortfolioValue));
176 var frame = Frame.CreateEmpty<DateTime, Tuple<SecurityType, OrderDirection>>();
178 foreach (var kvp
in holdingsByAssetClass)
190 new Tuple<SecurityType, OrderDirection>(kvp.Key, direction),
198 return frame.FillMissing(Direction.Forward)
199 .DropSparseColumnsAll()
200 .DropSparseRowsAll();