-
Notifications
You must be signed in to change notification settings - Fork 31
/
MainWindow.xaml.cs
648 lines (533 loc) · 20 KB
/
MainWindow.xaml.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
namespace StockSharp.Qsh2StockSharp
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using DevExpress.Xpf.Core;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using Ecng.Xaml;
using MoreLinq;
using QScalp;
using QScalp.History;
using QScalp.History.Reader;
using StockSharp.Algo;
using StockSharp.Algo.Storages;
using StockSharp.BusinessEntities;
using StockSharp.Localization;
using StockSharp.Logging;
using StockSharp.Messages;
using StockSharp.Plaza;
using StockSharp.Xaml;
public partial class MainWindow
{
private class Settings
{
public string QshFolder { get; set; }
public string StockSharpFolder { get; set; }
public StorageFormats Format { get; set; }
public string Board { get; set; }
public string SecurityLike { get; set; }
public bool MultiThread { get; set; }
public bool OrderLog2OrderBook { get; set; }
public int OrderBookMaxDepth { get; set; } = 5;
public string TimeStampZone { get; set; }
public string MarketDataZone { get; set; }
}
private readonly LogManager _logManager = new LogManager();
private bool _isStarted;
private DateTimeOffset _startConvertTime;
private const string _settingsDir = "Settings";
private static readonly string _settingsFile = Path.Combine(_settingsDir, "settings.xml");
private static readonly string _convertedFilesFile = Path.Combine(_settingsDir, "converted_files.txt");
private readonly HashSet<string> _convertedFiles = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
private readonly HashSet<string> _convertedPerTaskPoolFiles = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
public MainWindow()
{
InitializeComponent();
Title = TypeHelper.ApplicationNameWithVersion;
ApplicationThemeHelper.ApplicationThemeName = ThemeExtensions.DefaultTheme;
Directory.CreateDirectory(_settingsDir);
TimeStampZone.TimeZone = TimeZoneInfo.Utc;
MarketDataZone.TimeZone = TimeHelper.Moscow;
_logManager.Application.LogLevel = LogLevels.Verbose;
_logManager.Listeners.Add(new GuiLogListener(LogControl));
_logManager.Listeners.Add(new FileLogListener { LogDirectory = "Logs", SeparateByDates = SeparateByDateModes.FileName });
Format.SetDataSource<StorageFormats>();
Format.SetSelectedValue<StorageFormats>(StorageFormats.Binary);
Board.Boards.AddRange(ExchangeBoard.EnumerateExchangeBoards().Where(b => b.Exchange == Exchange.Moex));
Board.SelectedBoard = ExchangeBoard.Forts;
try
{
if (File.Exists(_settingsFile))
{
var settings = new XmlSerializer<Settings>().Deserialize(_settingsFile);
QshFolder.Folder = settings.QshFolder;
StockSharpFolder.Folder = settings.StockSharpFolder;
Format.SetSelectedValue<StorageFormats>(settings.Format);
SecurityLike.Text = settings.SecurityLike;
MultiThread.IsChecked = settings.MultiThread;
OrderLog2OrderBook.IsChecked = settings.OrderLog2OrderBook;
Board.SelectedBoard =
settings.Board.IsEmpty()
? ExchangeBoard.Forts
: Board.Boards.FirstOrDefault(b => b.Code.CompareIgnoreCase(settings.Board)) ?? ExchangeBoard.Forts;
if (!settings.TimeStampZone.IsEmpty())
TimeStampZone.TimeZone = TimeZoneInfo.FindSystemTimeZoneById(settings.TimeStampZone);
if (!settings.MarketDataZone.IsEmpty())
MarketDataZone.TimeZone = TimeZoneInfo.FindSystemTimeZoneById(settings.MarketDataZone);
}
if (File.Exists(_convertedFilesFile))
{
_convertedFiles.AddRange(File.ReadAllLines(_convertedFilesFile));
}
}
catch (Exception ex)
{
ex.LogError();
}
}
protected override void OnClosed(EventArgs e)
{
SaveSettings();
base.OnClosed(e);
}
private Settings SaveSettings()
{
var settings = new Settings
{
QshFolder = QshFolder.Folder,
StockSharpFolder = StockSharpFolder.Folder,
Format = Format.GetSelectedValue<StorageFormats>() ?? StorageFormats.Binary,
SecurityLike = SecurityLike.Text,
Board = Board.SelectedBoard?.Code,
MultiThread = MultiThread.IsChecked == true,
OrderLog2OrderBook = OrderLog2OrderBook.IsEnabled && OrderLog2OrderBook.IsChecked == true,
TimeStampZone = TimeStampZone.TimeZone.Id,
MarketDataZone = MarketDataZone.TimeZone.Id,
};
try
{
new XmlSerializer<Settings>().Serialize(settings, _settingsFile);
}
catch (Exception ex)
{
ex.LogError();
}
return settings;
}
private void LockControls(bool isEnabled)
{
FoldersGrid.IsEnabled = StorageSettingsBox.IsEnabled = SecSettingsBox.IsEnabled = isEnabled;
}
private void Convert_OnClick(object sender, RoutedEventArgs e)
{
Convert.IsEnabled = false;
if (_isStarted)
{
_logManager.Application.AddInfoLog("Остановка конвертации.");
_isStarted = false;
return;
}
LockControls(false);
_logManager.Application.AddInfoLog("Запуск конвертации.");
_isStarted = true;
var settings = SaveSettings();
var board = Board.SelectedBoard;
var orderLog2OrderBookBuilders = settings.OrderLog2OrderBook ? new Dictionary<SecurityId, IOrderLogMarketDepthBuilder>() : null;
var tz = TimeStampZone.TimeZone;
var mz = MarketDataZone.TimeZone;
Task.Factory.StartNew(() =>
{
var registry = new StorageRegistry();
((LocalMarketDataDrive)registry.DefaultDrive).Path = settings.StockSharpFolder;
this.GuiAsync(() =>
{
Convert.Content = LocalizedStrings.Str2890;
Convert.IsEnabled = true;
});
_startConvertTime = DateTimeOffset.Now;
ConvertDirectory(settings.QshFolder, registry, settings.Format, board, settings.SecurityLike, settings.MultiThread, orderLog2OrderBookBuilders, settings.OrderBookMaxDepth, tz, mz);
})
.ContinueWith(t =>
{
Convert.Content = LocalizedStrings.Str2932;
Convert.IsEnabled = true;
LockControls(true);
if (t.IsFaulted)
{
t.Exception.LogError();
new MessageBoxBuilder()
.Text("В процессе конвертации произошла ошибка. Ошибка записана в лог.")
.Error()
.Owner(this)
.Show();
return;
}
var text = "Конвертация {0} {1}.".Put(_isStarted ? "выполнена за" : "остановлена через",
(DateTimeOffset.Now - _startConvertTime).ToString("g"));
_logManager.Application.AddInfoLog(text);
new MessageBoxBuilder()
.Text(text)
.Owner(this)
.Show();
_isStarted = false;
}, TaskScheduler.FromCurrentSynchronizationContext());
}
private void ConvertDirectory(string path, IStorageRegistry registry, StorageFormats format, ExchangeBoard board, string securityLike, bool multithread, Dictionary<SecurityId, IOrderLogMarketDepthBuilder> orderLog2OrderBookBuilders, int orderBookMaxDepth, TimeZoneInfo tz, TimeZoneInfo mz)
{
if (!_isStarted)
return;
var files = Directory.GetFiles(path, "*.qsh");
if (!multithread)
files.ForEach(f => ConvertFile(f, registry, format, board, securityLike, orderLog2OrderBookBuilders, orderBookMaxDepth, tz, mz));
else
{
Parallel.ForEach(files, file => ConvertFile(file, registry, format, board, securityLike, orderLog2OrderBookBuilders, orderBookMaxDepth, tz, mz));
}
//пишем имена сконвертированных в деректории файлов qsh, в файл
File.AppendAllLines(_convertedFilesFile, _convertedPerTaskPoolFiles);
_convertedPerTaskPoolFiles.Clear();
Directory.GetDirectories(path).ForEach(d => ConvertDirectory(d, registry, format, board, securityLike, multithread, orderLog2OrderBookBuilders, orderBookMaxDepth, tz, mz));
}
private void TryFlushData<TMessage>(IStorageRegistry registry, SecurityId securityId, StorageFormats format, object arg, List<TMessage> messages, QshReader reader)
where TMessage : Messages.Message
{
const int maxBufCount = 1000;
if (messages.Count <= maxBufCount)
return;
_logManager.Application.AddDebugLog("Файл прочитан на {1}%.", messages.Count, (reader.FilePosition * 100) / reader.FileSize);
registry.GetStorage(securityId, typeof(TMessage), arg, null, format).Save(messages);
messages.Clear();
}
private void ConvertFile(string fileName, IStorageRegistry registry, StorageFormats format, ExchangeBoard board, string securityLike, Dictionary<SecurityId, IOrderLogMarketDepthBuilder> orderLog2OrderBookBuilders, int orderBookMaxDepth, TimeZoneInfo tz, TimeZoneInfo mz)
{
if (!_isStarted)
return;
var fileNameKey = format + "_" + fileName;
if (_convertedFiles.Contains(fileNameKey))
return;
_logManager.Application.AddInfoLog("Начата конвертация файла {0}.", fileName);
var securitiesStrings = securityLike.Split(",");
var data = new Dictionary<SecurityId, Tuple<List<QuoteChangeMessage>, List<ExecutionMessage>, List<Level1ChangeMessage>, List<ExecutionMessage>>>();
DateTimeOffset ToLocalDto(DateTime dt) => dt.ApplyTimeZone(tz);
DateTimeOffset ToServerDto(DateTime dt) => dt.ApplyTimeZone(mz);
using (var qr = QshReader.Open(fileName))
{
var reader = qr;
for (var i = 0; i < qr.StreamCount; i++)
{
var stream = (ISecurityStream)qr[i];
var securityId = GetSecurityId(stream.Security, board.Code);
var priceStep = (decimal)stream.Security.Step;
var lastTransactionId = 0L;
var builder = orderLog2OrderBookBuilders?.SafeAdd(securityId, key => new PlazaOrderLogMarketDepthBuilder(key));
if (securitiesStrings.Length > 0)
{
var secCode = securityId.SecurityCode;
var streamContainsSecurityFromMask = securitiesStrings.Any(mask =>
{
var isEndMulti = mask.EndsWith("*");
var isStartMulti = mask.StartsWith("*");
if (isStartMulti)
mask = mask.Substring(1);
if (isEndMulti)
mask = mask.Substring(0, mask.Length - 1);
if (isEndMulti)
{
if (isStartMulti)
return secCode.ContainsIgnoreCase(mask);
else
return secCode.StartsWith(mask, StringComparison.InvariantCultureIgnoreCase);
}
else if (isStartMulti)
return secCode.EndsWith(mask, StringComparison.InvariantCultureIgnoreCase);
else
return secCode.CompareIgnoreCase(mask);
});
if (!streamContainsSecurityFromMask)
continue;
}
var secData = data.SafeAdd(securityId, key => Tuple.Create(new List<QuoteChangeMessage>(), new List<ExecutionMessage>(), new List<Level1ChangeMessage>(), new List<ExecutionMessage>()));
switch (stream.Type)
{
case StreamType.Quotes:
{
((IQuotesStream)stream).Handler += quotes =>
{
var bids = new List<QuoteChange>();
var asks = new List<QuoteChange>();
foreach (var q in quotes)
{
switch (q.Type)
{
//case QuoteType.Unknown:
//case QuoteType.Free:
//case QuoteType.Spread:
// throw new ArgumentException(q.Type.ToString());
case QuoteType.Ask:
case QuoteType.BestAsk:
asks.Add(new QuoteChange(priceStep * q.Price, q.Volume));
break;
case QuoteType.Bid:
case QuoteType.BestBid:
bids.Add(new QuoteChange(priceStep * q.Price, q.Volume));
break;
default:
{
continue;
//throw new ArgumentException(q.Type.ToString());
}
}
}
var md = new QuoteChangeMessage
{
LocalTime = ToLocalDto(reader.CurrentDateTime),
SecurityId = securityId,
ServerTime = ToLocalDto(reader.CurrentDateTime),
Bids = bids.ToArray(),
Asks = asks.ToArray(),
};
//if (md.Verify())
//{
secData.Item1.Add(md);
TryFlushData(registry, securityId, format, null, secData.Item1, reader);
//}
//else
// _logManager.Application.AddErrorLog("Стакан для {0} в момент {1} не прошел валидацию. Лучший бид {2}, Лучший офер {3}.", security, qr.CurrentDateTime, md.BestBid, md.BestAsk);
};
break;
}
case StreamType.Deals:
{
((IDealsStream)stream).Handler += deal =>
{
secData.Item2.Add(new ExecutionMessage
{
LocalTime = ToLocalDto(reader.CurrentDateTime),
HasTradeInfo = true,
ExecutionType = ExecutionTypes.Tick,
SecurityId = securityId,
OpenInterest = deal.OI == 0 ? (long?)null : deal.OI,
ServerTime = ToServerDto(deal.DateTime),
TradeVolume = deal.Volume,
TradeId = deal.Id == 0 ? (long?)null : deal.Id,
TradePrice = deal.Price * priceStep,
OriginSide =
deal.Type == DealType.Buy
? Sides.Buy
: (deal.Type == DealType.Sell ? Sides.Sell : (Sides?)null)
});
TryFlushData(registry, securityId, format, ExecutionTypes.Tick, secData.Item2, reader);
};
break;
}
case StreamType.OrdLog:
{
((IOrdLogStream)stream).Handler += ol =>
{
var currTransactionId = ol.DateTime.Ticks;
if (lastTransactionId < currTransactionId)
lastTransactionId = currTransactionId;
else if (lastTransactionId >= currTransactionId)
lastTransactionId++;
var msg = new ExecutionMessage
{
LocalTime = ToLocalDto(reader.CurrentDateTime),
ExecutionType = ExecutionTypes.OrderLog,
SecurityId = securityId,
OpenInterest = ol.OI == 0 ? (long?)null : ol.OI,
OrderId = ol.OrderId,
OrderPrice = priceStep * ol.Price,
ServerTime = ToServerDto(ol.DateTime),
OrderVolume = ol.Amount,
Balance = ol.AmountRest,
TradeId = ol.DealId == 0 ? (long?)null : ol.DealId,
TradePrice = ol.DealPrice == 0 ? (decimal?)null : priceStep * ol.DealPrice,
TransactionId = lastTransactionId
};
var status = 0;
if (ol.Flags.Contains(OrdLogFlags.Add))
{
msg.OrderState = OrderStates.Active;
}
else if (ol.Flags.Contains(OrdLogFlags.Fill))
{
msg.OrderState = OrderStates.Done;
}
else if (ol.Flags.Contains(OrdLogFlags.Canceled))
{
msg.OrderState = OrderStates.Done;
status |= 0x200000;
}
else if (ol.Flags.Contains(OrdLogFlags.CanceledGroup))
{
msg.OrderState = OrderStates.Done;
status |= 0x400000;
}
else if (ol.Flags.Contains(OrdLogFlags.Moved))
{
status |= 0x100000;
}
if (ol.Flags.Contains(OrdLogFlags.Buy))
{
msg.Side = Sides.Buy;
}
else if (ol.Flags.Contains(OrdLogFlags.Sell))
{
msg.Side = Sides.Sell;
}
if (ol.Flags.Contains(OrdLogFlags.FillOrKill))
{
msg.TimeInForce = TimeInForce.MatchOrCancel;
status |= 0x00080000;
}
if (ol.Flags.Contains(OrdLogFlags.Quote))
{
msg.TimeInForce = TimeInForce.PutInQueue;
status |= 0x01;
}
if (ol.Flags.Contains(OrdLogFlags.Counter))
{
status |= 0x02;
}
if (ol.Flags.Contains(OrdLogFlags.CrossTrade))
{
status |= 0x20000000;
}
if (ol.Flags.Contains(OrdLogFlags.NonSystem))
{
msg.IsSystem = false;
status |= 0x04;
}
if (ol.Flags.Contains(OrdLogFlags.EndOfTransaction))
{
status |= 0x1000;
}
msg.OrderStatus = status;
if (builder == null)
{
secData.Item4.Add(msg);
TryFlushData(registry, securityId, format, ExecutionTypes.OrderLog, secData.Item4, reader);
}
else
{
//if (builder.Depth.Bids.Any() || builder.Depth.Asks.Any() || msg.ServerTime.TimeOfDay >= new TimeSpan(0, 18, 45, 00, 1))
{
QuoteChangeMessage updated;
try
{
updated = builder.Update(msg);
}
catch
{
updated = null;
}
if (updated != null)
{
secData.Item1.Add(updated);
TryFlushData(registry, securityId, format, null, secData.Item1, reader);
}
}
}
};
break;
}
case StreamType.AuxInfo:
{
((IAuxInfoStream)stream).Handler += info =>
{
var l1Msg = new Level1ChangeMessage
{
LocalTime = ToLocalDto(reader.CurrentDateTime),
SecurityId = securityId,
ServerTime = ToLocalDto(reader.CurrentDateTime),
}
.TryAdd(Level1Fields.LastTradePrice, priceStep * info.Price)
.TryAdd(Level1Fields.BidsVolume, (decimal)info.BidTotal)
.TryAdd(Level1Fields.AsksVolume, (decimal)info.AskTotal)
.TryAdd(Level1Fields.HighPrice, priceStep * info.HiLimit)
.TryAdd(Level1Fields.LowPrice, priceStep * info.LoLimit)
.TryAdd(Level1Fields.StepPrice, (decimal)info.Rate)
.TryAdd(Level1Fields.OperatingMargin, (decimal)info.Deposit)
.TryAdd(Level1Fields.OpenInterest, (decimal)info.OI);
if (l1Msg.Changes.Count == 0)
return;
secData.Item3.Add(l1Msg);
TryFlushData(registry, securityId, format, null, secData.Item3, reader);
};
break;
}
case StreamType.OwnOrders:
case StreamType.OwnTrades:
case StreamType.Messages:
case StreamType.None:
{
continue;
}
default:
throw new ArgumentOutOfRangeException("Неподдерживаемый тип потока {0}.".Put(stream.Type));
}
}
if (data.Count > 0)
{
while (qr.CurrentDateTime != DateTime.MaxValue && _isStarted)
qr.Read(true);
}
}
if (!_isStarted)
return;
foreach (var pair in data)
{
if (pair.Value.Item1.Any())
{
registry.GetQuoteMessageStorage(pair.Key, registry.DefaultDrive, format).Save(pair.Value.Item1);
}
if (pair.Value.Item2.Any())
{
registry.GetTickMessageStorage(pair.Key, registry.DefaultDrive, format).Save(pair.Value.Item2);
}
if (pair.Value.Item3.Any())
{
registry.GetLevel1MessageStorage(pair.Key, registry.DefaultDrive, format).Save(pair.Value.Item3);
}
if (pair.Value.Item4.Any())
{
registry.GetOrderLogMessageStorage(pair.Key, registry.DefaultDrive, format).Save(pair.Value.Item4);
}
}
if (data.Count > 0)
{
//File.AppendAllLines(_convertedFilesFile, new[] { fileNameKey });
_convertedFiles.Add(fileNameKey);
_convertedPerTaskPoolFiles.Add(fileNameKey);
}
_logManager.Application.AddInfoLog("Завершена конвертация файла {0}.", fileName);
}
private static SecurityId GetSecurityId(QScalp.Security security, string boardCode)
{
return new SecurityId
{
SecurityCode = security.Ticker,
BoardCode = boardCode,
};
}
private void TryEnable()
{
Convert.IsEnabled = !QshFolder.Folder.IsEmpty() && !StockSharpFolder.Folder.IsEmpty() && Board.SelectedBoard != null;
}
private void Board_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
TryEnable();
}
private void OnFolderChanged(string folder)
{
TryEnable();
}
}
}