public bool OnEvent(SymbolInfo symbol, int eventType, object eventDetail)
        {
            bool result = false;

            try {
                switch ((EventType)eventType)
                {
                case EventType.Tick:
                    TickBinaryBox binary = (TickBinaryBox)eventDetail;
                    result = readQueue.TryEnqueue(ref binary.TickBinary);
                    if (result)
                    {
                        tickPool.Free(binary);
                    }
                    break;

                case EventType.EndHistorical:
                    result = readQueue.TryEnqueue(EventType.EndHistorical, symbol);
                    break;

                case EventType.StartRealTime:
                    result = readQueue.TryEnqueue(EventType.StartRealTime, symbol);
                    break;

                case EventType.EndRealTime:
                    result = readQueue.TryEnqueue(EventType.EndRealTime, symbol);
                    break;

                case EventType.Error:
                    result = readQueue.TryEnqueue(EventType.Error, symbol);
                    break;

                case EventType.Terminate:
                    result = readQueue.TryEnqueue(EventType.Terminate, symbol);
                    break;

                case EventType.LogicalFill:
                case EventType.StartHistorical:
                case EventType.Initialize:
                case EventType.Open:
                case EventType.Close:
                case EventType.PositionChange:
                default:
                    // Skip these event types.
                    result = true;
                    break;
                }
            } catch (QueueException) {
                log.Warn("Already terminated.");
            }
            return(result);
        }
        public bool OnSend(TickBinaryBox o)
        {
            bool result = false;

            try {
                result = tickQueue.TryEnqueue(ref o.TickBinary);
                if (result)
                {
                    tickPool.Free(o);
                }
            } catch (QueueException) {
                // Queue already terminated.
            }
            return(result);
        }
Example #3
0
        private void TickEvent()
        {
            if (box == null)
            {
                throw new ApplicationException("Box is null.");
            }
            var item = new EventItem(tickFile.Symbol, EventType.Tick, box);

            agent.SendEvent(item);
            if (Diagnose.TraceTicks)
            {
                Diagnose.AddTick(diagnoseMetric, ref box.TickBinary);
            }
            box = null;
        }
Example #4
0
        private Yield StartEvent()
        {
            if (!receiver.OnEvent(symbol, (int)EventType.StartHistorical, null))
            {
                return(Yield.NoWork.Repeat);
            }
            else
            {
                if (!quietMode)
                {
                    LogInfo("Starting loading for " + symbol + " from " + tickIO.ToPosition());
                }
                box            = tickBoxPool.Create();
                box.TickBinary = tick;

                return(Yield.DidWork.Invoke(TickEventMethod));
            }
        }
        public bool OnEvent(SymbolInfo symbol, int eventType, object eventDetail)
        {
            if (isDisposed)
            {
                return(false);
            }
            bool result = false;

            try {
                switch ((EventType)eventType)
                {
                case EventType.Tick:
                    TickBinaryBox binary = (TickBinaryBox)eventDetail;
                    result = OnSend(binary);
                    break;

                case EventType.EndHistorical:
                    result = OnEndHistorical(symbol);
                    break;

                case EventType.StartHistorical:
                    result = OnHistorical(symbol);
                    break;

                case EventType.StartRealTime:
                    result = OnRealTime(symbol);
                    break;

                case EventType.EndRealTime:
                    result = OnEndRealTime(symbol);
                    break;

                case EventType.StartBroker:
                    result = OnStartBroker(symbol);
                    break;

                case EventType.EndBroker:
                    result = OnEndBroker(symbol);
                    break;

                case EventType.Error:
                    result = OnError((ErrorDetail)eventDetail);
                    break;

                case EventType.LogicalFill:
                    result = OnLogicalFill(symbol, (LogicalFillBinary)eventDetail);
                    break;

                case EventType.Terminate:
                    result = OnStop();
                    break;

                case EventType.Initialize:
                case EventType.Open:
                case EventType.Close:
                case EventType.PositionChange:
                    throw new ApplicationException("Unexpected EventType: " + eventType);

                default:
                    result = OnCustomEvent(symbol, eventType, eventDetail);
                    break;
                }
                return(result);
            } catch (QueueException) {
                log.Warn("Already terminated.");
            }
            return(false);
        }
Example #6
0
        private Yield FileReader()
        {
            lock (taskLocker) {
                if (isDisposed)
                {
                    return(Yield.Terminate);
                }
                long position = dataIn.BaseStream.Position;
                try {
                    if (position < length && !CancelPending && TryReadTick(length))
                    {
                        if (dataVersion == 0)
                        {
                            dataVersion = tickIO.DataVersion;
                        }
                        tick       = tickIO.Extract();
                        isDataRead = true;

                        if (Factory.TickCount > nextUpdate)
                        {
                            try {
                                progressCallback("Loading bytes...", dataIn.BaseStream.Position, length);
                            } catch (Exception ex) {
                                log.Debug("Exception on progressCallback: " + ex.Message);
                            }
                            nextUpdate = Factory.TickCount + 2000;
                        }

                        if (maxCount > 0 && count > maxCount)
                        {
                            if (debug)
                            {
                                log.Debug("Ending data read because count reached " + maxCount + " ticks.");
                            }
                            return(Yield.DidWork.Invoke(SendFinishMethod));
                        }

                        if (IsAtEnd(tick))
                        {
                            return(Yield.DidWork.Invoke(SendFinishMethod));
                        }

                        if (IsAtStart(tick))
                        {
                            count++;
                            if (debug && count < 10)
                            {
                                log.Debug("Read a tick " + tickIO);
                            }
                            else if (trace)
                            {
                                log.Trace("Read a tick " + tickIO);
                            }
                            tick.Symbol = symbol.BinaryIdentifier;

                            if (tick.UtcTime <= lastTime)
                            {
                                tick.UtcTime = lastTime + 1;
                            }
                            lastTime = tick.UtcTime;

                            if (isFirstTick)
                            {
                                isFirstTick = false;
                                return(Yield.DidWork.Invoke(StartEventMethod));
                            }
                            else
                            {
                                tickCount++;
                            }

                            box            = tickBoxPool.Create();
                            box.TickBinary = tick;

                            return(Yield.DidWork.Invoke(TickEventMethod));
                        }
                        tickCount++;
                    }
                    else
                    {
                        return(Yield.DidWork.Invoke(SendFinishMethod));
                    }
                } catch (ObjectDisposedException) {
                    return(Yield.DidWork.Invoke(SendFinishMethod));
//				} catch( ApplicationException ex) {
                    ////					dataIn.BaseStream.Position = position + 1;
//					return Yield.DidWork.Invoke(SendFinish);
//				} catch( ArgumentOutOfRangeException) {
                    ////					dataIn.BaseStream.Position = position + 1;
//					return Yield.DidWork.Invoke(SendFinish);
                }
                return(Yield.DidWork.Repeat);
            }
        }
Example #7
0
        public virtual Yield Invoke()
        {
            EventItem eventItem;

            if (fileReaderTask.Filter.Receive(out eventItem))
            {
                switch (eventItem.EventType)
                {
                default:
                    throw new ApplicationException("Unexpected event: " + eventItem);
                }
            }
            if (!isStarted)
            {
                return(Yield.NoWork.Repeat);
            }
            lock (taskLocker)
            {
                if (isDisposed)
                {
                    return(Yield.Terminate);
                }
                try
                {
                    var loopCount = 0;
                    while (!CancelPending && loopCount < 1000 && tickBoxPool.AllocatedCount < tickBoxPool.Capacity)
                    {
                        ++loopCount;
                        if (!tickFile.TryReadTick(tickIO))
                        {
                            if (debug)
                            {
                                log.Debug("Finished reading to file length: " + tickFile.Length);
                            }
                            return(SendFinish());
                        }

                        tick       = tickIO.Extract();
                        isDataRead = true;

                        if (Factory.TickCount > nextUpdate)
                        {
                            try {
                                progressCallback("Loading bytes...", tickFile.Position, tickFile.Length);
                            } catch (Exception ex) {
                                log.Debug("Exception on progressCallback: " + ex.Message);
                            }
                            nextUpdate = Factory.TickCount + 2000;
                        }

                        if (MaxCount > 0 && Count > MaxCount)
                        {
                            if (debug)
                            {
                                log.Debug("Ending data read because count reached " + MaxCount + " ticks.");
                            }
                            return(SendFinish());
                        }

                        if (IsAtEnd(tick))
                        {
                            return(SendFinish());
                        }

                        if (IsAtStart(tick))
                        {
                            count = Count + 1;
                            if (debug && Count < 10)
                            {
                                log.Debug("Read a tick " + tickIO);
                            }
                            else if (trace)
                            {
                                log.Trace("Read a tick " + tickIO);
                            }
                            tick.Symbol = tickFile.Symbol.BinaryIdentifier;

                            if (tick.UtcTime <= lastTime)
                            {
                                tick.UtcTime = lastTime + 1;
                            }
                            lastTime = tick.UtcTime;

                            if (isFirstTick)
                            {
                                isFirstTick = false;
                                StartEvent();
                            }
                            else
                            {
                                tickCount++;
                            }

                            box = tickBoxPool.Create(tickPoolCallerId);
                            if (debug)
                            {
                                log.Debug("Allocated box id in reader " + box.Id + ", count " + tickBoxPool.AllocatedCount);
                            }
                            var tickId = box.TickBinary.Id;
                            box.TickBinary    = tick;
                            box.TickBinary.Id = tickId;

                            TickEvent();
                        }
                        tickCount++;
                    }
                }
                catch (ObjectDisposedException)
                {
                    return(SendFinish());
                }
                return(Yield.DidWork.Repeat);
            }
        }