-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSmartInsiderIntention.cs
361 lines (331 loc) · 16.9 KB
/
SmartInsiderIntention.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* 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 System.Globalization;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using QuantConnect.Data;
using QuantConnect.Logging;
namespace QuantConnect.DataSource
{
/// <summary>
/// Smart Insider Intentions - Intention to execute a stock buyback and details about the future event
/// </summary>
public class SmartInsiderIntention : SmartInsiderEvent
{
/// <summary>
/// Data source ID
/// </summary>
public static int DataSourceId { get; } = 2018;
/// <summary>
/// Describes how the transaction was executed
/// </summary>
public SmartInsiderExecution? Execution { get; set; }
/// <summary>
/// Describes which entity intends to execute the transaction
/// </summary>
public SmartInsiderExecutionEntity? ExecutionEntity { get; set; }
/// <summary>
/// Describes what will be done with those shares following repurchase
/// </summary>
public SmartInsiderExecutionHolding? ExecutionHolding { get; set; }
/// <summary>
/// Number of shares to be or authorised to be traded
/// </summary>
public long? Amount { get; set; }
/// <summary>
/// Currency of the value of shares to be/Authorised to be traded (ISO Code)
/// </summary>
public string ValueCurrency { get; set; }
/// <summary>
/// Value of shares to be authorised to be traded
/// </summary>
public long? AmountValue { get; set; }
/// <summary>
/// Percentage of oustanding shares to be authorised to be traded
/// </summary>
public decimal? Percentage { get; set; }
/// <summary>
/// start of the period the intention/authorisation applies to
/// </summary>
public DateTime? AuthorizationStartDate { get; set; }
/// <summary>
/// End of the period the intention/authorisation applies to
/// </summary>
public DateTime? AuthorizationEndDate { get; set; }
/// <summary>
/// Currency of min/max prices (ISO Code)
/// </summary>
public string PriceCurrency { get; set; }
/// <summary>
/// Minimum price shares will or may be purchased at
/// </summary>
public decimal? MinimumPrice { get; set; }
/// <summary>
/// Maximum price shares will or may be purchased at
/// </summary>
public decimal? MaximumPrice { get; set; }
/// <summary>
/// Free text which explains further details about the trade
/// </summary>
public string NoteText { get; set; }
/// <summary>
/// Empty constructor required for <see cref="Slice.Get{T}()"/>
/// </summary>
public SmartInsiderIntention()
{
}
/// <summary>
/// Constructs instance of this via a *formatted* TSV line (tab delimited)
/// </summary>
/// <param name="line">Line of formatted TSV data</param>
public SmartInsiderIntention(string line) : base(line)
{
var tsv = line.Split('\t');
Execution = string.IsNullOrWhiteSpace(tsv[26]) ? (SmartInsiderExecution?)null : JsonConvert.DeserializeObject<SmartInsiderExecution>($"\"{tsv[26]}\"");
ExecutionEntity = string.IsNullOrWhiteSpace(tsv[27]) ? (SmartInsiderExecutionEntity?)null : JsonConvert.DeserializeObject<SmartInsiderExecutionEntity>($"\"{tsv[27]}\"");
ExecutionHolding = string.IsNullOrWhiteSpace(tsv[28]) ? (SmartInsiderExecutionHolding?)null : JsonConvert.DeserializeObject<SmartInsiderExecutionHolding>($"\"{tsv[28]}\"");
ExecutionHolding = ExecutionHolding == SmartInsiderExecutionHolding.Error ? SmartInsiderExecutionHolding.SatisfyStockVesting : ExecutionHolding;
Amount = string.IsNullOrWhiteSpace(tsv[29]) ? (int?)null : Convert.ToInt64(tsv[29], CultureInfo.InvariantCulture);
ValueCurrency = string.IsNullOrWhiteSpace(tsv[30]) ? null : tsv[30];
AmountValue = string.IsNullOrWhiteSpace(tsv[31]) ? (long?)null : Convert.ToInt64(new String(tsv[31].Where(Char.IsDigit).ToArray()), CultureInfo.InvariantCulture);
Percentage = string.IsNullOrWhiteSpace(tsv[32]) ? (decimal?)null : Convert.ToDecimal(tsv[32], CultureInfo.InvariantCulture);
AuthorizationStartDate = string.IsNullOrWhiteSpace(tsv[33]) ? (DateTime?)null : DateTime.ParseExact(tsv[33], "yyyyMMdd", CultureInfo.InvariantCulture);
AuthorizationEndDate = string.IsNullOrWhiteSpace(tsv[34]) ? (DateTime?)null : DateTime.ParseExact(tsv[34], "yyyyMMdd", CultureInfo.InvariantCulture);
PriceCurrency = string.IsNullOrWhiteSpace(tsv[35]) ? null : tsv[35];
MinimumPrice = string.IsNullOrWhiteSpace(tsv[36]) ? (decimal?)null : Convert.ToDecimal(tsv[36], CultureInfo.InvariantCulture);
MaximumPrice = string.IsNullOrWhiteSpace(tsv[37]) ? (decimal?)null : Convert.ToDecimal(tsv[37], CultureInfo.InvariantCulture);
NoteText = tsv.Length == 39? (string.IsNullOrWhiteSpace(tsv[38]) ? null : tsv[38]) : null;
}
/// <summary>
/// Constructs a new instance from unformatted TSV data
/// </summary>
/// <param name="line">Line of raw TSV (raw with fields 46, 36, 14, 7 removed in descending order)</param>
/// <param name="indexes">Index per header column</param>
/// <returns>success of the parsing task</returns>
public override bool FromRawData(string line, Dictionary<string, int> indexes)
{
try
{
var tsv = line.Split('\t');
if (!base.FromRawData(line, indexes))
{
return false;
}
Execution = null;
if (!string.IsNullOrWhiteSpace(tsv[indexes["IntentionVia"]]))
{
try
{
Execution = JsonConvert.DeserializeObject<SmartInsiderExecution>($"\"{tsv[indexes["IntentionVia"]]}\"");
}
catch (JsonSerializationException)
{
Log.Error($"SmartInsiderIntention.FromRawData(): New unexpected entry found for Execution: {tsv[indexes["IntentionVia"]]}. Parsed as Error.");
Execution = SmartInsiderExecution.Error;
}
}
ExecutionEntity = null;
if (!string.IsNullOrWhiteSpace(tsv[indexes["IntentionBy"]]))
{
try
{
ExecutionEntity = JsonConvert.DeserializeObject<SmartInsiderExecutionEntity>($"\"{tsv[indexes["IntentionBy"]]}\"");
}
catch (JsonSerializationException)
{
Log.Error($"SmartInsiderIntention.FromRawData(): New unexpected entry found for ExecutionEntity: {tsv[indexes["IntentionBy"]]}. Parsed as Error.");
ExecutionEntity = SmartInsiderExecutionEntity.Error;
}
}
ExecutionHolding = null;
if (!string.IsNullOrWhiteSpace(tsv[indexes["BuybackIntentionHoldingType"]]))
{
try
{
ExecutionHolding = JsonConvert.DeserializeObject<SmartInsiderExecutionHolding>($"\"{tsv[indexes["BuybackIntentionHoldingType"]]}\"");
if (ExecutionHolding == SmartInsiderExecutionHolding.Error)
{
// This error in particular represents a SatisfyStockVesting field.
ExecutionHolding = SmartInsiderExecutionHolding.SatisfyStockVesting;
}
}
catch (JsonSerializationException)
{
Log.Error($"SmartInsiderIntention.FromRawData(): New unexpected entry found for ExecutionHolding: {tsv[indexes["BuybackIntentionHoldingType"]]}. Parsed as Error.");
ExecutionHolding = SmartInsiderExecutionHolding.Error;
}
}
Amount = string.IsNullOrWhiteSpace(tsv[indexes["IntentionAmount"]]) ? null : Convert.ToInt64(tsv[indexes["IntentionAmount"]], CultureInfo.InvariantCulture);
ValueCurrency = string.IsNullOrWhiteSpace(tsv[indexes[nameof(ValueCurrency)]]) ? null : tsv[indexes[nameof(ValueCurrency)]];
AmountValue = string.IsNullOrWhiteSpace(tsv[indexes["IntentionValue"]]) ? null : Convert.ToInt64(tsv[indexes["IntentionValue"]], CultureInfo.InvariantCulture);
Percentage = string.IsNullOrWhiteSpace(tsv[indexes["IntentionPercentage"]]) ? null : Convert.ToDecimal(tsv[indexes["IntentionPercentage"]], CultureInfo.InvariantCulture);
AuthorizationStartDate = string.IsNullOrWhiteSpace(tsv[indexes["StartDate"]]) ? null : DateTime.ParseExact(tsv[indexes["StartDate"]], "yyyy-MM-dd", CultureInfo.InvariantCulture);
AuthorizationEndDate = string.IsNullOrWhiteSpace(tsv[indexes["EndDate"]]) ? null : DateTime.ParseExact(tsv[indexes["EndDate"]], "yyyy-MM-dd", CultureInfo.InvariantCulture);
PriceCurrency = string.IsNullOrWhiteSpace(tsv[indexes[nameof(PriceCurrency)]]) ? null : tsv[indexes[nameof(PriceCurrency)]];
MinimumPrice = string.IsNullOrWhiteSpace(tsv[indexes[nameof(MinimumPrice)]]) ? null : Convert.ToDecimal(tsv[indexes[nameof(MinimumPrice)]], CultureInfo.InvariantCulture);
MaximumPrice = string.IsNullOrWhiteSpace(tsv[indexes[nameof(MaximumPrice)]]) ? null : Convert.ToDecimal(tsv[indexes[nameof(MaximumPrice)]], CultureInfo.InvariantCulture);
NoteText = string.IsNullOrWhiteSpace(tsv[indexes["BuybackIntentionNoteText"]]) ? null : tsv[indexes["BuybackIntentionNoteText"]];
return true;
}
catch (Exception e)
{
Log.Error(e);
return false;
}
}
/// <summary>
/// Specifies the location of the data and directs LEAN where to load the data from
/// </summary>
/// <param name="config">Subscription configuration</param>
/// <param name="date">Algorithm date</param>
/// <param name="isLiveMode">Is live mode</param>
/// <returns>Subscription data source object pointing LEAN to the data location</returns>
public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
{
return new SubscriptionDataSource(
Path.Combine(
Globals.DataFolder,
"alternative",
"smartinsider",
"intentions",
$"{config.Symbol.Value.ToLowerInvariant()}.tsv"
),
SubscriptionTransportMedium.LocalFile,
FileFormat.Csv
);
}
/// <summary>
/// Loads and reads the data to be used in LEAN
/// </summary>
/// <param name="config">Subscription configuration</param>
/// <param name="line">TSV line</param>
/// <param name="date">Algorithm date</param>
/// <param name="isLiveMode">Is live mode</param>
/// <returns>Instance of the object</returns>
public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
{
return new SmartInsiderIntention(line)
{
Symbol = config.Symbol
};
}
/// <summary>
/// Clones the object to a new instance. This method
/// is required for custom data sources that make use
/// of properties with more complex types since otherwise
/// the values will default to null using the default clone method
/// </summary>
/// <returns>A new cloned instance of this object</returns>
public override BaseData Clone()
{
return new SmartInsiderIntention()
{
TransactionID = TransactionID,
EventType = EventType,
LastUpdate = LastUpdate,
LastIDsUpdate = LastIDsUpdate,
ISIN = ISIN,
USDMarketCap = USDMarketCap,
CompanyID = CompanyID,
ICBIndustry = ICBIndustry,
ICBSuperSector = ICBSuperSector,
ICBSector = ICBSector,
ICBSubSector = ICBSubSector,
ICBCode = ICBCode,
CompanyName = CompanyName,
PreviousResultsAnnouncementDate = PreviousResultsAnnouncementDate,
NextResultsAnnouncementsDate = NextResultsAnnouncementsDate,
NextCloseBegin = NextCloseBegin,
LastCloseEnded = LastCloseEnded,
SecurityDescription = SecurityDescription,
TickerCountry = TickerCountry,
TickerSymbol = TickerSymbol,
AnnouncementDate = AnnouncementDate,
TimeReleased = TimeReleased,
TimeProcessed = TimeProcessed,
TimeReleasedUtc = TimeReleasedUtc,
TimeProcessedUtc = TimeProcessedUtc,
AnnouncedIn = AnnouncedIn,
Execution = Execution,
ExecutionEntity = ExecutionEntity,
ExecutionHolding = ExecutionHolding,
Amount = Amount,
ValueCurrency = ValueCurrency,
AmountValue = AmountValue,
Percentage = Percentage,
AuthorizationStartDate = AuthorizationStartDate,
AuthorizationEndDate = AuthorizationEndDate,
PriceCurrency = PriceCurrency,
MinimumPrice = MinimumPrice,
MaximumPrice = MaximumPrice,
NoteText = NoteText,
Symbol = Symbol,
Value = Value,
Time = Time,
};
}
/// <summary>
/// Converts the data to TSV
/// </summary>
/// <returns>String of TSV</returns>
/// <remarks>Parsable by the constructor should you need to recreate the object from TSV</remarks>
public override string ToLine()
{
return string.Join("\t",
TimeProcessedUtc?.ToStringInvariant("yyyyMMdd HH:mm:ss"),
TransactionID,
EventType == null ? null : JsonConvert.SerializeObject(EventType).Replace("\"", ""),
LastUpdate.ToStringInvariant("yyyyMMdd"),
LastIDsUpdate?.ToStringInvariant("yyyyMMdd"),
ISIN,
USDMarketCap,
CompanyID,
ICBIndustry,
ICBSuperSector,
ICBSector,
ICBSubSector,
ICBCode,
CompanyName,
PreviousResultsAnnouncementDate?.ToStringInvariant("yyyyMMdd"),
NextResultsAnnouncementsDate?.ToStringInvariant("yyyyMMdd"),
NextCloseBegin?.ToStringInvariant("yyyyMMdd"),
LastCloseEnded?.ToStringInvariant("yyyyMMdd"),
SecurityDescription,
TickerCountry,
TickerSymbol,
AnnouncementDate?.ToStringInvariant("yyyyMMdd"),
TimeReleased?.ToStringInvariant("yyyyMMdd HH:mm:ss"),
TimeProcessed?.ToStringInvariant("yyyyMMdd HH:mm:ss"),
TimeReleasedUtc?.ToStringInvariant("yyyyMMdd HH:mm:ss"),
AnnouncedIn,
Execution == null ? null : JsonConvert.SerializeObject(Execution).Replace("\"", ""),
ExecutionEntity == null ? null : JsonConvert.SerializeObject(ExecutionEntity).Replace("\"", ""),
ExecutionHolding == null ? null : JsonConvert.SerializeObject(ExecutionHolding).Replace("\"", ""),
Amount,
ValueCurrency,
AmountValue,
Percentage,
AuthorizationStartDate?.ToStringInvariant("yyyyMMdd"),
AuthorizationEndDate?.ToStringInvariant("yyyyMMdd"),
PriceCurrency,
MinimumPrice,
MaximumPrice,
NoteText);
}
}
}