/*
* 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.Collections.Generic;
using static System.FormattableString;
namespace QuantConnect.Algorithm.Framework.Alphas.Analysis
{
///
/// Defines the security values at a given instant. This is analagous
/// to TimeSlice/Slice, but decoupled from the algorithm thread and is
/// intended to contain all of the information necessary to score all
/// insight at this particular time step
///
public class ReadOnlySecurityValuesCollection
{
private Dictionary _securityValuesBySymbol;
private readonly Func _securityValuesBySymbolFunc;
///
/// Initializes a new instance of the class
///
///
public ReadOnlySecurityValuesCollection(Dictionary securityValuesBySymbol)
{
_securityValuesBySymbol = securityValuesBySymbol;
_securityValuesBySymbolFunc = null;
}
///
/// Initializes a new instance of the class
///
/// This constructor has performance in mind. Only create the
/// for a if requested by a consumer.
/// Function used to get the
/// for a specified
public ReadOnlySecurityValuesCollection(Func securityValuesBySymbolFunc)
{
_securityValuesBySymbolFunc = securityValuesBySymbolFunc;
// lets be lazy for constructing the dictionary too!
_securityValuesBySymbol = null;
}
///
/// Symbol indexer into security values collection.
///
/// The symbol
/// The security values for the specified symbol
public SecurityValues this[Symbol symbol]
{
get
{
if (_securityValuesBySymbol == null)
{
_securityValuesBySymbol = new Dictionary();
}
SecurityValues result;
if(!_securityValuesBySymbol.TryGetValue(symbol, out result))
{
if (_securityValuesBySymbolFunc == null)
{
throw new KeyNotFoundException(Invariant($"SecurityValues for symbol {symbol} was not found"));
}
result = _securityValuesBySymbolFunc(symbol);
_securityValuesBySymbol[symbol] = result;
}
return result;
}
}
}
}