Michael Halls-Moore 算法交易后的错误
Errors Following Michael Halls-Moore Algorithmic Trading
我正在阅读 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
看起来您正在将代码粘贴到 python 交互式 window 中。当您使用交互式 window 编写一段缩进代码时(例如定义一个函数或开始一个 for 循环时),按两次 Enter 结束代码块部分(这就是为什么您在空行后得到错误当代码应该位于缩进代码块中时)。在您的代码中,您可以删除所有空行(代码块末尾的空行除外;这些是结束代码块所必需的)并将其粘贴到交互式 window 中,或者您可以将代码复制到文本文件中,使用 .py 文件扩展名保存文件,并使用命令 python your_script.py
从命令 prompt/powershell/terminal 中 运行 脚本。
或者,使用 python IDE(而不是交互式 window 或从命令行),如 pycharm (https://www.jetbrains.com/pycharm/)。
我正在阅读 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
看起来您正在将代码粘贴到 python 交互式 window 中。当您使用交互式 window 编写一段缩进代码时(例如定义一个函数或开始一个 for 循环时),按两次 Enter 结束代码块部分(这就是为什么您在空行后得到错误当代码应该位于缩进代码块中时)。在您的代码中,您可以删除所有空行(代码块末尾的空行除外;这些是结束代码块所必需的)并将其粘贴到交互式 window 中,或者您可以将代码复制到文本文件中,使用 .py 文件扩展名保存文件,并使用命令 python your_script.py
从命令 prompt/powershell/terminal 中 运行 脚本。
或者,使用 python IDE(而不是交互式 window 或从命令行),如 pycharm (https://www.jetbrains.com/pycharm/)。