forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KaikoCryptoReader.cs
259 lines (226 loc) · 9.42 KB
/
KaikoCryptoReader.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
/*
* 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 QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Logging;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using ZipEntry = Ionic.Zip.ZipEntry;
namespace QuantConnect.ToolBox.KaikoDataConverter
{
/// <summary>
/// Decompress single entry from Kaiko crypto raw data.
/// </summary>
public class KaikoDataReader
{
private Symbol _symbol;
private TickType _tickType;
/// <summary>
/// Initializes a new instance of the <see cref="KaikoDataReader"/> class.
/// </summary>
/// <param name="symbol">The symbol.</param>
/// <param name="tickType">Type of the tick.</param>
public KaikoDataReader(Symbol symbol, TickType tickType)
{
_symbol = symbol;
_tickType = tickType;
}
/// <summary>
/// Gets the ticks from Kaiko file zip entry.
/// </summary>
/// <param name="zipEntry">The zip entry.</param>
/// <returns></returns>
public IEnumerable<BaseData> GetTicksFromZipEntry(ZipEntry zipEntry)
{
var rawData = GetRawDataStreamFromEntry(zipEntry);
return _tickType == TickType.Trade ? ParseKaikoTradeFile(rawData) : ParseKaikoQuoteFile(rawData);
}
/// <summary>
/// Gets the raw data from entry.
/// </summary>
/// <param name="zipEntry">The zip entry.</param>
/// <returns>IEnumerable with the zip entry content.</returns>
private IEnumerable<string> GetRawDataStreamFromEntry(ZipEntry zipEntry)
{
using (var outerStream = new StreamReader(zipEntry.OpenReader()))
using (var innerStream = new GZipStream(outerStream.BaseStream, CompressionMode.Decompress))
using (var outputStream = new StreamReader(innerStream))
{
string line;
while ((line = outputStream.ReadLine()) != null)
{
yield return line;
}
}
}
/// <summary>
/// Parse order book information for Kaiko data files
/// </summary>
/// <param name="rawDataLines">The raw data lines.</param>
/// <returns>
/// IEnumerable of ticks representing the Kaiko data
/// </returns>
private IEnumerable<Tick> ParseKaikoQuoteFile(IEnumerable<string> rawDataLines)
{
var headerLine = rawDataLines.First();
var headerCsv = headerLine.ToCsv();
var typeColumn = headerCsv.FindIndex(x => x == "type");
var dateColumn = headerCsv.FindIndex(x => x == "date");
var priceColumn = headerCsv.FindIndex(x => x == "price");
var quantityColumn = headerCsv.FindIndex(x => x == "amount");
long currentEpoch = 0;
var currentEpochTicks = new List<KaikoTick>();
foreach (var line in rawDataLines.Skip(1))
{
if (line == null || line == string.Empty) continue;
var lineParts = line.Split(',');
var tickEpoch = Parse.Long(lineParts[dateColumn]);
decimal quantity;
decimal price;
try
{
quantity = ParseScientificNotationToDecimal(lineParts, quantityColumn);
price = ParseScientificNotationToDecimal(lineParts, priceColumn);
}
catch (Exception ex)
{
Log.Error($"KaikoDataConverter.ParseKaikoQuoteFile(): Raw data corrupted. Line {string.Join(" ", lineParts)}, Exception {ex}");
continue;
}
var currentTick = new KaikoTick
{
TickType = TickType.Quote,
Time = Time.UnixMillisecondTimeStampToDateTime(tickEpoch),
Quantity = quantity,
Value = price,
OrderDirection = lineParts[typeColumn]
};
if (currentEpoch != tickEpoch)
{
var quoteTick = CreateQuoteTick(Time.UnixMillisecondTimeStampToDateTime(currentEpoch), currentEpochTicks);
if (quoteTick != null) yield return quoteTick;
currentEpochTicks.Clear();
currentEpoch = tickEpoch;
}
currentEpochTicks.Add(currentTick);
}
}
/// <summary>
/// Take a minute snapshot of order book information and make a single Lean quote tick
/// </summary>
/// <param name="date">The data being processed</param>
/// <param name="currentEpcohTicks">The snapshot of bid/ask Kaiko data</param>
/// <returns>A single Lean quote tick</returns>
private Tick CreateQuoteTick(DateTime date, List<KaikoTick> currentEpcohTicks)
{
// lowest ask
var bestAsk = currentEpcohTicks.Where(x => x.OrderDirection == "a")
.OrderBy(x => x.Value)
.FirstOrDefault();
// highest bid
var bestBid = currentEpcohTicks.Where(x => x.OrderDirection == "b")
.OrderByDescending(x => x.Value)
.FirstOrDefault();
if (bestAsk == null && bestBid == null)
{
// Did not have enough data to create a tick
return null;
}
var tick = new Tick()
{
Symbol = _symbol,
Time = date,
TickType = TickType.Quote
};
if (bestBid != null)
{
tick.BidPrice = bestBid.Price;
tick.BidSize = bestBid.Quantity;
}
if (bestAsk != null)
{
tick.AskPrice = bestAsk.Price;
tick.AskSize = bestAsk.Quantity;
}
return tick;
}
/// <summary>
/// Parse a kaiko trade file
/// </summary>
/// <param name="unzippedFile">The path to the unzipped file</param>
/// <returns>Lean Ticks in the Kaiko file</returns>
private IEnumerable<Tick> ParseKaikoTradeFile(IEnumerable<string> rawDataLines)
{
var headerLine = rawDataLines.First();
var headerCsv = headerLine.ToCsv();
var dateColumn = headerCsv.FindIndex(x => x == "date");
var priceColumn = headerCsv.FindIndex(x => x == "price");
var quantityColumn = headerCsv.FindIndex(x => x == "amount");
foreach (var line in rawDataLines.Skip(1))
{
if (line == null || line == string.Empty) continue;
var lineParts = line.Split(',');
decimal quantity;
decimal price;
try
{
quantity = ParseScientificNotationToDecimal(lineParts, quantityColumn);
price = ParseScientificNotationToDecimal(lineParts, priceColumn);
}
catch (Exception ex)
{
Log.Error($"KaikoDataConverter.ParseKaikoTradeFile(): Raw data corrupted. Line {string.Join(" ", lineParts)}, Exception {ex}");
continue;
}
yield return new Tick
{
Symbol = _symbol,
TickType = TickType.Trade,
Time = Time.UnixMillisecondTimeStampToDateTime(Parse.Long(lineParts[dateColumn])),
Quantity = quantity,
Value = price
};
}
}
/// <summary>
/// Parse the quantity field of the kaiko ticks - can sometimes be expressed in scientific notation
/// </summary>
/// <param name="lineParts">The line from the Kaiko file</param>
/// <param name="column">The index of the quantity column </param>
/// <returns>The quantity as a decimal</returns>
private static decimal ParseScientificNotationToDecimal(string[] lineParts, int column)
{
var value = lineParts[column];
if (value.Contains("e"))
{
return Parse.Decimal(value, NumberStyles.Float);
}
return lineParts[column].ConvertInvariant<decimal>();
}
/// <summary>
/// Simple class to add order direction to Tick
/// used for aggregating Kaiko order book snapshots
/// </summary>
private class KaikoTick : Tick
{
public string OrderDirection { get; set; }
}
}
}