Files
quantconnect--lean/Common/CapacityEstimate.cs
T
Alexandre Catarino 58793acae8
Build & Test Lean / build (push) Has been cancelled
Removes Assets that Are Not Invested and Not Tradable From Capacity Calculations (#5515)
* Add property for capacity. Remove unused variable

* Move SymbolCapacity and CapacityEstimate to common, passed through Symbol to runtime statistics

* Add null checks

* Remove uninvested and untradable assets from capacty calculations

* Add SymbolCapacity influential period

* Updates Regression Tests

- DelistingEventsAlgorithm
  - Allows additional contributions from delisted AAA.1

- DelistingFutureOptionRegressionAlgorithm
  - Removes DC01H12 contributions one month later

- FutureOptionBuySellCallIntradayRegressionAlgorithm
  - Allows additional contributions from future after expiry replacing the contribution of the next contract option

- DelistedFutureLiquidateRegressionAlgorithm
- FutureOptionCallITMExpiryRegressionAlgorithm
- FutureOptionCallITMGreeksExpiryRegressionAlgorithm
- FutureOptionPutITMExpiryRegressionAlgorithm
- FutureOptionShortCallITMExpiryRegressionAlgorithm
- FutureOptionShortPutITMExpiryRegressionAlgorithm
- FuturesAndFuturesOptionsExpiryTimeAndLiquidationRegressionAlgorithm
  - Allows additional contributions from future after expiry

- FutureOptionCallOTMExpiryRegressionAlgorithm
- FutureOptionPutOTMExpiryRegressionAlgorithm
- FutureOptionShortPutOTMExpiryRegressionAlgorithm
- IndexOptionCallITMGreeksExpiryRegressionAlgorithm
- IndexOptionCallOTMExpiryRegressionAlgorithm
- IndexOptionShortCallOTMExpiryRegressionAlgorithm
  - Allows additional contributions from option after expiry

- MACDTrendAlgorithm
  - Removes contribution when SPY is not invested for over one month

- UniverseSelectionRegressionAlgorithm
  - Allows additional contributions from delisted GOOAV replacing GOOG (new symbols)

* Adds Lowest Capacity Asset to Regression Tests

* Normalize expected value -0, because -0 is also written to file if updated

* Write Symbol.Value for lowestCapacitySymbol or  empty string for empty Symbol

* Update Regressions

* Update 'Lowest Capacity Asset' to Symbol.ID

Co-authored-by: Jared Broad <jaredbroad@gmail.com>
Co-authored-by: Martin-Molinero <martin@quantconnect.com>
Co-authored-by: Colton Sellers <Colton.R.Sellers@gmail.com>
2021-05-13 20:25:19 -03:00

189 lines
8.3 KiB
C#

/*
* 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 System;
using System.Linq;
using QuantConnect.Util;
using QuantConnect.Orders;
using QuantConnect.Securities;
using QuantConnect.Interfaces;
using System.Collections.Generic;
namespace QuantConnect
{
/// <summary>
/// Estimates dollar volume capacity of algorithm (in account currency) using all Symbols in the portfolio.
/// </summary>
/// <remarks>
/// Any mention of dollar volume is volume in account currency, but "dollar volume" is used
/// to maintain consistency with financial terminology and our use
/// case of having alphas measured capacity be in USD.
/// </remarks>
public class CapacityEstimate
{
private readonly IAlgorithm _algorithm;
private readonly Dictionary<Symbol, SymbolCapacity> _capacityBySymbol;
private List<SymbolCapacity> _monitoredSymbolCapacity;
// We use multiple collections to avoid having to perform an O(n) lookup whenever
// we're wanting to check whether a particular SymbolData instance is being "monitored",
// but still want to preserve indexing via an integer index
// (monitored meaning it is currently aggregating market dollar volume for its capacity calculation).
// For integer indexing, we use the List above, v.s. for lookup we use this HashSet.
private HashSet<SymbolCapacity> _monitoredSymbolCapacitySet;
private DateTime _nextSnapshotDate;
private TimeSpan _snapshotPeriod;
private Symbol _smallestAssetSymbol;
/// <summary>
/// The total capacity of the strategy at a point in time
/// </summary>
/// <remarks>We wrap this value type because it's being read and written by multiple threads.
/// <see cref="IResultHandler"/></remarks>
public ReferenceWrapper<decimal> Capacity { get; private set; }
/// <summary>
/// Provide a reference to the lowest capacity symbol used in scaling down the capacity for debugging.
/// </summary>
public Symbol LowestCapacityAsset => _smallestAssetSymbol;
/// <summary>
/// Initializes an instance of the class.
/// </summary>
/// <param name="algorithm">Used to get data at the current time step and access the portfolio state</param>
public CapacityEstimate(IAlgorithm algorithm)
{
_algorithm = algorithm;
_capacityBySymbol = new Dictionary<Symbol, SymbolCapacity>();
_monitoredSymbolCapacity = new List<SymbolCapacity>();
_monitoredSymbolCapacitySet = new HashSet<SymbolCapacity>();
// Set the minimum snapshot period to one day, but use algorithm start/end if the algo runtime is less than seven days
_snapshotPeriod = TimeSpan.FromDays(Math.Max(Math.Min((_algorithm.EndDate - _algorithm.StartDate).TotalDays - 1, 7), 1));
_nextSnapshotDate = _algorithm.StartDate + _snapshotPeriod;
Capacity = new ReferenceWrapper<decimal>(0);
}
/// <summary>
/// Processes an order whenever it's encountered so that we can calculate the capacity
/// </summary>
/// <param name="orderEvent">Order event to use to calculate capacity</param>
public void OnOrderEvent(OrderEvent orderEvent)
{
if (orderEvent.Status != OrderStatus.Filled && orderEvent.Status != OrderStatus.PartiallyFilled)
{
return;
}
SymbolCapacity symbolCapacity;
if (!_capacityBySymbol.TryGetValue(orderEvent.Symbol, out symbolCapacity))
{
symbolCapacity = new SymbolCapacity(_algorithm, orderEvent.Symbol);
_capacityBySymbol[orderEvent.Symbol] = symbolCapacity;
}
symbolCapacity.OnOrderEvent(orderEvent);
if (_monitoredSymbolCapacitySet.Contains(symbolCapacity))
{
return;
}
_monitoredSymbolCapacity.Add(symbolCapacity);
_monitoredSymbolCapacitySet.Add(symbolCapacity);
}
/// <summary>
/// Updates the market capacity for any Symbols that require a market update.
/// Sometimes, after the specified <seealso cref="_snapshotPeriod"/>, we
/// take a "snapshot" (point-in-time capacity) of the portfolio's capacity.
///
/// This result will be written into the Algorithm Statistics via the <see cref="BacktestingResultHandler"/>
/// </summary>
public void UpdateMarketCapacity(bool forceProcess)
{
for (var i = _monitoredSymbolCapacity.Count - 1; i >= 0; --i)
{
var capacity = _monitoredSymbolCapacity[i];
if (capacity.UpdateMarketCapacity())
{
_monitoredSymbolCapacity.RemoveAt(i);
_monitoredSymbolCapacitySet.Remove(capacity);
}
}
var utcDate = _algorithm.UtcTime.Date;
if (forceProcess || utcDate >= _nextSnapshotDate && _capacityBySymbol.Count != 0)
{
var totalPortfolioValue = _algorithm.Portfolio.TotalPortfolioValue;
var totalSaleVolume = _capacityBySymbol.Values
.Sum(s => s.SaleVolume);
if (totalPortfolioValue == 0 || _capacityBySymbol.Count == 0)
{
return;
}
var smallestAsset = _capacityBySymbol.Values
.OrderBy(c => c.MarketCapacityDollarVolume)
.First();
_smallestAssetSymbol = smallestAsset.Security.Symbol;
// When there is no trading, rely on the portfolio holdings
var percentageOfSaleVolume = totalSaleVolume != 0
? smallestAsset.SaleVolume / totalSaleVolume
: 0;
var buyingPowerUsed = smallestAsset.Security.MarginModel.GetReservedBuyingPowerForPosition(new ReservedBuyingPowerForPositionParameters(smallestAsset.Security))
.AbsoluteUsedBuyingPower * smallestAsset.Security.Leverage;
var percentageOfHoldings = buyingPowerUsed / totalPortfolioValue;
var scalingFactor = Math.Max(percentageOfSaleVolume, percentageOfHoldings);
var dailyMarketCapacityDollarVolume = smallestAsset.MarketCapacityDollarVolume / smallestAsset.Trades;
var newCapacity = scalingFactor == 0
? Capacity.Value
: dailyMarketCapacityDollarVolume / scalingFactor;
if (Capacity.Value == 0)
{
Capacity = new ReferenceWrapper<decimal>(newCapacity);
}
else
{
Capacity = new ReferenceWrapper<decimal>((0.33m * newCapacity) + (Capacity.Value * 0.66m));
}
foreach (var capacity in _capacityBySymbol.Select(pair => pair.Value).ToList())
{
if (!capacity.ShouldRemove())
{
capacity.Reset();
continue;
}
// we remove non invested and non tradable (delisted, deselected) securities this will allow the 'smallestAsset'
// to be changing between snapshots, and avoid the collections to grow
_capacityBySymbol.Remove(capacity.Security.Symbol);
_monitoredSymbolCapacity.Remove(capacity);
_monitoredSymbolCapacitySet.Remove(capacity);
}
_nextSnapshotDate = utcDate + _snapshotPeriod;
}
}
}
}