Get the latest stock and stock index prices from Sina (. NET version), the latest market. net
In the procedural trading system that uses CTP to develop stock index futures options, the current points and closing of stock indexes are often used. In order to enable the program to automatically obtain the stock index, You need to query it through the network.
Currently, in addition to using Python for crawling, you can also use the JS quote server provided by sina. This article uses the latter.
1. the following fields are generally returned when you query the Stock Index:
Index name, current number of points, current price, rise/fall rate, transaction volume (hand), transaction amount (10 million RMB );
2. query a stock generally returns the following fields:
The name of the stock, which is opened today, closed yesterday, current price, current highest price, lowest price today, buy one price, sell one price, number of tickets sold, transaction amount, and purchase amount, buy one price ,..., buy five, buy five, sell one, sell one price ,..., five-volume purchase, five-price purchase, date, time
Because the stock index query does not return the limit close, you need to calculate the number of current points returned and the Rise/fall rate.
Show me your code:
Public class SinaStockIndexHelper {public static double GetThreePreCloseIndex () {double precloseindex = 0; try {// index query rules: s_sh000001, s_sz399001, s_sz399106, s_sh000300: Shanghai index, which is proved to be accurate, shenzhen Stock Index, Shanghai and Shenzhen 300, China
// Stock query rules: sh601857, sz002230: CNPC, KEDA xunfei (starting with sh, it indicates a-share in Shanghai Stock Exchange, starting with sz, representing Shenzhen Stock, followed by the corresponding stock code) string url = "http://hq.sinajs.cn/list=s_sh000300"; HttpWebRequest webrequest = (HttpWebRequest) HttpWebRequest. create (url); HttpWebResponse webreponse = (HttpWebResponse) webrequest. getResponse (); Stream stream = webreponse. getResponseStream (); byte [] rsByte = new Byte [webreponse. contentLength]; // save data in the stream. read (rsByte, 0, (int) webreponse. contentLength); Console. writeLine (System. text. encoding. UTF8.GetString (rsByte, 0, rsByte. length ). toString (); string tmp = System. text. encoding. UTF8.GetString (rsByte, 0, rsByte. length ). toString (); string [] index = tmp. split ('"'); string [] datas = index [1]. split (','); double todayindex = double. parse (datas [1]); double increaserate = double. parse (datas [3])/100; precloseindex = todayindex/(1 + increaserate); string pci = precloseindex. toString ("F2"); precloseindex = double. parse (pci);} catch (Exception exp) {Console. writeLine (exp. message) ;}return precloseindex ;}}