/// <summary> /// Raised when Historic Bars are recieved from MessageProcessor /// </summary> private void OnHistoricBarsArrived(HistoricBarData historicBarData, string strategyId) { try { if (Logger.IsDebugEnabled) { Logger.Debug("Historic Bars Event recieved from MessageProcessor.", _type.FullName, "OnHistoricBarsArrived"); } Dictionary <string, ClientMqParameters> strategyInfo; if (_strategiesMap.TryGetValue(strategyId, out strategyInfo)) { if (Logger.IsDebugEnabled) { Logger.Debug("Publishing Message for: " + strategyId, _type.FullName, "OnHistoricBarsArrived"); } // Create EasyNetQ message to be published Message <HistoricBarData> message = new Message <HistoricBarData>(historicBarData); // Publish Messages on the exchange PublishMessages(strategyInfo["HistoricBar"], message); } } catch (Exception exception) { Logger.Error(exception, _type.FullName, "OnHistoricBarsArrived"); } }
/// <summary> /// Raised when Historical bar data is received from Simulated Exchange /// </summary> /// <param name="historicBarData">Contains required Historical Data Info</param> private void MarketDataControlerHistoricBarArrived(HistoricBarData historicBarData) { if (HistoricBarDataArrived != null) { HistoricBarDataArrived(historicBarData); } }
/// <summary> /// Called when MDE-Client receives new historic bar data message from MDE-Server /// </summary> private void OnHistoricBarsArrived(HistoricBarData historicBarData) { try { if (_asyncClassLogger.IsDebugEnabled) { _asyncClassLogger.Debug("Data received from: " + historicBarData.MarketDataProvider, _type.FullName, "OnHistoricBarsArrived"); } HistoricDataRequest barsInformation; if (_subscriptionRequests.TryGetValue(historicBarData.ReqId, out barsInformation)) { // Insert received bars information historicBarData.BarsInformation = barsInformation; } if (_historicalDataArrived != null) { _historicalDataArrived(historicBarData); } } catch (Exception exception) { _asyncClassLogger.Error(exception, _type.FullName, "OnHistoricBarsArrived"); } }
/// <summary> /// Reads data for required symbol from stored files /// </summary> /// <param name="historicDataRequest">Contains historical request info for subscribing symbol</param> public virtual void ReadData(HistoricDataRequest historicDataRequest) { try { // Get all available Bars var barlist = _readMarketData.ReadBars(_providerName, historicDataRequest); // Create new historic bar data response object HistoricBarData historicBarData = new HistoricBarData(historicDataRequest.Security, MarketDataProvider.SimulatedExchange, DateTime.Now); // Add Bars historicBarData.Bars = barlist.ToArray(); // Add Request ID historicBarData.ReqId = historicDataRequest.Id; // Raise event to notify listeners if (HistoricalDataFired != null) { HistoricalDataFired(historicBarData); } } catch (Exception exception) { Logger.Error(exception, _type.FullName, "ReadData"); } }
/// <summary> /// Called when Historical Bar data is received from Market Data Server /// </summary> /// <param name="historicBarData">Contains requested Historical data details</param> private void OnHistoricalDataArrived(HistoricBarData historicBarData) { if (_historicalDataArrivedEvent != null) { _historicalDataArrivedEvent(historicBarData); } }
/// <summary> /// Called when requested Historical data is received /// </summary> /// <param name="historicBarData"></param> private void OnHistoricalDataArrived(HistoricBarData historicBarData) { // Raise Event to notify listeners if (HistoricBarDataArrived != null) { HistoricBarDataArrived(historicBarData); } }
/// <summary> /// Called when Historical data is received /// </summary> /// <param name="historicBarData">Contains requested historical bars</param> public override void OnHistoricalDataArrived(HistoricBarData historicBarData) { ConsoleWriter.WriteLine(ConsoleColor.Green, "Historical data received : " + historicBarData); //ConsoleWriter.WriteLine(ConsoleColor.Green, "Sleepng for 50 ms"); if (Logger.IsInfoEnabled) { Logger.Info("Historical data received : " + historicBarData, _type.FullName, "OnTickArrived"); } }
/// <summary> /// Writes Historical Bar Data to csv file. /// </summary> /// <param name="historicBarData"></param> public void Write(HistoricBarData historicBarData) { try { WriteHistoricBarsToFile(historicBarData); } catch (Exception exception) { Logger.Error(exception, _oType.FullName, "Write - Historic"); } }
/// <summary> /// Event Fired When Historic Bars Arrives /// </summary> /// <param name="obj"></param> private void MarketDataEngineClientHistoricBarsArrived(HistoricBarData obj) { try { _blockingCollectionHistoricBarData.TryAdd(obj); } catch (Exception exception) { Logger.Error(exception, _oType.FullName, "MarketDataEngineClientHistoricBarsArrived"); } }
/// <summary> /// Handles historic data event. /// </summary> private void OnHistoricDataUpdate(object sender, MsgHistResponse histMsg) { try { if (Logger.IsDebugEnabled) { Logger.Debug("Historic Data recieved for: " + histMsg.Symbol.Value + " containing: " + histMsg.BarCount + " bars.", _type.FullName, "OnHistoricDataUpdate"); } Bar[] bars = new Bar[histMsg.BarCount]; int count = 0; foreach (MsgHistResponse.BarData barData in histMsg.Bars) { // Create new TradeHub Bar Bar bar = new Bar(new Security() { Symbol = histMsg.Symbol.Value }, _marketDataProviderName, histMsg.ReqID.Value.ToString(CultureInfo.InvariantCulture), barData.Time) { Close = Convert.ToDecimal(barData.Close), Open = Convert.ToDecimal(barData.Open), High = Convert.ToDecimal(barData.High), Low = Convert.ToDecimal(barData.Low), Volume = barData.Volume }; // Add to the Array bars[count] = bar; count++; } // Create Historical Bar Data Message to be sent HistoricBarData historicBarData = new HistoricBarData(new Security() { Symbol = histMsg.Symbol.Value }, _marketDataProviderName, DateTime.Now) { Bars = bars, ReqId = histMsg.ReqID.Value.ToString(CultureInfo.InvariantCulture) }; // Raise Event if (HistoricBarDataArrived != null) { HistoricBarDataArrived(historicBarData); } } catch (Exception exception) { Logger.Error(exception, _type.FullName, "OnHistoricDataUpdate"); } }
/// <summary> /// Saves Historic bar data in Binary format /// </summary> /// <param name="historicBarData"></param> private void PersistBinaryData(HistoricBarData historicBarData) { try { // Write new Historical bar data in binary format _writerBinany.Write(historicBarData); } catch (Exception exception) { Logger.Error(exception, _type.FullName, "PersistBinaryData - HistoricBar"); } }
/// <summary> /// Called when a publisher has committed an event to the <see cref="T:Disruptor.RingBuffer`1"/> /// </summary> /// <param name="data">Data committed to the <see cref="T:Disruptor.RingBuffer`1"/></param><param name="sequence">Sequence number committed to the <see cref="T:Disruptor.RingBuffer`1"/></param><param name="endOfBatch">flag to indicate if this is the last event in a batch from the <see cref="T:Disruptor.RingBuffer`1"/></param> public void OnNext(HistoricBarData data, long sequence, bool endOfBatch) { // Save in CSV format if (_persistCsv) { PersistCsvData(data); } // Save in Binary format if (_persistBinary) { PersistBinaryData(data); } }
/// <summary> /// Publishes Bar Data /// </summary> public void PublishHistoricData(HistoricBarData historicBarData) { try { // Message to be published IMessage <HistoricBarData> message = new Message <HistoricBarData>(historicBarData); // Publish message _easyNetQBus.Publish(_easyNetQExchange, _historickDataQueueRoutingKey, true, false, message); } catch (Exception exception) { Logger.Error(exception, _type.FullName, "PublishHistoricData"); } }
/// <summary> /// Fire Historical Bar Data /// </summary> /// <param name="historicBarData">TradeHub HistoricalBarData contains requested historical bars</param> private void FetchMarketDataHistoricDataFired(HistoricBarData historicBarData) { try { _communicationController.PublishHistoricData(historicBarData); if (Logger.IsDebugEnabled) { Logger.Debug(historicBarData.ToString(), _type.FullName, "FetchMarketDataHistoricDataFired"); } } catch (Exception exception) { Logger.Error(exception, _type.FullName, "FetchMarketDataHistoricDataFired"); } }
/// <summary> /// Saves Historic Bar data to Medium /// </summary> public void SaveHistoricBarData(HistoricBarData obj) { try { if (Logger.IsDebugEnabled) { Logger.Info(obj.ToString(), _oType.FullName, "SaveHistoricBarData"); } _writerCsv.Write(obj); } catch (Exception exception) { Logger.Error(exception, _oType.FullName, "SaveHistoricBarData"); } }
/// <summary> /// Read Historic Data from Queue /// </summary> private void ReadHistoricBarFromQueue() { try { while (true) { HistoricBarData barData = _blockingCollectionHistoricBarData.Take(); _responseHandler.SaveHistoricBarData(barData); EventSystem.Publish(barData); } } catch (Exception exception) { Logger.Error(exception, _oType.FullName, "ReadTicksFromQueue"); } }
/// <summary> /// Handles incoming Historical Bar data for persistence /// </summary> /// <param name="historicBarData"></param> private void HandleHistoricBarData(HistoricBarData historicBarData) { // Send Historical Bar data to Disruptor _historicPublisher.PublishEvent((historicBarObject, sequenceNo) => { // Set parameters historicBarObject.ReqId = historicBarData.ReqId; historicBarObject.Bars = (Bar[])historicBarData.Bars.Clone(); historicBarObject.Security = historicBarData.Security; historicBarObject.DateTime = historicBarData.DateTime; historicBarObject.MarketDataProvider = historicBarData.MarketDataProvider; historicBarObject.BarsInformation = historicBarData.BarsInformation; // Return updated object return(historicBarObject); }); }
/// <summary> /// Process incoming historical data to extract information /// </summary> /// <param name="receivedBarData"></param> private void ProcessHistoricalData(string receivedBarData) { using (StringReader reader = new StringReader(receivedBarData)) { string singleLine; while ((singleLine = reader.ReadLine()) != null) { if (singleLine.Contains("ENDMSG")) { // Notify listeners if (_barCollection.Count > 0) { if (_historicalDataEvent != null) { // Create historical data object var historicalData = new HistoricBarData(_barCollection[0].Security, _marketDataProviderName, DateTime.UtcNow); historicalData.Bars = _barCollection.ToArray(); historicalData.ReqId = _barCollection[0].RequestId; // Raise Event _historicalDataEvent(historicalData); } } // Remove ID to Symbol mapping of received data _requestIdToSymbolsMap.Remove(_barCollection[0].RequestId); // Clear collection for future use _barCollection.Clear(); } else { // Convert into TradeSharp Bar message var bar = ParseHistoricalBarData(singleLine); // Add to collection if (bar != null) { _barCollection.Add(bar); } } } } }
/// <summary> /// Create TradeHub Historical Bar Data object from the Tradier data /// </summary> /// <param name="tradierHistoricalData"></param> /// <param name="security"></param> /// <param name="requestId"></param> private HistoricBarData CreateHistoricalBarData(TradierHistoricalData tradierHistoricalData, Security security, string requestId) { try { // Create new TradeHub Historical Bar Data object to be used withing the application HistoricBarData historicBarData = new HistoricBarData(security, _marketDataProviderName, DateTime.UtcNow); Bar[] bars = new Bar[tradierHistoricalData.history.day.Count]; int count = 0; foreach (var historicalDataDetail in tradierHistoricalData.history.day) { var time = DateTime.ParseExact(historicalDataDetail.date, "yyyy-MM-dd", CultureInfo.InvariantCulture); // Create new TradeHub Bar Bar bar = new Bar(security, _marketDataProviderName, requestId, time) { Close = Convert.ToDecimal(historicalDataDetail.close), Open = Convert.ToDecimal(historicalDataDetail.open), High = Convert.ToDecimal(historicalDataDetail.high), Low = Convert.ToDecimal(historicalDataDetail.low), Volume = historicalDataDetail.volume }; // Add to the Array bars[count] = bar; count++; } // Add Bars array to root object historicBarData.Bars = bars; // Add Request ID to root object historicBarData.ReqId = requestId; return(historicBarData); } catch (Exception exception) { _logger.Error(exception, _type.FullName, "CreateHistoricalBarData"); return(null); } }
/// <summary> /// Called when Historical Bars are recieved /// </summary> /// <param name="historicBarData">Incoming TradeHub HistoricalBarData</param> private void OnHistoricalBarDataArrived(HistoricBarData historicBarData) { try { if (_asyncClassLogger.IsDebugEnabled) { _asyncClassLogger.Debug("Historical Bar Data recieved from Market Data Engine: " + historicBarData, _type.FullName, "OnHistoricalBarDataArrived"); } if (_historicBarsArrived != null) { _historicBarsArrived(historicBarData); } } catch (Exception exception) { _asyncClassLogger.Error(exception, _type.FullName, "OnHistoricalBarDataArrived"); } }
/// <summary> /// Fire Historical Bar Data /// </summary> /// <param name="historicBarData">TradeHub HistoricalBarData contains requested historical bars</param> private void HistoricDataArrived(HistoricBarData historicBarData) { try { if (HistoricDataReceived != null) { HistoricDataReceived(historicBarData); } if (_classLogger.IsDebugEnabled) { _classLogger.Debug(historicBarData.ToString(), _type.FullName, "HistoricDataArrived"); } } catch (Exception exception) { _classLogger.Error(exception, _type.FullName, "HistoricDataArrived"); } }
/// <summary> /// Writes Historical bar data to binary file /// </summary> /// <param name="historicBarData"></param> public void Write(HistoricBarData historicBarData) { }
/// <summary> /// Called when request historical bar data is received from Market Data Server /// </summary> /// <param name="historicBarData"></param> private void OnHistoricalDataArrived(HistoricBarData historicBarData) { EventSystem.Publish <HistoricBarData>(historicBarData); }
/// <summary> /// Save Historic Bars To Csv File /// The also check that no Data is repeated. /// </summary> /// <param name="historicBarData"></param> private void WriteHistoricBarsToFile(HistoricBarData historicBarData) { try { int barLength = 60; // Retrieve orignal bar information for bar length if (historicBarData.BarsInformation != null) { barLength = (int)historicBarData.BarsInformation.Interval; } // Manually calculate bar length else { barLength = Convert.ToInt32( historicBarData.Bars[1].DateTime.Subtract(historicBarData.Bars[0].DateTime).TotalSeconds); } var date = historicBarData.Bars[0].DateTime.Date; var path = CreateDirectoryPathForHistoricBarObject( historicBarData.Bars[0].Security.Symbol, historicBarData.MarketDataProvider, barLength) + "//" + historicBarData.Bars[0].DateTime.Date.ToString("yyyyMMdd") + ".txt"; if (File.Exists(path)) { // Removes The File File.Delete(path); } // Traverse all bars foreach (Bar bar in historicBarData.Bars) { if (date != bar.DateTime.Date) { path = CreateDirectoryPathForHistoricBarObject(bar.Security.Symbol, historicBarData.MarketDataProvider, barLength) + "//" + bar.DateTime.Date.ToString("yyyyMMdd") + ".txt"; if (File.Exists(path)) { // Removes The File File.Delete(path); } date = bar.DateTime.Date; } try { StreamWriter sw = File.AppendText(path); //Using Reflection to write data to file sw.Write(bar.Close); sw.Write(","); sw.Write(bar.Open); sw.Write(","); sw.Write(bar.High); sw.Write(","); sw.Write(bar.Low); sw.Write(","); sw.Write(bar.Volume); sw.Write(","); sw.Write(bar.Security.Symbol); sw.Write(","); sw.Write(bar.DateTime.ToString("M/d/yyyy h:mm:ss tt")); sw.Write(","); sw.Write(bar.MarketDataProvider); sw.Write(Environment.NewLine); sw.Close(); } catch (Exception exception) { Logger.Error(exception, _oType.FullName, "WriteBarDataToFile"); } } } catch (Exception exception) { Logger.Error("Outer Exception" + exception, _oType.FullName, "WriteHistoricBarsToFile"); } }
/// <summary> /// Called when Historical Bars are received /// </summary> /// <param name="historicBarData">Contains all the historical data previously requested</param> public override void OnHistoricalDataArrived(HistoricBarData historicBarData) { }
/// <summary> /// Save Historic Bars To Csv File /// The also check that no Data is repeated. /// </summary> /// <param name="historicBarData"></param> private void WriteHistoricBarsToFile(HistoricBarData historicBarData) { try { var barLength = Convert.ToInt32(historicBarData.Bars[1].DateTime.Subtract(historicBarData.Bars[0].DateTime).TotalSeconds); var date = historicBarData.Bars[0].DateTime.Date; var path = CreateDirectoryPathForHistoricBarObject(historicBarData.Bars[0].Security.Symbol, historicBarData.MarketDataProvider, barLength) + "//" + historicBarData.Bars[0].DateTime.Date.ToString("yyyyMMdd") + ".txt"; if (File.Exists(path)) { // Removes The File File.Delete(path); } foreach (Bar bar in historicBarData.Bars) { if (date != bar.DateTime.Date) { path = CreateDirectoryPathForHistoricBarObject(bar.Security.Symbol, historicBarData.MarketDataProvider, barLength) + "//" + bar.DateTime.Date.ToString("yyyyMMdd") + ".txt"; if (File.Exists(path)) { // Removes The File File.Delete(path); } date = bar.DateTime.Date; } try { StreamWriter sw = File.AppendText(path); //Using Reflection to write data to file sw.Write(bar.Close); sw.Write(","); sw.Write(bar.Open); sw.Write(","); sw.Write(bar.High); sw.Write(","); sw.Write(bar.Low); sw.Write(","); sw.Write(bar.Volume); sw.Write(","); sw.Write(bar.Security.Symbol); sw.Write(","); sw.Write(bar.DateTime); sw.Write(","); sw.Write(bar.MarketDataProvider); sw.Write(Environment.NewLine); sw.Close(); } catch (Exception exception) { Logger.Error(exception, _oType.FullName, "WriteBarDataToFile"); } } } catch (Exception exception) { Logger.Error("Outer Exception" + exception, _oType.FullName, "WriteHistoricBarsToFile"); } }
/// <summary> /// Fetches historical data from FXCM server /// </summary> /// <param name="historicDataRequest"></param> public void GetHistoricalData(HistoricDataRequest historicDataRequest) { // Create a new factory O2GRequestFactory factory = _session.getRequestFactory(); // Get time frame O2GTimeframe timeframe = factory.Timeframes[GetFxcmTimeFrame(historicDataRequest)]; if (timeframe == null) { _logger.Error("Invalid time format", _type.FullName, "GetHistoricalData"); return; } // Clear any existing data _barCollection.Clear(); // Request historical snapshot O2GRequest request = factory.createMarketDataSnapshotRequestInstrument(historicDataRequest.Security.Symbol, timeframe, 300); DateTime endTime = historicDataRequest.EndTime; do // cause there is limit for returned candles amount { factory.fillMarketDataSnapshotRequestTime(request, historicDataRequest.StartTime, endTime, false); SetRequestId(request.RequestID); _session.sendRequest(request); if (!WaitEvents()) { _logger.Error("Response waiting timeout expired", _type.FullName, "GetHistoricalData"); return; } // Shift "to" bound to oldest datetime of returned data O2GResponse response = GetResponse(); if (response != null && response.Type == O2GResponseType.MarketDataSnapshot) { O2GResponseReaderFactory readerFactory = _session.getResponseReaderFactory(); if (readerFactory != null) { O2GMarketDataSnapshotResponseReader reader = readerFactory.createMarketDataSnapshotReader(response); if (reader.Count > 0) { if (DateTime.Compare(endTime, reader.getDate(0)) != 0) { endTime = reader.getDate(0); // earliest datetime of returned data } else { break; } } else { if (_logger.IsInfoEnabled) { _logger.Info("Response waiting timeout expired", _type.FullName, "GetHistoricalData"); } break; } } ExtractPrices(_session, response, historicDataRequest); } else { break; } } while (endTime > historicDataRequest.StartTime); if (_barCollection.Count > 0) { if (_historicalDataEvent != null) { _barCollection.Reverse(); // Create historical data object var historicalData = new HistoricBarData(historicDataRequest.Security, historicDataRequest.MarketDataProvider, DateTime.UtcNow); historicalData.Bars = _barCollection.ToArray(); historicalData.ReqId = historicDataRequest.Id; // Raise Event _historicalDataEvent(historicalData); } _barCollection.Clear(); } }