forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotifiedSecurityChanges.cs
More file actions
128 lines (120 loc) · 5.92 KB
/
NotifiedSecurityChanges.cs
File metadata and controls
128 lines (120 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* 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
{
/// <summary>
/// Provides convenience methods for updating collections in responses to securities changed events
/// </summary>
public static class NotifiedSecurityChanges
{
/// <summary>
/// Adds and removes the security changes to/from the collection
/// </summary>
/// <param name="securities">The securities collection to be updated with the changes</param>
/// <param name="changes">The changes to be applied to the securities collection</param>
public static void UpdateCollection(ICollection<Security> securities, SecurityChanges changes)
{
Update(changes, securities.Add, removed => securities.Remove(removed));
}
/// <summary>
/// Adds and removes the security changes to/from the collection
/// </summary>
/// <param name="securities">The securities collection to be updated with the changes</param>
/// <param name="changes">The changes to be applied to the securities collection</param>
/// <param name="valueFactory">Delegate used to create instances of <typeparamref name="TValue"/> from a <see cref="Security"/> object</param>
public static void UpdateCollection<TValue>(ICollection<TValue> securities, SecurityChanges changes, Func<Security, TValue> valueFactory)
{
Update(changes, added => securities.Add(valueFactory(added)), removed => securities.Remove(valueFactory(removed)));
}
/// <summary>
/// Adds and removes the security changes to/from the collection
/// </summary>
/// <param name="dictionary">The securities collection to be updated with the changes</param>
/// <param name="changes">The changes to be applied to the securities collection</param>
/// <param name="valueFactory">Factory for creating dictonary values for a key</param>
public static void UpdateDictionary<TValue>(
IDictionary<Security, TValue> dictionary,
SecurityChanges changes,
Func<Security, TValue> valueFactory
)
{
UpdateDictionary(dictionary, changes, security => security, valueFactory);
}
/// <summary>
/// Adds and removes the security changes to/from the collection
/// </summary>
/// <param name="dictionary">The securities collection to be updated with the changes</param>
/// <param name="changes">The changes to be applied to the securities collection</param>
/// <param name="valueFactory">Factory for creating dictonary values for a key</param>
public static void UpdateDictionary<TValue>(
IDictionary<Symbol, TValue> dictionary,
SecurityChanges changes,
Func<Security, TValue> valueFactory
)
{
UpdateDictionary(dictionary, changes, security => security.Symbol, valueFactory);
}
/// <summary>
/// Most generic form of <see cref="UpdateCollection"/>
/// </summary>
/// <typeparam name="TKey">The dictionary's key type</typeparam>
/// <typeparam name="TValue">The dictionary's value type</typeparam>
/// <param name="dictionary">The dictionary to update</param>
/// <param name="changes">The <seealso cref="SecurityChanges"/> to apply to the dictionary</param>
/// <param name="keyFactory">Selector pulling <typeparamref name="TKey"/> from a <seealso cref="Security"/></param>
/// <param name="valueFactory">Selector pulling <typeparamref name="TValue"/> from a <seealso cref="Security"/></param>
public static void UpdateDictionary<TKey, TValue>(
IDictionary<TKey, TValue> dictionary,
SecurityChanges changes,
Func<Security, TKey> keyFactory,
Func<Security, TValue> 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();
});
}
/// <summary>
/// Invokes the provided <paramref name="add"/> and <paramref name="remove"/> functions for each
/// </summary>
/// <param name="changes">The security changes to process</param>
/// <param name="add">Function called for each added security</param>
/// <param name="remove">Function called for each removed security</param>
public static void Update(SecurityChanges changes, Action<Security> add, Action<Security> remove)
{
foreach (var added in changes.AddedSecurities)
{
add(added);
}
foreach (var removed in changes.RemovedSecurities)
{
remove(removed);
}
}
}
}