Exemple #1
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 #2
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();
            }
        }