public static HtmlElement LocateParentElement(HtmlDocument doc, string text, int index, string parent) { // locate text element List <HtmlElement> elem_list = new List <HtmlElement>(); WebForm.LocateTextByWebBrowser(doc.Body, text, elem_list); if (elem_list.Count == 0) { return(null); } // locate table of text element return(WebForm.LocateParentElement(elem_list[index], parent)); }
public Main() { wir = new WorldInterestRate(cap); // update feature list feature_list.Add(FeaturesT.SUPPORTS_DELAYED_OPTIONS_CHAIN); feature_list.Add(FeaturesT.SUPPORTS_DELAYED_STOCK_QUOTE); feature_list.Add(FeaturesT.SUPPORTS_CONFIG_FORM); // update server list server_list.Add(Name); wbf = new WebForm(); wbf.Show(); wbf.Hide(); }
// get stock latest options chain public ArrayList GetOptionsChain(string ticker) { // date prefix list const string DatePrefixList = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec "; // correct symbol SymbolSet.SymbolTableRow tck = CorrectSymbolRow(ticker); if (tck == null || !cache_dict.ContainsKey(tck.Symbol)) { return(null); } // get first document HtmlDocument doc = cache_dict[tck.Symbol]; // global arraylist ArrayList option_list = new ArrayList(); // progress status int current, last = 0, total = 0, count = 0; BackgroundWorker bw = null; if (host != null) { bw = host.BackgroundWorker; } // rotate while (true) { DateTime exp_date = DateTime.MinValue; // locate table of "All" element HtmlElement table_elem = WebForm.LocateParentElement(doc, "All", 1, "TABLE"); if (table_elem == null) { break; } HtmlElementCollection list_elem = table_elem.GetElementsByTagName("SPAN"); if (list_elem == null || list_elem.Count == 0) { break; } HtmlElement date_elem = null; foreach (HtmlElement elem in list_elem) { try { if (!string.IsNullOrEmpty(elem.InnerText) && elem.InnerText.Trim().Length == 6 && DatePrefixList.Contains(elem.InnerText.Trim().Substring(0, 4))) { date_elem = elem; exp_date = Convert.ToDateTime("1 " + date_elem.InnerText, ci); // option expiration break; } } catch { } } if (date_elem == null) { break; } // get the day after the 3rd firday int days_to_1st_friday = (int)DayOfWeek.Friday - (int)exp_date.DayOfWeek; if (days_to_1st_friday < 0) { days_to_1st_friday += 7; } exp_date = exp_date.AddDays(15 + days_to_1st_friday); while (true) { // parse page and add to global result ArrayList page_list = GetOptionChainFromDocument(cache_dict[tck.Symbol], tck, exp_date); if (page_list != null && page_list.Count > 0) { option_list.AddRange(page_list); } // locate text element List <HtmlElement> click_elem = new List <HtmlElement>(); WebForm.LocateTextByWebBrowser(doc.Body, "Next Page >>", click_elem); if (click_elem.Count == 0) { break; } // get "Next Page >>" document if (click_elem != null && click_elem.Count > 0) { doc = wbf.GetNavigatedHtmlDocumentWithWebBrowser(click_elem[0], null, null, null, 20); } else { break; } } // locate table of "All" element table_elem = WebForm.LocateParentElement(doc, "All", 1, "TABLE"); if (table_elem == null) { break; } list_elem = table_elem.GetElementsByTagName("A"); if (list_elem == null || list_elem.Count == 0) { break; } // next element HtmlElement next_elem = null; total = 1; foreach (HtmlElement elem in list_elem) { if (!string.IsNullOrEmpty(elem.InnerText) && elem.InnerText.Trim().Length == 6 && DatePrefixList.Contains(elem.InnerText.Trim().Substring(0, 4))) { try { if (next_elem == null && Convert.ToDateTime("1 " + elem.InnerText, ci) > exp_date) { next_elem = elem; } total++; } catch { } } } if (next_elem != null && next_elem.InnerText != "All") { doc = wbf.GetNavigatedHtmlDocumentWithWebBrowser(next_elem, null, null, null, 60); } else { break; } // update progress bar count++; // update progress bar if (bw != null && total > 0) { current = count * 100 / total; if (current != last) { bw.ReportProgress(current); last = current; } } } return(option_list); }
private ArrayList GetOptionChainFromDocument(HtmlDocument doc, SymbolSet.SymbolTableRow tck, DateTime expdate) { // locate table of "Open Int" element HtmlElement table_elem = WebForm.LocateParentElement(doc, "Open Int", 0, "TABLE"); if (table_elem == null) { return(null); } // get quote table XmlDocument xml = cap.ConvertHtmlToXml(table_elem.OuterHtml); if (xml == null) { return(null); } // create options array list ArrayList options_list = new ArrayList(); for (int i = 2; ; i++) { XmlNode nd, row_nd = prs.GetXmlNodeByPath(xml.FirstChild, @"TBODY\TR(" + i.ToString() + ")"); if (row_nd == null) { break; } // get strike nd = prs.GetXmlNodeByPath(row_nd, @"TD(9)"); double strike = double.NaN; if (nd != null && !string.IsNullOrEmpty(nd.InnerText)) { try { strike = Convert.ToDouble(System.Web.HttpUtility.HtmlDecode(nd.InnerText).Trim(), ci); } catch { } } if (double.IsNaN(strike)) { continue; } for (int j = 0; j <= 9; j += 9) { // create new option Option option = new Option(); // type option.type = (j == 0) ? "Call" : "Put"; // strike option.strike = strike; // underlying option.stock = tck.Symbol; // update time stamp option.update_timestamp = DateTime.Now; // exipration date nd = prs.GetXmlNodeByPath(row_nd, @"TD(" + (j + 1).ToString() + ")"); if (nd == null || string.IsNullOrEmpty(nd.InnerText)) { break; } string[] split = nd.InnerText.Replace(",", "").Split(' '); option.expiration = expdate; DateTime.TryParse(split[1] + "-" + split[0] + "-" + split[2], ci, DateTimeStyles.None, out option.expiration); // symbol option.symbol = string.Format(".{0}{1:yyMMdd}{2}{3:00000000}", tck.Symbol.TrimStart(new char[] { '^' }), option.expiration, option.type[0], option.strike * 1000); // get last price nd = prs.GetXmlNodeByPath(row_nd, @"TD(" + (j + 2).ToString() + ")"); option.price.last = double.NaN; if (nd != null && !string.IsNullOrEmpty(nd.InnerText)) { try { string stmp = System.Web.HttpUtility.HtmlDecode(nd.InnerText).Trim(); if (!string.IsNullOrEmpty(stmp)) { option.price.last = Convert.ToDouble(stmp, ci); } } catch { } } // get price change nd = prs.GetXmlNodeByPath(row_nd, @"TD(" + (j + 3).ToString() + ")"); option.price.change = double.NaN; if (nd != null && !string.IsNullOrEmpty(nd.InnerText)) { try { string stmp = System.Web.HttpUtility.HtmlDecode(nd.InnerText).Trim(); if (!string.IsNullOrEmpty(stmp)) { option.price.change = Convert.ToDouble(stmp, ci); } } catch { } } // get bid price nd = prs.GetXmlNodeByPath(row_nd, @"TD(" + (j + 4).ToString() + ")"); option.price.bid = double.NaN; if (nd != null && !string.IsNullOrEmpty(nd.InnerText)) { try { string stmp = System.Web.HttpUtility.HtmlDecode(nd.InnerText).Trim(); if (!string.IsNullOrEmpty(stmp)) { option.price.bid = Convert.ToDouble(stmp, ci); } } catch { } } // get ask price nd = prs.GetXmlNodeByPath(row_nd, @"TD(" + (j + 5).ToString() + ")"); option.price.ask = double.NaN; if (nd != null && !string.IsNullOrEmpty(nd.InnerText)) { try { string stmp = System.Web.HttpUtility.HtmlDecode(nd.InnerText).Trim(); if (!string.IsNullOrEmpty(stmp)) { option.price.ask = Convert.ToDouble(stmp, ci); } } catch { } } // get volume nd = prs.GetXmlNodeByPath(row_nd, @"TD(" + (j + 6).ToString() + ")"); option.volume.total = 0; if (nd != null && !string.IsNullOrEmpty(nd.InnerText)) { try { string stmp = System.Web.HttpUtility.HtmlDecode(nd.InnerText).Trim(); if (!string.IsNullOrEmpty(stmp)) { option.volume.total = Convert.ToInt32(stmp, ci); } } catch { } } // get open interest nd = prs.GetXmlNodeByPath(row_nd, @"TD(" + (j + 7).ToString() + ")"); option.open_int = 0; if (nd != null && !string.IsNullOrEmpty(nd.InnerText)) { try { string stmp = System.Web.HttpUtility.HtmlDecode(nd.InnerText).Trim(); if (!string.IsNullOrEmpty(stmp)) { option.open_int = Convert.ToInt32(stmp, ci); } } catch { } } // add option to option-list options_list.Add(option); } } return(options_list); }
private Quote GetQuoteFromDocument(HtmlDocument doc, SymbolSet.SymbolTableRow tck) { // locate table of "Last Sale" element HtmlElement table_elem = WebForm.LocateParentElement(doc, "Last Sale", 0, "TABLE"); if (table_elem == null) { return(null); } // get quote table XmlDocument xml = cap.ConvertHtmlToXml(table_elem.OuterHtml); if (xml == null) { return(null); } XmlNode nd, row_nd = prs.GetXmlNodeByPath(xml.FirstChild, @"TBODY\TR(2)"); if (row_nd == null) { return(null); } // create qutote Quote quote = new Quote(); // get date and time quote.update_timestamp = DateTime.Now; // get underlying symbol quote.stock = tck.Symbol; // get underlying name nd = prs.GetXmlNodeByPath(row_nd, @"TD(1)"); if (nd != null && !string.IsNullOrEmpty(nd.InnerText)) { quote.name = System.Web.HttpUtility.HtmlDecode(nd.InnerText).Trim().Trim(); } else { quote.name = tck.Name; } // get underlying last price nd = prs.GetXmlNodeByPath(row_nd, @"TD(4)"); quote.price.last = double.NaN; if (nd != null && !string.IsNullOrEmpty(nd.InnerText)) { try { quote.price.last = Convert.ToDouble(System.Web.HttpUtility.HtmlDecode(nd.InnerText).Trim(), ci); } catch { } } // get underlying price change nd = prs.GetXmlNodeByPath(row_nd, @"TD(5)\SPAN\#text"); quote.price.change = double.NaN; if (nd != null && !string.IsNullOrEmpty(nd.InnerText)) { try { quote.price.change = Convert.ToDouble(System.Web.HttpUtility.HtmlDecode(nd.InnerText).Trim(), ci); } catch { } } // get open price quote.price.open = quote.price.last - quote.price.change; // set unsupported data to NaN quote.price.low = double.NaN; quote.price.high = double.NaN; quote.price.bid = double.NaN; quote.price.ask = double.NaN; quote.volume.total = double.NaN; return(quote); }