YQL 期权到期

发布于 2024-11-17 06:49:04 字数 1011 浏览 0 评论 0原文

我正在迁移一些雅虎财经 CSV/屏幕抓取界面以使用 YQL,并在 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

I'm migrating some Yahoo Finance CSV/screen-scraping interfaces to use YQL, and struggling with the yahoo.finance.options table. If I query for all options for a given symbol, I don't find the expiration dates associated with the options. If I query for symbol and expiration, then I find the expiration date associated with the chain, but not the options therein. While I'm familiar with the cycle of options expiration and can bootstrap it from a given date, that's a poor solution; for one thing, it will generate more queries. I'd much prefer to introspect it from the data, since it should be available (it can be screen-scraped). Anybody know how to get at this in YQL, or am I out of luck?

Here's some python code I'm using:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

简单 2024-11-24 06:49:04

显然,您可以进一步利用 YQL 的链接查询功能,

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

其中 %s 是您要查找的符号。这将从所有可用的到期日期中提取所有期权链,并为您节省一些查询。

You can further take advantage of YQL's ability to chain queries with

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

Where %s is the symbol you're seeking, obviously. This will pull all option chains from all available expiration dates, and save you several queries.

终弃我 2024-11-24 06:49:04

如果你没有在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]

注意:上面的代码忽略了假期,其中如果您使用此代码,请务必检查当前日是该月第三个星期五时雅虎决定返回的内容 - 我不确定到期日是否有效将是当前日期,或下个月的第三个星期五。)

要查看特定到期年/月(除了即将到来的第三个星期五)的期权链,您可以使用 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'}

If you don't set the expiration date in the YQL query, then I think the expiration date of the data set will be the upcoming third Friday of the month. Using dateutil, you could define this date in Python with:

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]

(Note: the above code ignores holidays, in which case the expiration date would be the third Thursday.... If you use this code, be sure to also check what Yahoo decides to return when the current day is the third Friday of the month -- I'm not sure if expiration date will be the current date, or the third Friday of the next month.)

To see the option chain for a particular expiration year/month (other than the upcoming third Friday), you might use a YQL query such as:

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

It is possible to obtain data on multiple expirations all in one YQL query:

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

Interestingly, when data on multiple expirations is requested, the chain.attrib does include an expiration key:

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)

yields

{'symbol': 'WFC', 'expiration': '2011-08-19'}
{'symbol': 'WFC', 'expiration': '2011-10-21'}
{'symbol': 'WFC', 'expiration': '2012-01-20'}
可爱暴击 2024-11-24 06:49:04

要获取可用合同日期的列表,请使用 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

In order to get the list of available contract dates use the yahoo.finance.option_contracts. To continue your code (which I appreciated) you could print all possible put options like this:

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
最好是你 2024-11-24 06:49:04

如果您没有在 YQL 查询中设置到期日期,那么我认为将返回本月最早到期(但晚于今天)的合约的数据集。这些不一定是每月选项,在第三个星期五到期。返回的数据集可能是本月到期的每周或每季度期权(如果您的底层证券有)。
即使您在月度到期后几天(​​第三个星期五)发出查询,您也有可能获得本月到期的数据,而不是下个月月度的数据。

因此,正如 unutbu 建议的那样,始终发出指定到期月份的查询来接收返回数据集中的到期日期是有意义的,并且确切地知道您将获得什么。或者甚至更好,指定到期日期

遗憾的是,查询 yahoo.finance.option_contracts 表不会返回到期日期,而只返回月份。它无助于查明您的标的资产是否还有月度合约以外的选项及其到期日。

If you don't set the expiration date in the YQL query, then I think the data set will be returned for the contracts expiring soonest this month (but later than today). Those are not necessarily the monthly options, expiring on third Friday. The returned dataset could be for weeklies or quartelies options expiring this month (if your underlying has ones).
Even if you issue the query a few days after expiration of the monthlies (3-rd Friday) there is a chance that you'll get data for something expiring this month rather than next month monthlies.

So, as unutbu suggested, it makes sense always issuing queries with expiration month specified to receive expiration date in the returned data set, and know exactly what you are getting. Or even better, with expiration date specified.

Too bad that querying the yahoo.finance.option_contracts table does not return expiration date, just the month. And it does not help finding out if your underlying has options other than monthlies, and their expiration dates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文