public string SubmitCloses(CloseSnapDTO[] closes) { using (SLog.NDC(ClientIP())) { SLog.log.InfoFormat("SubmitCloses({0})", closes.Count()); try { var sw = Stopwatch.StartNew(); var obj = ServiceHelpers.SubmitCloses(closes); SLog.log.DebugFormat("Submit closes done in {0} ms.", sw.ElapsedMilliseconds); } catch (Exception ex) { SLog.log.Error("SubmitCloses", ex); } } return "OK"; }
public static int SubmitCloses(CloseSnapDTO[] closes) { int numSaved = 0; try { using(var dc = Symmetry.Data.SymmetryDataSource.ReadWrite()) { var knownSources = new Dictionary<string, int>(); var instruments = dc.FIs.Select(i => new { i.SymmetryCode, i.FIID, i.InstrumentType }).ToDictionary(i => i.SymmetryCode.ToUpper(), i => new { i.FIID, i.InstrumentType} ); var snapFile = new StringBuilder(); snapFile.AppendLine("snap,date,source,instrument,type,value,force"); // go by snap then by date foreach (var closeSnap in closes.GroupBy(c => c.Close)) { foreach (var closeSnapOnDate in closeSnap.GroupBy(c => c.ValueDate)) { var snap = QuoteHelpers.GetOrCreateMarketSnap(closeSnap.Key, closeSnapOnDate.Key, dc, ThrowBehavior.Throw); SLog.log.InfoFormat("Attempting to process {0} closes for close {1} date {2}", closeSnapOnDate.Count(), closeSnap.Key, closeSnapOnDate.Key); foreach (var close in closeSnapOnDate) { try { var sourceCode = close.Source.Trim().ToUpper(); if (!knownSources.ContainsKey(sourceCode)) { var source = QuoteHelpers.GetOrCreateQuoteSource(sourceCode, dc, ThrowBehavior.Throw); knownSources.Add(sourceCode, source.QuoteSourceID); } int sourceID = knownSources[sourceCode]; //int fiid = 0; var instrumentCode = close.SymmetryCode.Trim().ToUpper(); QuoteValueType quoteType = QuoteValueType.NotSet; switch (close.QuoteType.Trim().ToLower()) { case "price": quoteType = QuoteValueType.Price; break; case "yield": quoteType = QuoteValueType.Yield; break; case "repo rate": quoteType = QuoteValueType.RepoRate; break; case "implied vol": quoteType = QuoteValueType.ImpliedVol; break; default: throw new Exception(String.Format("Unknown quote type {0} for instrument {1} with value {2}", close.QuoteType, instrumentCode, close.QuoteValue)); } if(instruments.ContainsKey(instrumentCode)) { var dictionaryValue = instruments[instrumentCode]; if (ConfigHelpers.GetConfigBool("SaveIRFixingsOnly") && quoteType==QuoteValueType.Price && dictionaryValue.InstrumentType != InstrumentType.IRFixing) continue; snapFile.AppendFormat("{0},{1},{2},{3},{4},{5},{6}{7}", close.Close, close.ValueDate, sourceCode, instrumentCode, close.QuoteType, close.QuoteValue, close.Force, Environment.NewLine); if (QuoteHelpers.SaveQuote(snap, sourceID, dictionaryValue.FIID, quoteType, close.QuoteValue, DateTime.UtcNow, dc, ThrowBehavior.Throw, close.Force)) { numSaved++; } } } catch (Exception ex) { SLog.log.Error(String.Format("Error processing close for instrument {0} source {1} value {2}", close.SymmetryCode, close.Source, close.QuoteValue), ex); } } } } try { var fileName = System.IO.Path.Combine(ConfigHelpers.GetConfigString("SnapFileDir"), String.Format("submitcloses_{0}_{1}.csv", (sCounter++), DateTime.UtcNow.ToString("yyyyMMdd_HHmmssfff"))); System.IO.File.WriteAllText( fileName, snapFile.ToString()); SLog.log.InfoFormat("Written closes to {0}", fileName); } catch (Exception ex) { SLog.log.Warn("Error saving snaps to file", ex); } } } catch(Exception ex) { SLog.log.Error("CloseSnap", ex); } return numSaved; }