/*
* 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 QuantConnect.Data.UniverseSelection;
using QuantConnect.Securities;
using QuantConnect.Util;
namespace QuantConnect.Algorithm.Framework
{
///
/// Provides convenience methods for updating collections in responses to securities changed events
///
public static class NotifiedSecurityChanges
{
///
/// Adds and removes the security changes to/from the collection
///
/// The securities collection to be updated with the changes
/// The changes to be applied to the securities collection
public static void UpdateCollection(ICollection securities, SecurityChanges changes)
{
Update(changes, securities.Add, removed => securities.Remove(removed));
}
///
/// Adds and removes the security changes to/from the collection
///
/// The securities collection to be updated with the changes
/// The changes to be applied to the securities collection
/// Delegate used to create instances of from a object
public static void UpdateCollection(ICollection securities, SecurityChanges changes, Func valueFactory)
{
Update(changes, added => securities.Add(valueFactory(added)), removed => securities.Remove(valueFactory(removed)));
}
///
/// Adds and removes the security changes to/from the collection
///
/// The securities collection to be updated with the changes
/// The changes to be applied to the securities collection
/// Factory for creating dictonary values for a key
public static void UpdateDictionary(
IDictionary dictionary,
SecurityChanges changes,
Func valueFactory
)
{
UpdateDictionary(dictionary, changes, security => security, valueFactory);
}
///
/// Adds and removes the security changes to/from the collection
///
/// The securities collection to be updated with the changes
/// The changes to be applied to the securities collection
/// Factory for creating dictonary values for a key
public static void UpdateDictionary(
IDictionary dictionary,
SecurityChanges changes,
Func valueFactory
)
{
UpdateDictionary(dictionary, changes, security => security.Symbol, valueFactory);
}
///
/// Most generic form of
///
/// The dictionary's key type
/// The dictionary's value type
/// The dictionary to update
/// The to apply to the dictionary
/// Selector pulling from a
/// Selector pulling from a
public static void UpdateDictionary(
IDictionary dictionary,
SecurityChanges changes,
Func keyFactory,
Func valueFactory
)
{
Update(changes,
added => dictionary.Add(keyFactory(added), valueFactory(added)),
removed =>
{
var key = keyFactory(removed);
var entry = dictionary[key];
dictionary.Remove(key);
// give the entry a chance to clean up after itself
var disposable = entry as IDisposable;
disposable.DisposeSafely();
});
}
///
/// Invokes the provided and functions for each
///
/// The security changes to process
/// Function called for each added security
/// Function called for each removed security
public static void Update(SecurityChanges changes, Action add, Action remove)
{
foreach (var added in changes.AddedSecurities)
{
add(added);
}
foreach (var removed in changes.RemovedSecurities)
{
remove(removed);
}
}
}
}