YQL选项到期

3 投票
4 回答
2903 浏览
提问于 2025-04-16 20:16

我正在把一些Yahoo财经的CSV和网页抓取接口迁移到使用YQL(Yahoo查询语言),但在处理yahoo.finance.options这个表时遇到了一些困难。如果我查询某个股票符号的所有期权,我找不到与这些期权相关的到期日期。如果我查询股票符号和到期日期,那么我可以找到与这个期权链相关的到期日期,但在里面的具体期权却找不到。虽然我对期权到期的周期比较了解,也可以从某个日期推算出来,但这样做并不好;因为这样会产生更多的查询。我更希望能直接从数据中获取这些信息,因为这些信息应该是可以获取的(可以通过网页抓取)。有没有人知道在YQL中怎么获取这些信息,还是说我没希望了?

这是我正在使用的一些Python代码:

from xml.etree.ElementTree import ElementTree
import urllib, urllib2

class YQL(object):
    url = 'http://query.yahooapis.com/v1/public/yql'
    env = 'store://datatables.org/alltableswithkeys'
    format = 'xml'

    @classmethod
    def query(cls, string):
        q = urllib.quote(string)
        url = cls.url + '&'.join(('?q=%s' % q, 'env=%s' % cls.env,
                                   'format=%s' % cls.format))
        resp = urllib2.urlopen(url)
        return ElementTree(file=resp).getroot().find('results')[:]

chain = YQL.query('select * from yahoo.finance.options where symbol="WFC"')[0]
chain.attrib
option = chain[0]
option.attrib
for attr in option:
    print attr.tag, attr.text

4 个回答

1

为了获取可用的合同日期列表,可以使用 yahoo.finance.option_contracts。接着,你可以像这样打印出所有可能的卖出期权:

xml_dates = YQL.query('select * from yahoo.finance.option_contracts where ' + 
                                                         'symbol="HYG"')[0]
dates = []
for attr in xml_dates:
    print attr.tag, attr.text
    dates.append(attr.text)

for expiration in dates:
    xml_contracts = YQL.query('select * from yahoo.finance.options where '
                             +'symbol="HYG" AND expiration="' + expiration +'"')
    for option in xml_contracts[0]:
        if (option.attrib['type']=='P'):
            print 'Put: strike=' + option.findtext('strikePrice')+ ', lowball '
                      + 'ask=' + option.findtext('ask') + ', date='+ expiration
1

如果你在YQL查询中没有设置到期日期,那么我觉得数据集的到期日期会是这个月的下一个第三个星期五。你可以使用dateutil库在Python中定义这个日期,代码如下:

import dateutil.relativedelta as relativedelta
import dateutil.rrule as rrule
import datetime as dt

expiration=rrule.rrule(
    rrule.MONTHLY,
    byweekday=(relativedelta.FR(3)), dtstart=dt.datetime.now())[0]

(注意:上面的代码没有考虑假期,如果有假期的话,到期日期会是第三个星期四……如果你使用这段代码,记得检查一下当今天是这个月的第三个星期五时,Yahoo会返回什么——我不确定到期日期是当前日期,还是下个月的第三个星期五。)

如果你想查看某个特定到期年/月的期权链(而不是即将到来的第三个星期五),你可以使用这样的YQL查询:

chain = YQL.query('''
    select * from yahoo.finance.options
    where symbol="WFC" and expiration="2011-08"''')[0]

你可以在一个YQL查询中获取多个到期日期的数据:

chains = YQL.query('''
    select * from yahoo.finance.options
    where symbol="WFC" and (
        expiration="2011-08" or
        expiration="2011-10" or
        expiration="2012-01"
        )
    ''')

有趣的是,当请求多个到期日期的数据时,chain.attrib中会包含一个expiration键:

for chain in chains:
    print(chain.attrib)
    # for option in chain:
    #     print(option.attrib)
    #     for attr in option:
    #         print attr.tag, attr.text
    # print('-'*80)

返回结果是

{'symbol': 'WFC', 'expiration': '2011-08-19'}
{'symbol': 'WFC', 'expiration': '2011-10-21'}
{'symbol': 'WFC', 'expiration': '2012-01-20'}
4

你可以利用YQL的一个功能,就是可以把多个查询连接在一起,这样可以更方便地获取数据。

SELECT * FROM yahoo.finance.options WHERE symbol="%s" AND expiration in (SELECT contract FROM yahoo.finance.option_contracts WHERE symbol="%s")

这里的%s就是你想要查找的符号,显而易见。这条查询会从所有可用的到期日期中提取所有的期权链,这样你就不用进行多次查询了。

撰写回答