迈克尔·霍尔斯 - 摩尔算法交易的错误
我跟随迈克尔·霍尔斯 - 摩尔算法交易书,并且遇到了一些代码问题。当我把代码粘贴到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']))
下面
[email protected]:~$ 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
的错误看起来像你将代码粘贴到一个Python交互窗口。当您使用交互式窗口编写一段缩进代码时(例如定义函数或启动for循环时),按两次enter键结束代码块部分(这就是为什么当代码是空行后出现错误应该是在一个缩进代码块)。在你的代码中,你可以删除所有的空白行(除了代码块末尾的空白行;这些都是结束代码块所必需的),并将其粘贴到交互式窗口中,或者可以将代码复制到一个文本文件,以.py文件扩展名保存该文件,然后使用命令python your_script.py
从命令提示符/ powershell/terminal运行该脚本。
或者,使用python IDE(而不是交互式窗口或从命令行)像pycharm(https://www.jetbrains.com/pycharm/)。
取出*全部*空白行不完全正确。每个缩进部分末尾需要一个空行。我发现'exec r'''
这是真的;我会编辑我的回复。感谢您指出了这一点! – crld
如果这是问题,那么使用iPython的魔术'%paste'而不是CTRL-V或同等的。 –
这是一个缩进错误,所以...检查您的缩进。使用空格而不是标签,绝对不是标签和空格。每个缩进级别必须是相同的空格数量。 –
缩进时是否有混合制表符和空格的机会?编辑:@ Two-BitAlchemist击败了我。 :) –
它实际上在堆栈跟踪中说4次IndentationError。检查你的标签与空格。 – lonewaft