public SortedDictionary <uint, MarketState> DuplicateTick(SortedDictionary <uint, MarketState> mostRecentState, DateTime timestamp) { // create a new time stamp if it doesn't exist (Only exist if there is a race condition) if (!_marketData.ContainsKey(timestamp)) { _marketData.Add(timestamp, new SortedDictionary <uint, MarketState>()); } var marketDataForTimeStamp = _marketData[timestamp]; lock (marketDataForTimeStamp) { DateTime newTimeStamp = DateTime.MinValue; var prevState = mostRecentState[(uint)mostRecentState.Count - 1]; var newState = new MarketState(_securityObj, prevState, timestamp); SortedDictionary <uint, MarketState> timeBin = _marketData[timestamp]; newState.BinCnt = (uint)marketDataForTimeStamp.Count; marketDataForTimeStamp.Add(newState.BinCnt, newState); if (LogEachTick) { string output = newState.ToStringAllData(); if (newState.StateType == MktStateType.Trade) { output += " " + newState.ToStringAllTradesNoIndentity(); } Console.WriteLine(output); } } return(_marketData[timestamp]); }
// constructor used to create a duplicate a state using a new timestamp public MarketState(Security security, MarketState previousMktState, DateTime timeStamp) { TimeStamp = DateTime.MinValue; VolumeTdy = 0; OrderFlowTdy = 0; VolAtBidTdy = 0; VolAtAskTdy = 0; _securityObj = security; if (security == null) { throw new ArgumentException("security object must not be null", "security"); } if (previousMktState == null) { throw new ArgumentException("Previous MarketState object must not be null", "previousMktState"); } if (timeStamp == null) { throw new ArgumentException("timeStamp must not be null", "timeStamp"); } TimeStamp = timeStamp; FirstOfInterval = true; CopyPrevState(previousMktState, FirstOfInterval); StateType = MktStateType.Duplicate; }
private void SetAskVolChg(MarketState prevMktState) { if (_securityObj.HasQuoteSize) { if (Ask == prevMktState.Ask) { AskVolChg = (int)(AskVol - prevMktState.AskVol); AskVolChgSum += AskVolChg; SetAskVolChgCnt(AskVolChg); } else { if (Ask == prevMktState.Bid) // just ticked down { AskVolChg = (int)(AskVol + prevMktState.BidVol); AskVolChgSum += AskVolChg; SetAskVolChgCnt(AskVolChg); //Console.WriteLine(SecurityObj.Name + " went offered @" + timeStamp.ToLongTimeString()); } else { if (Ask == prevMktState.PrevBid) // just ticked down, but we need to look two data points back for price/volume { AskVolChg = (int)(AskVol + prevMktState.PrevBidVol); AskVolChgSum += AskVolChg; SetAskVolChgCnt(AskVolChg); //Console.WriteLine(SecurityObj.Name + " went offered @" + timeStamp.ToLongTimeString()); } } } } }
private bool DuplicateOfPrevDataPoint(TickData newData, MarketState current) { double currPrice = 0; bool hasSizeData = false; switch (newData.Type) { case Type.Ask: currPrice = current.Ask; hasSizeData = ((_securityObj.HasQuoteSize) && (newData.Size != current.AskVol)); break; case Type.Bid: currPrice = current.Bid; hasSizeData = ((_securityObj.HasQuoteSize) && (newData.Size != current.BidVol)); break; case Type.Trade: currPrice = current.LastTrdPrice; hasSizeData = (_securityObj.HasTradeSize); break; } // if it doesn't have size data and the price hasn't changed flag it as as duplicate return((!hasSizeData) && (Math.Abs(newData.Price - currPrice) < Double.Epsilon)); }
private MarketState GetLatestState() { MarketState currentState = null; if (_marketData.Count > 0) // check to make sure we have some data { // get the current market state SortedDictionary <uint, MarketState> currTimeBin = _marketData.ElementAt(_marketData.Count - 1).Value; currentState = currTimeBin.ElementAt(currTimeBin.Count - 1).Value; } return(currentState); }
public void NewTick(TickData newData) { DateTime currTimeBinTimeStamp = getCurrentInterval(newData.TimeStamp); if (currTimeBinTimeStamp > DateTime.MinValue) // check to make sure the initialzation has happend { // get the current market state SortedDictionary <uint, MarketState> currTimeBin = _marketData[currTimeBinTimeStamp]; MarketState currentState = currTimeBin.ElementAt(currTimeBin.Count - 1).Value; // last data point in time bin // disregard duplicates if (!DuplicateOfPrevDataPoint(newData, currentState)) { bool addedNewTimeStamp = false; if (!_marketData.ContainsKey(newData.TimeStamp)) { _marketData.Add(newData.TimeStamp, new SortedDictionary <uint, MarketState>()); _latestTimeBin = newData.TimeStamp > _latestTimeBin ? newData.TimeStamp : _latestTimeBin; addedNewTimeStamp = true; } var marketDataForTimeStamp = _marketData[newData.TimeStamp]; lock (marketDataForTimeStamp) { // create a new updated market state var newState = new MarketState(_securityObj, currentState, newData); newState.BinCnt = (uint)marketDataForTimeStamp.Count; marketDataForTimeStamp.Add(newState.BinCnt, newState); if (LogEachTick) { string output = newState.ToStringAllData(); if (newState.StateType == MktStateType.Trade) { output += " " + newState.ToStringAllTradesNoIndentity(); } Console.WriteLine(output); } } // let the market aggregator know there is a new timestamp to aggregate if (addedNewTimeStamp) { _markets.AddTickData(this, _marketData[newData.TimeStamp], newData.TimeStamp); } } } }
public TradesAtPrice(double price, MarketState state) { Price = price; TradeCount++; if (price == state.Bid) { CntAtBid++; } else if (price == state.Ask) { CntAtAsk++; } }
private void CopyPrevState(MarketState previous, bool isFirstOfInterval) { Bid = previous.Bid; BidVol = previous.BidVol; Ask = previous.Ask; AskVol = previous.AskVol; PrevBid = previous.PrevBid; PrevBidVol = previous.PrevBidVol; PrevAsk = previous.PrevAsk; PrevAskVol = previous.PrevAskVol; Mid = previous.Mid; MidScaled = previous.MidScaled; if (!isFirstOfInterval) { BidOpen = previous.BidOpen; BidVolOpen = previous.BidVolOpen; AskOpen = previous.AskOpen; AskVolOpen = previous.AskVolOpen; MidOpen = previous.MidOpen; MidScaledOpen = previous.MidScaledOpen; LastPriceOpn = previous.LastPriceOpn; VolAtBid = previous.VolAtBid; VolAtAsk = previous.VolAtAsk; TrdCntBid = previous.TrdCntBid; TrdCntAsk = previous.TrdCntAsk; BidVolChgSum = previous.BidVolChgSum; BidVolChgCnt = previous.BidVolChgCnt; AskVolChgSum = previous.AskVolChgSum; AskVolChgCnt = previous.AskVolChgCnt; TrdsAtPrice = previous.TrdsAtPrice; //BinCnt++; } else { BidOpen = previous.Bid; BidVolOpen = previous.BidVol; AskOpen = previous.Ask; AskVolOpen = previous.AskVol; MidOpen = previous.Mid; MidScaledOpen = previous.MidScaled; LastPriceOpn = previous.LastTrdPrice; } }
public void NewTradeAtPrice(uint volume, MarketState state) { TotalVolume += volume; TradeCount++; if (Price == state.Bid) { VolAtBid += volume; CntAtBid++; } else { if (Price == state.Ask) { VolAtAsk += volume; CntAtAsk++; } } }
private void SetBidVolChg(MarketState prevMktState) { if (_securityObj.HasQuoteSize) { if ((Bid == prevMktState.Bid)) { BidVolChg = (int)(BidVol - prevMktState.BidVol); BidVolChgSum += BidVolChg; SetBidVolChgCnt(BidVolChg); } else { if ((Bid == prevMktState.Ask)) // just ticked up { BidVolChg = (int)(BidVol + prevMktState.AskVol); BidVolChgSum += BidVolChg; SetBidVolChgCnt(BidVolChg); //Console.WriteLine(SecurityObj.Name + " went Bid @" + timeStamp.ToLongTimeString()); } else { if ((Bid == prevMktState.PrevAsk)) // just ticked up, but need to look two data points back { BidVolChg = (int)(BidVol + prevMktState.PrevAskVol); BidVolChgSum += BidVolChg; SetBidVolChgCnt(BidVolChg); //Console.WriteLine(SecurityObj.Name + " went Bid @" + timeStamp.ToLongTimeString()); } else { if ((Bid < PrevBid)) // just ticked down { BidVolChg = (int)(PrevBidVol); BidVolChgSum -= BidVolChg; SetBidVolChgCnt(BidVolChg); Console.WriteLine("{0} went offered @ {1} {2} {3} {4}", Name, TimeStamp.ToLongTimeString(), Bid.ToString(), PrevBid.ToString(), BidVolChg.ToString()); } } } } } }
public TradesAtPrice(double price, uint volume, MarketState state) { Price = price; TotalVolume = volume; TradeCount++; if (price == state.Bid) { VolAtBid += volume; CntAtBid++; } else { if (price == state.Ask) { VolAtAsk += volume; CntAtAsk++; } } }
public void FirstTick(TickData bid, TickData ask, TickData trade) { Console.WriteLine("Summary for " + _securityObj.Name); DateTime timeBin = bid.TimeStamp; // no timestamp if (!_mktInitialized) { _mktInitialized = true; _marketData.Add(timeBin, new SortedDictionary <uint, MarketState>()); lock (_marketData[timeBin]) { // initialize the market var newState = new MarketState(_securityObj, bid, ask, trade); // Add the new state to its time bin _marketData[newState.TimeStamp].Add(newState.BinCnt, newState); _markets.AddTickData(this, _marketData[newState.TimeStamp], newState.TimeStamp); } } }
private string MarketStateToString(MarketState lastTick) { string output = lastTick.ToFlatFileStringAllData() + lastTick.ToFlatFileStringAllTrades(5); return(output); }
private void WriteOutFlatFile(OutPutMktMode mktMode, string filePath, int cutOffHour) { bool headerCreated = false; Dictionary <Security, MktOutput> MktsOutPut = new Dictionary <Security, MktOutput>(); foreach (var dataFactory in _securitites) { MktsOutPut.Add(dataFactory.SecurityObj, new MktOutput() { basePath = filePath, baseExtension = ".csv", security = dataFactory.SecurityObj }); } DateTime date = DateTime.MinValue; List <string> dataCacheAll = new List <string>(); Dictionary <Security, string> dataCacheByMkt = new Dictionary <Security, string>(); StringBuilder fileName = new StringBuilder(); StringBuilder allMktsHeader = new StringBuilder(); foreach (var timeStamp in Markets) { // calculate the header using the tickdata's built-in funnction if (!headerCreated) { foreach (var security in timeStamp.Value) { MarketState marketState = security.Value[0]; string mktHeaderString = marketState.GetHeadersString() + marketState.GetTradesHeaderString(5); allMktsHeader.Append(mktHeaderString); MktsOutPut[security.Key].header = mktHeaderString; } Console.WriteLine(allMktsHeader.ToString()); dataCacheAll.Add(allMktsHeader.ToString()); headerCreated = true; } StringBuilder data = new StringBuilder(); bool resetDate = false; foreach (var security in timeStamp.Value) { MktOutput mktOutPut = MktsOutPut[security.Key]; // Output a new file for each day. The end of each day is defined by a cutOffHour DateTime current = timeStamp.Key; if (date == DateTime.MinValue || ((current.Day != date.Day) && (current.Hour >= cutOffHour))) { if (resetDate == false) { resetDate = true; if (mktMode == OutPutMktMode.AggregatedMkts) { if (dataCacheAll.Count > 0) { writeCacheToFile(fileName.ToString(), dataCacheAll); dataCacheAll.Add(allMktsHeader.ToString()); } } } if ((mktMode == OutPutMktMode.SeperateMkts) || (mktMode == OutPutMktMode.BothMkts)) { // output each of the individual markets data if (mktOutPut.dataCache.Count > 0) { writeCacheToFile(mktOutPut.filePath.ToString(), mktOutPut.dataCache); } } // construct the new file name fileName.Clear(); fileName.Append(filePath); string dateStr = current.Year.ToString() + current.Month.ToString("00") + current.Day.ToString("00"); switch (mktMode) { case OutPutMktMode.SeperateMkts: mktOutPut.SetFilePath(dateStr); break; case OutPutMktMode.BothMkts: mktOutPut.SetFilePath(dateStr); fileName.Append("All_Mkts_"); break; case OutPutMktMode.AggregatedMkts: default: fileName.Append("All_Mkts_"); break; } fileName.Append(dateStr); fileName.Append(".csv"); } if ((mktMode == OutPutMktMode.AggregatedMkts) || (mktMode == OutPutMktMode.BothMkts)) { MarketState lastTick = security.Value[(uint)(security.Value.Count - 1)]; data.Append(MarketStateToString(lastTick) + ","); } else { foreach (var mktStates in security.Value) { mktOutPut.dataCache.Add(MarketStateToString(mktStates.Value) + ","); } } } if (resetDate) { date = timeStamp.Key; // reset the date if we moved passed the cut off for a new day } if ((mktMode == OutPutMktMode.AggregatedMkts) || (mktMode == OutPutMktMode.BothMkts)) { dataCacheAll.Add(data.ToString()); } } if ((mktMode == OutPutMktMode.AggregatedMkts) || (mktMode == OutPutMktMode.BothMkts)) { if (dataCacheAll.Count > 0) { writeCacheToFile(fileName.ToString(), dataCacheAll); } } if ((mktMode == OutPutMktMode.SeperateMkts) || (mktMode == OutPutMktMode.BothMkts)) { // output each of the individual markets final data set foreach (var mktOutPut in MktsOutPut.Values) { if (mktOutPut.dataCache.Count > 0) { writeCacheToFile(mktOutPut.filePath.ToString(), mktOutPut.dataCache); } } } }
public TradesAtPrice(double price, MarketState state) { Price = price; TradeCount++; if (price == state.Bid) CntAtBid++; else if (price == state.Ask) CntAtAsk++; }
private bool DuplicateOfPrevDataPoint(TickData newData, MarketState current) { double currPrice = 0; bool hasSizeData = false; switch (newData.Type) { case Type.Ask: currPrice = current.Ask; hasSizeData = ((_securityObj.HasQuoteSize) && (newData.Size != current.AskVol)); break; case Type.Bid: currPrice = current.Bid; hasSizeData = ((_securityObj.HasQuoteSize) && (newData.Size != current.BidVol)); break; case Type.Trade: currPrice = current.LastTrdPrice; hasSizeData = (_securityObj.HasTradeSize); break; } // if it doesn't have size data and the price hasn't changed flag it as as duplicate return ((!hasSizeData) && (Math.Abs(newData.Price - currPrice) < Double.Epsilon)); }
public void FirstTick(TickData bid, TickData ask, TickData trade) { Console.WriteLine("Summary for " + _securityObj.Name); DateTime timeBin = bid.TimeStamp; // no timestamp if (!_mktInitialized) { _mktInitialized = true; _marketData.Add(timeBin, new SortedDictionary<uint, MarketState>()); lock (_marketData[timeBin]) { // initialize the market var newState = new MarketState(_securityObj, bid, ask, trade); // Add the new state to its time bin _marketData[newState.TimeStamp].Add(newState.BinCnt, newState); _markets.AddTickData(this, _marketData[newState.TimeStamp], newState.TimeStamp); } } }
// constructor used to create a duplicate a state using a new timestamp public MarketState(Security security, MarketState previousMktState, DateTime timeStamp) { TimeStamp = DateTime.MinValue; VolumeTdy = 0; OrderFlowTdy = 0; VolAtBidTdy = 0; VolAtAskTdy = 0; _securityObj = security; if (security == null) throw new ArgumentException("security object must not be null", "security"); if (previousMktState == null) throw new ArgumentException("Previous MarketState object must not be null", "previousMktState"); if (timeStamp == null) throw new ArgumentException("timeStamp must not be null", "timeStamp"); TimeStamp = timeStamp; FirstOfInterval = true; CopyPrevState(previousMktState, FirstOfInterval); StateType = MktStateType.Duplicate; }
// constructor used for each successive data event after the initial market summary event public MarketState(Security security, MarketState previousMktState, TickData tickData) { VolumeTdy = 0; OrderFlowTdy = 0; VolAtBidTdy = 0; VolAtAskTdy = 0; _securityObj = security; if (previousMktState == null) { throw new ArgumentException("Previous MarketState object must not be null", "previousMktState"); } if (tickData.TimeStamp == null) { throw new ArgumentException("tickData.TimeStamp must not be null", "tickData.TimeStamp"); } TimeStamp = tickData.TimeStamp; FirstOfInterval = (tickData.TimeStamp.Subtract(previousMktState.TimeStamp).TotalSeconds > 0); CopyPrevState(previousMktState, FirstOfInterval); switch (tickData.Type) { case Type.Ask: StateType = MktStateType.Ask; OnAskQuote(tickData); SetAskVolChg(previousMktState); SetMid(); if (FirstOfInterval) { SetAskOpen(tickData); SetMidOpen(); } break; case Type.Bid: StateType = MktStateType.Bid; OnBidQuote(tickData); SetBidVolChg(previousMktState); SetMid(); if (FirstOfInterval) { SetBidOpen(tickData); SetMidOpen(); } break; case Type.Trade: StateType = MktStateType.Trade; OnTrade(tickData); if (FirstOfInterval) { SetTradeOpn(tickData); } break; default: throw new ArgumentException("TickData's 'Type' parameter must be of enum of type TickData.Type", "tickData"); } if ((FirstOfInterval) || (Codes == null)) { Codes = tickData.Codes; } else { if (tickData.Codes != null) { if (tickData.Codes.Count > 0) { foreach (var code in tickData.Codes) { if (!Codes.ContainsKey(code.Key)) { Codes.Add(code.Key, code.Value); } } } } } }
private string MarketStateToString(MarketState lastTick, int numTradePrices) { string output = lastTick.ToFlatFileStringAllData() + lastTick.ToFlatFileStringAllTrades(numTradePrices); return(output); }
private string MarketStateToString(MarketState lastTick) { string output = lastTick.ToFlatFileStringAllData() + lastTick.ToFlatFileStringAllTrades(5); return output; }
public void NewTick(TickData newData) { DateTime currTimeBinTimeStamp = getCurrentInterval(newData.TimeStamp); if (currTimeBinTimeStamp > DateTime.MinValue) // check to make sure the initialzation has happend { // get the current market state SortedDictionary<uint, MarketState> currTimeBin = _marketData[currTimeBinTimeStamp]; MarketState currentState = currTimeBin.ElementAt(currTimeBin.Count - 1).Value; // last data point in time bin // disregard duplicates if (!DuplicateOfPrevDataPoint(newData, currentState)) { bool addedNewTimeStamp = false; if (!_marketData.ContainsKey(newData.TimeStamp)) { _marketData.Add(newData.TimeStamp, new SortedDictionary<uint, MarketState>()); addedNewTimeStamp = true; } var marketDataForTimeStamp = _marketData[newData.TimeStamp]; lock (marketDataForTimeStamp) { // create a new updated market state var newState = new MarketState(_securityObj, currentState, newData); newState.BinCnt = (uint) marketDataForTimeStamp.Count; marketDataForTimeStamp.Add(newState.BinCnt, newState); if (LogEachTick) { string output = newState.ToStringAllData(); if (newState.StateType == MktStateType.Trade) output += " " + newState.ToStringAllTradesNoIndentity(); Console.WriteLine(output); } } // let the market aggregator know there is a new timestamp to aggregate if (addedNewTimeStamp) _markets.AddTickData(this, _marketData[newData.TimeStamp], newData.TimeStamp); } } }
// constructor used for each successive data event after the initial market summary event public MarketState(Security security, MarketState previousMktState, TickData tickData) { VolumeTdy = 0; OrderFlowTdy = 0; VolAtBidTdy = 0; VolAtAskTdy = 0; _securityObj = security; if (previousMktState == null) throw new ArgumentException("Previous MarketState object must not be null", "previousMktState"); if (tickData.TimeStamp == null) throw new ArgumentException("tickData.TimeStamp must not be null", "tickData.TimeStamp"); TimeStamp = tickData.TimeStamp; FirstOfInterval = (tickData.TimeStamp.Subtract(previousMktState.TimeStamp).TotalSeconds > 0); CopyPrevState(previousMktState, FirstOfInterval); switch (tickData.Type) { case Type.Ask: StateType = MktStateType.Ask; OnAskQuote(tickData); SetAskVolChg(previousMktState); SetMid(); if (FirstOfInterval) { SetAskOpen(tickData); SetMidOpen(); } break; case Type.Bid: StateType = MktStateType.Bid; OnBidQuote(tickData); SetBidVolChg(previousMktState); SetMid(); if (FirstOfInterval) { SetBidOpen(tickData); SetMidOpen(); } break; case Type.Trade: StateType = MktStateType.Trade; OnTrade(tickData); if (FirstOfInterval) { SetTradeOpn(tickData); } break; default: throw new ArgumentException("TickData's 'Type' parameter must be of enum of type TickData.Type", "tickData"); } if ((FirstOfInterval) || (Codes == null)) { Codes = tickData.Codes; } else { if (tickData.Codes != null) { if (tickData.Codes.Count > 0) { foreach (var code in tickData.Codes) { if (!Codes.ContainsKey(code.Key)) Codes.Add(code.Key, code.Value); } } } } }
public SortedDictionary<uint, MarketState> DuplicateTick(SortedDictionary<uint, MarketState> mostRecentState, DateTime timestamp) { // create a new time stamp if it doesn't exist (Only exist if there is a race condition) if (!_marketData.ContainsKey(timestamp)) _marketData.Add(timestamp, new SortedDictionary<uint, MarketState>()); var marketDataForTimeStamp = _marketData[timestamp]; lock (marketDataForTimeStamp) { DateTime newTimeStamp = DateTime.MinValue; var prevState = mostRecentState[(uint)mostRecentState.Count - 1]; var newState = new MarketState(_securityObj, prevState, timestamp); SortedDictionary<uint, MarketState> timeBin = _marketData[timestamp]; newState.BinCnt = (uint)marketDataForTimeStamp.Count; marketDataForTimeStamp.Add(newState.BinCnt, newState); if (LogEachTick) { string output = newState.ToStringAllData(); if (newState.StateType == MktStateType.Trade) output += " " + newState.ToStringAllTradesNoIndentity(); Console.WriteLine(output); } } return _marketData[timestamp]; }