Я запускаю Anaconda 2.1.0 с Python 2.7.8 и Cython 0.2.1. В файле Implied_Vola.pyx я определилCython: TypeError: требуется целое число
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
и
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
Когда я скомпилировать и запустить его с помощью
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()
Я получаю "Ошибка типа: требуется целое." Он выбрасывается при
implied_Volatility = calc_implied_volatility(underlyingPrice,strikePrice,interestRate,daysToExpiration, price, optiontype)
называется Почему? Я не могу найти ошибку.
аналогичный вопрос здесь: http://stackoverflow.com/questions/21985484/typeerror-integer-required-while -defining-a-function – amwinter
Я бы не указал тип переменной для 'optiontype', но вы могли бы использовать' str optiontype' в вашем случае. Указание типов приносит вам больше преимуществ, когда вам приходится перебирать массивы или передавать данные на C-функции, в этом случае он вводит излишнюю сложность для вашего кода. –