void updateSingleQuoteHistory(IDalSession session, TradeableInstrument instrument, string sISIN, string sExchange) { XmlTextReader reader = null; string companyname = ""; string issueexchange = ""; string issuename = ""; string issueid = ""; string quotecurrency = ""; string quotedate = ""; string lastquote = ""; string openquote = ""; string closedquote = ""; string highquote = ""; string lowquote = ""; string previousdate = ""; string previousquote = ""; try { reader = sendRequestXml(buildTBMXML(String.Format(TBMSingleQuoteHistoryRequest, sExchange, sISIN, TickerType, this.StartDate.ToString("yyyy/MM/dd"), this.EndDate.ToString("yyyy/MM/dd")))); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "Organisation") { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "Name") { reader.MoveToAttribute("long"); companyname = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "Issue") { reader.MoveToAttribute("exchange"); issueexchange = reader.Value; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "Name") { reader.MoveToAttribute("long"); issuename = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "Ticker") { reader.MoveToAttribute("id"); issueid = reader.Value; } //if (readMetaData(reader)) // break; if (reader.NodeType == XmlNodeType.Element && reader.Name == "Quote") { reader.MoveToAttribute("date"); quotedate = reader.Value; reader.MoveToAttribute("currency"); quotecurrency = reader.Value; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "Open") { reader.MoveToAttribute("value"); openquote = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "Close") { reader.MoveToAttribute("value"); closedquote = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "High") { reader.MoveToAttribute("value"); highquote = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "Low") { reader.MoveToAttribute("value"); lowquote = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "Last") { reader.MoveToAttribute("value"); lastquote = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "Previous") { reader.MoveToAttribute("value"); previousquote = reader.Value; reader.MoveToAttribute("date"); previousdate = reader.Value; } if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Quote") { // Only allow insert/update of a price when it exchange has closed if (closedquote.Length > 0) { CultureInfo culture = new CultureInfo(CultureInfo.CurrentCulture.Name); NumberFormatInfo numInfo = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo)); numInfo.NumberDecimalSeparator = "."; DateTime dtQuoteDate = DateTime.Parse(quotedate); ICurrency newcur = (ICurrency)InstrumentMapper.GetCurrencyByName(session, quotecurrency); // For TBM Data the price is the closed price decimal dClosedQuote = decimal.Parse(closedquote, numInfo); Price newclosedprice = new Price(new Money(dClosedQuote, newcur), instrument); IHistoricalPrice newhprice = new HistoricalPrice(newclosedprice, dtQuoteDate); newhprice.ClosedPrice = newclosedprice; Price newopenprice = null; Price newhighprice = null; Price newlowprice = null; if (openquote.Length > 0) { decimal dOpenQuote = decimal.Parse(openquote, numInfo); newopenprice = new Price(new Money(dOpenQuote, newcur), instrument); newhprice.OpenPrice = newopenprice; } if (highquote.Length > 0) { decimal dHighQuote = decimal.Parse(highquote, numInfo); newhighprice = new Price(new Money(dHighQuote, newcur), instrument); newhprice.HighPrice = newhighprice; } if (lowquote.Length > 0) { decimal dLowQuote = decimal.Parse(lowquote, numInfo); newlowprice = new Price(new Money(dLowQuote, newcur), instrument); newhprice.LowPrice = newlowprice; } if (instrument.HistoricalPrices.ContainsHistoricalPrice(newhprice)) { // Look for previous price to see if it differs too much instrument.HistoricalPrices.AddHistoricalPrice(newhprice); if (instrument.HistoricalPrices.Count > 1) { IHistoricalPrice prevhprice = (IHistoricalPrice)instrument.HistoricalPrices.GetItemByDate(dtQuoteDate.AddDays(-1)); decimal diff = Math.Abs((prevhprice.Price.Quantity - newhprice.Price.Quantity) / prevhprice.Price.Quantity) * 100; if (diff > 10) // More than 10% difference, raise warning throw new ApplicationException(String.Format("Price of instrument: {0} differs more than 10%. Old price: {1}, New price {2}, difference {3}", instrument.Isin, prevhprice.Price, newhprice.Price, diff)); } } else { IHistoricalPrice existinghprice = (IHistoricalPrice)instrument.HistoricalPrices.GetItemByDate(dtQuoteDate); existinghprice.Price = newclosedprice; existinghprice.ClosedPrice = newclosedprice; existinghprice.OpenPrice = newopenprice; existinghprice.HighPrice = newhighprice; existinghprice.LowPrice = newlowprice; } session.Update(instrument); } break; } } } } } if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Organisation") break; } } } } finally { // Unknown ISIN or error retrieving one if (reader != null) reader.Close(); } return; }
public void storeQuoteCollection() { string companyname = ""; string issueexchange = ""; string issuename = ""; string issueid = ""; string quotecurrency = ""; string quotedate = ""; string lastquote = ""; string openquote = ""; string closedquote = ""; string highquote = ""; string lowquote = ""; string previousdate = ""; string previousquote = ""; StreamWriter exportstream = File.CreateText("c:\\temp\\prices.txt"); XmlTextReader reader = sendRequestXml(buildTBMXML(TBMQuoteCollectionRequest)); //XmlTextReader reader = new XmlTextReader("c:\\temp\\sampletbm.xml"); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "Organisation") { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "Name") { reader.MoveToAttribute("long"); companyname = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "Issue") { reader.MoveToAttribute("exchange"); issueexchange = reader.Value; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "Name") { reader.MoveToAttribute("long"); issuename = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "Ticker") { reader.MoveToAttribute("id"); issueid = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "Quote") { reader.MoveToAttribute("date"); quotedate = reader.Value; reader.MoveToAttribute("currency"); quotecurrency = reader.Value; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "Open") { reader.MoveToAttribute("value"); openquote = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "Close") { reader.MoveToAttribute("value"); closedquote = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "High") { reader.MoveToAttribute("value"); highquote = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "Low") { reader.MoveToAttribute("value"); lowquote = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "Last") { reader.MoveToAttribute("value"); lastquote = reader.Value; } if (reader.NodeType == XmlNodeType.Element && reader.Name == "Previous") { reader.MoveToAttribute("value"); previousquote = reader.Value; reader.MoveToAttribute("date"); previousdate = reader.Value; } if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Quote") { exportstream.WriteLine(String.Format("{0};{1}",issueid,issuename)); IDalSession session = NHSessionFactory.CreateSession(); try { IList<ITradeableInstrument> instrumentlist = InstrumentMapper.GetInstrumentsByIsin(session, issueid); if (instrumentlist.Count > 0) { CultureInfo culture = new CultureInfo(CultureInfo.CurrentCulture.Name); NumberFormatInfo numInfo = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo)); numInfo.NumberDecimalSeparator = "."; TradeableInstrument instrument = (TradeableInstrument)instrumentlist[0]; decimal dLastQuote = decimal.Parse(lastquote,numInfo); DateTime dtQuoteDate = DateTime.Parse(quotedate); ICurrency newcur = (ICurrency)InstrumentMapper.GetCurrencyByName(session, quotecurrency); Price newprice = new Price(new Money(dLastQuote, newcur), instrument); IHistoricalPrice newhprice = new HistoricalPrice(newprice, dtQuoteDate); if (!instrument.HistoricalPrices.ContainsHistoricalPrice(newhprice)) { instrument.HistoricalPrices.AddHistoricalPrice(newhprice); } else { IHistoricalPrice existinghprice = (IHistoricalPrice)instrument.HistoricalPrices.GetItemByDate(dtQuoteDate); existinghprice.Price = newprice; } session.Update(instrument); } session.Close(); } catch (Exception ex) { break; } break; } } } } } } if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Organisation") break; } } exportstream.Close(); }
public static bool UpdateInstrumentPriceHistory(int[] dateKeys, int instrumentId, decimal newQuantity, bool ignoreWarning, out string errMessage) { bool success = true; errMessage = ""; if (dateKeys == null || dateKeys.Count() == 0) throw new B4F.TotalGiro.ApplicationLayer.Common.GridviewNoSelectionException(); DateTime[] dates = dateKeys .Select(k => getDataFromPriceHistoryKey(k).Item1) .OrderBy(k => k) .ToArray(); using (IDalSession session = NHSessionFactory.CreateSession()) { IInstrument instrument = InstrumentMapper.GetInstrument(session, instrumentId); List<DateTime> notExistingDates = new List<DateTime>(); bool saveStuff = false; if (instrument != null) { switch (instrument.SecCategory.Key) { case SecCategories.Cash: IList<IHistoricalExRate> exRates = HistoricalExRateMapper.GetHistoricalExRates(session, instrumentId, dates); if (exRates != null && exRates.Count > 0) { foreach (DateTime date in dates) { IHistoricalExRate exRate = exRates.Where(x => x.RateDate == date).FirstOrDefault(); if (exRate != null) { if (exRate.Rate != newQuantity) { exRate.Rate = newQuantity; if (!ignoreWarning) ignoreWarning = CheckNewRate(session, exRate, false, out errMessage); if (!string.IsNullOrEmpty(errMessage)) return false; saveStuff = true; } } else notExistingDates.Add(date); } } else notExistingDates.AddRange(dates); if (saveStuff) session.Update(exRates); saveStuff = false; ICurrency currency = instrument.ToCurrency; foreach (DateTime date in notExistingDates) { IHistoricalExRate newExRate = new HistoricalExRate(currency, newQuantity, date); currency.HistoricalExRates.AddExRate(newExRate); if (!ignoreWarning) ignoreWarning = CheckNewRate(session, newExRate, false, out errMessage); if (!string.IsNullOrEmpty(errMessage)) return false; saveStuff = true; } if (saveStuff) InstrumentMapper.Update(session, currency); break; default: IInstrumentsWithPrices iwp = (IInstrumentsWithPrices)instrument; Price newPrice = new Price(newQuantity, iwp.CurrencyNominal, iwp); IList<IHistoricalPrice> prices = HistoricalPriceMapper.GetHistoricalPrices(session, instrumentId, dates); if (prices != null && prices.Count > 0) { foreach (DateTime date in dates) { IHistoricalPrice price = prices.Where(x => x.Date == date).FirstOrDefault(); if (price != null) { if (!price.Price.Equals(newPrice)) { price.Price = newPrice; if (!ignoreWarning) ignoreWarning = CheckNewPrice(session, price, false, out errMessage); if (!string.IsNullOrEmpty(errMessage)) return false; saveStuff = true; } } else notExistingDates.Add(date); } } else notExistingDates.AddRange(dates); if (saveStuff) session.Update(prices); saveStuff = false; foreach (DateTime date in notExistingDates) { IHistoricalPrice newHistoricalPrice = new HistoricalPrice(newPrice, date); iwp.HistoricalPrices.AddHistoricalPrice(newHistoricalPrice); if (!ignoreWarning) ignoreWarning = CheckNewPrice(session, newHistoricalPrice, false, out errMessage); if (!string.IsNullOrEmpty(errMessage)) return false; saveStuff = true; } if (saveStuff) InstrumentMapper.Update(session, iwp); break; } } } return success; }
public static void UpdateInstrumentPriceHistory(decimal newQuantity, bool ignoreWarning, int DateKey, int Key, int instrumentId) { using (IDalSession session = NHSessionFactory.CreateSession()) { IInstrument instrument = InstrumentMapper.GetInstrument(session, instrumentId); var res = getDataFromPriceHistoryKey(DateKey); DateTime date = res.Item1; bool exist = res.Item2; if (instrument != null) { switch (instrument.SecCategory.Key) { case SecCategories.Cash: if (exist) { IHistoricalExRate newExRate = HistoricalExRateMapper.GetHistoricalExRate(session, Key); if (newExRate.Rate != newQuantity) { newExRate.Rate = newQuantity; if (!ignoreWarning) CheckNewRate(session, newExRate); HistoricalExRateMapper.Update(session, newExRate); } } else { ICurrency currency = instrument.ToCurrency; IHistoricalExRate newExRate = new HistoricalExRate(currency, newQuantity, date); currency.HistoricalExRates.AddExRate(newExRate); if (!ignoreWarning) CheckNewRate(session, newExRate); InstrumentMapper.Update(session, currency); } break; default: IInstrumentsWithPrices iwp = (IInstrumentsWithPrices)instrument; Price price = new Price(newQuantity, iwp.CurrencyNominal, iwp); if (exist) { IHistoricalPrice newPrice = HistoricalPriceMapper.GetHistoricalPrice(session, Key); if (newPrice.Price != price) { newPrice.Price = price; if (!ignoreWarning) CheckNewPrice(session, newPrice); HistoricalPriceMapper.Update(session, newPrice); } } else { IHistoricalPrice newPrice = new HistoricalPrice(price, date); iwp.HistoricalPrices.AddHistoricalPrice(newPrice); if (!ignoreWarning) CheckNewPrice(session, newPrice); InstrumentMapper.Update(session, iwp); } break; } } } }
protected static Price updateStockDividendPrices(IDalSession session, IStockDividend instrument, IDividendHistory history) { if (instrument == null) throw new ApplicationException("Stuff"); Price price = new Price(history.UnitPrice.Quantity, instrument.CurrencyNominal, instrument); IList<IHistoricalPrice> historicalPrices = HistoricalPriceMapper.GetHistoricalPrices(session, instrument.Key, history.ExDividendDate, history.SettlementDate); DateTime priceDate = history.ExDividendDate; while (priceDate <= history.SettlementDate) { IHistoricalPrice historicalPrice = historicalPrices.Where(x => x.Date == priceDate).FirstOrDefault(); if (historicalPrice == null) { historicalPrice = new HistoricalPrice(price, priceDate); instrument.HistoricalPrices.AddHistoricalPrice(historicalPrice); InstrumentMapper.Update(session, instrument); } else { if (historicalPrice.Price != price) { historicalPrice.Price = price; HistoricalPriceMapper.Update(session, historicalPrice); } } priceDate = priceDate.AddDays(1); } return price; }
public static void UpdateHistoricalPrice(DateTime Date, bool ignoreWarning, decimal newQuantity, int original_InstrumentId) { using (IDalSession session = NHSessionFactory.CreateSession()) { IInstrumentsWithPrices instrument = (IInstrumentsWithPrices)InstrumentMapper.GetInstrument(session, original_InstrumentId); Price price = new Price(newQuantity, instrument.CurrencyNominal, instrument); IList<IHistoricalPrice> historicalPrices = HistoricalPriceMapper.GetHistoricalPrices(session, instrument, Date); IHistoricalPrice historicalPrice = null; if (historicalPrices.Count == 0) { historicalPrice = new HistoricalPrice(price, Date); instrument.HistoricalPrices.AddHistoricalPrice(historicalPrice); if (!ignoreWarning) InstrumentPriceUpdateAdapter.CheckNewPrice(session, historicalPrice); InstrumentMapper.Update(session, instrument); } else { historicalPrice = (IHistoricalPrice)historicalPrices[0]; if (historicalPrice.Price != price) { historicalPrice.Price = price; if (!ignoreWarning) InstrumentPriceUpdateAdapter.CheckNewPrice(session, historicalPrice); HistoricalPriceMapper.Update(session, historicalPrice); } } } }