protected override QuoteOptionsResult ConvertResult(YahooManaged.Base.ConnectionInfo connInfo, System.IO.Stream stream, YahooManaged.Base.SettingsBase settings) { List <QuoteOptionsDataChain> options = new List <QuoteOptionsDataChain>(); System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US"); XDocument doc = MyHelper.ParseXmlDocument(stream); XElement[] mainLst = XPath.GetElements("//optionsChain", doc); foreach (XElement chain in mainLst) { string idAtt = MyHelper.GetXmlAttributeValue(chain, "symbol"); string expirationDateAtt = MyHelper.GetXmlAttributeValue(chain, "expiration"); DateTime expirationDate = default(DateTime); if (!System.DateTime.TryParseExact(expirationDateAtt, "yyyy-MM-dd", culture, System.Globalization.DateTimeStyles.None, out expirationDate)) { System.DateTime.TryParseExact(expirationDateAtt, "yyyy-MM", culture, System.Globalization.DateTimeStyles.None, out expirationDate); } List <QuoteOptionsData> lst = new List <QuoteOptionsData>(); foreach (XElement optionNode in chain.Elements()) { if (optionNode.Name.LocalName == "option") { QuoteOptionsData opt = ImportExport.ToQuoteOption(optionNode, culture); if (opt != null) { lst.Add(opt); } } } options.Add(new QuoteOptionsDataChain(idAtt, expirationDate, lst)); } return(new QuoteOptionsResult(options.ToArray())); }
/// <summary> /// Tries to read a list of quote options from a CSV formatted text /// </summary> /// <param name="csvText">The CSV formatted text</param> /// <param name="delimiter">The delimiter of the CSV text</param> /// <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param> /// <returns>The converted quote values or Nothing</returns> /// <remarks></remarks> public static QuoteOptionsData[] ToQuoteOptions(string csvText, char delimiter, System.Globalization.CultureInfo culture = null) { List <QuoteOptionsData> lst = new List <QuoteOptionsData>(); if (csvText != string.Empty) { System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture; if (culture != null) { ci = culture; } string[][] table = MyHelper.CsvTextToStringTable(csvText, delimiter); if (table.Length > 1) { for (int i = 0; i <= table.Length - 1; i++) { if (table[i].Length == 10) { QuoteOptionsData qd = new QuoteOptionsData(); qd.Symbol = table[i][0]; qd.Type = (QuoteOptionType)(table[i][1].ToLower() == "p" ? QuoteOptionType.Put : QuoteOptionType.Call); double t1; double t2; double t3; double t4; double t5; int t6; int t7; if (double.TryParse(table[i][2], System.Globalization.NumberStyles.Currency, ci, out t1) && double.TryParse(table[i][3], System.Globalization.NumberStyles.Currency, ci, out t2) && double.TryParse(table[i][4], System.Globalization.NumberStyles.Currency, ci, out t3) && double.TryParse(table[i][6], System.Globalization.NumberStyles.Currency, ci, out t4) && double.TryParse(table[i][7], System.Globalization.NumberStyles.Currency, ci, out t5) && int.TryParse(table[i][8], System.Globalization.NumberStyles.Integer, ci, out t6) && int.TryParse(table[i][9], System.Globalization.NumberStyles.Integer, ci, out t7)) { qd.LastPrice = t1; qd.StrikePrice = t2; qd.Change = t3; qd.Bid = t4; qd.Ask = t5; qd.Volume = t6; qd.OpenInterest = t7; qd.Change *= Convert.ToInt32((table[i][5].ToLower() == "down" ? -1 : 1)); lst.Add(qd); } } } } } return(lst.ToArray()); }
/// <summary> /// Writes a QuoteOption to XML format /// </summary> /// <param name="writer">The used XML writer</param> /// <param name="quoteOption">The used QuoteOption</param> /// <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param> /// <remarks></remarks> public static void FromQuoteOption(System.Xml.XmlWriter writer, QuoteOptionsData quoteOption, System.Globalization.CultureInfo culture = null) { System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture; if (culture != null) { ci = culture; } writer.WriteStartElement("Option"); writer.WriteAttributeString(FinanceHelper.NameOptionSymbol, quoteOption.Symbol); writer.WriteAttributeString(FinanceHelper.NameOptionType, (quoteOption.Type == QuoteOptionType.Call ? "C" : "P").ToString()); writer.WriteStartElement(FinanceHelper.NameOptionLastPrice); writer.WriteValue(MyHelper.ObjectToString(quoteOption.LastPrice, ci)); writer.WriteEndElement(); writer.WriteStartElement(FinanceHelper.NameOptionStrikePrice); writer.WriteValue(MyHelper.ObjectToString(quoteOption.StrikePrice, ci)); writer.WriteEndElement(); writer.WriteStartElement(FinanceHelper.NameOptionChange); writer.WriteValue(MyHelper.ObjectToString(Math.Abs(quoteOption.Change), ci)); writer.WriteEndElement(); writer.WriteStartElement(FinanceHelper.NameOptionChangeDir); writer.WriteValue((quoteOption.Change >= 0 ? "Up" : "Down").ToString()); writer.WriteEndElement(); writer.WriteStartElement(FinanceHelper.NameOptionBid); writer.WriteValue(MyHelper.ObjectToString(quoteOption.Bid, ci)); writer.WriteEndElement(); writer.WriteStartElement(FinanceHelper.NameOptionAsk); writer.WriteValue(MyHelper.ObjectToString(quoteOption.Ask, ci)); writer.WriteEndElement(); writer.WriteStartElement(FinanceHelper.NameOptionVolume); writer.WriteValue(MyHelper.ObjectToString(quoteOption.Volume, ci)); writer.WriteEndElement(); writer.WriteStartElement(FinanceHelper.NameOptionOpenInterest); writer.WriteValue(MyHelper.ObjectToString(quoteOption.OpenInterest, ci)); writer.WriteEndElement(); writer.WriteEndElement(); }