Deal with Multiple Stocks - Google Finance API

        [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();
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值