Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple ZeroDivisionError error on Windows #1

Closed
sauravkedia10 opened this issue May 14, 2017 · 6 comments
Closed

Multiple ZeroDivisionError error on Windows #1

sauravkedia10 opened this issue May 14, 2017 · 6 comments

Comments

@sauravkedia10
Copy link

sauravkedia10 commented May 14, 2017

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
Copy link
Contributor

lbrichards commented Sep 5, 2018

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
Copy link
Contributor

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
Copy link
Contributor

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

@lbrichards
Copy link
Contributor

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
Copy link

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
Copy link
Contributor

_testcapi now removed from github repository version.ee

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants