[英]Cython: TypeError: an integer is required
I run Anaconda 2.1.0 with Python 2.7.8 and Cython 0.2.1. 我使用Python 2.7.8和Cython 0.2.1运行Anaconda 2.1.0。 In file Implied_Vola.pyx I have defined
在Implied_Vola.pyx文件中我定义了
def Implied_Vola(underlyingPrice,strikePrice,interestRate,daysToExpiration,price,optiontype):
underlyingPrice = float(underlyingPrice)
strikePrice = float(strikePrice)
interestRate = float(interestRate) / 100
daysToExpiration = float(daysToExpiration) / 365
price = round(float(price), 6)
implied_Volatility = calc_implied_volatility(underlyingPrice,strikePrice,interestRate,daysToExpiration, price, optiontype)
return implied_Volatility
and 和
cdef float calc_implied_volatility(float underlyingPrice, float strikePrice, float interestRate, float daysToExpiration, float price, char optiontype):
'''Returns the estimated implied volatility'''
cdef float target, high, low, mid, volatility, estimate
cdef int decimals
target = price
high=500.0
low=0.0
decimals = len(str(target).split('.')[1]) # Count decimals
for i in range(10000): # To avoid infinite loops
mid = (high + low) / 2
if mid < 0.00001:
mid = 0.00001
if optiontype=='Call':
volatility=mid
estimate = callPrice(underlyingPrice,strikePrice,interestRate,daysToExpiration, volatility)
if optiontype=='Put':
volatility=mid
estimate = putPrice(underlyingPrice,strikePrice,interestRate,daysToExpiration, volatility)
if round(estimate, decimals) == target:
break
elif estimate > target:
high = mid
elif estimate < target:
low = mid
return mid
When I compile and run it via 当我编译并运行它时
import Implied_Vola
underlyingPrice=5047
strikePrice=4600
interestRate=3
daysToExpiration=218
price=724.5
optiontype='Call'
start=time.time()
vola= Implied_Vola.Implied_Vola(underlyingPrice,strikePrice,interestRate,daysToExpiration,price,optiontype)
end=time.time()
I get "TypeError: an integer is required." 我得到“TypeError:需要一个整数。” It is thrown when
扔的时候
implied_Volatility = calc_implied_volatility(underlyingPrice,strikePrice,interestRate,daysToExpiration, price, optiontype)
is called Why is that? 被称为为什么? I can't find any bug.
我找不到任何错误。
It's the optiontype
param. 这是
optiontype
参数。 Weirdly, the Cython char
type expects an integer. 奇怪的是,Cython
char
类型需要一个整数。 You should either change the cython type to str
or char*
, or pass ord(string[0])
from python. 您应该将cython类型更改为
str
或char*
,或者从python传递ord(string[0])
。
Small example: 小例子:
# file: cdef.pyx
cpdef f(char c):
print c
Then, in a python shell: 然后,在python shell中:
>>> import pyximport; pyximport.install(); import cdef
>>> cdef.f('hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "cdef.pyx", line 1, in cdef.f (.../cdef.c:598)
cpdef f(char c):
TypeError: an integer is required
>>> cdef.f('h')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "cdef.pyx", line 1, in cdef.f (.../cdef.c:598)
cpdef f(char c):
TypeError: an integer is required
>>> cdef.f(5)
5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.