forked from ssg100/ninjaindicators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GomFileManager.cs
1406 lines (1108 loc) · 30.9 KB
/
GomFileManager.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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define FORCEFLUSH
//#undef FORCEFLUSH
using System;
using System.ComponentModel;
using System.IO;
using System.Globalization;
using System.Text;
using System.Diagnostics;
using System.Reflection;
namespace Gom
{
abstract partial class FileManager : IDataManager, IDisposable
{
#region FileManager
#region Backward reader
//class to read file backwards. found it on
//http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/9acdde1a-03cd-4018-9f87-6e201d8f5d09
//we need it to find last written date without parsing all the file
public class GomBackwardReader : IDisposable
{
private FileStream fs = null;
public GomBackwardReader(string path)
{
fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
fs.Seek(0, SeekOrigin.End);
}
public string Readline()
{
byte[] line;
byte[] text = new byte[1];
long position = 0;
int count;
fs.Seek(0, SeekOrigin.Current);
position = fs.Position;
//do we have trailing \r\n?
if (fs.Length > 1)
{
byte[] vagnretur = new byte[2];
fs.Seek(-2, SeekOrigin.Current);
fs.Read(vagnretur, 0, 2);
if (ASCIIEncoding.ASCII.GetString(vagnretur).Equals("\r\n"))
{
//move it back
fs.Seek(-2, SeekOrigin.Current);
position = fs.Position;
}
}
while (fs.Position > 0)
{
text.Initialize();
//read one char
fs.Read(text, 0, 1);
string asciiText = ASCIIEncoding.ASCII.GetString(text);
//moveback to the charachter before
fs.Seek(-2, SeekOrigin.Current);
if (asciiText.Equals("\n"))
{
fs.Read(text, 0, 1);
asciiText = ASCIIEncoding.ASCII.GetString(text);
if (asciiText.Equals("\r"))
{
fs.Seek(1, SeekOrigin.Current);
break;
}
}
}
count = int.Parse((position - fs.Position).ToString());
line = new byte[count];
fs.Read(line, 0, count);
fs.Seek(-count, SeekOrigin.Current);
return ASCIIEncoding.ASCII.GetString(line);
}
public bool SOF { get { return fs.Position == 0; } }
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (fs != null)
{
fs.Close();
fs.Dispose();
fs = null;
}
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
#endregion
abstract public string Name { get; }
virtual public bool IsWritable { get { return false; } }
virtual public bool IsMillisecCompliant { get { return false; } }
public const string dateformat = "yyMMddHHmmss";
public const string dateformatmillisec = "yyMMddHHmmssfff";
public string[] dateformats = { dateformat, dateformatmillisec };
protected CultureInfo curCulture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
private string _InstrName;
private bool _writeData;
private string _converterFileName;
private Gom.FileModeType _fileMode;
private bool _writeOK = true;
protected bool _useMillisec;
protected long curDateGMTTicks;
protected long newDateGMTTicks;
protected DateTime curReadDate = Gom.Utils.nullDT;
protected double _tickSize;
protected bool _isBinary;
private long _lastTimeInFile;
protected StreamReader sr;
protected StreamWriter sw;
protected BinaryReader br;
protected BinaryWriter bw;
protected string _filename="";
// FILE MANAGEMENT
#region File Management
private void initWrite()
{
freewriter();
_filename = GetFileName(new DateTime(curDateGMTTicks),false);
if (!File.Exists(_filename))
{
FileStream fs = File.Create(_filename);
fs.Close();
}
if (_writeData && IsWritable)
{
try
{
if (_isBinary)
bw = new BinaryWriter(File.Open(_filename, FileMode.Append, FileAccess.Write, FileShare.Read));
else
sw = new StreamWriter(File.Open(_filename, FileMode.Append, FileAccess.Write, FileShare.Read));
_writeOK = true;
}
catch (IOException)
{
_writeOK = false;
}
}
if (_writeOK)
_lastTimeInFile = GetMaxTimeInFile().Ticks;
else
_lastTimeInFile = DateTime.MaxValue.Ticks;
}
private bool initread()
{
bool found=false;
freereader();
if (!String.IsNullOrEmpty(_filename))
{
try
{
if (_isBinary)
br = new BinaryReader(File.Open(_filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
else
sr = new StreamReader(File.Open(_filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
found = true;
}
catch (IOException)
{
found = false;
}
}
return (found);
}
private bool FindFileNameAndOpen(DateTime date)
{
bool found = false;
if (_fileMode == Gom.FileModeType.SingleFile)
{
_filename = GetFileName(Gom.Utils.nullDT);
found = initread();
}
else
{
while (date <= DateTime.Now.ToUniversalTime().Date)
{
_filename = GetFileName(date);
if (File.Exists(_filename))
{
FileInfo f = new FileInfo(_filename);
if (f.Length > 0)
{
curReadDate = date;
found = initread();
break;
}
}
date = date.AddDays(1);
}
}
return found;
}
protected bool ManageFileChange()
{
bool found = false;
freereader();
if (_fileMode == Gom.FileModeType.OnePerDay)
found = FindFileNameAndOpen(curReadDate.AddDays(1));
return found;
}
private string GetFileName(DateTime date)
{
string FileName=GetFileName(date,false);
if (!File.Exists(FileName))
FileName = GetFileName(date,true);
return FileName;
}
private string GetFileName(DateTime date, bool addinstrfolder)
{
if (_converterFileName == null)
{
//file name is instrument name
string nomfic=_InstrName;
char[] invalidFileChars = Path.GetInvalidFileNameChars();
// remove invalid chars (*,/,\ etx)
foreach (char invalidFChar in invalidFileChars)
nomfic = nomfic.Replace(invalidFChar.ToString(), "");
if (_fileMode == Gom.FileModeType.OnePerDay)
nomfic += "." + date.ToString("yyyyMMdd");
string extension;
if (_isBinary)
extension = ".dat";
else
extension = ".txt";
string folder = Environment.GetEnvironmentVariable("GOMFOLDER");
if (folder == null)
folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (addinstrfolder)
folder+="\\"+_InstrName;
return (folder + "\\" + nomfic + "." + Name + extension);
}
else
return _converterFileName;
}
#endregion
//Helpers
#region Helpers
public bool RecordTick(DateTime date, TickTypeEnum tickType, double price, int volume)
{
DateTime newDateTimeGMT = date.ToUniversalTime();
newDateGMTTicks = newDateTimeGMT.Date.Ticks;
if (((_fileMode == Gom.FileModeType.OnePerDay) && (_writeOK) && (newDateGMTTicks > curDateGMTTicks)) || (curDateGMTTicks == 0))
{
curDateGMTTicks = newDateGMTTicks;
initWrite();
}
if ((_writeOK)&&(newDateTimeGMT.Ticks > _lastTimeInFile))
RecordTickGMT(newDateTimeGMT, tickType, price, volume);
return _writeOK;
}
protected void SwapCulture()
{
if (curCulture.NumberFormat.NumberDecimalSeparator == ".")
curCulture.NumberFormat.NumberDecimalSeparator = ",";
else
curCulture.NumberFormat.NumberDecimalSeparator = ".";
}
#endregion
// IDataManager
#region IDataManager
public bool RecordTick(DateTime date, double bid, double ask, double price, int volume)
{
return RecordTick(date, Gom.Utils.GetIntTickType(bid, ask, price), price, volume);
}
public abstract void GetNextTick(ref MarketDataType gomData);
public void SetCursorTime(DateTime time0, ref MarketDataType gomData)
{
bool found = false;
gomData.Time=Gom.Utils.nullDT;
long time0tick = time0.Ticks;
long ticktime;
found = FindFileNameAndOpen(time0.ToUniversalTime().Date);
if (found)
do
{
GetNextTick(ref gomData);
ticktime = gomData.Time.Ticks;
}
while ((ticktime != 0L) && (ticktime < time0tick));
}
#endregion
//IDisposable
#region IDisposable
private void freereader()
{
if (br != null)
{
br.Close();
br = null;
}
if (sr != null)
{
sr.Close();
sr.Dispose();
sr = null;
}
}
private void freewriter()
{
if (bw != null)
{
bw.Flush();
bw.Close();
bw = null;
}
if (sw != null)
{
sw.Flush();
sw.Close();
sw.Dispose();
sw = null;
}
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
freereader();
freewriter();
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
// Virtual Methods
virtual public void RecordTickGMT(DateTime date, TickTypeEnum tickType, double price, int volume)
{ }
virtual protected DateTime GetMaxTimeInFile()
{
return Gom.Utils.nullDT;
}
//Contructors
#region constructors
public FileManager()
{
curCulture.NumberFormat.NumberGroupSeparator = "";
}
public FileManager(bool isInstr, string name, double tickSize, bool writeData, Gom.FileModeType fileMode)
: this()
{
if (isInstr)
_InstrName = name;
else
_converterFileName = name;
_tickSize = tickSize;
_writeData = writeData;
_fileMode = fileMode;
}
public FileManager(string filename)
: this()
{
_converterFileName = filename;
_writeData = false;
_fileMode = Gom.FileModeType.SingleFile;
}
#endregion
}
#endregion
#region Flat
class FileManagerFlat : FileManager //YYMMAAHHMMSS \t TickType \t Price \t Volume
{
public override string Name { get { return "Flat"; } }
public override bool IsWritable { get { return true; } }
public override bool IsMillisecCompliant { get { return true; } }
public FileManagerFlat() : base() { }
public FileManagerFlat(bool isInstr, string name, double tickSize, bool writedata, Gom.FileModeType fileMode)
: base(isInstr, name, tickSize, writedata, fileMode)
{
_useMillisec = true;
}
public override void RecordTickGMT(DateTime time, TickTypeEnum tickType, double price, int volume)
{
sw.WriteLine(time.ToString((_useMillisec)?dateformatmillisec:dateformat) + "\t" + (int)tickType + "\t" + price.ToString("G10", CultureInfo.InvariantCulture) + "\t" + volume);
#if FORCEFLUSH
sw.Flush();
#endif
}
private string GetNextLinePivotFormatted()
{
string retString = null;
if (!sr.EndOfStream)
{
retString = sr.ReadLine();
}
return retString;
}
public override void GetNextTick(ref MarketDataType gomData)
{
string retString = GetNextLinePivotFormatted();
if (retString != null)
{
string[] split = retString.Split('\t');
gomData.Time = DateTime.ParseExact(split[0], dateformats, CultureInfo.InvariantCulture, DateTimeStyles.None).ToLocalTime();
gomData.TickType = (TickTypeEnum)Enum.Parse(typeof(TickTypeEnum), split[1]);
try
{
gomData.Price = Double.Parse(split[2], curCulture);
}
catch (FormatException)
{
SwapCulture();
gomData.Price = Double.Parse(split[2], curCulture);
}
gomData.Volume = Int32.Parse(split[3]);
}
else
{
gomData.Time = Gom.Utils.nullDT;
if (ManageFileChange())
GetNextTick(ref gomData);
}
}
protected override DateTime GetMaxTimeInFile()
{
DateTime retTime = new DateTime(0L);
string stringRead, lastTimeInFileString = "";
using (GomBackwardReader br = new GomBackwardReader(_filename))
{
if (!br.SOF)
{
stringRead = br.Readline();
if ((stringRead != null) && (stringRead.Length > 12))
{
lastTimeInFileString = stringRead.Substring(0, 12); // get the date
retTime = DateTime.ParseExact(lastTimeInFileString, dateformats, CultureInfo.CurrentCulture, DateTimeStyles.None);//.ToLocalTime();
}
}
}
return retTime;
}
}
#endregion
#region Short
// DYYMMAAHHMM00 \t Price on every minute
// SS TickType \t Delta per reference price of the minute in ticks \t \t Volume on every tick.
// SS is omitted is same as previous line
class FileManagerShort : FileManager
{
public override string Name { get { return "Short"; } }
public override bool IsWritable{ get { return true; } }
private int lastMinute = -1;
private int lastSecond;
private double pivotPrice;
private DateTime lastReadMinute = Gom.Utils.nullDT;
private DateTime lastReadSecond;
private double lastReadPivot;
public FileManagerShort() : base() { }
public FileManagerShort(bool isInstr, string name, double tickSize, bool writedata, Gom.FileModeType fileMode)
: base(isInstr, name, tickSize, writedata, fileMode) { }
public override void RecordTickGMT(DateTime time, TickTypeEnum tickType, double price, int volume)
{
bool newMinuteHappened = false;
int newMinute = Int32.Parse(time.ToString("yyMMddHHmm"));
if (newMinute != lastMinute)
{
sw.WriteLine(newMinute.ToString("D10") + "\t" + price.ToString("G10", CultureInfo.InvariantCulture));
lastMinute = newMinute;
pivotPrice = price;
newMinuteHappened = true;
}
if ((time.Second != lastSecond) || newMinuteHappened)
{
sw.WriteLine(time.Second.ToString("D2") + "\t" + (int)tickType + "\t" + Convert.ToInt32((price - pivotPrice) / _tickSize) + "\t" + volume);
lastSecond = time.Second;
}
else
sw.WriteLine((int)tickType + "\t" + Convert.ToInt32((price - pivotPrice) / _tickSize) + "\t" + volume);
#if FORCEFLUSH
sw.Flush();
#endif
}
public override void GetNextTick(ref MarketDataType gomData)
{
string readString = null;
string[] split;
gomData.Time = Gom.Utils.nullDT;
if (!sr.EndOfStream)
{
readString = sr.ReadLine();
if (readString != null)
{
split = readString.Split('\t');
if (split.Length == 2)
{
lastReadMinute = DateTime.ParseExact(split[0] + "00", dateformat, CultureInfo.InvariantCulture).ToLocalTime();
try
{
lastReadPivot = Double.Parse(split[1], curCulture);
}
catch (FormatException)
{
SwapCulture();
lastReadPivot = Double.Parse(split[1], curCulture);
}
GetNextTick(ref gomData);
return;
}
else if (split.Length == 4)
{
gomData.Price = Int32.Parse(split[2]) * _tickSize + lastReadPivot;
gomData.TickType = (TickTypeEnum)Enum.Parse(typeof(TickTypeEnum), split[1]);
gomData.Volume = Int32.Parse(split[3]);
lastReadSecond = lastReadMinute.AddSeconds(Int32.Parse(split[0])); ;
gomData.Time = lastReadSecond;
}
else if (split.Length == 3)
{
gomData.Price = Int32.Parse(split[1]) * _tickSize + lastReadPivot;
gomData.TickType = (TickTypeEnum)Enum.Parse(typeof(TickTypeEnum), split[0]);
gomData.Volume = Int32.Parse(split[2]);
gomData.Time = lastReadSecond;
}
}
}
else
{
if (ManageFileChange())
GetNextTick(ref gomData);
}
}
protected override DateTime GetMaxTimeInFile()
{
DateTime retTime = new DateTime(0L);
string stringRead, lastTimeInFileString = "";
string readSeconds;
string[] split;
using (GomBackwardReader br = new GomBackwardReader(_filename))
{
if (!br.SOF)
{
do
{
stringRead = br.Readline();
split = stringRead.Split('\t');
}
while ((!br.SOF) && (split.Length != 4));
if (!br.SOF)
{
readSeconds = split[0];
do
{
stringRead = br.Readline();
split = stringRead.Split('\t');
}
while ((!br.SOF) && (split.Length != 2));
if (!br.SOF)
{
split = stringRead.Split('\t');
lastTimeInFileString = split[0] + readSeconds;
retTime = DateTime.ParseExact(lastTimeInFileString, dateformat, CultureInfo.CurrentCulture);//.ToLocalTime();
}
}
}
}
return retTime;
}
}
#endregion
#region Binary
class FileManagerBinary : FileManager
{
public override string Name { get {return "Binary";}}
public override bool IsWritable { get { return true; } }
private int lastMinute = -1;
private int lastSecond;
private double pivotPrice;
private DateTime lastReadMinute = Gom.Utils.nullDT;
private DateTime lastReadSecond;
private double lastReadPivot;
public FileManagerBinary()
: base()
{
_isBinary = true;
}
public FileManagerBinary(bool isInstr, string name, double tickSize, bool writedata, Gom.FileModeType fileMode)
: base(isInstr, name, tickSize, writedata, fileMode)
{
_isBinary = true;
}
public void writedata(int second, TickTypeEnum tickType, double price, int volume, bool withsecond)
{
Byte statbyte;
Byte sec;
int diff;
if (withsecond)
statbyte = 3 << 6;
else
statbyte = 2 << 6;
statbyte += checked((Byte)((int)tickType << 3));
diff = Convert.ToInt32(((price - pivotPrice) / _tickSize));
if (diff >= -8 && diff <= +7 && volume <= 15)
statbyte += 7;
else
{
if ((diff > SByte.MaxValue) || (diff < SByte.MinValue))
statbyte += 1 << 2;
if (volume > UInt16.MaxValue)
statbyte += 2;
else if (volume > Byte.MaxValue)
statbyte += 1;
}
bw.Write(statbyte);
if (withsecond)
{
sec = checked((Byte)second);
bw.Write(sec);
}
// bw.Write((byte)0);
if (diff >= -8 && diff <= +7 && volume <= 15)
{
SByte res = checked((SByte)((SByte)(diff << 4) + volume));
bw.Write(res);
}
else
{
if ((diff > SByte.MaxValue) || (diff < SByte.MinValue))
{
Int16 res = checked((Int16)diff);
bw.Write(res);
}
else
{
SByte res = checked((SByte)diff);
bw.Write(res);
}
if (volume > UInt16.MaxValue)
{
Int32 res = checked((Int32)volume);
bw.Write(res);
}
else if (volume > Byte.MaxValue)
{
UInt16 res = checked((UInt16)volume);
bw.Write(res);
}
else
{
Byte res = checked((Byte)volume);
bw.Write(res);
}
}
}
public override void RecordTickGMT(DateTime time, TickTypeEnum tickType, double price, int volume)
{
bool newMinuteHappened = false;
//int newMinute = Int32.Parse(time.ToString("yyMMddHHmm"));
int newMinute = (time.Year - 2000) * 100000000 + time.Month * 1000000 + time.Day * 10000 + time.Hour * 100 + time.Minute;
if (newMinute != lastMinute)
{
Byte n1 = checked((Byte)1 << 6);
n1 += checked((Byte)(newMinute % 61));
bw.Write(n1);
UInt32 n2 = checked((UInt32)newMinute);
bw.Write(n2);
UInt32 n3 = checked(Convert.ToUInt32(price / _tickSize));
bw.Write(n3);
lastMinute = newMinute;
pivotPrice = price;
newMinuteHappened = true;
}
if ((time.Second != lastSecond) || newMinuteHappened)
{
writedata(time.Second, tickType, price, volume, true);
lastSecond = time.Second;
}
else
writedata(time.Second, tickType, price, volume, false);
#if FORCEFLUSH
bw.Flush();
#endif
}
public override void GetNextTick(ref MarketDataType gomData)
{
byte statbyte;
gomData.IsNewTimeStamp=Gom.MarketDataType.TimeStampStatus.Different;
try
{
statbyte = br.ReadByte();
}
catch (EndOfStreamException)
{
gomData.Time = Gom.Utils.nullDT;
if (ManageFileChange())
GetNextTick(ref gomData);
return;
}
if (statbyte >> 6 == 1)
{
lastReadMinute = DateTime.ParseExact(br.ReadUInt32().ToString("D10") + "00", dateformat, CultureInfo.InvariantCulture).ToLocalTime();
lastReadPivot = br.ReadUInt32() * _tickSize;
GetNextTick(ref gomData);
return;
}
else
{
if (statbyte >> 6 == 3)
lastReadSecond = lastReadMinute.AddSeconds(br.ReadByte());
else
gomData.IsNewTimeStamp=Gom.MarketDataType.TimeStampStatus.Same;
gomData.TickType = (TickTypeEnum)((statbyte & 56 /*00111000*/) >> 3);
gomData.Time = lastReadSecond;
if ((statbyte & 7 /*00000111*/ ) == 7)
{
SByte toto = br.ReadSByte();
gomData.Volume = toto & 15 /*00001111*/;
gomData.Price = lastReadPivot + ((SByte)(toto & 240 /*11110000*/ ) >> 4) * _tickSize;
}
else
{
if ((statbyte & 4 /*00000100*/) > 0)
gomData.Price = lastReadPivot + br.ReadInt16() * _tickSize;
else
gomData.Price = lastReadPivot + br.ReadSByte() * _tickSize;
if ((statbyte & 3 /*00000011*/) == 0)
gomData.Volume = br.ReadByte();
else if ((statbyte & 3 /*00000011*/) == 1)
gomData.Volume = br.ReadUInt16();
else if ((statbyte & 3 /*00000011*/) == 2)
gomData.Volume = br.ReadInt32();
}
}
}
protected override DateTime GetMaxTimeInFile()
{
DateTime retTime = Gom.Utils.nullDT;
string readTimeInFile;
using (FileStream fs = new FileStream(_filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
fs.Seek(0, SeekOrigin.End);
if (fs.Position == 0)
goto end;
byte[] data = new byte[5];
bool found = false;
do
{
fs.Seek(-6, SeekOrigin.Current);
fs.Read(data, 0, 5);
if (((data[0] & 192) == 64) && ((data[0] & 63) == BitConverter.ToUInt32(data, 1) % 61))
try
{
DateTime.ParseExact(BitConverter.ToUInt32(data, 1).ToString("D10") + "00", dateformat, CultureInfo.CurrentCulture);
found = true;
}
catch (FormatException)
{
}
}
while ((fs.Position >= 6) && (!found));
if (!found)
goto end;
readTimeInFile = BitConverter.ToUInt32(data, 1).ToString("D10");
int enumsize = Enum.GetValues(typeof(TickTypeEnum)).Length;
found = false;
data = new byte[2];
fs.Seek(0, SeekOrigin.End);
do
{
fs.Seek(-3, SeekOrigin.Current);
fs.Read(data, 0, 2);
// readByte = checked((byte)fs.ReadByte());
if (((data[0] >> 6) == 3) && (((data[0] & 56) >> 3) <= enumsize) && (data[1] < 60))
found = true;
// fs.Seek(-1, SeekOrigin.Current);
} while ((fs.Position >= 3) && (!found));
if (!found)
goto end;
readTimeInFile += data[1].ToString("D2");
retTime = DateTime.ParseExact(readTimeInFile, dateformat, CultureInfo.CurrentCulture);//.ToLocalTime();
}
end:
return retTime;
}
}
#endregion
#region Millisec
class FileManagerMillisec : FileManager
{
public override string Name { get { return "Millisec"; } }
public override bool IsWritable { get { return true; } }
public override bool IsMillisecCompliant { get { return true; } }