private bool HandleTick(int expectedCount, Action <TickIO, TickIO, ulong> assertTick, SymbolInfo symbol) { try { while (!tickQueue.TryDequeue(ref tickBinary)) { if (propagateException != null) { throw propagateException; } Thread.Sleep(1); } tick.Inject(tickBinary); if (debug && countLog < 5) { log.Debug("Received a tick " + tick); countLog++; } startTime = Environment.TickCount; count++; if (count > 0) { assertTick(tick, lastTick, symbol.BinaryIdentifier); } lastTick.Copy(tick); if (propagateException != null) { throw propagateException; } if (count >= expectedCount) { return(true); } } catch (QueueException ex) { switch (ex.EntryType) { case EventType.EndHistorical: case EventType.StartRealTime: case EventType.EndRealTime: break; case EventType.Terminate: return(true); default: throw new ApplicationException("Unexpected QueueException: " + (EventType)ex.EntryType); } } return(false); }
private bool TryGetNextTick(TickQueue queue, ref TickBinary binary) { bool result = false; do { try { result = queue.TryDequeue(ref binary); } catch (QueueException ex) { // Ignore any other events. if (ex.EntryType == EventType.EndHistorical) { throw; } } } while(!result); return(result); }
public void EnQueueItemTest() { TickBinary tick = new TickBinary(); TickIO tickIO = Factory.TickUtil.TickIO(); long start = Factory.TickCount; for (int i = 0; i < 10; i++) { Assert.IsTrue(queue.TryEnqueue(ref tick)); } long stop = Factory.TickCount; log.Notice("Enqueue elapsed time is " + (stop - start) + "ms"); start = Factory.TickCount; for (int i = 0; i < 10; i++) { Assert.IsTrue(queue.TryDequeue(ref tick)); } stop = Factory.TickCount; log.Notice("Dequeue elapsed time is " + (stop - start) + "ms"); }
private void FilterFile(string symbol, string inputPath, string outputPath, TimeStamp startTime, TimeStamp endTime) { TickReader reader = Factory.TickUtil.TickReader(); TickWriter writer = Factory.TickUtil.TickWriter(true); writer.KeepFileOpen = true; writer.Initialize(outputPath, symbol); reader.Initialize(inputPath, symbol); TickQueue inputQueue = reader.ReadQueue; TickIO firstTick = Factory.TickUtil.TickIO(); TickIO lastTick = Factory.TickUtil.TickIO(); TickIO prevTick = Factory.TickUtil.TickIO(); long count = 0; long fast = 0; long dups = 0; TickIO tickIO = Factory.TickUtil.TickIO(); TickBinary tickBinary = new TickBinary(); inputQueue.Dequeue(ref tickBinary); tickIO.Inject(tickBinary); count++; firstTick.Copy(tickIO); firstTick.IsSimulateTicks = true; prevTick.Copy(tickIO); prevTick.IsSimulateTicks = true; if (tickIO.Time >= startTime) { writer.Add(firstTick); } try { while (true) { while (!inputQueue.TryDequeue(ref tickBinary)) { Thread.Sleep(1); } tickIO.Inject(tickBinary); count++; if (tickIO.Time >= startTime) { if (tickIO.Time > endTime) { break; } // if( tickIO.Bid == prevTick.Bid && tickIO.Ask == prevTick.Ask) { // dups++; // } else { // Elapsed elapsed = tickIO.Time - prevTick.Time; prevTick.Copy(tickIO); prevTick.IsSimulateTicks = true; // if( elapsed.TotalMilliseconds < 5000) { // fast++; // } else { while (!writer.TryAdd(prevTick)) { Thread.Sleep(1); } // } // } } } } catch (QueueException ex) { if (ex.EntryType != EventType.EndHistorical) { throw new ApplicationException("Unexpected QueueException: " + ex); } } lastTick.Copy(tickIO); Console.WriteLine(reader.Symbol + ": " + count + " ticks from " + firstTick.Time + " to " + lastTick.Time + " " + dups + " duplicates, " + fast + " less than 50 ms"); Factory.TickUtil.TickReader().CloseAll(); writer.Close(); }
protected virtual Yield Invoke() { EventItem eventItem; if (filter.Receive(out eventItem)) { switch ((EventType)eventItem.EventType) { case EventType.Shutdown: Dispose(); filter.Pop(); break; default: throw new ApplicationException("Unexpected event: " + eventItem); } } var result = Yield.NoWork.Repeat; try { if (writeQueue.Count == 0) { return(result); } while (writeQueue.Count > 0) { if (!writeQueue.TryPeek(ref tick)) { break; } tickIO.Inject(tick); if (tickFile.TryWriteTick(tickIO)) { writeQueue.TryDequeue(ref tick); } result = Yield.DidWork.Repeat; } return(result); } catch (QueueException ex) { if (ex.EntryType == EventType.Terminate) { log.Notice("Last tick written: " + tickIO); if (debug) { log.Debug("Exiting, queue terminated."); } Finalize(); return(Yield.Terminate); } else { Exception exception = new ApplicationException("Queue returned unexpected: " + ex.EntryType); writeQueue.SetException(exception); writeQueue.Dispose(); Dispose(); throw ex; } } catch (Exception ex) { writeQueue.SetException(ex); writeQueue.Dispose(); Dispose(); throw; } }
public long Verify(int expectedCount, Action <TickIO, TickIO, long> assertTick, SymbolInfo symbol, int timeout, Action action) { if (debug) { log.Debug("Verify"); } if (SyncTicks.Enabled) { tickSync = SyncTicks.GetTickSync(symbol.BinaryIdentifier); } long endTime = Factory.Parallel.TickCount + timeout * 1000; count = 0; while (Factory.Parallel.TickCount < endTime) { if (propagateException != null) { throw propagateException; } try { if (tickQueue.TryDequeue(ref tickBinary)) { tickIO.Inject(tickBinary); if (debug && countLog < 5) { log.Debug("Received a tick " + tickIO); countLog++; } startTime = Factory.TickCount; count++; if (count > 0 && assertTick != null) { assertTick(tickIO, lastTick, symbol.BinaryIdentifier); } lastTick.Copy(tickIO); if (!actionAlreadyRun && action != null) { actionAlreadyRun = true; action(); } if (SyncTicks.Enabled) { tickSync.RemoveTick(); } if (count >= expectedCount) { break; } } else { Thread.Sleep(100); } } catch (QueueException ex) { if (HandleQueueException(ex)) { break; } } } return(count); }
protected virtual Yield AppendData() { try { if (writeQueue.Count == 0) { return(Yield.NoWork.Repeat); } if (!keepFileOpen) { fs = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.Read); if (trace) { log.Trace("!keepFileOpen - Open()"); } memory = new MemoryStream(); } while (writeQueue.TryDequeue(ref tick)) { tickIO.Inject(tick); if (trace) { log.Trace("Writing to file: " + tickIO); } WriteToFile(memory, tickIO); } if (!keepFileOpen) { fs.Close(); if (trace) { log.Trace("!keepFileOpen - Close()"); } fs = null; } return(Yield.DidWork.Repeat); } catch (QueueException ex) { if (ex.EntryType == EventType.Terminate) { log.Debug("Exiting, queue terminated."); if (fs != null) { fs.Close(); log.Debug("Terminate - Close()"); } return(Yield.Terminate); } else { Exception exception = new ApplicationException("Queue returned unexpected: " + ex.EntryType); writeQueue.Terminate(exception); throw ex; } } catch (Exception ex) { writeQueue.Terminate(ex); if (fs != null) { fs.Close(); } throw; } }
public bool Receive(ref TickBinary tick) { return(tickQueue.TryDequeue(ref tick)); }