forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactorFileGenerator.cs
346 lines (301 loc) · 14.1 KB
/
FactorFileGenerator.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
/*
* 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.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Data.Auxiliary;
using QuantConnect.Data.Market;
namespace QuantConnect.ToolBox
{
/// <summary>
/// Generates a factor file from a list of splits and dividends for a specified equity
/// </summary>
public class FactorFileGenerator
{
/// <summary>
/// The symbol for which the factor file is being generated
/// </summary>
public Symbol Symbol { get; set; }
/// <summary>
/// Data for this equity at daily resolution
/// </summary>
private readonly List<TradeBar> _dailyDataForEquity;
/// <summary>
/// The last date in the _dailyEquityData
/// </summary>
private readonly DateTime _lastDateFromEquityData;
/// <summary>
/// Constructor for the FactorFileGenerator
/// </summary>
/// <param name="symbol">The equity for which the factor file respresents</param>
/// <param name="pathForDailyEquityData">The path to the daily data for the specified equity</param>
public FactorFileGenerator(Symbol symbol, string pathForDailyEquityData)
{
Symbol = symbol;
_dailyDataForEquity = ReadDailyEquityData(pathForDailyEquityData);
_lastDateFromEquityData = _dailyDataForEquity.Last().Time;
}
/// <summary>
/// Create FactorFile instance
/// </summary>
/// <param name="dividendSplitList">List of Dividends and Splits</param>
/// <returns><see cref="FactorFile"/> instance</returns>
public FactorFile CreateFactorFile(List<BaseData> dividendSplitList)
{
var orderedDividendSplitQueue = new Queue<BaseData>(
CombineIntraDayDividendSplits(dividendSplitList)
.OrderByDescending(x => x.Time));
var factorFileRows = new List<FactorFileRow>
{
// First Factor Row is set far into the future and by definition has 1 for both price and split factors
new FactorFileRow(
Time.EndOfTime,
priceFactor: 1,
splitFactor: 1
)
};
return RecursivlyGenerateFactorFile(orderedDividendSplitQueue, factorFileRows);
}
/// <summary>
/// If dividend and split occur on the same day,
/// combine them into IntraDayDividendSplit object
/// </summary>
/// <param name="splitDividendList">List of split and dividends</param>
/// <returns>A list of splits, dividends with intraday split and dividends combined into <see cref="IntraDayDividendSplit"/></returns>
private List<BaseData> CombineIntraDayDividendSplits(List<BaseData> splitDividendList)
{
var splitDividendCollection = new Collection<BaseData>(splitDividendList);
var dateKeysLookup = splitDividendCollection.GroupBy(x => x.Time)
.OrderByDescending(x => x.Key)
.Select(group => group)
.ToList();
var baseDataList = new List<BaseData>();
foreach (var kvpLookup in dateKeysLookup)
{
if (kvpLookup.Count() > 1)
{
// Intraday dividend split found
var dividend = kvpLookup.First(x => x.GetType() == typeof(Dividend)) as Dividend;
var split = kvpLookup.First(x => x.GetType() == typeof(Split)) as Split;
baseDataList.Add(new IntraDayDividendSplit(split, dividend));
}
else
{
baseDataList.Add(kvpLookup.First());
}
}
return baseDataList;
}
/// <summary>
/// Recursively generate a <see cref="FactorFile"/>
/// </summary>
/// <param name="orderedDividendSplits">Queue of dividends and splits ordered by date</param>
/// <param name="factorFileRows">The list of factor file rows</param>
/// <returns><see cref="FactorFile"/> instance</returns>
private FactorFile RecursivlyGenerateFactorFile(Queue<BaseData> orderedDividendSplits, List<FactorFileRow> factorFileRows)
{
// If there is no more dividends or splits, return
if (!orderedDividendSplits.Any())
{
factorFileRows.Add(CreateLastFactorFileRow(factorFileRows));
return new FactorFile(Symbol.ID.Symbol, factorFileRows);
}
var nextEvent = orderedDividendSplits.Dequeue();
// If there is no more daily equity data to use, return
if (_lastDateFromEquityData > nextEvent.Time)
{
factorFileRows.Add(CreateLastFactorFileRow(factorFileRows));
return new FactorFile(Symbol.ID.Symbol, factorFileRows);
}
var nextFactorFileRow = CalculateNextFactorFileRow(factorFileRows, nextEvent);
if (nextFactorFileRow != null)
factorFileRows.Add(nextFactorFileRow);
return RecursivlyGenerateFactorFile(orderedDividendSplits, factorFileRows);
}
/// <summary>
/// Create the last FileFactorRow.
/// Represents the earliest date that the daily equity data contains.
/// </summary>
/// <param name="factorFileRows">The list of factor file rows</param>
/// <returns><see cref="FactorFileRow"/></returns>
private FactorFileRow CreateLastFactorFileRow(List<FactorFileRow> factorFileRows)
{
return new FactorFileRow(
_dailyDataForEquity.Last().Time.Date,
factorFileRows.Last().PriceFactor,
factorFileRows.Last().SplitFactor
);
}
/// <summary>
/// Calculates the next <see cref="FactorFileRow"/>
/// </summary>
/// <param name="factorFileRows">The current list of factorFileRows</param>
/// <param name="nextEvent">The next dividend, split or intradayDividendSplit</param>
/// <returns>A single factor file row</returns>
private FactorFileRow CalculateNextFactorFileRow(List<FactorFileRow> factorFileRows, BaseData nextEvent)
{
FactorFileRow nextFactorFileRow;
var t = nextEvent.GetType();
switch (t.Name)
{
case "Dividend":
nextFactorFileRow = CalculateNextDividendFactor(nextEvent, factorFileRows.Last());
break;
case "Split":
nextFactorFileRow = CalculateNextSplitFactor(nextEvent, factorFileRows.Last());
break;
case "IntraDayDividendSplit":
nextFactorFileRow = CalculateIntradayDividendSplit((IntraDayDividendSplit)nextEvent, factorFileRows.Last());
break;
default:
throw new ArgumentException("Unhandled BaseData type for FactorFileGenerator.");
}
return nextFactorFileRow;
}
/// <summary>
/// Generates the <see cref="FactorFileRow"/> that represents a intraday dividend split.
/// Applies the dividend first.
/// </summary>
/// <param name="intraDayDividendSplit"><see cref="IntraDayDividendSplit"/> instance that holds the intraday dividend and split information</param>
/// <param name="last">The last <see cref="FactorFileRow"/> generated recursivly</param>
/// <returns><see cref="FactorFileRow"/> that represents an intraday dividend and split</returns>
private FactorFileRow CalculateIntradayDividendSplit(IntraDayDividendSplit intraDayDividendSplit, FactorFileRow last)
{
var row = CalculateNextDividendFactor(intraDayDividendSplit.Dividend, last);
return CalculateNextSplitFactor(intraDayDividendSplit.Split, row);
}
/// <summary>
/// Calculates the price factor of a <see cref="Dividend"/>
/// </summary>
/// <param name="dividend">The next dividend</param>
/// <param name="previousFactorFileRow">The previous <see cref="FactorFileRow"/> generated</param>
/// <returns><see cref="FactorFileRow"/> that represents the dividend event</returns>
private FactorFileRow CalculateNextDividendFactor(BaseData dividend, FactorFileRow previousFactorFileRow)
{
var eventDayData = GetDailyDataForDate(dividend.Time);
// If you don't have the equity data nothing can be calculated
if (eventDayData == null)
{
return null;
}
TradeBar previousClosingPrice = FindPreviousTradableDayClosingPrice(eventDayData.Time);
var priceFactor = previousFactorFileRow.PriceFactor - (dividend.Value / ((previousClosingPrice.Close) * previousFactorFileRow.SplitFactor));
return new FactorFileRow(
previousClosingPrice.Time,
priceFactor.RoundToSignificantDigits(7),
previousFactorFileRow.SplitFactor,
previousClosingPrice.Close
);
}
/// <summary>
/// Calculates the split factor of a <see cref="Split"/>
/// </summary>
/// <param name="split">The next <see cref="Split"/></param>
/// <param name="previousFactorFileRow">The previous <see cref="FactorFileRow"/> generated</param>
/// <returns><see cref="FactorFileRow"/> that represents the split event</returns>
private FactorFileRow CalculateNextSplitFactor(BaseData split, FactorFileRow previousFactorFileRow)
{
var eventDayData = GetDailyDataForDate(split.Time);
// If you don't have the equity data nothing can be done
if (eventDayData == null)
{
return null;
}
TradeBar previousClosingPrice = FindPreviousTradableDayClosingPrice(eventDayData.Time);
return new FactorFileRow(
previousClosingPrice.Time,
previousFactorFileRow.PriceFactor,
(previousFactorFileRow.SplitFactor * split.Value).RoundToSignificantDigits(6),
previousClosingPrice.Close
);
}
/// <summary>
/// Gets the data for a specified date
/// </summary>
/// <param name="date">The current specified date</param>
/// <returns><see cref="TradeBar"/>representing that date</returns>
private TradeBar GetDailyDataForDate(DateTime date)
{
return _dailyDataForEquity.FirstOrDefault(x => x.Time.Date == date.Date);
}
/// <summary>
/// Gets the data for the previous tradable day
/// </summary>
/// <param name="date">The current specified date</param>
/// <returns>The last tradeble days data</returns>
private TradeBar FindPreviousTradableDayClosingPrice(DateTime date)
{
TradeBar previousDayData = null;
var lastDateforData = _dailyDataForEquity.Last();
while (previousDayData == null && date > lastDateforData.EndTime)
{
previousDayData = _dailyDataForEquity.FirstOrDefault(x => x.Time == date.AddDays(-1));
date = date.AddDays(-1);
}
return previousDayData;
}
/// <summary>
/// Read the daily equity date from file
/// </summary>
/// <param name="pathForDailyEquityData">Path the the daily data</param>
/// <returns>A list of <see cref="TradeBar"/> read from file</returns>
private List<TradeBar> ReadDailyEquityData(string pathForDailyEquityData)
{
using (var zipToOpen = new FileStream(pathForDailyEquityData, FileMode.Open))
{
using (var archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read))
{
foreach (var entry in archive.Entries)
{
var parser = new LeanParser();
var stream = entry.Open();
return parser.Parse(pathForDailyEquityData, stream)
.OrderByDescending(x => x.Time)
.Select(x => (TradeBar)x)
.ToList();
}
}
}
return new List<TradeBar>();
}
/// <summary>
/// Pairs split and dividend data into one type
/// </summary>
private class IntraDayDividendSplit : BaseData
{
public Split Split { get; }
public Dividend Dividend { get; }
public IntraDayDividendSplit(Split split, Dividend dividend)
{
if (split == null)
{
throw new ArgumentNullException("split");
}
if (dividend == null)
{
throw new ArgumentNullException("dividend");
}
Split = split;
Dividend = dividend;
Time = Split.Time;
}
}
}
}