Lean  $LEAN_TAG$
PointInTimePortfolio.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using QuantConnect.Orders;
18 using System;
19 using System.Collections.Generic;
20 using System.Linq;
21 using Newtonsoft.Json;
22 using Newtonsoft.Json.Serialization;
23 
24 namespace QuantConnect.Report
25 {
26  /// <summary>
27  /// Lightweight portfolio at a point in time
28  /// </summary>
29  public class PointInTimePortfolio
30  {
31  /// <summary>
32  /// Time that this point in time portfolio is for
33  /// </summary>
34  public DateTime Time { get; private set; }
35 
36  /// <summary>
37  /// The total value of the portfolio. This is cash + absolute value of holdings
38  /// </summary>
39  public decimal TotalPortfolioValue { get; private set; }
40 
41  /// <summary>
42  /// The cash the portfolio has
43  /// </summary>
44  public decimal Cash { get; private set; }
45 
46  /// <summary>
47  /// The order we just processed
48  /// </summary>
49  [JsonIgnore]
50  public Order Order { get; private set; }
51 
52  /// <summary>
53  /// A list of holdings at the current moment in time
54  /// </summary>
55  public List<PointInTimeHolding> Holdings { get; private set; }
56 
57  /// <summary>
58  /// Portfolio leverage - provided for convenience
59  /// </summary>
60  public decimal Leverage { get; private set; }
61 
62  /// <summary>
63  /// Creates an instance of the PointInTimePortfolio object
64  /// </summary>
65  /// <param name="order">Order applied to the portfolio</param>
66  /// <param name="portfolio">Algorithm portfolio at a point in time</param>
68  {
69  Time = order.Time;
70  Order = order;
72  Cash = portfolio.Cash;
73  Holdings = portfolio.Securities.Values.Select(x => new PointInTimeHolding(x.Symbol, x.Holdings.HoldingsValue, x.Holdings.Quantity)).ToList();
74  Leverage = Holdings.Sum(x => x.AbsoluteHoldingsValue) / TotalPortfolioValue;
75  }
76 
77  /// <summary>
78  /// Clones the provided portfolio
79  /// </summary>
80  /// <param name="portfolio">Portfolio</param>
81  /// <param name="time">Time</param>
82  public PointInTimePortfolio(PointInTimePortfolio portfolio, DateTime time)
83  {
84  Time = time;
85  Order = portfolio.Order;
87  Cash = portfolio.Cash;
88  Holdings = portfolio.Holdings.Select(x => new PointInTimeHolding(x.Symbol, x.HoldingsValue, x.Quantity)).ToList();
89  Leverage = portfolio.Leverage;
90  }
91 
92  /// <summary>
93  /// Filters out any empty holdings from the current <see cref="Holdings"/>
94  /// </summary>
95  /// <returns>Current object, but without empty holdings</returns>
97  {
98  Holdings = Holdings.Where(h => h.Quantity != 0).ToList();
99  return this;
100  }
101 
102  /// <summary>
103  /// Holding of an asset at a point in time
104  /// </summary>
105  public class PointInTimeHolding
106  {
107  /// <summary>
108  /// Symbol of the holding
109  /// </summary>
110  public Symbol Symbol { get; private set; }
111 
112  /// <summary>
113  /// Value of the holdings of the asset. Can be negative if shorting an asset
114  /// </summary>
115  public decimal HoldingsValue { get; private set; }
116 
117  /// <summary>
118  /// Quantity of the asset. Can be negative if shorting an asset
119  /// </summary>
120  public decimal Quantity { get; private set; }
121 
122  /// <summary>
123  /// Absolute value of the holdings.
124  /// </summary>
125  [JsonIgnore]
126  public decimal AbsoluteHoldingsValue => Math.Abs(HoldingsValue);
127 
128  /// <summary>
129  /// Absolute value of the quantity
130  /// </summary>
131  [JsonIgnore]
132  public decimal AbsoluteHoldingsQuantity => Math.Abs(Quantity);
133 
134  /// <summary>
135  /// Creates an instance of PointInTimeHolding, representing a holding at a given point in time
136  /// </summary>
137  /// <param name="symbol">Symbol of the holding</param>
138  /// <param name="holdingsValue">Value of the holding</param>
139  /// <param name="holdingsQuantity">Quantity of the holding</param>
140  public PointInTimeHolding(Symbol symbol, decimal holdingsValue, decimal holdingsQuantity)
141  {
142  Symbol = symbol;
143  HoldingsValue = holdingsValue;
144  Quantity = holdingsQuantity;
145  }
146  }
147  }
148 }