首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:需要一个整数

TypeError:需要一个整数
EN

Stack Overflow用户
提问于 2015-01-17 17:32:03
回答 1查看 2.6K关注 0票数 2

我使用Python2.7.8和Cython 0.2.1运行Anaconda2.1.0。在文件Implied_Vola.pyx中,我定义了

代码语言:javascript
复制
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

代码语言:javascript
复制
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

当我编译并运行它时

代码语言:javascript
复制
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()

我得到"TypeError:需要一个整数“。它是在

代码语言:javascript
复制
implied_Volatility = calc_implied_volatility(underlyingPrice,strikePrice,interestRate,daysToExpiration, price, optiontype) 

叫什么?我找不到任何窃听器。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-17 19:31:22

这是optiontype param。奇怪的是,Cython char类型需要一个整数。您应该将cython类型更改为strchar*,或者从python传递ord(string[0])

小例子:

代码语言:javascript
复制
# file: cdef.pyx
cpdef f(char c):
  print c

然后,在python shell中:

代码语言:javascript
复制
>>> 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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28002214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档