我正在寻找创建一个可以放在 .html 中的 javascript 函数
我想向函数发送股票代码、开始日期和结束日期。
我想让函数返回一个二维数组,其中每一行是请求库存的 EOD 或 OHLC 数据的一天。
我想使用雅虎,因为谷歌股票数据将被淘汰。
我已经用其他语言做到了这一点,但我是 java 脚本的新手,几乎迷路了。
以下代码是在 Stack 上找到的,是我能找到的最接近的代码,但我不明白如何使用它。
<script type="text/javascript" src="jquery-1.5.1.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var symbol='goog';
var callback = function(data) {
var price=data.query.results.span[0].content;
alert('Stock Price: ' + price);
};
var url = 'http://query.yahooapis.com/v1/public/yql';
// this is the lovely YQL query (html encoded) which lets us get the stock price:
// select * from html where url="http://finance.yahoo.com/q?s=goog" and xpath='//span[@id="yfs_l10_goog"]'
var data = "q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Ffinance.yahoo.com%2Fq%3Fs%3D" + symbol + "%22%20and%20xpath%3D'%2F%2Fspan%5B%40id%3D%22yfs_l10_" + symbol + "%22%5D'&format=json";
$.getJSON(url, data, callback);
});
以下是我在 javascript 中想要的,但它在 python 中
def get_historical_prices(symbol, start_date, end_date):
"""
Get historical prices for the given ticker symbol.
Date format is 'YYYYMMDD'
Returns a nested list.
"""
url = 'http://ichart.yahoo.com/table.csv?s=%s&' % symbol + \
'd=%s&' % str(int(end_date[4:6]) - 1) + \
'e=%s&' % str(int(end_date[6:8])) + \
'f=%s&' % str(int(end_date[0:4])) + \
'g=d&' + \
'a=%s&' % str(int(start_date[4:6]) - 1) + \
'b=%s&' % str(int(start_date[6:8])) + \
'c=%s&' % str(int(start_date[0:4])) + \
'ignore=.csv'
days = urllib.urlopen(url).readlines()
data = [day[:-2].split(',') for day in days]
return data
对不起,如果我要求太多。因为我是 javascript 新手,但我真的有兴趣尽可能多地学习。