Python Forum
xlwings UDF showing name error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
xlwings UDF showing name error
#1
Hi,

I'm trying to write a simple UDF using xlwings. Note that I don't have much experience with Python.

The Python code that I'm trying to call via xlwings is:

import xlwings as xw
import mibian

@xw.func
def BSPutOptionImpVol(underlyingPrice,strike,interestRate,expiryDays,premium):
    c = mibian.BS([underlyingPrice, strike, interestRate, expiryDays], putPrice=premium)
    return c.impliedVolatility 
I have then called the function on Excel (via xlwings) with the formula =BSPutOptionImpVol(45,32,1,127,0.95)

Unfortunately, it's returning a very long error message

Error:
"NameError: name 'norm' is not defined call = self.underlyingPrice * norm.cdf(self._d1_) - \ File ""C:\Users\...\anaconda3\lib\site-packages\mibian\__init__.py"", line 307, in _price [self.callPrice, self.putPrice] = self._price() File ""C:\Users\...\anaconda3\lib\site-packages\mibian\__init__.py"", line 276, in __init__ estimate = eval(className)(args, volatility=mid, performance=True).putPrice File ""C:\Users\...\anaconda3\lib\site-packages\mibian\__init__.py"", line 29, in impliedVolatility self.__class__.__name__, args, putPrice=self.putPrice) File ""C:\Users\...\anaconda3\lib\site-packages\mibian\__init__.py"", line 293, in __init__ c = mibian.BS([underlyingPrice, strike, interestRate, expiryDays], putPrice=premium) File ""c:\users\...\documents\python scripts\BSPutImVol.py"", line 6, in BSPutOptionImpVol ret = func(*args) File ""C:\Users\...\anaconda3\lib\site-packages\xlwings\udfs.py"", line 298, in call_udf res = call_udf(script, fname, args, this_workbook, FromVariant(caller)) File ""C:\Users\...\anaconda3\lib\site-packages\xlwings\server.py"", line 195, in CallUDF return func(*args) File ""C:\Users\...\anaconda3\lib\site-packages\win32com\server\policy.py"", line 586, in _invokeex_ return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None) File ""C:\Users\...\anaconda3\lib\site-packages\win32com\server\policy.py"", line 283, in _invoke_ return self._invoke_(dispid, lcid, wFlags, args) File ""C:\Users\...\anaconda3\lib\site-packages\win32com\server\policy.py"", line 278, in _Invoke_"
Any one has an idea what I've done wrong? Thanks.
Reply
#2
it cannot find scipy installed. mibian needs it to be installed in order to use scipy.stats.norm (https://github.com/yassinemaaroufi/Mibia...it__.py#L9)
After answering your other thread I was playing with mibian and got same massage because initially I didn't install it

by the way it should print Mibian requires scipy to work properly
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks for the quick response. I thought I have mibian already installed. How do I check if mibian or scipy is installed or not? I'm using Spyder (Python 3.7) btw.
Reply
#4
installing mibian does not install scipy. Frankly, it's poorly written in many aspects.
however your other code was working, right? did you change something
mibian is instaled, otherwise you would get ImportError when try to import mibian.
This error comes later. By the way, I don't think this is the code that cause the error. Is there more code to it? norm is used in calculating greeks, not when calculating implied volatility
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(May-29-2020, 05:08 AM)buran Wrote: installing mibian does not install scipy. Frankly, it's poorly written in many aspects.
however your other code was working, right? did you change something
mibian is instaled, otherwise you would get ImportError when try to import mibian.
This error comes later. By the way, I don't think this is the code that cause the error. Is there more code to it? norm is used in calculating greeks, not when calculating implied volatility

Yes, it's not a great code. I've never worked with Python until 3 days ago. I use VBA at times and spend more time with SQL. So, steep learning curve for me atm.

My other code, which works, is this:

import mibian

def BSModel():
    c = mibian.BS([40.75, 32, 1, 127], putPrice=0.95)
    impvol = c.impliedVolatility
    print("Implied vol is ",impvol)
    c = mibian.BS([40.75, 32, 1, 127], volatility =impvol, putPrice=0.95)
    print("Delta is ",c.putDelta)
    print("Theta is ",c.putTheta)
    print("Vega is ",c.vega)
    print("Gamma is ",c.gamma)
The difference now is that I'm trying to pass the input parameters (which will be entered from Excel by users) into the Python function and then return the results to Excel. To keep it simple initially, I'm trying to just return the implied volatility results, which my code (first post) is trying to do.
Reply
#6
I am trying to say that:
1. The error shown in first post is from mibibian, being not able to use scipy.stats.norm for whatever reason, not because of xlwings
2. Just the code shown in first post cannot produce the error (as far as I can see, looking at the source code of mibian) because in calculating just the implied volatility norm is not used
3. If the code from post #5 is working both mibian and scipy are installed (assuming you use same python installation for both snippets).

Maybe post the full long traceback

EDIT - item2 is not correct statement
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Silly question but how do I get the full long traceback?
Reply
#8
(May-29-2020, 06:28 AM)pwt Wrote: Silly question but how do I get the full long traceback?
maybe I got it wrong... I was under impression you cut part of the traceback in the first psot
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
OK, item 2 in my post #6 is not correct
Anyway, the reason is it cannot find scipy, but I don't see why if the code in post #5 is working
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
(May-29-2020, 06:31 AM)buran Wrote: maybe I got it wrong... I was under impression you cut part of the traceback in the first psot

No, that was the full error message that appeared on Excel. I'm not sure if it's truncated by Excel though.

I've tested the code further by commenting out xlwings and doing a print as below. It works fine.
#import xlwings as xw
import mibian

#@xw.func
def BSPutOptionImpVol(underlyingPrice,strike,interestRate,expiryDays,premium):
    c = mibian.BS([underlyingPrice, strike, interestRate, expiryDays], putPrice=premium)
 #   return c.impliedVolatility
    print(c.impliedVolatility)
Output:
BSPutOptionImpVol(40.75,32,1,127,0.95) 45.654296875
So, I am a bit stumped why if I were to rewrite it for Excel usage, the Mibian library is not able to use scipy.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  xlwings error when reading a workbook Mishal0488 1 1,043 Aug-01-2023, 02:05 AM
Last Post: deanhystad
  Help using a dynamic array excel formula with XLWings FXMonkey 2 1,210 Jun-06-2023, 09:46 PM
Last Post: FXMonkey
  Showing error Sakeeb 3 20,770 Mar-31-2020, 01:03 PM
Last Post: buran
  No output from xlwings RunPython Macro branches 0 3,005 Oct-29-2017, 04:27 PM
Last Post: branches

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020