vollib / py_vollib

MIT License
308 stars 68 forks source link

Multiple ZeroDivisionError error on Windows #1

Closed sauravkedia10 closed 1 year ago

sauravkedia10 commented 7 years ago

I am using Winpython64bit Python3.5 distribution on my Windows10 64 bit pc. When I first ran function to find black implied volatility I got an error saying that

ImportError: No module named _testcapi

Apparently _testcapi is not a package which can be installed using pip. It comes pre-installed. Neverthless, I went to py_lets_be_rational\constants and removed the from _testcapi import DBL_MIN, DBL_MAX line and added the following:

DBL_MIN = sys.float_info.min
DBL_MAX = sys.float_info.max

The values captured by DBL_MIN and DBL_MAX are as follows:

import sys
DBL_MIN = sys.float_info.min
DBL_MAX = sys.float_info.max
DBL_MAX
Out[34]: 1.7976931348623157e+308
DBL_MIN
Out[35]: 2.2250738585072014e-308

After this the code was compiling and I could run the example mentioned in the documentation. It worked okay. Next, I tried to find the iv of an actual trading option on link: https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=ACC&instrument=FUTSTK&type=-&strike=-&expiry=25MAY2017

I used the following and got the error:

import py_vollib.black.implied_volatility as biv
P = 30.70
F = 1685
K = 1680
r= .06
flag = "c"
biv.implied_volatility(P, F, K, r, 0.35, flag)
Traceback (most recent call last):

  File "<ipython-input-29-bcdbf833ce25>", line 1, in <module>
    biv.implied_volatility(P, F, K, r, 0.35, flag)

  File "D:\Filehistory Backup D\WinPython-64bit-3.5.2.2\python-3.5.2.amd64\lib\site-packages\py_vollib\black\implied_volatility.py", line 99, in implied_volatility_of_discounted_option_price
    binary_flag[flag]

  File "D:\Filehistory Backup D\WinPython-64bit-3.5.2.2\python-3.5.2.amd64\lib\site-packages\py_lets_be_rational\lets_be_rational.py", line 653, in implied_volatility_from_a_transformed_rational_guess
    price, F, K, T, q, implied_volatility_maximum_iterations)

  File "D:\Filehistory Backup D\WinPython-64bit-3.5.2.2\python-3.5.2.amd64\lib\site-packages\py_lets_be_rational\lets_be_rational.py", line 613, in implied_volatility_from_a_transformed_rational_guess_with_limited_iterations
    price / (sqrt(F) * sqrt(K)), x, q, N) / sqrt(T)

  File "D:\Filehistory Backup D\WinPython-64bit-3.5.2.2\python-3.5.2.amd64\lib\site-packages\py_lets_be_rational\lets_be_rational.py", line 388, in _unchecked_normalised_implied_volatility_from_a_transformed_rational_guess_with_limited_iterations
    s_l = s_c - b_c/v_c

ZeroDivisionError: float division by zero

I keep getting this error for almost all options. Then on my windows machine again, I tried calculating the implied vol using the py_vollib.ref_python modules and it worked. Next, I used another pc with linux and Anaconda python distribution and ran the original py_vollib.black.implied_volatility module and it worked well.

So, we have a situation where I am able to calculate options prices on linux distribution and on Windows 64bit platform using ref_python module but not through the recommended way on Windows 64 bit platform. Any suggestions? I want to use this package on a windows machine, would you recommend me using the ref_python modules in the interim? What could be the challenge?

lbrichards commented 6 years ago

ref_python, as the name implies, is just there for reference. It does not benefit from any of the speedups of Peter Jaeckel's algorithms. That said, if you don't need the speed, ref_python should work for you.

I don't understand why there should be any Windows dependencies. We made py_vollib to remove all SWIG and C compiler dependencies, and we thoroughly tested it on Python 3.x. When I run the example above on Python 3.6.4 on Mac OSX, I get:

>>> import py_vollib.black.implied_volatility as biv
>>> P = 30.70
>>> F = 1685
>>> K = 1680
>>> r= .06
>>> flag = "c"
>>> biv.implied_volatility(P, F, K, r, 0.35, flag)
0.07248728185534212

It has been a long time since you opened this issue. (Apologies for not noticing until now) I would be interested to know whether you still have the problem and with which versions of Python & Windows.

Larry

lbrichards commented 6 years ago

By the way, you're using Black IV in your example. Black IV expect you to use the forward price (or futures price, in the case of futures options). Are you confident that your F is the forward price, and not the spot?

lbrichards commented 6 years ago

If you want to use the spot price, you should use py_vollib.black_scholes.implied_volatility instead.

lbrichards commented 6 years ago

BTW, thank you for highlighting this misuse of _testcapi. It should not be included in a production library, and sys.float_info should be used instead, as you have indicated. I will put on my TODO list to fix this and issue an update.

romanrdgz commented 5 years ago

I just ran into the same problem while launching it in an Amazon compute engine with Linux. Has this issue been solved? Just installed py_vollib using pip for python3, so I would expect it to be the latest.

lbrichards commented 1 year ago

_testcapi now removed from github repository version.ee