/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using QuantConnect.Orders;
using QuantConnect.Securities;
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace QuantConnect.Report
{
///
/// Lightweight portfolio at a point in time
///
public class PointInTimePortfolio
{
///
/// Time that this point in time portfolio is for
///
public DateTime Time { get; private set; }
///
/// The total value of the portfolio. This is cash + absolute value of holdings
///
public decimal TotalPortfolioValue { get; private set; }
///
/// The cash the portfolio has
///
public decimal Cash { get; private set; }
///
/// The order we just processed
///
[JsonIgnore]
public Order Order { get; private set; }
///
/// A list of holdings at the current moment in time
///
public List Holdings { get; private set; }
///
/// Portfolio leverage - provided for convenience
///
public decimal Leverage { get; private set; }
///
/// Creates an instance of the PointInTimePortfolio object
///
/// Order applied to the portfolio
/// Algorithm portfolio at a point in time
public PointInTimePortfolio(Order order, SecurityPortfolioManager portfolio)
{
Time = order.Time;
Order = order;
TotalPortfolioValue = portfolio.TotalPortfolioValue;
Cash = portfolio.Cash;
Holdings = portfolio.Securities.Values.Select(x => new PointInTimeHolding(x.Symbol, x.Holdings.HoldingsValue, x.Holdings.Quantity)).ToList();
Leverage = Holdings.Sum(x => x.AbsoluteHoldingsValue) / TotalPortfolioValue;
}
///
/// Clones the provided portfolio
///
/// Portfolio
/// Time
public PointInTimePortfolio(PointInTimePortfolio portfolio, DateTime time)
{
Time = time;
Order = portfolio.Order;
TotalPortfolioValue = portfolio.TotalPortfolioValue;
Cash = portfolio.Cash;
Holdings = portfolio.Holdings.Select(x => new PointInTimeHolding(x.Symbol, x.HoldingsValue, x.Quantity)).ToList();
Leverage = portfolio.Leverage;
}
///
/// Filters out any empty holdings from the current
///
/// Current object, but without empty holdings
public PointInTimePortfolio NoEmptyHoldings()
{
Holdings = Holdings.Where(h => h.Quantity != 0).ToList();
return this;
}
///
/// Holding of an asset at a point in time
///
public class PointInTimeHolding
{
///
/// Symbol of the holding
///
public Symbol Symbol { get; private set; }
///
/// Value of the holdings of the asset. Can be negative if shorting an asset
///
public decimal HoldingsValue { get; private set; }
///
/// Quantity of the asset. Can be negative if shorting an asset
///
public decimal Quantity { get; private set; }
///
/// Absolute value of the holdings.
///
[JsonIgnore]
public decimal AbsoluteHoldingsValue => Math.Abs(HoldingsValue);
///
/// Absolute value of the quantity
///
[JsonIgnore]
public decimal AbsoluteHoldingsQuantity => Math.Abs(Quantity);
///
/// Creates an instance of PointInTimeHolding, representing a holding at a given point in time
///
/// Symbol of the holding
/// Value of the holding
/// Quantity of the holding
public PointInTimeHolding(Symbol symbol, decimal holdingsValue, decimal holdingsQuantity)
{
Symbol = symbol;
HoldingsValue = holdingsValue;
Quantity = holdingsQuantity;
}
}
}
}