示例#1
0
 public Tick(DateTime dt, int number, float price, int volue)
 {
     this.number = number;
     this.price  = price;
     this.volume = volue;
     this.dt     = DateTime2Int.Int(dt);
 }
示例#2
0
        public TicksFile(string fileName)
        {
            this.fileName = fileName;
            if (!File.Exists(fileName))
            {
                l.Error("Файл не существует" + fileName);
                return;
            }
            using (StreamReader sr = new StreamReader(fileName, new System.Text.UTF8Encoding()))
            {
                if ((sr.ReadLine() != CaptionLine1) || (sr.ReadLine() != CaptionLine2))
                {
                    l.Error("Не верный формат файла " + fileName);
                    return;
                }

                symbol = Core.Data.GetSymbol(sr.ReadLine());

                System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
                startDateTime = DateTime2Int.Int(DateTime.ParseExact(sr.ReadLine(), "yyyy.MM.dd", provider));
                endDateTime   = DateTime2Int.Int(DateTime.ParseExact(sr.ReadLine(), "yyyy.MM.dd", provider).AddDays(1).AddSeconds(-1));

                Count = int.Parse(sr.ReadLine());
            }
        }
示例#3
0
        public static TicksFile LoadFromAllTicksFileList(string dataDir, StreamReader sr)
        {
            string fileName = Path.Combine(dataDir, sr.ReadLine());

            if (!File.Exists(fileName))
            {
                l.Error("Файл не существует " + fileName);
                return(null);
            }
            if ((sr.ReadLine() != CaptionLine1) || (sr.ReadLine() != CaptionLine2))
            {
                l.Error("Не верный формат файла " + fileName);
                return(null);
            }

            string symbol = sr.ReadLine();

            System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
            DateTime startDate = DateTime.ParseExact(sr.ReadLine(), "yyyy.MM.dd", provider);
            DateTime endDate   = DateTime.ParseExact(sr.ReadLine(), "yyyy.MM.dd", provider).AddDays(1).AddSeconds(-1);

            int count = int.Parse(sr.ReadLine());

            return(new TicksFile(fileName, symbol, DateTime2Int.Int(startDate), DateTime2Int.Int(endDate), count));
        }
示例#4
0
        TicksFile GetFile(int dt)
        {
            _lock.AcquireReaderLock(1000);
            try
            {
                if ((file_GetFileCache != null) && (file_GetFileCache.Include(dt)))
                {
                    return(file_GetFileCache);
                }

                foreach (TicksFile f in ticksFileList)
                {
                    if (f.Include(dt))
                    {
                        file_GetFileCache = f;
                        return(f);
                    }
                }

                LockCookie lc = _lock.UpgradeToWriterLock(1000);
                try
                {
                    DateTime startDate = new DateTime(DateTime2Int.DateTime(dt).Year, DateTime2Int.DateTime(dt).Month, 1);
                    DateTime endDate   = startDate.AddMonths(1).AddSeconds(-1);

                    string fileName = Path.Combine(dataDir, symbol + " " + startDate.ToString("yyyyMM") + TicksFile.FileExt);
                    //Создаю уникальное имя для файла, чтобы не затереть данные
                    int i = 1;
                    while (File.Exists(fileName))
                    {
                        l.Error("Не уникальное имя файла с тиками " + fileName);
                        fileName = Path.Combine(dataDir, symbol + " " + startDate.ToString("yyyyMM") + "[" + i + "]" + TicksFile.FileExt);
                        ++i;
                    }

                    TicksFile newTickFile = new TicksFile(
                        fileName,
                        symbol,
                        DateTime2Int.Int(startDate),
                        DateTime2Int.Int(endDate));

                    ticksFileList.Add(newTickFile);

                    staticLock.AcquireWriterLock(10000);
                    try { allTicksFileList.Add(newTickFile); }
                    finally { staticLock.ReleaseLock(); }

                    file_GetFileCache = newTickFile;
                    return(newTickFile);
                }
                finally
                {
                    _lock.DowngradeFromWriterLock(ref lc);
                }
            }
            finally
            {
                _lock.ReleaseReaderLock();
            }
        }
示例#5
0
        TicksFile forConcatCreateFile(DateTime from, DateTime till, string maskForDateTime, int count)
        {
            int intTill = DateTime2Int.Int(till);
            int intFrom = DateTime2Int.Int(from);

            // Проверяю, возможно данный файл уже существует
            foreach (TicksFile tf in ticksFileList)
            {
                if ((tf.startDateTime == intFrom) && (tf.endDateTime == intTill))
                {
                    return(tf);
                }
            }

            // ну а если нет, то создаю
            IBar bar = Get(DateTime2Int.Int(from));

            if (bar == null)
            {
                bar = First;
            }

            IList <IBar> ticks = new List <IBar>(count);

            while ((bar != null) && (bar.DT <= intTill))
            {
                if (bar.DT >= intFrom)
                {
                    ticks.Add(bar);
                }
                bar = GetNext(bar);
            }

            string fileName = string.Empty;

            if (maskForDateTime == string.Empty)
            {
                fileName = Path.Combine(dataDir, symbol + TicksFile.FileExt);
            }
            else
            {
                fileName = Path.Combine(dataDir, symbol + from.ToString(maskForDateTime) + TicksFile.FileExt);
            }

            //  Создаю уникальное имя для файла, чтобы не затереть данные
            int i = 1;

            while (File.Exists(fileName))
            {
                l.Info("Не уникальное имя файла с тиками " + fileName);
                fileName = Path.Combine(dataDir, symbol + from.ToString(maskForDateTime) + "[" + i + "]" + TicksFile.FileExt);
                ++i;
            }

            TicksFile result = new TicksFile(fileName, symbol, intFrom, intTill);

            result.ticks = ticks;
            return(result);
        }
示例#6
0
 private void exportButton_Click(object sender, EventArgs e)
 {
     EnableAll(false);
     backgroundWorker1.RunWorkerAsync(new DoWorkParam(
                                          selectSymbol1.GetSelectedSymbols(),
                                          (int)numericUpDown1.Value,
                                          DateTime2Int.Int(dateTimePicker1.Value),
                                          DateTime2Int.Int(dateTimePicker2.Value),
                                          folderTextBox.Text,
                                          comboBox1.Text
                                          ));
 }
示例#7
0
        List <IBar> Load()
        {
            _lock.AcquireWriterLock(1000);
            try
            {
                if (!File.Exists(fileName))
                {
                    return(new List <IBar>(10000));
                }
                List <IBar> data = null;
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    long length   = fs.Length;
                    long position = CaptionLength;
                    if (length <= CaptionLength)
                    {
                        return(new List <IBar>(10000));
                    }
                    if (DateTime2Int.Int(DateTime.Now) < endDateTime)
                    {
                        data = new List <IBar>((int)(2 * (length - position) / Tick.Size));
                    }
                    else
                    {
                        data = new List <IBar>((int)(1.1 * (length - position) / Tick.Size));
                    }

                    fs.Position = CaptionLength;
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        while (position < length)
                        {
                            data.Add(new Tick(br));
                            position += Tick.Size;
                        }
                    }
                }
                Count      = data.Count;
                changeFrom = int.MaxValue;
                return(data);
            }
            finally
            {
                _lock.ReleaseWriterLock();
            }
        }
示例#8
0
        // Объеденяет файлы
        public void Concat(int count = 100 *1024 *1024 / Simple.Tick.Size)
        {
            _lock.AcquireWriterLock(10000);
            try
            {
                List <TicksFile> newTicksFileList = new List <TicksFile>();

                if (Count <= count)
                {
                    //Объединяю всё в один файл
                    newTicksFileList.Add(
                        forConcatCreateFile(DateTime.MinValue, DateTime.MaxValue, string.Empty, Count)
                        );
                }
                else
                {
                    // Как минимум разбиваю по годам
                    IBar bar      = First;
                    int  year     = bar.GetDateTime().Year;
                    int  lastYear = Last.GetDateTime().Year;
                    while (year <= lastYear)
                    {
                        // расчитываю кво баров в данном году
                        int barCountOfYear = 0;
                        int nextYear       = DateTime2Int.Int(new DateTime(year + 1, 1, 1));
                        while ((bar != null) && (bar.DT < nextYear))
                        {
                            ++barCountOfYear;
                            bar = GetNext(bar);
                        }
                        if (barCountOfYear <= count)
                        {
                            //Объединяю год в один файл
                            if (barCountOfYear > 0)
                            {
                                newTicksFileList.Add(
                                    forConcatCreateFile(new DateTime(year, 1, 1), new DateTime(year + 1, 1, 1).AddSeconds(-1), " yyyy", barCountOfYear)
                                    );
                            }
                        }
                        else
                        {
                            //Разбивыаю по месяцам
                            for (int i = 1; i <= 12; ++i)
                            {
                                TicksFile tf = forConcatCreateFile(new DateTime(year, i, 1), new DateTime(year, i, 1).AddMonths(1).AddSeconds(-1), " yyyyMM", (barCountOfYear / 12 * 2));
                                if (tf.Count > 0)
                                {
                                    newTicksFileList.Add(tf);
                                }
                            }
                        }
                        ++year;
                    }
                }
                // Удаляю все существующие файлы
                staticLock.AcquireWriterLock(10000);
                try
                {
                    foreach (TicksFile tf in ticksFileList)
                    {
                        if (!newTicksFileList.Contains(tf))
                        {
                            tf.Delete();
                            allTicksFileList.Remove(tf); // TODO возможно здесь баг, т.к. после Concat файл CaptionOfAllFiles.txt был больше 15M, а после пересоздания стал 800K
                        }
                    }

                    ticksFileList = newTicksFileList;
                    foreach (TicksFile tf in ticksFileList)
                    {
                        if (!allTicksFileList.Contains(tf))
                        {
                            allTicksFileList.Add(tf);
                        }
                    }
                }
                finally
                {
                    staticLock.ReleaseWriterLock();
                }
            }
            finally
            {
                _lock.ReleaseWriterLock();
            }
        }