[WebMethod]
public String getMultipleStockDetails(String qStr)
{
try
{
String q = this.getStockString("http://finance.google.com/finance/info?client=ig&q=" + qStr).Trim();
if (q.Equals(""))
{
// do nothing
return "";
}
else
{
// parsing if more than 1 stock information requested.
String[] op = { "{\n", "}\n", ",{\n" };
String[] s1 = q.Split(op, StringSplitOptions.RemoveEmptyEntries);
String s1f = "";
StringBuilder output = new StringBuilder();
for (int i = 1; i < s1.Length - 1; i++)
{
s1f = s1[i];
// 2. , \n
s1f = s1f.Replace(" : ", ":");
s1f = s1f.Replace(": ", ":");
s1f = s1f.Replace(" :", ":");
s1f = s1f.Replace(",", "");
String[] op2 = { "\n" };
String[] s2 = s1f.Split(op2, StringSplitOptions.RemoveEmptyEntries);
foreach (String s2f in s2)
{
// 3. ":"
String[] op3 = { "\":\"" };
String[] s3 = s2f.Split(op3, StringSplitOptions.RemoveEmptyEntries);
String param, value;
param = s3[0].Replace("\"", "");
value = s3[1].Replace("\"", "");
// 4. addin
/*
id : Internal Google Security ID?
t : Ticker
e : Exchange Name
l : Last Price
l_cur : Last Price with Currency
lt : Last Trade Date/Time
c : Change (in ccy) - formatted with +/-
cp : Change (%)
*/
if (param.Equals("id"))
output.AppendLine("id|"+value);
else if (param.Equals("t"))
output.AppendLine("t|" + value);
else if (param.Equals("e"))
output.AppendLine("e|" + value);
else if (param.Equals("l"))
output.AppendLine("l|" + value);
else if (param.Equals("l_cur"))
output.AppendLine("l_cur|" + value);
else if (param.Equals("lt"))
output.AppendLine("lt|" + value);
else if (param.Equals("c"))
output.AppendLine("c|" + value);
else if (param.Equals("cp"))
output.AppendLine("cp|" + value);
}
output.AppendLine("===");
}
return output.ToString();
}
}
catch (Exception ex)
{
return "";
}
}
more function here:
protected GoogleFinanceModel gfm_g;
protected void Timer1_Tick(object sender, EventArgs e)
{
gfs_g = this.getMultipleStockDetails("NASDAQ:GOOG,NASDAQ:YHOO,ETR:DBK");
String[] stockName = { "NASDAQGOOG", "NASDAQYHOO", "ETRDBK" };
StringBuilder output = new StringBuilder();
for (int i = 0; i < gfs_g.Length; i++)
{
gfm_g = gfs_g[i];
String lp = gfm_g.lastPrice;
float llp = 0;
if (Session["lastPrice"+stockName[i]] == null)
{
Session.Add("lastPrice" + stockName[i], lp);
llp = float.Parse(lp);
}
else
{
llp = float.Parse(Session["lastPrice" + stockName[i]].ToString());
Session["lastPrice" + stockName[i]] = lp;
}
float diff = llp - float.Parse(lp);
if (diff == 0)
{
lp = "<font color='black'>" + lp + "</font>";
}
else if (diff > 0)
{
lp = "<font color='red'>" + lp + "</font>";
}
else if (diff < 0)
{
lp = "<font color='green'>" + lp + "</font>";
}
String changccy = gfm_g.changeInCCY;
String frontc = changccy.Substring(0, 1);
if (frontc.Equals("+"))
{
changccy = "<font color='green'>" + changccy + "</font>";
}
else if (frontc.Equals("-"))
{
changccy = "<font color='red'>" + changccy + "</font>";
}
output.AppendFormat("id:{0}<br/>Ticker:{1}<br/>Exchange Name:{2}<br/>Last Price:{3}<br/>Trade Time:{4}<br/>Change in CCY:{5}<br/>Change in Percentage:{6}<br/>{7}<br/>===<br/>"
, gfm_g.id, gfm_g.ticker, gfm_g.exchangeName, lp, gfm_g.lastTradeDateTime, changccy, gfm_g.changPercentage, DateTime.UtcNow.ToString());
}
Label1.Text = output.ToString();
}