在GDocs表格中计算任意股票历史上任意天的收盘价
forcode:很多人可能都没有尝试过google docs spreadsheet 强大的函数功能,尤其是google finance函数太强大了!你不仅可以在google docs spreadsheet 中查看股票、外汇、黄金等实时行情,还可以根据你的金融知识自定义各种公式来计算各种指标帮助你进行投资决策。比如,你可以把黄金、外汇、股票的价格、成交量、相对于基期的波动幅度等呈现在一个页面,并且可以按照你喜欢的风格来修饰这个表格,更重要的是,你还可以将这个表格发布成一个页面,与朋友分享,太棒了。
当我们需要以历史上某一个特殊日期(比如2007年10月16日上证指数最高点)作为基期计算目前股票价格相对于基期的指数时,就需要一个函数来获得该值,但是,google docs spreadsheet 本身并没有函数可以在“一个单元格”中获取任何一支股票在历史上任何一天的收盘价。
GoogleFinance()函数用来获取股票历史行情的表达式:
=GoogleFinance("symbol", "attribute", "start_date", "num_days|end_date", "interval")
获得的是一个2×2(或N×2)的表格,如下图所示:
经过forcode半个小时的钻研,终于编写出在google docs spreadsheet中获取历史上任何一支股票任何一天股票收盘价的函数表达式了,如下所示:
=value(mid(concatenate(GoogleFinance(B2,"price",$M$1,$M$1)),29,8))
其中$M$1是日期所在的单元格,B2是股票代码所在的单元格,该函数仅对沪市和深市有效,其他股市可能需要修改29为其他数字。
下面forcode一步一步来解释是如何构造这个函数表达式的。
首先,通过这个表达式:
=GoogleFinance(B2,"price",$M$1,$M$1)
可以获得B2单元格中股票代码所对应股票在$M$1单元格中的日期的价格,但是googlefinance()函数获得的结果是一个2×2的表格,而不是一个单元格,如下图所示:
GoogleFinance()函数的表达式是这样的: 其中symbol是股票代码,也可以指向一个含有股票代码的单元格,例如A2;attribute是股票参数,默认的是price,即股票价格。常用参数如下:
=GoogleFinance("symbol"; "attribute")
- price: market price of the stock - delayed by up to 20 minutes.
- priceopen: the opening price of the stock for the current day.
- high: the highest price the stock traded for the current day.
- low: the lowest price the stock traded for the current day.
- volume: number of shares traded of this stock for the current day.
- marketcap: the market cap of the stock.
- tradetime: the last time the stock traded.
- datadelay: the delay in the data presented for this stock using the googleFinance() function.
- volumeavg: the average volume for this stock.
- pe: the Price-to-Earnings ratio for this stock.
- eps: the earnings-per-share for this stock.
- high52: the 52-week high for this stock.
- low52: the 52-week low for this stock.
- change: the change in the price of this stock since yesterday's market close.
- beta: the beta value of this stock.
- changepct: the percentage change in the price of this stock since yesterday's close.
- closeyest: yesterday's closing price of this stock.
- shares: the number of shares outstanding of this stock.
- currency: the currency in which this stock is traded.
更多GoogleFinance()函数的资料请参考google docs的帮助文档中的相关内容。
为了提取我们所需要的历史上任何一天某股票的收盘价,我们需要先使用concatenate()函数将这2×2的四个单元格的内容合并到一个单元格。
concatenate()函数的表达式如下:
CONCATENATE(text_1, text_2, ..., text_30)
concatenate()函数的作用是:Combines several text strings into one string. Text_1, text_2, ... text_30 are text passages that are to be combined into one string.
通过这个表达式:
=concatenate(GoogleFinance(B2,"price",$M$1,$M$1))
可以看到合并之后的字符串的样子:
DateClose10/16/2007 15:00:0019358.441
美国股市的股票合并之后的字符串略有不同,因为收盘时间不一样,如下图所示:
然后,我们需要从这一长串字符串中截取我们需要的股票价格,即“DateClose10/16/2007 15:00:0019358.441”中的19358.441,我们可以使用mid()函数,这个函数其实也是openoffice中的函数。
mid函数的表达式为:MID(text, start, number)
mid函数的作用是:Returns a text segment of a character string. The parameters specify the starting position and the number of characters. Text is the text containing the characters to extract. Start is the position of the first character in the text to extract. Number is the number of characters in the part of the text.
通过这个表达式:
=mid(concatenate(GoogleFinance(B2,"price",$M$1,$M$1)),29,8)
可以截取我们所需要的股票价格,但此时获得的仍然是字符串格式的股票价格,我们还需要使用value()函数将它转化为数字格式,表达式如下:
=value(mid(concatenate(GoogleFinance(B2,"price",$M$1,$M$1)),29,8))
经过这样的转换,就可以获得任何一支股票历史上任何一天的收盘价,但是某些股票的收盘价小数点后的位数过多,可以稍微修改一下格式为小数点后2位的形式、右对齐就完美了!
forcode在琢磨这个表达式的过程中,受到了国外网站上“Tracking your stock portfolio using Google Docs”这篇文章的启发,才知道google docs的spreadsheet支持函数表达式嵌套,才会去尝试各种表达式组合,在此表示感谢。但是这篇文章中提到的方法:
First in cell M2 we enter
=concatenate(GoogleFinance(B2,"price",C2))
and then in the cell where the historical price is needed we enter
=value(mid(M2,3+find(":",M2,1+find(":",M2)),99))
经forcode测试没有成功,而且,这个方法有一个很大的缺点:它由两个表达式组成,需要用另外一个表格来计算中间结果,不是直接用一个公式得到任何一支股票在历史上某一天价格,可移植性大大减弱。
热议文章
2 Pings to “在GDocs表格中计算任意股票历史上任意天的收盘价”
Leave a Reply
市场研究
统计和数据
- 结息周期对年收益率的影响
- forcode自定义的股票指标:“相对大盘”
- 利用google docs spreadsheet制作股票实时行情(图)
- 在GDocs表格中计算任意股票历史上任意天的收盘价
- 平均数不是众数,反驳对职工平均工资数据的质疑!
- SAS V9.1.3的35个热门补丁迅雷下载地址列表
- excel也可以做出很专业的图表来
- 用SAS提取picasa相册缩略图源代码中的图片URL列表(组图)
- forcode教程:用arcgis制作结合统计数据的地图(图解)
- 美国普查问卷长表2000年(组图)
- 北大数据挖掘演讲-张俊妮博士-4月3号(周四),晚上7点到9点
- 说说中美两国政府的数据公开问题
- 美国人对流亡印度藏人的公开资金援助(截图)
- forcode点评:从美国人口教育结构看美国科幻市场的规模
- forcode点评:层次分析法(AHP)的缺陷
- Skype和SAS的正确读音
- forcode:SPSS多选项问题录入的一个高效率方法(组图)
- stata9安装宏clarify故障r(603)解决办法
- 080108统计模拟法计算pai的近似值.xls
- forcode翻译:人类行为分析中基因类型-环境的交互与相关
| 科幻奇幻 | 奇思妙想 |
|
|
| 科学探索与新技术 | 未来趋势 |
|
|
| 社会学研究 | 资料搜集 |
|
|
| 新闻精选 | 有趣的东西 |
|
|


手机阅读
04月 26th, 2009 at 08:12
[...] 另外,google docs spreadsheet本身不支持只用一个单元格和一个表达式来获取任何一支股票历史上任何一天的收盘价,我弄了一个表达式来实现这一点: =value(mid(concatenate(GoogleFinance(B2,"price",$M$1,$M$1)),29,8)) 参考forcode昨天(090417)写的帖子: 在GDocs表格中计算任意股票历史上任意天的收盘价 http://qixianglu.cn/20090424080344.html [...]
04月 28th, 2009 at 08:13
[...] 另外,可以参考: 在GDocs表格中计算任意股票历史上任意天的收盘价 利用google docs [...]