GithubHelp home page GithubHelp logo

lienus10 / yoptions Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 10.0 45.3 MB

License: MIT License

Python 97.63% C 0.95% Fortran 0.04% Makefile 0.01% MATLAB 0.01% TeX 0.12% PowerShell 0.01% Batchfile 0.01% Shell 0.01% HTML 0.07% CSS 0.24% JavaScript 0.04% Roff 0.02% Cython 0.86%

yoptions's Introduction

Yahoo Options Data for Python

This module retrieves option data from the yahoo finance website and calculates greeks from it.

Warning: The retrieved data and greeks might be outdated or incorrect. Use at your own risk.

Installation

You can install the library by using:

pip install yoptions

Import

import yoptions as yo

Download option chain with greeks

To download the option chain for a specific stock for the next expiration date, you can simply use:

# Chain of all FORD MOTOR COMPANY call options for next expiration date
chain = yo.get_chain_greeks(stock_ticker='F', dividend_yield=0, option_type='c', risk_free_rate=None)
print(chain.head().to_string())

risk_free_rate is optional. If it is left empty, the current risk free rate from the US treasury will be retrieved and used.

This will calculate and print Delta, Gamma, Theta, Vega and Rho for every option in the chain.

OUTPUT:

                      Symbol  Strike  Last Price    Bid    Ask  Impl. Volatility   Delta   Gamma   Theta    Vega     Rho
0   F210528C00000500     0.5       12.65  12.60  12.70         16.750005  0.9994  0.0001 -0.0081  0.0000  0.0000
1   F210528C00001500     1.5       10.00  10.35  12.90          9.125004  0.9998  0.0001 -0.0017  0.0000  0.0001
2   F210528C00002500     2.5       10.81  10.50  11.20         11.562503  0.9910  0.0022 -0.0685  0.0002  0.0001
3   F210528C00005000     5.0        6.65   8.00   8.45          5.812503  0.9930  0.0035 -0.0277  0.0002  0.0003
4   F210528C00005500     5.5        6.15   7.60   7.95          5.750003  0.9879  0.0056 -0.0441  0.0003  0.0003
5   F210528C00006000     6.0        5.55   7.05   7.20          3.375002  0.9994  0.0006 -0.0018  0.0000  0.0003
6   F210528C00007000     7.0        6.05   6.05   6.25          3.234377  0.9969  0.0030 -0.0075  0.0001  0.0004
7   F210528C00008000     8.0        5.06   5.05   5.15          2.625003  0.9958  0.0048 -0.0080  0.0001  0.0004
8   F210528C00008500     8.5        4.85   4.50   4.70          2.765628  0.9868  0.0127 -0.0230  0.0003  0.0005
9   F210528C00009000     9.0        4.08   4.05   4.20          1.750001  0.9985  0.0029 -0.0023  0.0000  0.0005
10  F210528C00009500     9.5        3.60   3.55   3.65          1.828126  0.9928  0.0112 -0.0091  0.0002  0.0005
11  F210528C00010000    10.0        3.05   3.05   3.20          1.312503  0.9977  0.0056 -0.0025  0.0001  0.0005
12  F210528C00010500    10.5        2.60   2.59   2.66          1.125004  0.9966  0.0093 -0.0031  0.0001  0.0006
13  F210528C00011000    11.0        2.15   2.09   2.14          0.750003  0.9993  0.0034 -0.0007  0.0000  0.0006
14  F210528C00011500    11.5        1.81   1.60   1.68          0.828127  0.9850  0.0472 -0.0079  0.0004  0.0006
15  F210528C00012000    12.0        1.12   1.11   1.15          0.554692  0.9852  0.0694 -0.0053  0.0004  0.0006
16  F210528C00012500    12.5        0.70   0.70   0.72          0.589848  0.8674  0.3746 -0.0310  0.0021  0.0006
17  F210528C00013000    13.0        0.33   0.32   0.33          0.496099  0.5984  0.8033 -0.0468  0.0038  0.0004
18  F210528C00013500    13.5        0.14   0.13   0.14          0.519536  0.2292  0.6010 -0.0383  0.0029  0.0002
19  F210528C00014000    14.0        0.06   0.05   0.06          0.570317  0.0626  0.2224 -0.0171  0.0012  0.0000
20  F210528C00014500    14.5        0.03   0.03   0.04          0.679691  0.0240  0.0857 -0.0093  0.0005  0.0000
21  F210528C00015000    15.0        0.03   0.02   0.03          0.789065  0.0114  0.0391 -0.0057  0.0003  0.0000
22  F210528C00015500    15.5        0.02   0.01   0.02          0.843752  0.0040  0.0146 -0.0024  0.0001  0.0000
23  F210528C00016000    16.0        0.01   0.00   0.01          0.843752  0.0008  0.0033 -0.0006  0.0000  0.0000
24  F210528C00016500    16.5        0.01   0.00   0.02          1.031255  0.0015  0.0048 -0.0012  0.0000  0.0000
25  F210528C00017000    17.0        0.01   0.00   0.00          0.500005  0.0000  0.0000 -0.0000  0.0000  0.0000
26  F210528C00017500    17.5        0.01   0.00   0.01          1.125004  0.0003  0.0010 -0.0003  0.0000  0.0000
27  F210528C00018000    18.0        0.01   0.00   0.01          1.250004  0.0004  0.0011 -0.0004  0.0000  0.0000
28  F210528C00018500    18.5        0.02   0.00   0.01          1.312503  0.0002  0.0007 -0.0003  0.0000  0.0000
29  F210528C00019500    19.5        0.01   0.00   0.01          1.500002  0.0002  0.0006 -0.0003  0.0000  0.0000


To download the option chain on a specific expiration date, simply use this:

# Chain of all FORD MOTOR COMPANY put options that expire on January 21, 2022

chain = yo.get_chain_greeks_date(stock_ticker='F', dividend_yield=0, option_type='p', 
                                 expiration_date='2022-01-21',risk_free_rate=None)
print(chain.head().to_string())

Any date has to be in the format 'YYYY-MM-DD'.

OUTPUT:

             Symbol  Strike  Last Price  Bid   Ask  Impl. Volatility   Delta   Gamma   Theta    Vega     Rho
0  F220121P00000500     0.5        0.01  0.0  0.01          1.562502 -0.0006  0.0001 -0.0001  0.0002 -0.0001
1  F220121P00001000     1.0        0.01  0.0  0.02          1.312503 -0.0015  0.0003 -0.0001  0.0005 -0.0002
2  F220121P00001500     1.5        0.03  0.0  0.03          1.171879 -0.0027  0.0007 -0.0002  0.0009 -0.0003
3  F220121P00002000     2.0        0.02  0.0  0.02          0.968750 -0.0025  0.0007 -0.0002  0.0008 -0.0003
4  F220121P00002500     2.5        0.03  0.0  0.03          0.890626 -0.0037  0.0012 -0.0002  0.0012 -0.0004
...

Download greeks of an option

There are two ways to download data and greeks from a specific option. Either by entering the option ticker symbol or by putting in the basic information of the option

# Both commands return the call option of BP that will expire on June 17, 2022 at a strike price of 30.
print(yo.get_option_greeks('BP', '2022-06-17', 'c', 30, dividend_yield=0.04, risk_free_rate=0.014).to_string())

print(yo.get_option_greeks_ticker(option_ticker='BP220617C00030000', dividend_yield=0.04, 
                                  risk_free_rate=0.014).to_string())

BP is a British company, so the US risk free rate doesn't apply. Therefore we put in the British risk free rate at the end.

OUTPUT:
              Symbol  Strike  Last Price   Bid   Ask  Impl. Volatility   Delta   Gamma  Theta    Vega     Rho
0  BP220617C00030000    30.0        1.76  1.75  1.84          0.279792  0.3577  0.0486 -0.004  0.1024  0.0805

Download historical option prices

This again can be achieved in two ways: By putting in the option ticker symbol or basic information about the option.

# Both commands download all historical data for the Apple put option that will expire at July 16, 2021 at the 
# strike price of 90
print(yo.get_historical_option_ticker(option_ticker='AAPL210716P00090000'))

print(yo.get_historical_option('AAPL', '2021-07-16', 90, 'p'))
OUTPUT:
           Date  Open  High   Low  Close  Adj Close  Volume
0    2020-11-23  3.01  3.27  3.01   3.15       3.15    58.0
1    2020-11-24  3.15  3.15  2.93   2.93       2.93  2611.0
2    2020-11-25  2.79  2.94  2.78   2.94       2.94   161.0
5    2020-11-30  2.79  2.79  2.74   2.74       2.74   417.0
6    2020-12-01  2.71  2.71  2.54   2.63       2.63   147.0
..          ...   ...   ...   ...    ...        ...     ...
144  2021-05-17  0.31  0.31  0.30   0.30       0.30    21.0
145  2021-05-18  0.26  0.26  0.26   0.26       0.26   100.0
146  2021-05-19  0.38  0.38  0.35   0.37       0.37   214.0
148  2021-05-21  0.24  0.26  0.24   0.25       0.25   219.0
150  2021-05-24  0.20  0.20  0.18   0.18       0.18   237.0

Download option chain without greeks

This can be achieved in a very similar way than download the chain with greeks.

# Both lines return the chain for MICROSOFT call options at the next expiration date (May 28, 2021)
print(yo.get_plain_chain(option_ticker='MSFT', option_type='c').head().to_string())

print(yo.get_plain_chain_date(option_ticker='MSFT', option_type='c', 
                              expiration_date='2021-05-28').head().to_string())
OUTPUT:
                Symbol              Last Trade  Strike  Last Price     Bid     Ask  Change  % Change Volume  Open Interest  Impl. Volatility
0  MSFT210528C00145000  2021-05-19 03:58:33 PM   145.0       95.30  106.30  106.70     0.0       0.0      -              9          2.062505
1  MSFT210528C00150000  2021-05-19 03:46:36 PM   150.0       90.55  101.30  101.90     0.0       0.0      -              5          2.164067
2  MSFT210528C00155000  2021-05-19 07:41:08 PM   155.0       86.70   96.30   96.80     0.0       0.0      -              9          1.949219
3  MSFT210528C00160000  2021-05-19 07:22:26 PM   160.0       82.35   91.35   91.75     0.0       0.0     16             17          1.835938
4  MSFT210528C00165000  2021-05-19 09:20:34 PM   165.0       76.70   86.35   86.75     0.0       0.0      7              9          1.722658
...

Download data of a single option without greeks

This again can be achieved in a similar way as downloading greek data for a single option.

# Both lines return data for the VISA put option that expires on December 12, 2021 at a strike price of 275.
print(yo.get_plain_option(stock_ticker='V', expiration_date='2021-12-17', 
                          option_type='p', strike=275).to_string())
                          
print(yo.get_plain_option_ticker(option_ticker='V211217P00275000').to_string())
OUTPUT:
             Symbol              Last Trade  Strike  Last Price    Bid    Ask  Change  % Change  Volume  Open Interest  Impl. Volatility
0  V211217P00275000  2021-05-07 04:21:37 PM   275.0       47.15  48.65  49.15     0.0       0.0       6              8            0.2342

Get the price of the underlying stock

The current price of the underlying stock can of an option can be downloaded by doing this:

# This downloads the current stock price for COCA-COLA, based on this COCA-COLA call option
print(yo.get_underlying_price(option_ticker='KO210528C00055000'))
OUTPUT:
54.5127

Get all expiration dates for a stock

All expiration dates can be downloaded as a list by doing this:

# This downloads all expiration dates for THE WALT DISNEY COMPANY options
print(yo.get_expiration_dates(stock_ticker='DIS'))
OUTPUT:
['2021-05-28', '2021-06-04', '2021-06-11', '2021-06-18', '2021-06-25', '2021-07-02', '2021-07-16', '2021-08-20', '2021-10-15', '2022-01-21', '2022-06-17', '2023-01-20']```

yoptions's People

Contributors

lienus10 avatar rafacost avatar d-lopes avatar

Stargazers

agodonnell avatar  avatar w00w avatar Carlo Eugster avatar Nuno Goncalves avatar  avatar Kori Vernon avatar Joseph Loss avatar Swapnil Parekh avatar

Watchers

 avatar agodonnell avatar

yoptions's Issues

KeyError Exception Occurred

In the example query the following error is thrown:

File main.py", line 6, in <module>
    chain = yo.get_chain_greeks(stock_ticker='F', dividend_yield=0, option_type='c', risk_free_rate=None)
KeyError: -1

def get_historical_option: ticker wrong assembly

I fixed this function, because i received error 404 and the ticker was wrong made. I share my solution:

def get_historical_option(stock_ticker, expiration_date, strike, option_type):
zeroes = ''
for i in range(5 - len(str(int(float(strike))))):
zeroes += '0'

fecha = Timestamp(expiration_date).strftime("%Y%m%d")
fecha = fecha[2:len(fecha)]
ticker = (str(stock_ticker).upper() + fecha
          + str(option_type).upper() + zeroes + format(float(strike), ".2f") + '0').replace('.', '')

return ticker

Deprecation Warning: setup.py install

When using pip install yoptions, I get the below error:
DEPRECATION: yoptions is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559

First example in documentation results in divide by 0.

If run today:

# Chain of all FORD MOTOR COMPANY call options for next expiration date chain = yo.get_chain_greeks(stock_ticker='F', dividend_yield=0, option_type='c', risk_free_rate=None) print(chain.head().to_string())

results in:

Traceback (most recent call last): File "[redacted path]/option_chain.py", line 3, in <module> chain = yo.get_chain_greeks(stock_ticker='F', dividend_yield=0.01, option_type='c', risk_free_rate=None) File "[redacted path]/.local/lib/python3.10/site-packages/yoptions/main.py", line 19, in get_chain_greeks return __get_chain(option_type, data, dividend_yield, risk_free_rate) File "[redacted path]/.local/lib/python3.10/site-packages/yoptions/main.py", line 245, in __get_chain return __greeks(data, chain, option_type, r, dividend_yield) File "[redacted path]/.local/lib/python3.10/site-packages/yoptions/main.py", line 344, in __greeks d1 = (log(float(underlying_price) / strike[-1]) + ((r - dividend_yield) + v * v / 2.) * t) / ( ZeroDivisionError: float division by zero

Changing to a value different than 0 like 0.1 presents the same error. None will output the frame but obviously not calculate the greeks. Not sure if PEBKAC, env or bunk data.

risk free scraping problem

d:\Miniconda3\envs\icm\lib\site-packages\yoptions\main.py in __risk_free(days)
384
385 table = soup.find("table", attrs={'class': 't-chart'})
--> 386 rows = table.find_all('tr')
387 lastrow = len(rows) - 1
388 cells = rows[lastrow].find_all("td")

AttributeError: 'NoneType' object has no attribute 'find_all'

API no longer functional

`
import yoptions as yo

Chain of all FORD MOTOR COMPANY call options for next expiration date

chain = yo.get_chain_greeks(stock_ticker='F', dividend_yield=0, option_type='c', risk_free_rate=None)
print(chain.head().to_string())
`

AttributeError Traceback (most recent call last)
Input In [2], in <cell line: 2>()
1 # Chain of all FORD MOTOR COMPANY call options for next expiration date
----> 2 chain = yo.get_chain_greeks(stock_ticker='F', dividend_yield=0, option_type='c', risk_free_rate=None)
3 print(chain.head().to_string())

File ~/opt/miniconda3/envs/pytorch_quant/lib/python3.9/site-packages/yoptions/main.py:19, in get_chain_greeks(stock_ticker, dividend_yield, option_type, risk_free_rate)
16 with urllib.request.urlopen("https://query2.finance.yahoo.com/v7/finance/options/" + stock_ticker) as url:
17 data = json.loads(url.read().decode())
---> 19 return __get_chain(option_type, data, dividend_yield, risk_free_rate)

File ~/opt/miniconda3/envs/pytorch_quant/lib/python3.9/site-packages/yoptions/main.py:245, in __get_chain(option_type, data, dividend_yield, r)
243 if option_type == 'c':
244 chain = data["optionChain"]["result"][0]["options"][0]["calls"]
--> 245 return __greeks(data, chain, option_type, r, dividend_yield)
246 else:
247 if option_type == 'p':

File ~/opt/miniconda3/envs/pytorch_quant/lib/python3.9/site-packages/yoptions/main.py:260, in __greeks(data, chain, option_type, r, dividend_yield)
257 today = datetime.datetime.now()
259 if r is None:
--> 260 r = __risk_free((expiration_date - today).days)
262 contract_symbols = []
263 last_traded = []

File ~/opt/miniconda3/envs/pytorch_quant/lib/python3.9/site-packages/yoptions/main.py:386, in __risk_free(days)
383 soup = BeautifulSoup(r.text, 'html.parser')
385 table = soup.find("table", attrs={'class': 't-chart'})
--> 386 rows = table.find_all('tr')
387 lastrow = len(rows) - 1
388 cells = rows[lastrow].find_all("td")

AttributeError: 'NoneType' object has no attribute 'find_all'

timestamp to time using local timezone when it should use UTC

when the timestamp conversion from yahoo supplies timestamps is performed the conversion is taking place using the local timezone rather than UTC. Yahoo provides its timetamps using UTC so rather than using datetime.dateime.fromtimestamp datetime.datetime.utcfromtimestamp should be used

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.