我正在遵循Michael Halls-Moore算法交易一书,并对代码有一些问题。当我将代码粘贴到python中时,我得到了一堆错误。
我是不是遗漏了什么,因为它和书中写的一模一样?
from __future__ import print_function
from numpy import cumsum, log, polyfit, sqrt, std, subtract
from numpy.random import randn
def hurst(ts):
"""Returns the Hurst Exponent of the time series vector ts"""
# Create the range of lag values
lags = range(2, 100)
# Calculate the array of the variances of the lagged differences
tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]
# Use a linear fit to estimate the Hurst Exponent
poly = polyfit(log(lags), log(tau), 1)
# Return the Hurst exponent from the polyfit output
return poly[0]*2.0
# Create a Geometric Brownian Motion, Mean-Reverting and Trending Series
gbm = log(cumsum(randn(100000))+1000)
mr = log(randn(100000)+1000)
tr = log(cumsum(randn(100000)+1)+1000)
# Output the Hurst Exponent for each of the above series
# and the price of Amazon (the Adjusted Close price) for
# the ADF test given above in the article
print("Hurst(GBM): %s" % hurst(gbm))
print("Hurst(MR): %s" % hurst(mr))
print("Hurst(TR): %s" % hurst(tr))
# Assuming you have run the above code to obtain 'amzn'!
print("Hurst(AMZN): %s" % hurst(amzn['Adj Close']))下面的错误
james@ubuntu:~$ python
Python 2.7.12 (default, Jul 1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>>
>>> from numpy import cumsum, log, polyfit, sqrt, std, subtract
>>> from numpy.random import randn
>>>
>>>
>>> def hurst(ts):
... """Returns the Hurst Exponent of the time series vector ts"""
... # Create the range of lag values
...
>>> lags = range(2, 100)
File "<stdin>", line 1
lags = range(2, 100)
^
IndentationError: unexpected indent
>>>
>>> # Calculate the array of the variances of the lagged differences
...
>>> tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]
File "<stdin>", line 1
tau = [sqrt(std(subtract(ts[lag:], ts[:-lag]))) for lag in lags]
^
IndentationError: unexpected indent
>>>
>>> # Use a linear fit to estimate the Hurst Exponent
...
>>> poly = polyfit(log(lags), log(tau), 1)
File "<stdin>", line 1
poly = polyfit(log(lags), log(tau), 1)
^
IndentationError: unexpected indent
>>>
>>> # Return the Hurst exponent from the polyfit output
...
>>> return poly[0]*2.0
File "<stdin>", line 1
return poly[0]*2.0
^
IndentationError: unexpected indent
>>>
>>> # Create a Gometric Brownian Motion, Mean-Reverting and Trending Series
... gbm = log(cumsum(randn(100000))+1000)
>>> mr = log(randn(100000)+1000)
>>> tr = log(cumsum(randn(100000)+1)+1000)
>>>
>>> # Output the Hurst Exponent for each of the above series
... # and the price of Amazon (the Adjusted Close price) for
... # the ADF test given above in the article
... print("Hurst(GBM): %s" % hurst(gbm))
Hurst(GBM): None
>>> print("Hurst(MR): %s" % hurst(mr))
Hurst(MR): None
>>> print("Hurst(TR): %s" % hurst(tr))
Hurst(TR): None
>>>
>>> # Assuming you have run the above code to obtain 'amzn'!
... print("Hurst(AMZN): %s" % hurst(amzn['Adj Close']))
Hurst(AMZN): None发布于 2016-08-04 01:12:24
看起来您正在将代码粘贴到python交互窗口中。当您使用交互式窗口编写缩进代码块时(如定义函数或启动for循环时),按两次enter键将结束代码块部分(这就是为什么当代码应该在缩进代码块中时,在空行之后得到错误的原因)。在代码中,您可以删除所有空行(代码块末尾的空行除外;这些行是结束代码块所必需的)并将其粘贴到交互式窗口中,也可以将代码复制到文本文件中,以.py文件扩展名保存该文件,然后使用命令python your_script.py从命令提示符/powershell/终端运行脚本。
或者,使用python IDE (而不是交互式窗口或从命令行),如pycharm (https://www.jetbrains.com/pycharm/)。
https://stackoverflow.com/questions/38749037
复制相似问题