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); // update server list server_list.Add(Name); wbf = new WebForm(); wbf.Show(); wbf.Hide(); }
// get stock latest options chain public ArrayList GetOptionsChain(string ticker) { // correct symbol ticker = CorrectSymbol(ticker); string url; if (ticker.StartsWith("^")) { url = string.Format(@"http://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbol={0}&instrument=OPTIDX&date=-", ticker.TrimStart(new char[] { '^' })); } else { url = string.Format(@"http://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbol={0}&instrument=OPTSTK&date=-", ticker.TrimStart(new char[] { '^' })); } // get page HtmlDocument doc = wbf.GetHtmlDocumentWithWebBrowser(url, null, null, null, 60); if (doc == null || doc.Body == null || string.IsNullOrEmpty(doc.Body.InnerText)) { return(null); } // patch html to bypass bug in web-page string html = doc.Body.OuterHtml; html = html.Replace("solid; 1px block;border-right:#ffffff border-right-style:", ""); // convert web-page to xml XmlDocument xml = cap.ConvertHtmlToXml(html); if (xml == null) { return(null); } XmlNode nd, select_nd; List <string> expdate_list = new List <string>(); select_nd = prs.FindXmlNodeByName(xml.FirstChild, "SELECT", "", 2, 0); if (select_nd == null) { return(null); } for (int r = 2; ; r++) { nd = prs.FindXmlNodeByName(select_nd, "OPTION", "", r, 0); if (nd == null || nd.InnerText == null) { break; } expdate_list.Add(nd.InnerText.Trim()); } // create options array list ArrayList options_list = new ArrayList(); options_list.Clear(); options_list.Capacity = 1024; int i = 0; foreach (string expdate in expdate_list) { if (expdate == "") { continue; } if (i++ > 0) { if (ticker.StartsWith("^")) { url = string.Format(@"http://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbol={0}&instrument=OPTIDX&date={1}", ticker.TrimStart(new char[] { '^' }), expdate); } else { url = string.Format(@"http://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbol={0}&instrument=OPTSTK&date={1}", ticker.TrimStart(new char[] { '^' }), expdate); } // get page doc = wbf.GetHtmlDocumentWithWebBrowser(url, null, null, null, 60); if (doc == null || doc.Body == null || string.IsNullOrEmpty(doc.Body.InnerText)) { return(null); } // patch html to bypass bug in web-page html = doc.Body.OuterHtml; html = html.Replace("solid; 1px block;border-right:#ffffff border-right-style:", ""); // convert web-page to xml xml = cap.ConvertHtmlToXml(html); if (xml == null) { return(null); } } // report progress if (host.BackgroundWorker != null) { host.BackgroundWorker.ReportProgress(100 * i / expdate_list.Count); } // option chain table HtmlElement elem = WebForm.LocateParentElement(doc, "Volume", 1, "TABLE"); if (elem == null) { continue; } xml = cap.ConvertHtmlToXml(elem.OuterHtml); if (xml == null) { continue; } for (int r = 1; ; r++) { XmlNode row_nd = prs.GetXmlNodeByPath(xml.FirstChild, @"TBODY\TR(" + r + @")"); if (row_nd == null) { break; } for (int j = 1; j <= 2; j++) { try { Option option = new Option(); // option stock ticker and number of stocks per contract option.stock = ticker; option.stocks_per_contract = 1; option.update_timestamp = DateTime.Now; // option type if (j == 2) { option.type = "Put"; } else if (j == 1) { option.type = "Call"; } else { continue; } // get option detail link string symbol_href = null; nd = prs.GetXmlNodeByPath(row_nd, @"TD(" + ((j == 1) ? 1 : 23) + @")\A"); try { if (nd != null && nd.Attributes != null) { foreach (XmlAttribute attr in nd.Attributes) { if (attr.Name == "href") { symbol_href = System.Web.HttpUtility.HtmlDecode(attr.Value).Trim(); break; } } } } catch { } if (symbol_href == null) { continue; } // symbol option.symbol = "." + symbol_href.Replace("javascript:chartPopup(", "").Replace(");", "").Replace(" ", "").Replace(",", "").Replace(".", "").Replace("'", ""); // expiration date DateTime.TryParse(expdate, ci, DateTimeStyles.None, out option.expiration); // strike price nd = prs.GetXmlNodeByPath(row_nd, @"TD(12)"); if (nd == null || nd.InnerText == null || !double.TryParse(nd.InnerText, NumberStyles.Number, ci, out option.strike)) { continue; } // option bid price nd = prs.GetXmlNodeByPath(row_nd, @"TD(" + ((j == 1) ? 9 : 14) + @")"); option.price.bid = double.NaN; if (nd.InnerText != "-") { double.TryParse(nd.InnerText, NumberStyles.Number, ci, out option.price.bid); } // option ask price nd = prs.GetXmlNodeByPath(row_nd, @"TD(" + ((j == 1) ? 10 : 15) + @")"); option.price.ask = double.NaN; if (nd.InnerText != "-") { double.TryParse(nd.InnerText, NumberStyles.Number, ci, out option.price.ask); } // option last price nd = prs.GetXmlNodeByPath(row_nd, @"TD(" + ((j == 1) ? 6 : 18) + @")"); option.price.last = double.NaN; if (nd.InnerText != "-") { double.TryParse(nd.InnerText, NumberStyles.Number, ci, out option.price.last); } // option price change nd = prs.GetXmlNodeByPath(row_nd, @"TD(" + ((j == 1) ? 7 : 17) + @")"); option.price.change = double.NaN; if (nd.InnerText != "-") { double.TryParse(nd.InnerText, NumberStyles.Number, ci, out option.price.change); } // option volume nd = prs.GetXmlNodeByPath(row_nd, @"TD(" + ((j == 1) ? 4 : 20) + @")"); option.volume.total = 0; if (nd.InnerText != "-") { double.TryParse(nd.InnerText, NumberStyles.Number, ci, out option.volume.total); } // open int nd = prs.GetXmlNodeByPath(row_nd, @"TD(" + ((j == 1) ? 2 : 22) + @")"); option.open_int = 0; if (nd.InnerText != "-") { int.TryParse(nd.InnerText, NumberStyles.Number, ci, out option.open_int); } options_list.Add(option); } catch { } } } } return(options_list); }