forked from StockSharp/StockSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContinuousSecurity.cs
327 lines (268 loc) · 10.3 KB
/
ContinuousSecurity.cs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#region S# License
/******************************************************************************************
NOTICE!!! This program and source code is owned and licensed by
StockSharp, LLC, www.stocksharp.com
Viewing or use of this code requires your acceptance of the license
agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE
Removal of this comment is a violation of the license agreement.
Project: StockSharp.Algo.Algo
File: ContinuousSecurity.cs
Created: 2015, 11, 11, 2:32 PM
Copyright 2010 by StockSharp, LLC
*******************************************************************************************/
#endregion S# License
namespace StockSharp.Algo
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Ecng.Collections;
using Ecng.Common;
using Ecng.ComponentModel;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
using StockSharp.Localization;
/// <summary>
/// Continuous security (generally, a futures contract), containing expirable securities.
/// </summary>
[DisplayNameLoc(LocalizedStrings.ContinuousSecurityKey)]
[DescriptionLoc(LocalizedStrings.Str696Key)]
public class ContinuousSecurity : BasketSecurity
{
/// <summary>
/// The interface describing the internal instruments collection <see cref="ContinuousSecurity.ExpirationJumps"/>.
/// </summary>
public interface IExpirationJumpList : ISynchronizedCollection<KeyValuePair<Security, DateTimeOffset>>, IDictionary<Security, DateTimeOffset>
{
/// <summary>
/// To get the instrument for the specified expiration time.
/// </summary>
/// <param name="time">The expiration time.</param>
/// <returns>Security.</returns>
Security this[DateTimeOffset time] { get; }
/// <summary>
/// To get the first instrument by expiration.
/// </summary>
/// <returns>The first instrument. If the <see cref="ContinuousSecurity.ExpirationJumps"/> is empty, the <see langword="null" /> will be returned.</returns>
Security FirstSecurity { get; }
/// <summary>
/// To get the last instrument by expiration.
/// </summary>
/// <returns>The last instrument. If the <see cref="ContinuousSecurity.ExpirationJumps"/> is empty, the <see langword="null" /> will be returned.</returns>
Security LastSecurity { get; }
/// <summary>
/// To get the next instrument.
/// </summary>
/// <param name="security">Security.</param>
/// <returns>The next instrument. If the <paramref name="security" /> is the last instrument then <see langword="null" /> will be returned.</returns>
Security GetNextSecurity(Security security);
/// <summary>
/// To get the previous instrument.
/// </summary>
/// <param name="security">Security.</param>
/// <returns>The previous instrument. If the <paramref name="security" /> is the first instrument then <see langword="null" /> will be returned.</returns>
Security GetPrevSecurity(Security security);
/// <summary>
/// To get the range of operation of the internal instrument.
/// </summary>
/// <param name="security">The internal instrument.</param>
/// <returns>The range of operation.</returns>
Range<DateTimeOffset> GetActivityRange(Security security);
}
private sealed class ExpirationJumpsDictionary : SynchronizedPairSet<Security, DateTimeOffset>, IExpirationJumpList
{
private readonly SortedDictionary<Range<DateTimeOffset>, Security> _expirationRanges;
private IEnumerator<KeyValuePair<Range<DateTimeOffset>, Security>> _enumerator;
private KeyValuePair<Range<DateTimeOffset>, Security>? _current;
public ExpirationJumpsDictionary()
{
Func<Range<DateTimeOffset>, Range<DateTimeOffset>, int> comparer = (r1, r2) => r1.Max.CompareTo(r2.Max);
_expirationRanges = new SortedDictionary<Range<DateTimeOffset>, Security>(comparer.ToComparer());
InnerSecurities = ArrayHelper.Empty<Security>();
}
public Security[] InnerSecurities { get; private set; }
public override void Add(Security key, DateTimeOffset value)
{
lock (SyncRoot)
{
base.Add(key, value);
RefreshRanges();
}
}
public override bool Remove(Security key)
{
lock (SyncRoot)
{
if (base.Remove(key))
{
RefreshRanges();
return true;
}
}
return false;
}
public override void Clear()
{
lock (SyncRoot)
{
base.Clear();
_expirationRanges.Clear();
_current = null;
InnerSecurities = ArrayHelper.Empty<Security>();
DisposeEnumerator();
}
}
private void RefreshRanges()
{
var prevTime = DateTimeOffset.MinValue;
_expirationRanges.Clear();
foreach (var pair in this.OrderBy(kv => kv.Value).ToArray())
{
_expirationRanges.Add(new Range<DateTimeOffset>(prevTime, pair.Value), pair.Key);
prevTime = pair.Value + TimeSpan.FromTicks(1);
}
InnerSecurities = _expirationRanges.Values.ToArray();
DisposeEnumerator();
_enumerator = ((IEnumerable<KeyValuePair<Range<DateTimeOffset>, Security>>)new CircularBuffer<KeyValuePair<Range<DateTimeOffset>, Security>>(_expirationRanges)).GetEnumerator();
MoveNext();
}
private void MoveNext()
{
if (_enumerator.MoveNext())
_current = _enumerator.Current;
else
_current = null;
}
public Security GetSecurity(DateTimeOffset marketTime)
{
lock (SyncRoot)
{
Security security = null;
while (_current != null)
{
var kv = _current.Value;
// зациклилось
if (kv.Value == security)
break;
if (kv.Key.Contains(marketTime))
return kv.Value;
if (security == null)
security = _current.Value.Value;
MoveNext();
}
return null;
}
}
private void DisposeEnumerator()
{
if (_enumerator != null)
_enumerator.Dispose();
}
Security IExpirationJumpList.FirstSecurity => GetSecurity(DateTimeOffset.MinValue);
Security IExpirationJumpList.LastSecurity => _expirationRanges.LastOrDefault().Value;
Security IExpirationJumpList.GetNextSecurity(Security security)
{
lock (SyncRoot)
{
if (!ContainsKey(security))
throw new ArgumentException(LocalizedStrings.Str697Params.Put(security));
var index = InnerSecurities.IndexOf(security);
return index == InnerSecurities.Length - 1 ? null : InnerSecurities[index + 1];
}
}
Security IExpirationJumpList.GetPrevSecurity(Security security)
{
lock (SyncRoot)
{
if (!ContainsKey(security))
throw new ArgumentException(LocalizedStrings.Str697Params.Put(security));
var index = InnerSecurities.IndexOf(security);
return index == 0 ? null : InnerSecurities[index - 1];
}
}
Range<DateTimeOffset> IExpirationJumpList.GetActivityRange(Security security)
{
if (security == null)
throw new ArgumentNullException(nameof(security));
lock (SyncRoot)
{
return (from expirationRange in _expirationRanges
where expirationRange.Value == security
select expirationRange.Key).First();
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="ContinuousSecurity"/>.
/// </summary>
public ContinuousSecurity()
{
Type = SecurityTypes.Future;
_expirationJumps = new ExpirationJumpsDictionary();
}
private readonly ExpirationJumpsDictionary _expirationJumps;
/// <summary>
/// Instruments and dates of transition at which the transition to the next instrument takes place.
/// </summary>
[Browsable(false)]
public IExpirationJumpList ExpirationJumps => _expirationJumps;
/// <summary>
/// Instruments, from which this basket is created.
/// </summary>
[Browsable(false)]
public override IEnumerable<Security> InnerSecurities => _expirationJumps.InnerSecurities;
///// <summary>
///// Проверить, используется ли указанный инструмент в настоящее время.
///// </summary>
///// <param name="security">Инструмент, который необходимо проверить.</param>
///// <returns><see langword="true"/>, если указанный инструмент используется в настоящее время, иначе, <see langword="false"/>.</returns>
//public override bool Contains(Security security)
//{
// var innerSecurity = GetSecurity();
// var basket = innerSecurity as BasketSecurity;
// if (basket == null)
// return innerSecurity == security;
// return basket.Contains(security);
//}
///// <summary>
///// Получить инструмент, который торгуется в текущий момент (текущее время вычисляется через метод <see cref="TraderHelper.GetMarketTime"/>).
///// </summary>
///// <returns>Инструмент. Если не существует инструмента для указанного времени, то будет возвращено <see langword="null"/>.</returns>
//public Security GetSecurity()
//{
// return GetSecurity(this.GetMarketTime());
//}
/// <summary>
/// To get the instrument that trades for the specified exchange time.
/// </summary>
/// <param name="marketTime">The exchange time.</param>
/// <returns>The instrument. If there is no instrument for the specified time then the <see langword="null" /> will be returned.</returns>
public Security GetSecurity(DateTimeOffset marketTime)
{
return _expirationJumps.GetSecurity(marketTime);
}
/// <summary>
/// To shift the expiration of internal instruments <see cref="ContinuousSecurity.ExpirationJumps"/> to a size equas to <paramref name="offset" />.
/// </summary>
/// <param name="offset">The size of the expiration shift.</param>
public void Offset(TimeSpan offset)
{
lock (_expirationJumps.SyncRoot)
{
var dict = new PairSet<Security, DateTimeOffset>();
foreach (var security in InnerSecurities)
{
if (security.ExpiryDate == null)
throw new InvalidOperationException(LocalizedStrings.Str698Params.Put(security.Id));
var expiryDate = (DateTimeOffset)security.ExpiryDate + offset;
if (expiryDate > security.ExpiryDate)
throw new ArgumentOutOfRangeException(nameof(offset), offset, LocalizedStrings.Str699Params.Put(security.Id, expiryDate, security.ExpiryDate));
dict.Add(security, expiryDate);
}
_expirationJumps.Clear();
_expirationJumps.AddRange(dict);
}
}
}
}