Exemple #1
0
        private void Check(bool isHistory, MapsFolder mapsFolder)
        {
            TicksFileData ticksFileData = isHistory ? _history : _cache;

            if (ticksFileData == null)
            {
                return;
            }
            TicksFileMapData[]      mapsFiles  = mapsFolder.Pop(_symbolName);
            List <TicksFileMapData> listDelete = new List <TicksFileMapData>();

            foreach (TicksFileMapData mapFile in mapsFiles)
            {
                if (ticksFileData.Count == mapFile.CountTicks)
                {
                    if (isHistory)
                    {
                        _map.History = mapFile;
                    }
                    else
                    {
                        _map.Cache = mapFile;
                    }
                }
                else
                {
                    listDelete.Add(mapFile);
                }
            }
            foreach (TicksFileMapData barsFile in listDelete)
            {
                barsFile.CloseStream();
                barsFile.File.Delete();
            }
        }
Exemple #2
0
        public TicksCacher(TicksFileData ticksFileData, BarsFileData[] barsFiles)
        {
            _listBarsFileData = new List <BarsFileData>(barsFiles);
            _ticksFileData    = ticksFileData;
            string dir = _ticksFileData.File.Directory.FullName + "\\" + BARS_DIR;

            _dirBars = new DirectoryInfo(dir);
            if (!_dirBars.Exists)
            {
                _dirBars.Create();
            }
        }
Exemple #3
0
        private void Check(bool isHistory, BarsFolder barsFolder)
        {
            TicksFileData ticksFileData = isHistory ? _history : _cache;

            BarsFileData[]      barsFiles  = barsFolder.Pop(_symbolName);
            List <BarsFileData> listDelete = new List <BarsFileData>();

            foreach (BarsFileData barsFile in barsFiles)
            {
                if (ticksFileData.Count == barsFile.CountTicks)
                {
                    BarManager bm = (BarManager)this._barsManager[barsFile.TimeFrameSecond];
                    if (bm == null)
                    {
                        listDelete.Add(barsFile);
                    }
                    else
                    {
                        if (isHistory)
                        {
                            bm.History = barsFile;
                        }
                        else
                        {
                            bm.Cache = barsFile;
                        }
                    }
                }
                else
                {
                    listDelete.Add(barsFile);
                }
            }
            foreach (BarsFileData barsFile in listDelete)
            {
                barsFile.CloseStream();
                barsFile.File.Delete();
            }
        }
Exemple #4
0
        private void ExecuteTaskUpdateTicks(TaskUpdateTicks task)
        {
            Trace.TraceInformation("{0} TicksManager.ExecuteTaskUpdateTicks({1}) - Start", this.Name, task);

            bool isCutBeginCache = false;

            Tick[] ticks = task.Ticks;

            #region if (ticks.Length == 0) {...}
            if (ticks.Length == 0)
            {
                if (task.Task == TicksManagerTask.UpdateCache &&
                    this.Cache.Count > 0 && this.History.Count > 0 &&
                    this.Cache.TimeFrom < this.History.TimeTo)
                {
                    isCutBeginCache = true;
                }
                else
                {
                    return;
                }
            }
            #endregion

            TicksFileData tfdCurrent = task.Task == TicksManagerTask.UpdateHistory ? this.History : this.Cache;

            FileInfo ticksFileTemp = new FileInfo(tfdCurrent.File.FullName + ".tmp");

            if (ticksFileTemp.Exists)
            {
                ticksFileTemp.Delete();
            }

            TicksFileData tfdTemp = new TicksFileData(ticksFileTemp, this.Name, this.Digits);

            int index = 0;

            bool abort     = false;
            long savedTime = DateTime.Now.Ticks;

            if (isCutBeginCache)
            {
                Trace.TraceInformation("{0} - Cut Begin Ticks in Cache File", this.Name);

                long limitBeginTime = this.History.TimeTo;

                int cCount = this.Cache.Count;
                for (int i = 0; i < cCount; i++)
                {
                    Tick tick = this.Cache.Read(i);
                    if (tick.Time < limitBeginTime)
                    {
                        continue;
                    }
                    tfdTemp.Write(tick, index++);
                    savedTime = ChangeUpdateProgress(savedTime, cCount, i);
                    if (CheckTaskPriorityUpdateTicks())
                    {
                        break;
                    }
                }
            }
            else
            {
                UpdateMethod method = UpdateMethod.Unknow;
                if (tfdCurrent.Count == 0)
                {
                    method = UpdateMethod.AppendInEmpty;
                }

                if (method == UpdateMethod.AppendInEmpty)
                {
                    Trace.TraceInformation("{0} - StartUpdate[Method={1}]", this.Name, method);
                    for (int i = 0; i < ticks.Length; i++)
                    {
                        tfdTemp.Write(ticks[i], index++);
                        savedTime = ChangeUpdateProgress(savedTime, ticks.Length, i);
                        if (CheckTaskPriorityUpdateTicks())
                        {
                            break;
                        }
                    }
                }
                else
                {
                    #region Init Property
                    long mTimeFrom = tfdCurrent.TimeFrom;
                    long mTimeTo   = tfdCurrent.TimeTo;
                    long cTimeFrom = ticks[0].Time;
                    long cTimeTo   = ticks[ticks.Length - 1].Time;

                    int mCount = tfdCurrent.Count;
                    int cCount = ticks.Length;

                    int  mIndex = 0, cIndex = 0;
                    Tick tick       = new Tick();
                    Tick mTick      = new Tick();
                    Tick cTick      = new Tick();
                    int  level      = 0;
                    Tick cFirstTick = ticks[0];
                    Tick cEndTick   = ticks[ticks.Length - 1];
                    #endregion

                    if (cTimeFrom > mTimeTo)
                    {
                        method = UpdateMethod.AdditionInEnd;
                    }
                    else if (cTimeTo < mTimeFrom)
                    {
                        method = UpdateMethod.InsertInBeginning;
                    }
                    else if (cTimeFrom < mTimeFrom && mTimeTo < cTimeTo)
                    {
                        method = UpdateMethod.ReplacementOfGreater;
                    }
                    else if (mTimeFrom < cTimeFrom && cTimeTo < mTimeTo)
                    {
                        method = UpdateMethod.InsertInBody;
                    }
                    else if (cTimeFrom <= mTimeFrom && cTimeTo > mTimeFrom && cTimeTo < mTimeTo)
                    {
                        method = UpdateMethod.AdditionInBeginReplacement;
                    }
                    else if (cTimeFrom > mTimeFrom && cTimeFrom < mTimeTo && cTimeTo > mTimeTo)
                    {
                        method = UpdateMethod.AdditionInEndReplacement;
                    }

                    string mPS = string.Format("Main[B={0},E={1},Count={2}]", new DateTime(mTimeFrom), new DateTime(mTimeTo), mCount);
                    string cPS = string.Format("New[B={0},E={1},Count={2}]", new DateTime(cTimeFrom), new DateTime(cTimeTo), cCount);

                    Trace.TraceInformation("{0} - StartUpdate[Method={1}], {2}, {3}", this.Name, method, mPS, cPS);

                    if (method == UpdateMethod.AdditionInEnd)
                    {
                        tfdTemp.CloseStream();
                        tfdTemp.File.Delete();
                        File.Copy(tfdCurrent.File.FullName, tfdTemp.File.FullName, true);
                        tfdTemp = new TicksFileData(ticksFileTemp);
                        tfdTemp.SeekWriterEnd();
                        index = mCount;
                        for (int i = 0; i < cCount; i++)
                        {
                            tfdTemp.Write(ticks[i], index++);
                            savedTime = ChangeUpdateProgress(savedTime, cCount, i);
                            if (CheckTaskPriorityUpdateTicks())
                            {
                                break;
                            }
                        }
                    }
                    else if (method == UpdateMethod.InsertInBeginning) // if (cTimeTo < mTimeFrom)
                    {
                        int count = tfdCurrent.Count + ticks.Length;

                        for (int i = 0; i < count; i++)
                        {
                            //if (cTimeFrom > mTimeTo)
                            //  tick = (i < mCount) ? tfdCurrent.Read(i) : ticks[i - mCount];
                            //else
                            tick = (i < cCount) ? ticks[i] : tfdCurrent.Read(i - cCount);

                            tfdTemp.Write(tick, index++);
                            savedTime = ChangeUpdateProgress(savedTime, count, i);
                            if (CheckTaskPriorityUpdateTicks())
                            {
                                break;
                            }
                        }
                    }
                    else if (method == UpdateMethod.ReplacementOfGreater) //if (cTimeFrom < mTimeFrom && mTimeTo < cTimeTo)
                    {
                        #region Replacement of the greater
                        for (int i = 0; i < ticks.Length; i++)
                        {
                            tfdTemp.Write(ticks[i], index++);
                            savedTime = ChangeUpdateProgress(savedTime, ticks.Length, i);
                            if (CheckTaskPriorityUpdateTicks())
                            {
                                break;
                            }
                        }
                        #endregion
                    }
                    else if (mTimeFrom < cTimeFrom && cTimeTo < mTimeTo)
                    {
                        #region Insert in a body
                        while (mIndex < mCount)
                        {
                            tick = new Tick(-1, 0);

                            mTick = tfdCurrent.Read(mIndex);
                            if (cIndex < cCount)
                            {
                                cTick = ticks[cIndex];
                            }

                            if (level == 0)
                            {
                                if (mTick.Time < cTick.Time)
                                {
                                    tick = mTick;
                                    mIndex++;
                                }
                                else
                                {
                                    level++;
                                }
                            }

                            if (level == 1)
                            {
                                if (cIndex < cCount)
                                {
                                    tick = cTick;
                                    cIndex++;
                                }
                                else
                                {
                                    level++;
                                }
                            }

                            if (level == 2)
                            {
                                if (mTick.Time > cTick.Time)
                                {
                                    tick = mTick;
                                }
                                mIndex++;
                            }
                            if (tick.Time > -1)
                            {
                                tfdTemp.Write(tick, index++);
                            }

                            savedTime = ChangeUpdateProgress(savedTime, mCount + cCount, index);
                            if (CheckTaskPriorityUpdateTicks())
                            {
                                break;
                            }
                        }
                        #endregion
                    }
                    else if (cTimeFrom <= mTimeFrom && cTimeTo > mTimeFrom && cTimeTo < mTimeTo)
                    {
                        #region To add in the beginning
                        while (mIndex < mCount)
                        {
                            tick = new Tick(-1, 0);
                            if (cIndex < cCount)
                            {
                                tick = cTick = ticks[cIndex++];
                            }
                            else
                            {
                                mTick = tfdCurrent.Read(mIndex);
                                if (mTick.Time > cTick.Time)
                                {
                                    tick = mTick;
                                }
                                mIndex++;
                            }
                            if (tick.Time > -1)
                            {
                                tfdTemp.Write(tick, index++);
                            }
                            savedTime = ChangeUpdateProgress(savedTime, mCount + cCount, index);
                            if (CheckTaskPriorityUpdateTicks())
                            {
                                break;
                            }
                        }
                        #endregion
                    }
                    else if (cTimeFrom > mTimeFrom && cTimeFrom < mTimeTo && cTimeTo > mTimeTo)
                    {
                        #region To add in the end
                        while (cIndex < cCount)
                        {
                            tick  = new Tick(-1, 0);
                            cTick = ticks[cIndex];

                            if (mIndex < mCount)
                            {
                                mTick = tfdCurrent.Read(mIndex);
                                if (mTick.Time < cTick.Time)
                                {
                                    tick = mTick;
                                    mIndex++;
                                }
                                else
                                {
                                    mIndex = mCount;
                                }
                            }
                            else
                            {
                                tick = cTick;
                                cIndex++;
                            }

                            if (tick.Time > -1)
                            {
                                tfdTemp.Write(tick, index++);
                            }
                            savedTime = ChangeUpdateProgress(savedTime, mCount + cCount, index);
                            if (CheckTaskPriorityUpdateTicks())
                            {
                                break;
                            }
                        }
                        #endregion
                    }
                    else
                    {
                        abort = true;
                    }
                }
            }
            tfdTemp.CloseStream();

            if (_abortTask)
            {
                abort = true;
            }

            Trace.TraceInformation("{0} - Stop Update {1}", this.Name, abort?"Abort":"");

            if (abort)
            {
                tfdTemp.File.Delete();
                return;
            }

            if (task.Task == TicksManagerTask.UpdateHistory && this.Cache.Count > 0)
            {
                this.Update(new Tick[0], false);
            }

            lock (_locked) {
                tfdCurrent.CloseStream();
                tfdCurrent.File.Delete();
                tfdTemp.CloseStream();

                File.Move(tfdTemp.File.FullName, tfdCurrent.File.FullName);

                tfdTemp = new TicksFileData(tfdCurrent.File);

                if (task.Task == TicksManagerTask.UpdateHistory)
                {
                    _history = tfdTemp;
                    _sessionSource.IncrementsLevel1();
                }
                else
                {
                    _cache = tfdTemp;
                    _sessionSource.IncrementsLevel2();
                }
            }
            this.AddTask(new TaskDataCache());
            Trace.TraceInformation("{0} TicksManager.ExecuteTaskUpdateTicks({1}) - Stop", this.Name, task);
        }
Exemple #5
0
        private void CacheBars(bool isHistory, Dictionary <int, BarsFileData> bfds, long beginTime)
        {
            List <BarsFileDataCacher> cacher = new List <BarsFileDataCacher>();
            TicksFileData             tfd    = isHistory ? this.History : this.Cache;
            TicksFileMapData          tfmd   = isHistory ? this.Map.History : this.Map.Cache;

            TicksFileMapDataCacher mapCacher = new TicksFileMapDataCacher(this, isHistory, tfmd);

            foreach (BarManager bm in (BarsManager)this.BarsDataList)
            {
                BarsFileData bfdBad = null;
                bfds.TryGetValue(bm.TimeFrame.Second, out bfdBad);
                BarsFileDataCacher bfdc =
                    new BarsFileDataCacher(this, bm, isHistory, bfdBad, bm.TimeFrame);

                cacher.Add(bfdc);
            }

            long time = DateTime.Now.Ticks;

            for (int i = 0; i < tfd.Count; i++)
            {
                Tick tick = tfd.Read(i);
                for (int ii = 0; ii < cacher.Count; ii++)
                {
                    cacher[ii].Add(tick);
                }

                mapCacher.Add(tick);
                if (DateTime.Now.Ticks - time > 5000000L)
                {
                    this.OnTaskProcessChanged(new TicksManagerProcessEventArgs(_currentTask.Task, tfd.Count, i));
                    time = DateTime.Now.Ticks;
                }

                if (this.CheckTaskPriority())
                {
                    _abortTask = true;
                    break;
                }
            }

            for (int i = 0; i < cacher.Count; i++)
            {
                if (_abortTask)
                {
                    cacher[i].Abort();
                }
                else
                {
                    /* блокировка */
                    lock (_locked)
                        cacher[i].Complete();
                }
            }
            if (_abortTask)
            {
                mapCacher.Abort();
            }
            else
            {
                lock (_locked)
                    mapCacher.Complete();
            }
        }
Exemple #6
0
        public void Load()
        {
            List <FileInfo> filesHistory = new List <FileInfo>();
            List <FileInfo> filesCache   = new List <FileInfo>();
            Dictionary <string, TicksManager> symbols = new Dictionary <string, TicksManager>();

            FileInfo[] files = _dirHistory.GetFiles("*." + TICKS_FILE_EXT);
            foreach (FileInfo file in files)
            {
                try {
                    TicksManager ticksManager = new TicksManager();
                    ticksManager.History = new TicksFileData(file);
                    symbols.Add(ticksManager.Name, ticksManager);
                } catch (Exception e) {
                    Trace.TraceError("Load {0} - {1}", file.FullName, e.Message);
                }
            }

            files = _dirCache.GetFiles("*." + TICKS_FILE_EXT);
            foreach (FileInfo file in files)
            {
                try {
                    TicksManager  ticksManager = null;
                    TicksFileData fileData     = new TicksFileData(file);
                    symbols.TryGetValue(fileData.SymbolName, out ticksManager);
                    if (ticksManager == null)
                    {
                        ticksManager = new TicksManager();
                        symbols.Add(fileData.SymbolName, ticksManager);
                    }
                    ticksManager.Cache = fileData;
                } catch (Exception e) {
                    Trace.TraceError("Load {0} - {1}", file.FullName, e.Message);
                }
            }

            BarsFolder barsFolderHistory = new BarsFolder(_dirHistory);
            BarsFolder barsFolderCache   = new BarsFolder(_dirCache);

            MapsFolder mapsFolderHistory = new MapsFolder(_dirHistory);
            MapsFolder mapsFolderCache   = new MapsFolder(_dirCache);

            this.Clear();
            foreach (TicksManager ticks in symbols.Values)
            {
                if (ticks.History == null)
                {
                    ticks.History = new TicksFileData(_dirHistory, ticks.Name, ticks.Digits);
                }

                if (ticks.Cache == null)
                {
                    ticks.Cache = new TicksFileData(_dirCache, ticks.Name, ticks.Digits);
                }

                ticks.InitializeBarsFiles(barsFolderHistory, barsFolderCache);
                ticks.InitializeMapsFiles(mapsFolderHistory, mapsFolderCache);
                Symbol symbol = new Symbol(ticks);

                this.Add(symbol);
            }
            barsFolderHistory.DeleteEmpty();
            barsFolderCache.DeleteEmpty();
        }